forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): add unit tests of framework module (tronprotocol#5534)
Co-authored-by: morgan.peng <morgan.p@qq.com>
- Loading branch information
Showing
7 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
framework/src/test/java/org/tron/core/capsule/utils/DecodeResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.tron.core.capsule.utils; | ||
|
||
import org.bouncycastle.util.encoders.Hex; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.tron.common.utils.ByteUtil; | ||
|
||
public class DecodeResultTest { | ||
|
||
@Test | ||
public void testConstruct() { | ||
DecodeResult decodeResult = new DecodeResult(0, "decoded"); | ||
Assert.assertEquals(decodeResult.getPos(), 0); | ||
Assert.assertEquals(decodeResult.getDecoded(), "decoded"); | ||
Assert.assertEquals(decodeResult.toString(), "decoded"); | ||
} | ||
|
||
@Test | ||
public void testToString() { | ||
DecodeResult decodeResult = new DecodeResult(0, "decoded"); | ||
Assert.assertEquals(decodeResult.toString(), "decoded"); | ||
decodeResult = new DecodeResult(0, ByteUtil.intToBytes(1000)); | ||
Assert.assertEquals(Hex.toHexString(ByteUtil.intToBytes(1000)), decodeResult.toString()); | ||
Object[] decodedData = {"aa","bb"}; | ||
decodeResult = new DecodeResult(0, decodedData); | ||
Assert.assertEquals("aabb", decodeResult.toString()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
framework/src/test/java/org/tron/core/capsule/utils/RLPListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.tron.core.capsule.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class RLPListTest { | ||
|
||
@Test | ||
public void testRecursivePrint() { | ||
RLPItem element = new RLPItem("rlpItem".getBytes()); | ||
Assert.assertEquals(new String(element.getRLPData()), "rlpItem"); | ||
RLPList.recursivePrint(element); | ||
RLPList rlpList = new RLPList(); | ||
rlpList.add(new RLPItem("rlpItem0".getBytes())); | ||
RLPList.recursivePrint(rlpList); | ||
Assert.assertThrows(RuntimeException.class, () -> RLPList.recursivePrint(null)); | ||
} | ||
|
||
@Test | ||
public void testGetRLPData() { | ||
RLPList rlpList = new RLPList(); | ||
rlpList.setRLPData("rlpData".getBytes()); | ||
Assert.assertEquals(new String(rlpList.getRLPData()), "rlpData"); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
framework/src/test/java/org/tron/core/services/jsonrpc/BlockResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.tron.core.services.jsonrpc; | ||
|
||
import com.google.protobuf.ByteString; | ||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.common.utils.ByteArray; | ||
import org.tron.core.Constant; | ||
import org.tron.core.Wallet; | ||
import org.tron.core.capsule.BlockCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.services.jsonrpc.types.BlockResult; | ||
import org.tron.protos.Protocol; | ||
|
||
public class BlockResultTest extends BaseTest { | ||
|
||
@Resource | ||
private Wallet wallet; | ||
|
||
static { | ||
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); | ||
} | ||
|
||
@Test | ||
public void testBlockResult() { | ||
Protocol.Transaction.raw.Builder raw = Protocol.Transaction.raw.newBuilder(); | ||
Protocol.Transaction.Contract.Builder contract = Protocol.Transaction.Contract.newBuilder(); | ||
contract.setType(Protocol.Transaction.Contract.ContractType.UpdateBrokerageContract); | ||
raw.addContract(contract.build()); | ||
|
||
Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); | ||
BlockCapsule blockCapsule = new BlockCapsule(Protocol.Block.newBuilder().setBlockHeader( | ||
Protocol.BlockHeader.newBuilder().setRawData(Protocol.BlockHeader.raw.newBuilder() | ||
.setParentHash(ByteString.copyFrom(ByteArray.fromHexString( | ||
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b82"))) | ||
.setNumber(0))).addTransactions(transaction).build()); | ||
|
||
BlockResult blockResult = new BlockResult(blockCapsule.getInstance(),true, wallet); | ||
Assert.assertEquals(blockResult.getHash(), | ||
"0x000000000000000036393ced0658419d3c251bc14ffab8d10c8b0898451054fa"); | ||
Assert.assertEquals(blockResult.getTransactions().length, 1); | ||
Assert.assertEquals(blockResult.getGasUsed(),"0x0"); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.tron.core.services.jsonrpc; | ||
|
||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.core.Constant; | ||
import org.tron.core.Wallet; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.exception.JsonRpcInvalidParamsException; | ||
import org.tron.core.exception.JsonRpcInvalidRequestException; | ||
import org.tron.core.services.jsonrpc.types.BuildArguments; | ||
import org.tron.core.services.jsonrpc.types.CallArguments; | ||
import org.tron.protos.Protocol; | ||
|
||
public class BuildArgumentsTest extends BaseTest { | ||
|
||
@Resource | ||
private Wallet wallet; | ||
|
||
private BuildArguments buildArguments; | ||
|
||
static { | ||
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); | ||
} | ||
|
||
@Before | ||
public void initBuildArgs() { | ||
buildArguments = new BuildArguments( | ||
"0x0000000000000000000000000000000000000000", | ||
"0x0000000000000000000000000000000000000001","0x10","0.01","0x100", | ||
"","0",9L,10000L,"",10L, | ||
2000L,"args",1,"",true); | ||
} | ||
|
||
|
||
@Test | ||
public void testBuildArgument() { | ||
CallArguments callArguments = new CallArguments( | ||
"0x0000000000000000000000000000000000000000", | ||
"0x0000000000000000000000000000000000000001","0x10","0.01","0x100", | ||
"","0"); | ||
BuildArguments buildArguments = new BuildArguments(callArguments); | ||
Assert.assertEquals(buildArguments.getFrom(), | ||
"0x0000000000000000000000000000000000000000"); | ||
Assert.assertEquals(buildArguments.getTo(), | ||
"0x0000000000000000000000000000000000000001"); | ||
Assert.assertEquals(buildArguments.getGas(), "0x10"); | ||
Assert.assertEquals(buildArguments.getGasPrice(), "0.01"); | ||
} | ||
|
||
@Test | ||
public void testGetContractType() | ||
throws JsonRpcInvalidRequestException, JsonRpcInvalidParamsException { | ||
Protocol.Transaction.Contract.ContractType contractType = | ||
buildArguments.getContractType(wallet); | ||
Assert.assertEquals(contractType, Protocol.Transaction.Contract.ContractType.TransferContract); | ||
} | ||
|
||
@Test | ||
public void testParseValue() throws JsonRpcInvalidParamsException { | ||
long value = buildArguments.parseValue(); | ||
Assert.assertEquals(value, 256L); | ||
} | ||
|
||
@Test | ||
public void testParseGas() throws JsonRpcInvalidParamsException { | ||
long gas = buildArguments.parseGas(); | ||
Assert.assertEquals(gas, 16L); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.tron.core.services.jsonrpc; | ||
|
||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.core.Constant; | ||
import org.tron.core.Wallet; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.exception.JsonRpcInvalidParamsException; | ||
import org.tron.core.exception.JsonRpcInvalidRequestException; | ||
import org.tron.core.services.jsonrpc.types.CallArguments; | ||
import org.tron.protos.Protocol; | ||
|
||
public class CallArgumentsTest extends BaseTest { | ||
|
||
@Resource | ||
private Wallet wallet; | ||
|
||
private CallArguments callArguments; | ||
|
||
static { | ||
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); | ||
} | ||
|
||
@Before | ||
public void init() { | ||
callArguments = new CallArguments("0x0000000000000000000000000000000000000000", | ||
"0x0000000000000000000000000000000000000001","0x10","0.01","0x100", | ||
"","0"); | ||
} | ||
|
||
@Test | ||
public void testGetContractType() | ||
throws JsonRpcInvalidRequestException, JsonRpcInvalidParamsException { | ||
Protocol.Transaction.Contract.ContractType contractType = callArguments.getContractType(wallet); | ||
Assert.assertEquals(Protocol.Transaction.Contract.ContractType.TransferContract, contractType); | ||
} | ||
|
||
@Test | ||
public void testParseValue() throws JsonRpcInvalidParamsException { | ||
long value = callArguments.parseValue(); | ||
Assert.assertEquals(256L, value); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
framework/src/test/java/org/tron/core/services/jsonrpc/TransactionReceiptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.tron.core.services.jsonrpc; | ||
|
||
import com.google.protobuf.ByteString; | ||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.common.utils.ByteArray; | ||
import org.tron.core.Constant; | ||
import org.tron.core.Wallet; | ||
import org.tron.core.capsule.TransactionRetCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.services.jsonrpc.types.TransactionReceipt; | ||
import org.tron.core.store.TransactionRetStore; | ||
import org.tron.protos.Protocol; | ||
|
||
public class TransactionReceiptTest extends BaseTest { | ||
|
||
@Resource | ||
private Wallet wallet; | ||
|
||
@Resource | ||
private TransactionRetStore transactionRetStore; | ||
|
||
static { | ||
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); | ||
} | ||
|
||
@Test | ||
public void testTransactionReceipt() { | ||
Protocol.TransactionInfo transactionInfo = Protocol.TransactionInfo.newBuilder() | ||
.setId(ByteString.copyFrom("1".getBytes())) | ||
.setContractAddress(ByteString.copyFrom("address1".getBytes())) | ||
.setReceipt(Protocol.ResourceReceipt.newBuilder() | ||
.setEnergyUsageTotal(0L) | ||
.setResult(Protocol.Transaction.Result.contractResult.DEFAULT) | ||
.build()) | ||
.addLog(Protocol.TransactionInfo.Log.newBuilder() | ||
.setAddress(ByteString.copyFrom("address1".getBytes())) | ||
.setData(ByteString.copyFrom("data".getBytes())) | ||
.build()) | ||
.build(); | ||
TransactionRetCapsule transactionRetCapsule = new TransactionRetCapsule(); | ||
transactionRetCapsule.addTransactionInfo(transactionInfo); | ||
transactionRetStore.put(ByteArray.fromLong(1), transactionRetCapsule); | ||
|
||
Protocol.Transaction.raw.Builder raw = Protocol.Transaction.raw.newBuilder(); | ||
Protocol.Transaction.Contract.Builder contract = Protocol.Transaction.Contract.newBuilder(); | ||
contract.setType(Protocol.Transaction.Contract.ContractType.UpdateBrokerageContract); | ||
raw.addContract(contract.build()); | ||
Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); | ||
|
||
TransactionReceipt transactionReceipt = new TransactionReceipt( | ||
Protocol.Block.newBuilder().setBlockHeader( | ||
Protocol.BlockHeader.newBuilder().setRawData( | ||
Protocol.BlockHeader.raw.newBuilder().setNumber(1))).addTransactions( | ||
transaction).build(), transactionInfo, wallet); | ||
|
||
Assert.assertEquals(transactionReceipt.getBlockNumber(),"0x1"); | ||
Assert.assertEquals(transactionReceipt.getTransactionIndex(),"0x0"); | ||
Assert.assertEquals(transactionReceipt.getLogs().length,1); | ||
Assert.assertEquals(transactionReceipt.getBlockHash(), | ||
"0x0000000000000001464f071c8a336fd22eb5145dff1b245bda013ec89add8497"); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
framework/src/test/java/org/tron/core/services/jsonrpc/TransactionResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.tron.core.services.jsonrpc; | ||
|
||
import com.google.protobuf.ByteString; | ||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.common.utils.ByteArray; | ||
import org.tron.core.Constant; | ||
import org.tron.core.Wallet; | ||
import org.tron.core.capsule.BlockCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.services.jsonrpc.types.TransactionResult; | ||
import org.tron.protos.Protocol; | ||
|
||
public class TransactionResultTest extends BaseTest { | ||
|
||
@Resource | ||
private Wallet wallet; | ||
|
||
static { | ||
Args.setParam(new String[]{"-d", dbPath()}, Constant.TEST_CONF); | ||
} | ||
|
||
@Test | ||
public void testBuildTransactionResultWithBlock() { | ||
Protocol.Transaction.raw.Builder raw = Protocol.Transaction.raw.newBuilder().addContract( | ||
Protocol.Transaction.Contract.newBuilder().setType( | ||
Protocol.Transaction.Contract.ContractType.TriggerSmartContract)); | ||
Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); | ||
BlockCapsule blockCapsule = new BlockCapsule(Protocol.Block.newBuilder().setBlockHeader( | ||
Protocol.BlockHeader.newBuilder().setRawData(Protocol.BlockHeader.raw.newBuilder() | ||
.setParentHash(ByteString.copyFrom(ByteArray.fromHexString( | ||
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b82"))) | ||
.setNumber(9))).addTransactions(transaction).build()); | ||
|
||
TransactionResult transactionResult = new TransactionResult(blockCapsule,0, transaction, | ||
100,1, wallet); | ||
Assert.assertEquals(transactionResult.getBlockNumber(), "0x9"); | ||
Assert.assertEquals(transactionResult.getHash(), | ||
"0xdebef90d0a8077620711b1b5af2b702665887ddcbf80868108026e1ab5e0bfb7"); | ||
Assert.assertEquals(transactionResult.getGasPrice(), "0x1"); | ||
Assert.assertEquals(transactionResult.getGas(), "0x64"); | ||
} | ||
|
||
@Test | ||
public void testBuildTransactionResult() { | ||
Protocol.Transaction.raw.Builder raw = Protocol.Transaction.raw.newBuilder().addContract( | ||
Protocol.Transaction.Contract.newBuilder().setType( | ||
Protocol.Transaction.Contract.ContractType.TriggerSmartContract)); | ||
Protocol.Transaction transaction = Protocol.Transaction.newBuilder().setRawData(raw).build(); | ||
TransactionResult transactionResult = new TransactionResult(transaction, wallet); | ||
Assert.assertEquals(transactionResult.getHash(), | ||
"0xdebef90d0a8077620711b1b5af2b702665887ddcbf80868108026e1ab5e0bfb7"); | ||
Assert.assertEquals(transactionResult.getGasPrice(), "0x"); | ||
Assert.assertEquals(transactionResult.getNonce(), "0x0000000000000000"); | ||
} | ||
|
||
} |