Skip to content

Commit e200865

Browse files
committed
test: clean up remaining test fixtures and assertions
1 parent 37ccdc4 commit e200865

39 files changed

Lines changed: 1039 additions & 1410 deletions

‎framework/src/test/java/org/tron/common/crypto/BouncyCastleTest.java‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThrows;
56
import static org.tron.common.utils.client.utils.AbiUtil.generateOccupationConstantPrivateKey;
67

78
import java.math.BigInteger;
89
import java.security.SignatureException;
910
import java.util.Arrays;
1011
import org.bouncycastle.crypto.digests.SM3Digest;
1112
import org.bouncycastle.util.encoders.Hex;
12-
import org.junit.Assert;
1313
import org.junit.Test;
1414
import org.tron.common.crypto.sm2.SM2;
1515
import org.tron.common.utils.Sha256Hash;
@@ -135,19 +135,18 @@ public void testSM2SpongySignature() throws SignatureException {
135135
}
136136

137137
@Test
138-
public void testSignToAddress() {
138+
public void testSignToAddressWrapsInvalidSignature() {
139139
String messageHash = "818e0e76976123b9b78b6076cc2b5d53e61b49ff9cf78304de688a860ce7cb95";
140-
String base64Sign = "G1y76mVO6TRpFwp3qOiLVzHA8uFsrDiOL7hbC2uN9qTHHiLypaW4vnQkfkoUygjo5qBd"
141-
+ "+NlYQ/mAPVWKu6K00co=";
142-
try {
143-
SignUtils.signatureToAddress(Hex.decode(messageHash), base64Sign, Boolean.TRUE);
144-
} catch (Exception e) {
145-
Assert.assertTrue(e instanceof SignatureException);
146-
}
147-
try {
148-
SignUtils.signatureToAddress(Hex.decode(messageHash), base64Sign, Boolean.FALSE);
149-
} catch (Exception e) {
150-
Assert.assertTrue(e instanceof SignatureException);
151-
}
140+
String truncatedBase64Signature = "AA==";
141+
142+
SignatureException ecException = assertThrows(SignatureException.class,
143+
() -> SignUtils.signatureToAddress(
144+
Hex.decode(messageHash), truncatedBase64Signature, true));
145+
assertEquals(SignatureException.class, ecException.getCause().getClass());
146+
147+
SignatureException sm2Exception = assertThrows(SignatureException.class,
148+
() -> SignUtils.signatureToAddress(
149+
Hex.decode(messageHash), truncatedBase64Signature, false));
150+
assertEquals(SignatureException.class, sm2Exception.getCause().getClass());
152151
}
153152
}

‎framework/src/test/java/org/tron/common/crypto/SignatureInterfaceTest.java‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNull;
56

7+
import java.nio.charset.StandardCharsets;
8+
import java.security.SignatureException;
69
import java.util.Arrays;
7-
import lombok.extern.slf4j.Slf4j;
810
import org.bouncycastle.util.encoders.Hex;
911
import org.junit.Test;
1012
import org.tron.common.crypto.sm2.SM2;
1113
import org.tron.common.utils.PublicMethod;
1214

