Skip to content

Commit 7047201

Browse files
upgrade mockito and optimize tests coverage
1 parent a365ee4 commit 7047201

File tree

9 files changed

+2555
-1
lines changed

9 files changed

+2555
-1
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ subprojects {
5252
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
5353

5454
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
55-
testImplementation "org.mockito:mockito-core:2.13.0"
55+
testImplementation "org.mockito:mockito-core:4.11.0"
56+
testImplementation "org.mockito:mockito-inline:4.11.0"
5657
}
5758

5859
task sourcesJar(type: Jar, dependsOn: classes) {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.tron.common.logsfilter;
2+
3+
4+
import com.google.protobuf.ByteString;
5+
6+
import java.lang.reflect.Method;
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
11+
import org.junit.After;
12+
import org.junit.Assert;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule;
16+
import org.tron.common.logsfilter.trigger.InternalTransactionPojo;
17+
import org.tron.common.runtime.InternalTransaction;
18+
import org.tron.common.runtime.ProgramResult;
19+
import org.tron.common.runtime.RuntimeImpl;
20+
import org.tron.common.utils.Sha256Hash;
21+
import org.tron.core.capsule.BlockCapsule;
22+
import org.tron.core.capsule.ReceiptCapsule;
23+
import org.tron.core.capsule.TransactionCapsule;
24+
import org.tron.core.db.TransactionTrace;
25+
import org.tron.p2p.utils.ByteArray;
26+
import org.tron.protos.Protocol;
27+
import org.tron.protos.contract.BalanceContract;
28+
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.spy;
31+
import static org.mockito.Mockito.when;
32+
33+
34+
public class TransactionLogTriggerCapsuleMockTest {
35+
36+
private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
37+
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";
38+
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";
39+
40+
private TransactionCapsule transactionCapsule;
41+
private BlockCapsule blockCapsule;
42+
43+
@Before
44+
public void setup() {
45+
blockCapsule = new BlockCapsule(1,
46+
Sha256Hash.ZERO_HASH,
47+
System.currentTimeMillis(),
48+
Sha256Hash.ZERO_HASH.getByteString()
49+
);
50+
}
51+
52+
@After
53+
public void clearMocks() {
54+
55+
}
56+
57+
58+
@Test
59+
public void testConstructorWithTransactionTrace() {
60+
BalanceContract.TransferContract.Builder builder2 =
61+
BalanceContract.TransferContract.newBuilder()
62+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
63+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
64+
transactionCapsule = spy(new TransactionCapsule(builder2.build(),
65+
Protocol.Transaction.Contract.ContractType.TransferContract));
66+
67+
TransactionTrace trace = mock(TransactionTrace.class);
68+
ReceiptCapsule receiptCapsule = new ReceiptCapsule(Sha256Hash.ZERO_HASH);
69+
RuntimeImpl runtime = mock(RuntimeImpl.class);
70+
List<Protocol.TransactionInfo.Log> logs = new ArrayList<>();
71+
logs.add(Protocol.TransactionInfo.Log.newBuilder()
72+
.setAddress(ByteString.copyFrom("address".getBytes()))
73+
.setData(ByteString.copyFrom("data".getBytes()))
74+
.addTopics(ByteString.copyFrom("topic".getBytes()))
75+
.build());
76+
77+
Protocol.TransactionInfo.Builder builder = Protocol.TransactionInfo.newBuilder()
78+
.addAllLog(logs);
79+
80+
ProgramResult programResult = ProgramResult.createEmpty();
81+
programResult.setHReturn("hreturn".getBytes());
82+
programResult.setContractAddress(CONTRACT_ADDRESS.getBytes());
83+
84+
when(transactionCapsule.getTrxTrace()).thenReturn(trace);
85+
when(trace.getReceipt()).thenReturn(receiptCapsule);
86+
when(trace.getRuntime()).thenReturn(runtime);
87+
when(runtime.getResult()).thenReturn(programResult);
88+
89+
transactionCapsule.setTrxTrace(trace);
90+
91+
TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule(
92+
transactionCapsule, blockCapsule,0,0,0,
93+
builder.build(),0);
94+
95+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger());
96+
}
97+
98+
@Test
99+
public void testGetInternalTransactionList() throws Exception {
100+
BalanceContract.TransferContract.Builder builder2 =
101+
BalanceContract.TransferContract.newBuilder()
102+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
103+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
104+
transactionCapsule = new TransactionCapsule(builder2.build(),
105+
Protocol.Transaction.Contract.ContractType.TransferContract);
106+
InternalTransaction internalTransaction = new InternalTransaction(
107+
"parentHash".getBytes(), 10, 0,
108+
"sendAddress".getBytes(),
109+
"transferToAddress".getBytes(),
110+
100L, "data".getBytes(), "note",
111+
0L, new HashMap<>()
112+
);
113+
List<InternalTransaction> internalTransactionList = new ArrayList<>();
114+
internalTransactionList.add(internalTransaction);
115+
TransactionLogTriggerCapsule triggerCapsule =
116+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
117+
118+
Method privateMethod = TransactionLogTriggerCapsule.class.getDeclaredMethod(
119+
"getInternalTransactionList", List.class);
120+
privateMethod.setAccessible(true);
121+
List<InternalTransactionPojo> pojoList = (List<InternalTransactionPojo>)
122+
privateMethod.invoke(triggerCapsule, internalTransactionList);
123+
124+
Assert.assertNotNull(pojoList);
125+
}
126+
127+
}

framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import org.tron.core.capsule.TransactionCapsule;
1414
import org.tron.p2p.utils.ByteArray;
1515
import org.tron.protos.Protocol;
16+
import org.tron.protos.contract.AssetIssueContractOuterClass;
1617
import org.tron.protos.contract.BalanceContract;
1718
import org.tron.protos.contract.Common;
19+
import org.tron.protos.contract.SmartContractOuterClass;
1820

1921
public class TransactionLogTriggerCapsuleTest {
2022

2123
private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
2224
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";
25+
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";
2326

2427
public TransactionCapsule transactionCapsule;
2528
public BlockCapsule blockCapsule;
@@ -175,4 +178,70 @@ public void testConstructorWithCancelAllUnfreezeTrxCapsule() {
175178
triggerCapsule.getTransactionLogTrigger().getExtMap().get(BANDWIDTH.name()).longValue());
176179
}
177180

181+
182+
@Test
183+
public void testConstructorWithTransferCapsule() {
184+
BalanceContract.TransferContract.Builder builder2 =
185+
BalanceContract.TransferContract.newBuilder()
186+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
187+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
188+
transactionCapsule = new TransactionCapsule(builder2.build(),
189+
Protocol.Transaction.Contract.ContractType.TransferContract);
190+
191+
TransactionLogTriggerCapsule triggerCapsule =
192+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
193+
194+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
195+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
196+
}
197+
198+
@Test
199+
public void testConstructorWithTransferAssetCapsule() {
200+
AssetIssueContractOuterClass.TransferAssetContract.Builder builder2 =
201+
AssetIssueContractOuterClass.TransferAssetContract.newBuilder()
202+
.setAssetName(ByteString.copyFrom("AssetName".getBytes()))
203+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
204+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
205+
transactionCapsule = new TransactionCapsule(builder2.build(),
206+
Protocol.Transaction.Contract.ContractType.TransferAssetContract);
207+
208+
TransactionLogTriggerCapsule triggerCapsule =
209+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
210+
211+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
212+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
213+
}
214+
215+
@Test
216+
public void testConstructorWithTriggerSmartContract() {
217+
SmartContractOuterClass.TriggerSmartContract.Builder builder2 =
218+
SmartContractOuterClass.TriggerSmartContract.newBuilder()
219+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
220+
.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS)));
221+
transactionCapsule = new TransactionCapsule(builder2.build(),
222+
Protocol.Transaction.Contract.ContractType.TriggerSmartContract);
223+
224+
TransactionLogTriggerCapsule triggerCapsule =
225+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
226+
227+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
228+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
229+
}
230+
231+
@Test
232+
public void testConstructorWithCreateSmartContract() {
233+
SmartContractOuterClass.CreateSmartContract.Builder builder2 =
234+
SmartContractOuterClass.CreateSmartContract.newBuilder()
235+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
236+
transactionCapsule = new TransactionCapsule(builder2.build(),
237+
Protocol.Transaction.Contract.ContractType.CreateSmartContract);
238+
239+
TransactionLogTriggerCapsule triggerCapsule =
240+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
241+
242+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
243+
}
244+
245+
246+
178247
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.tron.common.runtime;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.After;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.tron.core.vm.program.Program;
8+
9+
import java.lang.reflect.Method;
10+
11+
@Slf4j
12+
public class RuntimeImplMockTest {
13+
@After
14+
public void clearMocks() {
15+
16+
}
17+
18+
@Test
19+
public void testSetResultCode1() throws Exception {
20+
RuntimeImpl runtime = new RuntimeImpl();
21+
ProgramResult programResult = new ProgramResult();
22+
Method privateMethod = RuntimeImpl.class.getDeclaredMethod(
23+
"setResultCode", ProgramResult.class);
24+
privateMethod.setAccessible(true);
25+
26+
Program.BadJumpDestinationException badJumpDestinationException
27+
= new Program.BadJumpDestinationException("Operation with pc isn't 'JUMPDEST': PC[%d];", 0);
28+
programResult.setException(badJumpDestinationException);
29+
privateMethod.invoke(runtime, programResult);
30+
31+
Program.OutOfTimeException outOfTimeException
32+
= new Program.OutOfTimeException("CPU timeout for 0x0a executing");
33+
programResult.setException(outOfTimeException);
34+
privateMethod.invoke(runtime, programResult);
35+
36+
Program.PrecompiledContractException precompiledContractException
37+
= new Program.PrecompiledContractException("precompiled contract exception");
38+
programResult.setException(precompiledContractException);
39+
privateMethod.invoke(runtime, programResult);
40+
41+
Program.StackTooSmallException stackTooSmallException
42+
= new Program.StackTooSmallException("Expected stack size %d but actual %d;", 100, 10);
43+
programResult.setException(stackTooSmallException);
44+
privateMethod.invoke(runtime, programResult);
45+
46+
Program.JVMStackOverFlowException jvmStackOverFlowException
47+
= new Program.JVMStackOverFlowException();
48+
programResult.setException(jvmStackOverFlowException);
49+
privateMethod.invoke(runtime, programResult);
50+
51+
Assert.assertTrue(true);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)