Skip to content

Commit

Permalink
Merge pull request #6117 from CarlChaoCarl/feature/upgrade_mockito_an…
Browse files Browse the repository at this point in the history
…d_optimize_tests_coverage

feat(test): upgrade mockito and optimize tests coverage
  • Loading branch information
CodeNinjaEvan authored Jan 13, 2025
2 parents f16c3f8 + 81a04e5 commit 92ae447
Show file tree
Hide file tree
Showing 12 changed files with 2,542 additions and 12 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ subprojects {
targetCompatibility = JavaVersion.VERSION_1_8

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
jacoco {
toolVersion = "0.8.12" // see https://www.jacoco.org/jacoco/trunk/doc/changes.html
}

buildscript {
repositories {
Expand Down Expand Up @@ -52,7 +55,8 @@ subprojects {
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'

testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation "org.mockito:mockito-core:2.13.0"
testImplementation "org.mockito:mockito-core:4.11.0"
testImplementation "org.mockito:mockito-inline:4.11.0"
}

task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down
5 changes: 0 additions & 5 deletions chainbase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ description = "chainbase – a decentralized database for blockchain."

// Dependency versions
// ---------------------------------------
def jacocoVersion = "0.8.0"
def jansiVersion = "1.16"
// --------------------------------------

Expand Down Expand Up @@ -41,10 +40,6 @@ test {
}
}

jacoco {
toolVersion = jacocoVersion // See http://www.eclemma.org/jacoco/.
}

jacocoTestReport {
reports {
xml.enabled = true
Expand Down
3 changes: 0 additions & 3 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ def versions = [
checkstyle: '8.7',
]

jacoco {
toolVersion = "0.8.1"
}


configurations {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.tron.common.logsfilter;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import com.google.protobuf.ByteString;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule;
import org.tron.common.logsfilter.trigger.InternalTransactionPojo;
import org.tron.common.runtime.InternalTransaction;
import org.tron.common.runtime.ProgramResult;
import org.tron.common.runtime.RuntimeImpl;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.ReceiptCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.db.TransactionTrace;
import org.tron.p2p.utils.ByteArray;
import org.tron.protos.Protocol;
import org.tron.protos.contract.BalanceContract;




public class TransactionLogTriggerCapsuleMockTest {

private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";

private TransactionCapsule transactionCapsule;
private BlockCapsule blockCapsule;

@Before
public void setup() {
blockCapsule = new BlockCapsule(1,
Sha256Hash.ZERO_HASH,
System.currentTimeMillis(),
Sha256Hash.ZERO_HASH.getByteString()
);
}

@After
public void clearMocks() {

}


@Test
public void testConstructorWithTransactionTrace() {
BalanceContract.TransferContract.Builder builder2 =
BalanceContract.TransferContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
transactionCapsule = spy(new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.TransferContract));

TransactionTrace trace = mock(TransactionTrace.class);
ReceiptCapsule receiptCapsule = new ReceiptCapsule(Sha256Hash.ZERO_HASH);
RuntimeImpl runtime = mock(RuntimeImpl.class);
List<Protocol.TransactionInfo.Log> logs = new ArrayList<>();
logs.add(Protocol.TransactionInfo.Log.newBuilder()
.setAddress(ByteString.copyFrom("address".getBytes()))
.setData(ByteString.copyFrom("data".getBytes()))
.addTopics(ByteString.copyFrom("topic".getBytes()))
.build());

Protocol.TransactionInfo.Builder builder = Protocol.TransactionInfo.newBuilder()
.addAllLog(logs);

ProgramResult programResult = ProgramResult.createEmpty();
programResult.setHReturn("hreturn".getBytes());
programResult.setContractAddress(CONTRACT_ADDRESS.getBytes());

when(transactionCapsule.getTrxTrace()).thenReturn(trace);
when(trace.getReceipt()).thenReturn(receiptCapsule);
when(trace.getRuntime()).thenReturn(runtime);
when(runtime.getResult()).thenReturn(programResult);

transactionCapsule.setTrxTrace(trace);

TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule(
transactionCapsule, blockCapsule,0,0,0,
builder.build(),0);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger());
}

@Test
public void testGetInternalTransactionList() throws Exception {
BalanceContract.TransferContract.Builder builder2 =
BalanceContract.TransferContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.TransferContract);
InternalTransaction internalTransaction = new InternalTransaction(
"parentHash".getBytes(), 10, 0,
"sendAddress".getBytes(),
"transferToAddress".getBytes(),
100L, "data".getBytes(), "note",
0L, new HashMap<>()
);
List<InternalTransaction> internalTransactionList = new ArrayList<>();
internalTransactionList.add(internalTransaction);
TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Method privateMethod = TransactionLogTriggerCapsule.class.getDeclaredMethod(
"getInternalTransactionList", List.class);
privateMethod.setAccessible(true);
List<InternalTransactionPojo> pojoList = (List<InternalTransactionPojo>)
privateMethod.invoke(triggerCapsule, internalTransactionList);

Assert.assertNotNull(pojoList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import org.tron.core.capsule.TransactionCapsule;
import org.tron.p2p.utils.ByteArray;
import org.tron.protos.Protocol;
import org.tron.protos.contract.AssetIssueContractOuterClass;
import org.tron.protos.contract.BalanceContract;
import org.tron.protos.contract.Common;
import org.tron.protos.contract.SmartContractOuterClass;

public class TransactionLogTriggerCapsuleTest {

private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";

public TransactionCapsule transactionCapsule;
public BlockCapsule blockCapsule;
Expand Down Expand Up @@ -175,4 +178,70 @@ public void testConstructorWithCancelAllUnfreezeTrxCapsule() {
triggerCapsule.getTransactionLogTrigger().getExtMap().get(BANDWIDTH.name()).longValue());
}


@Test
public void testConstructorWithTransferCapsule() {
BalanceContract.TransferContract.Builder builder2 =
BalanceContract.TransferContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.TransferContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
}

@Test
public void testConstructorWithTransferAssetCapsule() {
AssetIssueContractOuterClass.TransferAssetContract.Builder builder2 =
AssetIssueContractOuterClass.TransferAssetContract.newBuilder()
.setAssetName(ByteString.copyFrom("AssetName".getBytes()))
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.TransferAssetContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
}

@Test
public void testConstructorWithTriggerSmartContract() {
SmartContractOuterClass.TriggerSmartContract.Builder builder2 =
SmartContractOuterClass.TriggerSmartContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.TriggerSmartContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
}

@Test
public void testConstructorWithCreateSmartContract() {
SmartContractOuterClass.CreateSmartContract.Builder builder2 =
SmartContractOuterClass.CreateSmartContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.CreateSmartContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.tron.common.runtime;

import java.lang.reflect.Method;

import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Test;
import org.tron.core.vm.program.Program;



@Slf4j
public class RuntimeImplMockTest {
@After
public void clearMocks() {

}

@Test
public void testSetResultCode1() throws Exception {
RuntimeImpl runtime = new RuntimeImpl();
ProgramResult programResult = new ProgramResult();
Method privateMethod = RuntimeImpl.class.getDeclaredMethod(
"setResultCode", ProgramResult.class);
privateMethod.setAccessible(true);

Program.BadJumpDestinationException badJumpDestinationException
= new Program.BadJumpDestinationException("Operation with pc isn't 'JUMPDEST': PC[%d];", 0);
programResult.setException(badJumpDestinationException);
privateMethod.invoke(runtime, programResult);

Program.OutOfTimeException outOfTimeException
= new Program.OutOfTimeException("CPU timeout for 0x0a executing");
programResult.setException(outOfTimeException);
privateMethod.invoke(runtime, programResult);

Program.PrecompiledContractException precompiledContractException
= new Program.PrecompiledContractException("precompiled contract exception");
programResult.setException(precompiledContractException);
privateMethod.invoke(runtime, programResult);

Program.StackTooSmallException stackTooSmallException
= new Program.StackTooSmallException("Expected stack size %d but actual %d;", 100, 10);
programResult.setException(stackTooSmallException);
privateMethod.invoke(runtime, programResult);

Program.JVMStackOverFlowException jvmStackOverFlowException
= new Program.JVMStackOverFlowException();
programResult.setException(jvmStackOverFlowException);
privateMethod.invoke(runtime, programResult);
}

}
Loading

0 comments on commit 92ae447

Please sign in to comment.