13-
14-
@Slf4j
1515
public class SignatureInterfaceTest {
1616

1717
private String SM2_privString = PublicMethod.getSM2RandomPrivateKey();
@@ -31,14 +31,9 @@ public class SignatureInterfaceTest {
3131

3232

3333
@Test
34-
public void testContructor() {
35-
SignInterface sign = new SM2();
36-
logger.info(Hex.toHexString(sign.getPrivateKey()) + " :SM2 Generated privkey");
37-
logger.info(Hex.toHexString(sign.getPubKey()) + " :SM2 Generated pubkey");
38-
39-
sign = new ECKey();
40-
logger.info(Hex.toHexString(sign.getPrivateKey()) + " :ECDSA Generated privkey");
41-
logger.info(Hex.toHexString(sign.getPubKey()) + " :ECDSA Generated pubkey");
34+
public void testConstructorGeneratesUsableKeys() throws SignatureException {
35+
assertGeneratedKey(new SM2(), false);
36+
assertGeneratedKey(new ECKey(), true);
4237
}
4338

4439
@Test
@@ -63,10 +58,10 @@ public void testPublicKey() {
6358
@Test
6459
public void testNullKey() {
6560
SignInterface sign = new SM2(SM2_pubKey, false);
66-
assertEquals(null, sign.getPrivateKey());
61+
assertNull(sign.getPrivateKey());
6762

6863
sign = new ECKey(EC_pubKey, false);
69-
assertEquals(null, sign.getPrivateKey());
64+
assertNull(sign.getPrivateKey());
7065
}
7166

7267
@Test
@@ -75,11 +70,25 @@ public void testAddress() {
7570
byte[] prefix_address = sign.getAddress();
7671
byte[] address = Arrays.copyOfRange(prefix_address, 1, prefix_address.length);
7772
byte[] addressTmp = Arrays.copyOfRange(Hex.decode(SM2_address), 1, prefix_address.length);
78-
assertEquals(Hex.toHexString(addressTmp), Hex.toHexString(address));
73+
assertArrayEquals(addressTmp, address);
7974
sign = new ECKey(EC_pubKey, false);
8075
prefix_address = sign.getAddress();
8176
address = Arrays.copyOfRange(prefix_address, 1, prefix_address.length);
8277
byte[] ecAddressTmp = Arrays.copyOfRange(Hex.decode(EC_address), 1, prefix_address.length);
83-
assertEquals(Hex.toHexString(ecAddressTmp), Hex.toHexString(address));
78+
assertArrayEquals(ecAddressTmp, address);
79+
}
80+
81+
private void assertGeneratedKey(SignInterface sign, boolean ecKeyCryptoEngine)
82+
throws SignatureException {
83+
assertEquals(32, sign.getPrivateKey().length);
84+
assertEquals(65, sign.getPubKey().length);
85+
assertEquals(21, sign.getAddress().length);
86+
assertEquals(64, sign.getNodeId().length);
87+
88+
byte[] hash = Hash.sha3("signature-interface".getBytes(StandardCharsets.UTF_8));
89+
String signature = sign.signHash(hash);
90+
assertEquals(65, sign.Base64toBytes(signature).length);
91+
assertArrayEquals(sign.getAddress(),
92+
SignUtils.signatureToAddress(hash, signature, ecKeyCryptoEngine));
8493
}
8594
}

‎framework/src/test/java/org/tron/common/jetty/SizeLimitHandlerTest.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,23 @@ public void testWireBytesMatchCheckBodySizeForAsciiJson() throws Exception {
328328
}
329329

330330
/**
331-
* For UTF-8 JSON with multi-byte characters (CJK), wire bytes and
332-
* {@code body.getBytes().length} must still be identical - UTF-8 round-trips
333-
* through {@code request.getReader()} -> {@code String.getBytes()} losslessly.
331+
* {@code Util.checkBodySize()} uses the platform default charset after the request has been
332+
* decoded into a String. That value is not necessarily the UTF-8 wire size (for example, a
333+
* US-ASCII default charset replaces CJK characters). The primary SizeLimitHandler assertion is
334+
* covered by {@link #testLimitIsBasedOnBytesNotCharacters()}; this test mirrors the deprecated
335+
* servlet-side check without assuming a particular process charset.
334336
*/
335337
@Test
336-
public void testWireBytesMatchCheckBodySizeForUtf8Json() throws Exception {
338+
public void testCheckBodySizeUsesPlatformCharsetForUtf8Json() throws Exception {
337339
String jsonBody = "{\"name\":\"测试地址\",\"amount\":100}";
338-
int wireBytes = jsonBody.getBytes("UTF-8").length;
340+
int expectedServletBytes = jsonBody.getBytes().length;
339341

340342
String respBody = postForBody(httpServerUri, new StringEntity(jsonBody, "UTF-8"));
341343
JSONObject json = JSONObject.parseObject(respBody);
342344
int servletBytes = json.getIntValue("bytes");
343345

344-
Assert.assertEquals("wire bytes should equal checkBodySize for UTF-8 JSON",
345-
wireBytes, servletBytes);
346+
Assert.assertEquals("checkBodySize should use the platform default charset",
347+
expectedServletBytes, servletBytes);
346348
}
347349

348350
/**

‎framework/src/test/java/org/tron/common/logsfilter/EventLoaderTest.java‎

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertSame;
56
import static org.junit.Assert.assertTrue;
67
import static org.mockito.Mockito.mock;
78
import static org.mockito.Mockito.when;
89

910
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.HashMap;
1013
import java.util.List;
11-
import org.junit.Assert;
14+
import java.util.Map;
1215
import org.junit.Test;
1316
import org.pf4j.PluginDescriptor;
1417
import org.pf4j.PluginManager;
1518
import org.pf4j.PluginWrapper;
1619
import org.tron.common.logsfilter.trigger.BlockLogTrigger;
20+
import org.tron.common.logsfilter.trigger.InternalTransactionPojo;
21+
import org.tron.common.logsfilter.trigger.LogPojo;
1722
import org.tron.common.logsfilter.trigger.TransactionLogTrigger;
23+
import org.tron.common.logsfilter.trigger.Trigger;
1824

1925
public class EventLoaderTest {
2026

@@ -82,49 +88,97 @@ private static boolean checkVersion(String version) {
8288
@Test
8389
public void testBlockLogTrigger() {
8490
BlockLogTrigger blt = new BlockLogTrigger();
85-
blt.setBlockHash(blt.getBlockHash());
86-
blt.setBlockNumber(blt.getBlockNumber());
87-
blt.setTransactionSize(blt.getTransactionSize());
88-
blt.setLatestSolidifiedBlockNumber(blt.getLatestSolidifiedBlockNumber());
89-
blt.setTriggerName(blt.getTriggerName());
90-
blt.setTimeStamp(blt.getTimeStamp());
91-
blt.setTransactionList(blt.getTransactionList());
92-
Assert.assertNotNull(blt.toString());
91+
List<String> transactionIds = Arrays.asList("tx-1", "tx-2");
92+
blt.setBlockHash("block-hash");
93+
blt.setBlockNumber(42L);
94+
blt.setTransactionSize(2L);
95+
blt.setLatestSolidifiedBlockNumber(40L);
96+
blt.setTimeStamp(123456L);
97+
blt.setTransactionList(transactionIds);
98+
blt.setRemoved(true);
99+
100+
assertEquals(Trigger.BLOCK_TRIGGER_NAME, blt.getTriggerName());
101+
assertEquals("block-hash", blt.getBlockHash());
102+
assertEquals(42L, blt.getBlockNumber());
103+
assertEquals(2L, blt.getTransactionSize());
104+
assertEquals(40L, blt.getLatestSolidifiedBlockNumber());
105+
assertEquals(123456L, blt.getTimeStamp());
106+
assertSame(transactionIds, blt.getTransactionList());
107+
assertTrue(blt.isRemoved());
108+
assertEquals("triggerName: blockTrigger, timestamp: 123456, blockNumber: 42, "
109+
+ "blockhash: block-hash, transactionSize: 2, latestSolidifiedBlockNumber: 40, "
110+
+ "removed: true, transactionList: [tx-1, tx-2]", blt.toString());
93111
}
94112

95113
@Test
96114
public void testTransactionLogTrigger() {
97115
TransactionLogTrigger tlt = new TransactionLogTrigger();
98-
tlt.setBlockHash(tlt.getBlockHash());
99-
tlt.setBlockNumber(tlt.getBlockNumber());
100-
tlt.setTransactionId(tlt.getTransactionId());
101-
tlt.setLatestSolidifiedBlockNumber(tlt.getLatestSolidifiedBlockNumber());
102-
tlt.setTriggerName(tlt.getTriggerName());
103-
tlt.setTimeStamp(tlt.getTimeStamp());
104-
tlt.setEnergyFee(tlt.getEnergyFee());
105-
tlt.setNetFee(tlt.getNetFee());
106-
tlt.setEnergyUsage(tlt.getEnergyUsage());
107-
tlt.setAssetAmount(tlt.getAssetAmount());
108-
tlt.setContractAddress(tlt.getContractAddress());
109-
tlt.setResult(tlt.getResult());
110-
tlt.setContractResult(tlt.getContractResult());
111-
tlt.setContractType(tlt.getContractType());
112-
tlt.setContractCallValue(tlt.getContractCallValue());
113-
tlt.setFromAddress(tlt.getFromAddress());
114-
tlt.setToAddress(tlt.getToAddress());
115-
tlt.setTransactionIndex(tlt.getTransactionIndex());
116-
tlt.setFeeLimit(tlt.getFeeLimit());
117-
tlt.setCumulativeEnergyUsed(tlt.getCumulativeEnergyUsed());
118-
tlt.setData(tlt.getData());
119-
tlt.setOriginEnergyUsage(tlt.getOriginEnergyUsage());
120-
tlt.setEnergyUsageTotal(tlt.getEnergyUsageTotal());
121-
tlt.setNetUsage(tlt.getNetUsage());
122-
tlt.setAssetName(tlt.getAssetName());
123-
tlt.setInternalTransactionList(tlt.getInternalTransactionList());
124-
tlt.setPreCumulativeLogCount(tlt.getPreCumulativeLogCount());
125-
tlt.setLogList(tlt.getLogList());
126-
tlt.setEnergyUnitPrice(tlt.getEnergyUnitPrice());
127-
tlt.setTimeStamp(1L);
128-
Assert.assertNotNull(tlt.toString());
116+
List<InternalTransactionPojo> internalTransactions = new ArrayList<>();
117+
List<LogPojo> logs = new ArrayList<>();
118+
Map<String, Long> extensions = new HashMap<>();
119+
extensions.put("retryCount", 3L);
120+
121+
tlt.setTransactionId("transaction-id");
122+
tlt.setBlockHash("block-hash");
123+
tlt.setBlockNumber(101L);
124+
tlt.setEnergyUsage(11L);
125+
tlt.setEnergyFee(12L);
126+
tlt.setOriginEnergyUsage(13L);
127+
tlt.setEnergyUsageTotal(14L);
128+
tlt.setNetUsage(15L);
129+
tlt.setNetFee(16L);
130+
tlt.setResult("SUCCESS");
131+
tlt.setContractAddress("contract-address");
132+
tlt.setContractType("TriggerSmartContract");
133+
tlt.setFeeLimit(17L);
134+
tlt.setContractCallValue(18L);
135+
tlt.setContractResult("contract-result");
136+
tlt.setFromAddress("from-address");
137+
tlt.setToAddress("to-address");
138+
tlt.setAssetName("asset-name");
139+
tlt.setAssetAmount(19L);
140+
tlt.setLatestSolidifiedBlockNumber(100L);
141+
tlt.setInternalTransactionList(internalTransactions);
142+
tlt.setData("deadbeef");
143+
tlt.setTransactionIndex(2);
144+
tlt.setCumulativeEnergyUsed(20L);
145+
tlt.setPreCumulativeLogCount(21L);
146+
tlt.setLogList(logs);
147+
tlt.setEnergyUnitPrice(22L);
148+
tlt.setExtMap(extensions);
149+
tlt.setRemoved(true);
150+
tlt.setTimeStamp(123456L);
151+
152+
assertEquals(Trigger.TRANSACTION_TRIGGER_NAME, tlt.getTriggerName());
153+
assertEquals("transaction-id", tlt.getTransactionId());
154+
assertEquals("block-hash", tlt.getBlockHash());
155+
assertEquals(101L, tlt.getBlockNumber());
156+
assertEquals(11L, tlt.getEnergyUsage());
157+
assertEquals(12L, tlt.getEnergyFee());
158+
assertEquals(13L, tlt.getOriginEnergyUsage());
159+
assertEquals(14L, tlt.getEnergyUsageTotal());
160+
assertEquals(15L, tlt.getNetUsage());
161+
assertEquals(16L, tlt.getNetFee());
162+
assertEquals("SUCCESS", tlt.getResult());
163+
assertEquals("contract-address", tlt.getContractAddress());
164+
assertEquals("TriggerSmartContract", tlt.getContractType());
165+
assertEquals(17L, tlt.getFeeLimit());
166+
assertEquals(18L, tlt.getContractCallValue());
167+
assertEquals("contract-result", tlt.getContractResult());
168+
assertEquals("from-address", tlt.getFromAddress());
169+
assertEquals("to-address", tlt.getToAddress());
170+
assertEquals("asset-name", tlt.getAssetName());
171+
assertEquals(19L, tlt.getAssetAmount());
172+
assertEquals(100L, tlt.getLatestSolidifiedBlockNumber());
173+
assertSame(internalTransactions, tlt.getInternalTransactionList());
174+
assertEquals("deadbeef", tlt.getData());
175+
assertEquals(2, tlt.getTransactionIndex());
176+
assertEquals(20L, tlt.getCumulativeEnergyUsed());
177+
assertEquals(21L, tlt.getPreCumulativeLogCount());
178+
assertSame(logs, tlt.getLogList());
179+
assertEquals(22L, tlt.getEnergyUnitPrice());
180+
assertSame(extensions, tlt.getExtMap());
181+
assertTrue(tlt.isRemoved());
182+
assertEquals(123456L, tlt.getTimeStamp());
129183
}
130184
}

‎framework/src/test/java/org/tron/common/logsfilter/FilterQueryTest.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ public synchronized void testMatchFilter() {
103103
}
104104

105105
TriggerCapsule triggerCapsule = new TriggerCapsule();
106-
try {
107-
triggerCapsule.processTrigger();
108-
} catch (Exception e) {
109-
assertTrue(e instanceof UnsupportedOperationException);
110-
}
106+
assertThrows(UnsupportedOperationException.class, triggerCapsule::processTrigger);
111107
}
112108
}

0 commit comments

Comments
 (0)