Skip to content

Commit 13bf1a0

Browse files
authored
Merge pull request #5162 from lurais/feature/optimize_unit_test
feat(test): add unit test
2 parents 17f9304 + add119e commit 13bf1a0

6 files changed

Lines changed: 289 additions & 1 deletion

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.tron.core.db;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Test;
5+
import org.testng.Assert;
6+
import org.tron.common.utils.ByteArray;
7+
8+
@Slf4j
9+
public class ByteArrayWrapperTest {
10+
11+
@Test
12+
public void createByteArray() {
13+
ByteArrayWrapper byteArrayWrapper1 = new ByteArrayWrapper(ByteArray.fromHexString("1"));
14+
ByteArrayWrapper byteArrayWrapper2 = new ByteArrayWrapper(ByteArray.fromHexString("2"));
15+
Assert.assertEquals(byteArrayWrapper1.compareTo(byteArrayWrapper2), -1);
16+
Assert.assertFalse(byteArrayWrapper1.equals(byteArrayWrapper2));
17+
Assert.assertFalse(byteArrayWrapper1.getData().equals(byteArrayWrapper2.getData()));
18+
Assert.assertTrue(byteArrayWrapper1.hashCode() != byteArrayWrapper2.hashCode());
19+
Assert.assertEquals(byteArrayWrapper1.toString().equals(byteArrayWrapper2.toString()),false);
20+
}
21+
22+
}

framework/src/test/java/org/tron/core/db/ManagerTest.java

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.ArrayList;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.Set;
1516
import java.util.concurrent.atomic.AtomicInteger;
1617
import java.util.stream.Collectors;
1718
import java.util.stream.IntStream;
@@ -20,11 +21,14 @@
2021
import org.junit.Assert;
2122
import org.junit.Before;
2223
import org.junit.Test;
24+
import org.testng.collections.Sets;
2325
import org.tron.common.application.TronApplicationContext;
2426
import org.tron.common.crypto.ECKey;
27+
import org.tron.common.runtime.RuntimeImpl;
2528
import org.tron.common.utils.ByteArray;
2629
import org.tron.common.utils.FileUtil;
2730
import org.tron.common.utils.JsonUtil;
31+
import org.tron.common.utils.ReflectUtils;
2832
import org.tron.common.utils.Sha256Hash;
2933
import org.tron.common.utils.StringUtil;
3034
import org.tron.common.utils.Utils;
@@ -38,8 +42,12 @@
3842
import org.tron.core.capsule.TransactionCapsule;
3943
import org.tron.core.capsule.WitnessCapsule;
4044
import org.tron.core.config.DefaultConfig;
45+
import org.tron.core.config.Parameter;
4146
import org.tron.core.config.args.Args;
4247
import org.tron.core.consensus.ConsensusService;
48+
import org.tron.core.db.accountstate.AccountStateEntity;
49+
import org.tron.core.db.accountstate.TrieService;
50+
import org.tron.core.db.accountstate.storetrie.AccountStateStoreTrie;
4351
import org.tron.core.exception.AccountResourceInsufficientException;
4452
import org.tron.core.exception.BadBlockException;
4553
import org.tron.core.exception.BadItemException;
@@ -67,6 +75,7 @@
6775
import org.tron.core.store.ExchangeStore;
6876
import org.tron.core.store.ExchangeV2Store;
6977
import org.tron.core.store.IncrementalMerkleTreeStore;
78+
import org.tron.core.store.StoreFactory;
7079
import org.tron.protos.Protocol.Account;
7180
import org.tron.protos.Protocol.Block;
7281
import org.tron.protos.Protocol.Transaction;
@@ -142,6 +151,17 @@ public void updateRecentTransaction() throws Exception {
142151
0, ByteString.copyFrom(new byte[64]));
143152
b.addTransaction(trx);
144153
dbManager.updateRecentTransaction(b);
154+
try {
155+
dbManager.consumeBandwidth(trx, new TransactionTrace(trx, StoreFactory.getInstance(),
156+
new RuntimeImpl()));
157+
} catch (Exception e) {
158+
Assert.assertTrue(e instanceof ContractValidateException);
159+
}
160+
dbManager.consumeMemoFee(trx, new TransactionTrace(trx, StoreFactory.getInstance(),
161+
new RuntimeImpl()));
162+
Assert.assertTrue(dbManager.getTxListFromPending().isEmpty());
163+
Assert.assertNull(dbManager.getTxFromPending(trx.getTransactionId().toString()));
164+
Assert.assertEquals(0, dbManager.getPendingSize());
145165
Assert.assertEquals(1, chainManager.getRecentTransactionStore().size());
146166
byte[] key = ByteArray.subArray(ByteArray.fromLong(1), 6, 8);
147167
byte[] value = chainManager.getRecentTransactionStore().get(key).getData();
@@ -212,6 +232,9 @@ public void pushBlock() {
212232
} catch (Exception e) {
213233
Assert.assertTrue("pushBlock is error", false);
214234
}
235+
TrieService trieService = context.getBean(TrieService.class);
236+
Assert.assertTrue(trieService.getFullAccountStateRootHash().length > 0);
237+
Assert.assertTrue(trieService.getSolidityAccountStateRootHash().length > 0);
215238

