Skip to content

Commit 1ee8957

Browse files
authored
Merge pull request #4972 from lxcmyf/feature/unit_test
fix(test): add some test cases of utils
2 parents 3664fff + 80e700f commit 1ee8957

10 files changed

Lines changed: 352 additions & 188 deletions

File tree

framework/src/main/java/org/tron/core/capsule/utils/RLP.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static long decodeLong(byte[] data, int index) {
204204
return value;
205205
}
206206

207-
private static String decodeStringItem(byte[] data, int index) {
207+
public static String decodeStringItem(byte[] data, int index) {
208208

209209
final byte[] valueBytes = decodeItemBytes(data, index);
210210

@@ -229,12 +229,12 @@ public static BigInteger decodeBigInteger(byte[] data, int index) {
229229
}
230230
}
231231

232-
private static byte[] decodeByteArray(byte[] data, int index) {
232+
public static byte[] decodeByteArray(byte[] data, int index) {
233233

234234
return decodeItemBytes(data, index);
235235
}
236236

237-
private static int nextItemLength(byte[] data, int index) {
237+
public static int nextItemLength(byte[] data, int index) {
238238

239239
if (index >= data.length) {
240240
return -1;

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class BandWidthRuntimeOutOfTimeTest extends BaseTest {
6666
private static final String TriggerOwnerAddress = "TCSgeWapPJhCqgWRxXCKb6jJ5AgNWSGjPA";
6767

6868
static {
69-
dbPath = "output_BandWidthRuntimeOutOfTimeTest_test";
69+
dbPath = "output_bandwidth_runtime_out_of_time_test";
7070
Args.setParam(
7171
new String[]{
7272
"--output-directory", dbPath,

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeOutOfTimeWithCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class BandWidthRuntimeOutOfTimeWithCheckTest extends BaseTest {
6767
private static final String TriggerOwnerAddress = "TCSgeWapPJhCqgWRxXCKb6jJ5AgNWSGjPA";
6868

6969
static {
70-
dbPath = "output_BandWidthRuntimeOutOfTimeTest_test";
70+
dbPath = "output_bandwidth_runtime_out_of_time_with_check_test";
7171
Args.setParam(
7272
new String[]{
7373
"--output-directory", dbPath,

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.protobuf.ByteString;
2020
import org.junit.Assert;
2121
import org.junit.Before;
22+
import org.junit.BeforeClass;
2223
import org.junit.Test;
2324
import org.tron.common.BaseTest;
2425
import org.tron.common.runtime.RuntimeImpl;
@@ -56,8 +57,9 @@ public class BandWidthRuntimeTest extends BaseTest {
5657
private static final String TriggerOwnerTwoAddress = "TPMBUANrTwwQAPwShn7ZZjTJz1f3F8jknj";
5758
private static boolean init;
5859

59-
static {
60-
dbPath = "output_BandWidthRuntimeTest_test";
60+
@BeforeClass
61+
public static void init() {
62+
dbPath = "output_bandwidth_runtime_test";
6163
Args.setParam(
6264
new String[]{
6365
"--output-directory", dbPath,
@@ -73,7 +75,7 @@ public class BandWidthRuntimeTest extends BaseTest {
7375
* Init data.
7476
*/
7577
@Before
76-
public void init() {
78+
public void before() {
7779
if (init) {
7880
return;
7981
}

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeWithCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class BandWidthRuntimeWithCheckTest extends BaseTest {
7070
private static boolean init;
7171

7272
static {
73-
dbPath = "output_BandWidthRuntimeTest_test";
73+
dbPath = "output_bandwidth_runtime_with_check_test";
7474
Args.setParam(
7575
new String[]{
7676
"--output-directory", dbPath,

framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsVerifyProofTest.java

Lines changed: 202 additions & 172 deletions
Large diffs are not rendered by default.

framework/src/test/java/org/tron/core/tire/TrieTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void testOrder() {
136136
trie2.put(RLP.encodeInt(i), String.valueOf(i).getBytes());
137137
}
138138
byte[] rootHash2 = trie2.getRootHash();
139-
Assert.assertTrue(java.util.Arrays.equals(rootHash1, rootHash2));
139+
Assert.assertArrayEquals(rootHash1, rootHash2);
140140
}
141141

142142
private void assertTrue(byte[] key, TrieImpl trieCopy) {

framework/src/test/java/org/tron/program/LiteFullNodeToolTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import org.junit.After;
99
import org.junit.Before;
1010
import org.junit.Ignore;
11-
import org.junit.Rule;
1211
import org.junit.Test;
13-
import org.junit.rules.ExpectedException;
1412
import org.tron.api.WalletGrpc;
1513
import org.tron.common.application.Application;
1614
import org.tron.common.application.ApplicationFactory;
@@ -35,9 +33,6 @@ public class LiteFullNodeToolTest {
3533
private Application appTest;
3634
private String databaseDir;
3735

38-
@Rule
39-
public ExpectedException thrown = ExpectedException.none();
40-
4136
private static final String DB_PATH = "output_lite_fn";
4237

4338
/**

framework/src/test/java/org/tron/program/SolidityNodeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SolidityNodeTest {
2121
private static TronApplicationContext context;
2222

2323
private static RpcApiService rpcApiService;
24-
private static String dbPath = "output_witness_test";
24+
private static String dbPath = "output_sn_test";
2525

2626
static {
2727
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF);
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package org.tron.program;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.File;
8+
import java.math.BigInteger;
9+
import javax.annotation.Resource;
10+
import org.junit.BeforeClass;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.rules.ExpectedException;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.test.annotation.DirtiesContext;
16+
import org.springframework.test.context.ContextConfiguration;
17+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18+
import org.tron.common.config.DbBackupConfig;
19+
import org.tron.common.entity.PeerInfo;
20+
import org.tron.common.utils.CompactEncoder;
21+
import org.tron.common.utils.JsonUtil;
22+
import org.tron.common.utils.Value;
23+
import org.tron.core.Constant;
24+
import org.tron.core.capsule.StorageRowCapsule;
25+
import org.tron.core.capsule.utils.RLP;
26+
import org.tron.core.config.DefaultConfig;
27+
import org.tron.core.config.args.Args;
28+
import org.tron.core.services.http.HttpSelfFormatFieldName;
29+
import org.tron.core.store.StorageRowStore;
30+
import org.tron.keystore.WalletUtils;
31+
32+
@RunWith(SpringJUnit4ClassRunner.class)
33+
@DirtiesContext
34+
@ContextConfiguration(classes = {DefaultConfig.class})
35+
public class SupplementTest {
36+
37+
private static final String dbPath = "output_coverage_test";
38+
39+
@Resource
40+
private StorageRowStore storageRowStore;
41+
42+
@Rule
43+
public ExpectedException thrown = ExpectedException.none();
44+
45+
@BeforeClass
46+
public static void init() {
47+
Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, Constant.TEST_CONF);
48+
}
49+
50+
@Test
51+
public void testGet() throws Exception {
52+
StorageRowCapsule storageRowCapsule = storageRowStore.get(new byte[]{});
53+
assertNotNull(storageRowCapsule);
54+
55+
DbBackupConfig dbBackupConfig = new DbBackupConfig();
56+
dbBackupConfig.initArgs(true, "propPath", "bak1path/", "bak2path/", 1);
57+
58+
WalletUtils.generateFullNewWalletFile("123456", new File(dbPath));
59+
WalletUtils.generateLightNewWalletFile("123456", new File(dbPath));
60+
WalletUtils.getDefaultKeyDirectory();
61+
WalletUtils.getTestnetKeyDirectory();
62+
WalletUtils.getMainnetKeyDirectory();
63+
64+
Value value = new Value(new byte[]{1});
65+
value.asBytes();
66+
value = new Value(1);
67+
value.asInt();
68+
value = new Value(100L);
69+
value.asLong();
70+
value = new Value(new BigInteger("1000"));
71+
value.asBigInt();
72+
value = new Value("1000");
73+
value.asString();
74+
value.isEmpty();
75+
value = new Value(new byte[]{1, 2, 3});
76+
value.isList();
77+
value.isReadableString();
78+
value.isHexString();
79+
value.isHashCode();
80+
value.isNull();
81+
value.length();
82+
assertNotNull(value.toString());
83+
value.countBranchNodes();
84+
85+
PeerInfo peerInfo = new PeerInfo();
86+
peerInfo.setAvgLatency(peerInfo.getAvgLatency());
87+
peerInfo.setBlockInPorcSize(peerInfo.getBlockInPorcSize());
88+
peerInfo.setConnectTime(peerInfo.getConnectTime());
89+
peerInfo.setDisconnectTimes(peerInfo.getDisconnectTimes());
90+
peerInfo.setHeadBlockTimeWeBothHave(peerInfo.getHeadBlockTimeWeBothHave());
91+
peerInfo.setHeadBlockWeBothHave(peerInfo.getHeadBlockWeBothHave());
92+
peerInfo.setHost(peerInfo.getHost());
93+
peerInfo.setInFlow(peerInfo.getInFlow());
94+
peerInfo.setLastBlockUpdateTime(peerInfo.getLastBlockUpdateTime());
95+
peerInfo.setLastSyncBlock(peerInfo.getLastSyncBlock());
96+
peerInfo.setLocalDisconnectReason(peerInfo.getLocalDisconnectReason());
97+
peerInfo.setNodeCount(peerInfo.getNodeCount());
98+
peerInfo.setNodeId(peerInfo.getNodeId());
99+
peerInfo.setRemainNum(peerInfo.getRemainNum());
100+
peerInfo.setRemoteDisconnectReason(peerInfo.getRemoteDisconnectReason());
101+
peerInfo.setScore(peerInfo.getScore());
102+
peerInfo.setPort(peerInfo.getPort());
103+
peerInfo.setSyncFlag(peerInfo.isSyncFlag());
104+
peerInfo.setNeedSyncFromPeer(peerInfo.isNeedSyncFromPeer());
105+
peerInfo.setNeedSyncFromUs(peerInfo.isNeedSyncFromUs());
106+
peerInfo.setSyncToFetchSize(peerInfo.getSyncToFetchSize());
107+
peerInfo.setSyncToFetchSizePeekNum(peerInfo.getSyncToFetchSizePeekNum());
108+
peerInfo.setSyncBlockRequestedSize(peerInfo.getSyncBlockRequestedSize());
109+
peerInfo.setUnFetchSynNum(peerInfo.getUnFetchSynNum());
110+
peerInfo.setActive(peerInfo.isActive());
111+
112+
assertNotNull(JsonUtil.json2Obj("{}", PeerInfo.class));
113+
assertNotNull(JsonUtil.obj2Json(peerInfo));
114+
115+
assertTrue(HttpSelfFormatFieldName.isAddressFormat(
116+
"protocol.DelegatedResourceMessage.fromAddress"));
117+
assertTrue(HttpSelfFormatFieldName.isNameStringFormat(
118+
"protocol.MarketPriceList.buy_token_id"));
119+
120+
CompactEncoder.packNibbles(new byte[] {1,2,3,4,5,6,7});
121+
assertFalse(CompactEncoder.hasTerminator(new byte[] {1,2,3,4,5,6,7}));
122+
CompactEncoder.unpackToNibbles(new byte[] {1,2,3,4,5,6,7});
123+
CompactEncoder.binToNibblesNoTerminator(new byte[] {1,2,3,4,5,6,7});
124+
125+
assertNotNull(RLP.decodeIP4Bytes(new byte[] {1,2,3,4,5,6,7}, 0));
126+
RLP.decodeByteArray(new byte[] {1,2,3,4,5,6,7}, 0);
127+
RLP.nextItemLength(new byte[] {1,2,3,4,5,6,7}, 0);
128+
RLP.decodeStringItem(new byte[] {1,2,3,4,5,6,7}, 0);
129+
RLP.decodeInt(new byte[] {1,2,3,4,5,6,7}, 0);
130+
RLP.decode2OneItem(new byte[] {1,2,3,4,5,6,7}, 0);
131+
RLP.decode2(new byte[] {1,2,3,4,5,6,7}, 1);
132+
RLP.decode2(new byte[] {1,2,3,4,5,6,7});
133+
thrown.expect(ClassCastException.class);
134+
RLP.unwrapList(new byte[] {1,2,3,4,5,6,7});
135+
}
136+
137+
}

0 commit comments

Comments
 (0)