Skip to content

Commit 333d25c

Browse files
feat(test): improve tests coverage (#6034)
* optimize tests coverage * optimize tests coverage --------- Co-authored-by: chaozhu <[email protected]>
1 parent c81c2fc commit 333d25c

File tree

10 files changed

+2393
-2
lines changed

10 files changed

+2393
-2
lines changed

build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ 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:3.10.0"
56+
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
57+
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
58+
5659
}
5760

5861
task sourcesJar(type: Jar, dependsOn: classes) {

framework/build.gradle

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ plugins {
22
id "org.gradle.test-retry" version "1.5.9"
33
id "org.sonarqube" version "2.6"
44
id "com.gorylenko.gradle-git-properties" version "2.4.1"
5+
id "io.github.surpsg.offlins" version "0.3.0"
56
}
67

78
gitProperties.failOnNoGitDirectory = false;
89

910
apply plugin: 'application'
1011
apply plugin: 'checkstyle'
12+
apply plugin: "io.github.surpsg.offlins"
1113

1214
mainClassName = 'org.tron.program.FullNode'
1315

@@ -16,7 +18,7 @@ def versions = [
1618
]
1719

1820
jacoco {
19-
toolVersion = "0.8.1"
21+
toolVersion = "0.8.8"
2022
}
2123

2224

@@ -121,10 +123,12 @@ test {
121123
testLogging {
122124
exceptionFormat = 'full'
123125
}
126+
/*
124127
jacoco {
125128
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
126129
classDumpDir = file("$buildDir/jacoco/classpathdumps")
127130
}
131+
*/
128132
if (isWindows()) {
129133
exclude '**/ShieldedTransferActuatorTest.class'
130134
exclude '**/BackupDbUtilTest.class'
@@ -141,6 +145,7 @@ test {
141145
}
142146
}
143147

148+
/*
144149
jacocoTestReport {
145150
reports {
146151
xml.enabled true
@@ -149,6 +154,26 @@ jacocoTestReport {
149154
}
150155
getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec"))
151156
}
157+
*/
158+
159+
offlinsCoverage {
160+
jacocoVersion = '0.8.8' // Optional. By default `0.8.8`
161+
162+
reports {
163+
html.enabled.set true // Optional. By default `true`
164+
html.location.set project.file('build/reports/jacoco/test/html') // Optional. By default `build/reports/jacoco/html`
165+
166+
xml.enabled.set true // Optional. By default `false`
167+
xml.location.set project.file('build/reports/jacoco/test/jacocoTestReport.xml')
168+
// Optional. By default `build/reports/jacoco/coverageReport.xml`
169+
170+
csv.enabled.set true // Optional. By default `false`
171+
csv.location.set project.file('build/reports/jacoco/test/csvCoverage.csv')
172+
// Optional. By default `build/reports/jacoco/coverageReport.csv`
173+
}
174+
}
175+
176+
152177

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

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

0 commit comments

Comments
 (0)