Skip to content

Commit ac528da

Browse files
committed
clean code
1 parent b1970a6 commit ac528da

1 file changed

Lines changed: 34 additions & 40 deletions

File tree

framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,24 +1017,28 @@ public void testNewFilterFinalizedBlock() {
10171017
}
10181018
}
10191019

1020+
private String generateLongHexString(int length) {
1021+
StringBuilder longInput = new StringBuilder();
1022+
for (int i = 0; i < length; i++) {
1023+
longInput.append("a");
1024+
}
1025+
return longInput.toString();
1026+
}
1027+
10201028
@Test
10211029
public void testGetBlockReceipts() {
10221030
// Test null blockNumOrTag
10231031
try {
1024-
tronJsonRpc.getBlockReceipts( null);
1032+
tronJsonRpc.getBlockReceipts(null);
10251033
Assert.fail("Should throw JsonRpcInvalidParamsException for null blockNumOrTag");
10261034
} catch (Exception e) {
10271035
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
10281036
e instanceof JsonRpcInvalidParamsException);
10291037
}
10301038

1031-
// Test too long blockNumOrTag (DDoS protection)
1032-
StringBuilder longInput = new StringBuilder();
1033-
for (int i = 0; i < 130; i++) { // Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1034-
longInput.append("a");
1035-
}
10361039
try {
1037-
tronJsonRpc.getBlockReceipts(longInput.toString());
1040+
// Test Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1041+
tronJsonRpc.getBlockReceipts(generateLongHexString(130));
10381042
Assert.fail("Should throw JsonRpcInvalidParamsException for too long blockNumOrTag");
10391043
} catch (Exception e) {
10401044
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
@@ -1044,7 +1048,7 @@ public void testGetBlockReceipts() {
10441048
try {
10451049
List<TransactionReceipt> transactionReceiptList = tronJsonRpc.getBlockReceipts("0x2710");
10461050
Assert.assertFalse(transactionReceiptList.isEmpty());
1047-
for (TransactionReceipt transactionReceipt: transactionReceiptList) {
1051+
for (TransactionReceipt transactionReceipt : transactionReceiptList) {
10481052
TransactionReceipt transactionReceipt1
10491053
= tronJsonRpc.getTransactionReceipt(transactionReceipt.getTransactionHash());
10501054

@@ -1131,19 +1135,17 @@ public void testValidateBlockNumOrHashOrTag_EthGetBlockTransactionCountByNumber(
11311135
tronJsonRpc.ethGetBlockTransactionCountByNumber(null);
11321136
Assert.fail("Should throw JsonRpcInvalidParamsException for null input");
11331137
} catch (Exception e) {
1134-
Assert.assertTrue("Should be JsonRpcInvalidParamsException", e instanceof JsonRpcInvalidParamsException);
1138+
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
1139+
e instanceof JsonRpcInvalidParamsException);
11351140
}
11361141

1137-
// Test too long input (DDoS protection)
1138-
StringBuilder longInput = new StringBuilder();
1139-
for (int i = 0; i < 130; i++) { // Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1140-
longInput.append("a");
1141-
}
11421142
try {
1143-
tronJsonRpc.ethGetBlockTransactionCountByNumber(longInput.toString());
1143+
// Test Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1144+
tronJsonRpc.ethGetBlockTransactionCountByNumber(generateLongHexString(130));
11441145
Assert.fail("Should throw JsonRpcInvalidParamsException for too long input");
11451146
} catch (Exception e) {
1146-
Assert.assertTrue("Should be JsonRpcInvalidParamsException", e instanceof JsonRpcInvalidParamsException);
1147+
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
1148+
e instanceof JsonRpcInvalidParamsException);
11471149
}
11481150

11491151
// Test valid input (should not throw exception)
@@ -1163,19 +1165,17 @@ public void testValidateBlockNumOrHashOrTag_EthGetBlockByNumber() {
11631165
tronJsonRpc.ethGetBlockByNumber(null, false);
11641166
Assert.fail("Should throw JsonRpcInvalidParamsException for null input");
11651167
} catch (Exception e) {
1166-
Assert.assertTrue("Should be JsonRpcInvalidParamsException", e instanceof JsonRpcInvalidParamsException);
1168+
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
1169+
e instanceof JsonRpcInvalidParamsException);
11671170
}
11681171

1169-
// Test too long input (DDoS protection)
1170-
StringBuilder longInput = new StringBuilder();
1171-
for (int i = 0; i < 130; i++) { // Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1172-
longInput.append("a");
1173-
}
11741172
try {
1175-
tronJsonRpc.ethGetBlockByNumber(longInput.toString(), false);
1173+
// Test Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1174+
tronJsonRpc.ethGetBlockByNumber(generateLongHexString(130), false);
11761175
Assert.fail("Should throw JsonRpcInvalidParamsException for too long input");
11771176
} catch (Exception e) {
1178-
Assert.assertTrue("Should be JsonRpcInvalidParamsException", e instanceof JsonRpcInvalidParamsException);
1177+
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
1178+
e instanceof JsonRpcInvalidParamsException);
11791179
}
11801180

11811181
// Test valid input (should not throw exception)
@@ -1195,19 +1195,17 @@ public void testValidateBlockNumOrHashOrTag_GetTrxBalance() {
11951195
tronJsonRpc.getTrxBalance(OWNER_ADDRESS, null);
11961196
Assert.fail("Should throw JsonRpcInvalidParamsException for null blockNumOrTag");
11971197
} catch (Exception e) {
1198-
Assert.assertTrue("Should be JsonRpcInvalidParamsException", e instanceof JsonRpcInvalidParamsException);
1198+
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
1199+
e instanceof JsonRpcInvalidParamsException);
11991200
}
12001201