216239
if (isUnlinked) {
217240
Assert.assertEquals("getBlockIdByNum is error",
@@ -233,10 +256,50 @@ public void pushBlock() {
233256
} catch (ItemNotFoundException e) {
234257
Assert.assertTrue(true);
235258
}
236-
259+
try {
260+
dbManager.getBlockChainHashesOnFork(blockCapsule2.getBlockId());
261+
} catch (Exception e) {
262+
Assert.assertTrue(e instanceof NonCommonBlockException);
263+
}
237264
Assert.assertTrue("hasBlocks is error", chainManager.hasBlocks());
238265
}
239266

267+
@Test
268+
public void transactionTest() {
269+
TransactionCapsule trans0 = new TransactionCapsule(Transaction.newBuilder()
270+
.setRawData(Transaction.raw.newBuilder().setData(ByteString.copyFrom(
271+
new byte[Parameter.ChainConstant.BLOCK_SIZE + Constant.ONE_THOUSAND]))).build(),
272+
ContractType.ShieldedTransferContract);
273+
ShieldContract.ShieldedTransferContract trx1 = ShieldContract.ShieldedTransferContract
274+
.newBuilder()
275+
.setFromAmount(10)
276+
.setToAmount(10)
277+
.build();
278+
TransactionCapsule trans = new TransactionCapsule(trx1, ContractType.ShieldedTransferContract);
279+
try {
280+
dbManager.pushTransaction(trans0);
281+
dbManager.pushTransaction(trans);
282+
} catch (Exception e) {
283+
Assert.assertTrue(e instanceof TaposException);
284+
}
285+
dbManager.rePush(trans0);
286+
ReflectUtils.invokeMethod(dbManager,"filterOwnerAddress",
287+
new Class[]{trans.getClass(), Set.class},trans, Sets.newHashSet());
288+
Assert.assertNotNull(dbManager.getTxListFromPending());
289+
290+
try {
291+
dbManager.validateTapos(trans);
292+
} catch (Exception e) {
293+
Assert.assertTrue(e instanceof TaposException);
294+
}
295+
try {
296+
dbManager.pushVerifiedBlock(chainManager.getHead());
297+
dbManager.getBlockChainHashesOnFork(chainManager.getHeadBlockId());
298+
} catch (Exception e) {
299+
Assert.assertTrue(e instanceof TaposException);
300+
}
301+
}
302+
240303
@Test
241304
public void GetterInstanceTest() {
242305

@@ -260,6 +323,24 @@ public void GetterInstanceTest() {
260323

261324
}
262325

326+
@Test
327+
public void entityTest() {
328+
AccountStateStoreTrie trie = context.getBean(AccountStateStoreTrie.class);
329+
Assert.assertNull(trie.getAccount("".getBytes()));
330+
Assert.assertNull(trie.getAccount("".getBytes(), "".getBytes()));
331+
Assert.assertNull(trie.getSolidityAccount("".getBytes()));
332+
Assert.assertTrue(trie.isEmpty());
333+
AccountStateEntity entity = new AccountStateEntity();
334+
AccountStateEntity parsedEntity = AccountStateEntity.parse("".getBytes());
335+
Assert.assertTrue(parsedEntity != null);
336+
Assert.assertTrue(parsedEntity.getAccount() != null);
337+
Assert.assertTrue(org.tron.core.db.api.pojo.Account.of() != null);
338+
Assert.assertTrue(org.tron.core.db.api.pojo.AssetIssue.of() != null);
339+
Assert.assertTrue(org.tron.core.db.api.pojo.Block.of() != null);
340+
Assert.assertTrue(org.tron.core.db.api.pojo.Transaction.of() != null);
341+
342+
}
343+
263344
@Test
264345
public void getHeadTest() {
265346
try {

framework/src/test/java/org/tron/core/services/filter/RpcApiAccessInterceptorTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import io.grpc.ManagedChannel;
44
import io.grpc.ManagedChannelBuilder;
55
import io.grpc.StatusRuntimeException;
6+
import io.grpc.stub.ServerCallStreamObserver;
67
import java.io.File;
78
import java.util.ArrayList;
89
import java.util.Collections;
910
import java.util.List;
11+
import java.util.Objects;
1012
import lombok.extern.slf4j.Slf4j;
1113
import org.junit.AfterClass;
1214
import org.junit.Assert;
@@ -119,6 +121,106 @@ public void testAccessDisabledFullNode() {
119121
blockingStubFull.getBlockByNum(message);
120122
}
121123

124+
@Test
125+
public void testRpcApiService() {
126+
RpcApiService rpcApiService = context.getBean(RpcApiService.class);
127+
ServerCallStreamObserverTest serverCallStreamObserverTest = new ServerCallStreamObserverTest();
128+
rpcApiService.getBlockCommon(GrpcAPI.BlockReq.getDefaultInstance(),
129+
serverCallStreamObserverTest);
130+
Assert.assertTrue("Get block Common failed!", serverCallStreamObserverTest.isReady());
131+
serverCallStreamObserverTest.isCancelled();
132+
rpcApiService.getBrokerageInfoCommon(GrpcAPI.BytesMessage.newBuilder().build(),
133+
serverCallStreamObserverTest);
134+
Assert.assertTrue("Get brokerage info Common failed!",
135+
serverCallStreamObserverTest.isReady());
136+
serverCallStreamObserverTest.isCancelled();
137+
rpcApiService.getBurnTrxCommon(GrpcAPI.EmptyMessage.newBuilder().build(),
138+
serverCallStreamObserverTest);
139+
Assert.assertTrue("Get burn trx common failed!",
140+
serverCallStreamObserverTest.isReady());
141+
serverCallStreamObserverTest.isCancelled();
142+
rpcApiService.getPendingSizeCommon(GrpcAPI.EmptyMessage.getDefaultInstance(),
143+
serverCallStreamObserverTest);
144+
Assert.assertTrue("Get pending size common failed!",
145+
serverCallStreamObserverTest.isReady());
146+
serverCallStreamObserverTest.isCancelled();
147+
rpcApiService.getRewardInfoCommon(GrpcAPI.BytesMessage.newBuilder().build(),
148+
serverCallStreamObserverTest);
149+
Assert.assertTrue("Get reward info common failed!",
150+
serverCallStreamObserverTest.isReady());
151+
serverCallStreamObserverTest.isCancelled();
152+
rpcApiService.getTransactionCountByBlockNumCommon(
153+
GrpcAPI.NumberMessage.newBuilder().getDefaultInstanceForType(),
154+
serverCallStreamObserverTest);
155+
Assert.assertTrue("Get transaction count by block num failed!",
156+
serverCallStreamObserverTest.isReady());
157+
serverCallStreamObserverTest.isCancelled();
158+
rpcApiService.getTransactionFromPendingCommon(GrpcAPI.BytesMessage.newBuilder().build(),
159+
serverCallStreamObserverTest);
160+
Assert.assertTrue("Get transaction from pending failed!",
161+
serverCallStreamObserverTest.isReady() == false);
162+
serverCallStreamObserverTest.isCancelled();
163+
rpcApiService.getTransactionListFromPendingCommon(GrpcAPI.EmptyMessage.newBuilder()
164+
.getDefaultInstanceForType(), serverCallStreamObserverTest);
165+
Assert.assertTrue("Get transaction list from pending failed!",
166+
serverCallStreamObserverTest.isReady());
167+
}
168+
169+
170+
class ServerCallStreamObserverTest extends ServerCallStreamObserver {
171+
172+
Object ret;
173+
174+
@Override
175+
public boolean isCancelled() {
176+
ret = null;
177+
return true;
178+
}
179+
180+
@Override
181+
public void setOnCancelHandler(Runnable onCancelHandler) {
182+
}
183+
184+
@Override
185+
public void setCompression(String compression) {
186+
}
187+
188+
@Override
189+
public boolean isReady() {
190+
return Objects.nonNull(ret);
191+
}
192+
193+
@Override
194+
public void setOnReadyHandler(Runnable onReadyHandler) {
195+
}
196+
197+
@Override
198+
public void disableAutoInboundFlowControl() {
199+
}
200+
201+
@Override
202+
public void request(int count) {
203+
}
204+
205+
@Override
206+
public void setMessageCompression(boolean enable) {
207+
}
208+
209+
@Override
210+
public void onNext(Object value) {
211+
ret = value;
212+
}
213+
214+
@Override
215+
public void onError(Throwable t) {
216+
}
217+
218+
@Override
219+
public void onCompleted() {
220+
}
221+
}
222+
223+
122224
@Test
123225
public void testAccessDisabledSolidityNode() {
124226
List<String> disabledApiList = new ArrayList<>();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.tron.keystore;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.security.SecureRandom;
5+
import junit.framework.TestCase;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.junit.Test;
8+
import org.springframework.util.Assert;
9+
import org.tron.common.crypto.SignUtils;
10+
import org.tron.common.crypto.sm2.SM2;
11+
import org.tron.common.utils.ByteUtil;
12+
13+
@Slf4j
14+
public class CredentialsTest extends TestCase {
15+
16+
@Test
17+
public void testCreate() throws NoSuchAlgorithmException {
18+
Credentials credentials = Credentials.create(SignUtils.getGeneratedRandomSign(
19+
SecureRandom.getInstance("NativePRNG"),true));
20+
Assert.hasText(credentials.getAddress(),"Credentials address create failed!");
21+
Assert.notNull(credentials.getSignInterface(),
22+
"Credentials cryptoEngine create failed");
23+
}
24+
25+
@Test
26+
public void testCreateFromSM2() {
27+
try {
28+
Credentials.create(SM2.fromNodeId(ByteUtil.hexToBytes("fffffffffff"
29+
+ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
30+
+ "fffffffffffffffffffffffffffffffffffffff")));
31+
} catch (Exception e) {
32+
Assert.isInstanceOf(IllegalArgumentException.class, e);
33+
}
34+
}
35+
36+
@Test
37+
public void testEquals() throws NoSuchAlgorithmException {
38+
Credentials credentials1 = Credentials.create(SignUtils.getGeneratedRandomSign(
39+
SecureRandom.getInstance("NativePRNG"),true));
40+
Credentials credentials2 = Credentials.create(SignUtils.getGeneratedRandomSign(
41+
SecureRandom.getInstance("NativePRNG"),true));
42+
Assert.isTrue(!credentials1.equals(credentials2),
43+
"Credentials instance should be not equal!");
44+
Assert.isTrue(!(credentials1.hashCode() == credentials2.hashCode()),
45+
"Credentials instance hashcode should be not equal!");
46+
}
47+
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.tron.keystore;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.security.SecureRandom;
5+
import junit.framework.TestCase;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.tron.common.crypto.SignUtils;
10+
import org.tron.core.exception.CipherException;
11+
12+
@Slf4j
13+
public class WalletFileTest extends TestCase {
14+
15+
16+
@Test
17+
public void testGetAddress() throws NoSuchAlgorithmException, CipherException {
18+
WalletFile walletFile1 = Wallet.createStandard("", SignUtils.getGeneratedRandomSign(
19+
SecureRandom.getInstance("NativePRNG"),true));
20+
WalletFile walletFile2 = Wallet.createStandard("", SignUtils.getGeneratedRandomSign(
21+
SecureRandom.getInstance("NativePRNG"),true));
22+
Assert.assertTrue(!walletFile1.getAddress().equals(walletFile2.getAddress()));
23+
Assert.assertTrue(!walletFile1.getCrypto().equals(walletFile2.getCrypto()));
24+
Assert.assertTrue(!walletFile1.getId().equals(walletFile2.getId()));
25+
Assert.assertTrue(walletFile1.getVersion() == walletFile2.getVersion());
26+
}
27+
28+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ public void testSolidityGrpcCall() {
8989
Block genesisBlock = databaseGrpcClient.getBlock(0);
9090
Assert.assertNotNull(genesisBlock);
9191
Assert.assertFalse(genesisBlock.getTransactionsList().isEmpty());
92+
Block invalidBlock = databaseGrpcClient.getBlock(-1);
93+
Assert.assertNotNull(invalidBlock);
94+
try {
95+
databaseGrpcClient = new DatabaseGrpcClient(address, -1);
96+
} catch (Exception e) {
97+
logger.error("Failed to create database grpc client {}", address);
98+
}
9299
databaseGrpcClient.shutdown();
93100
}
94101

0 commit comments

Comments
 (0)