1201-
// Test too long blockNumOrTag (DDoS protection)
1202-
StringBuilder longInput = new StringBuilder();
1203-
for (int i = 0; i < 130; i++) { // Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1204-
longInput.append("a");
1205-
}
12061202
try {
1207-
tronJsonRpc.getTrxBalance(OWNER_ADDRESS, longInput.toString());
1203+
// Test Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1204+
tronJsonRpc.getTrxBalance(OWNER_ADDRESS, generateLongHexString(130));
12081205
Assert.fail("Should throw JsonRpcInvalidParamsException for too long blockNumOrTag");
12091206
} catch (Exception e) {
1210-
Assert.assertTrue("Should be JsonRpcInvalidParamsException", e instanceof JsonRpcInvalidParamsException);
1207+
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
1208+
e instanceof JsonRpcInvalidParamsException);
12111209
}
12121210

12131211
// Test valid blockNumOrTag (should not throw validation exception)
@@ -1224,25 +1222,21 @@ public void testValidateBlockNumOrHashOrTag_GetTrxBalance() {
12241222
public void testValidateBlockNumOrHashOrTag_getTransactionByBlockNumberAndIndex() {
12251223
// Test null blockNumOrTag
12261224
try {
1227-
tronJsonRpc.getTransactionByBlockNumberAndIndex( null, "index");
1225+
tronJsonRpc.getTransactionByBlockNumberAndIndex(null, "0x0");
12281226
Assert.fail("Should throw JsonRpcInvalidParamsException for null blockNumOrTag");
12291227
} catch (Exception e) {
12301228
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
12311229
e instanceof JsonRpcInvalidParamsException);
12321230
}
12331231

1234-
// Test too long blockNumOrTag (DDoS protection)
1235-
StringBuilder longInput = new StringBuilder();
1236-
for (int i = 0; i < 130; i++) { // Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1237-
longInput.append("a");
1238-
}
12391232
try {
1240-
tronJsonRpc.getTransactionByBlockNumberAndIndex(longInput.toString(), "index");
1233+
// Test Exceeds MAX_BLOCK_IDENTIFIER_LENGTH (128)
1234+
tronJsonRpc.getTransactionByBlockNumberAndIndex(generateLongHexString(130), "0x0");
12411235
Assert.fail("Should throw JsonRpcInvalidParamsException for too long blockNumOrTag");
12421236
} catch (Exception e) {
12431237
Assert.assertTrue("Should be JsonRpcInvalidParamsException",
12441238
e instanceof JsonRpcInvalidParamsException);
12451239
}
12461240
}
12471241

1248-
}
1242+
}

0 commit comments

Comments
 (0)