Skip to content

Commit 81dd2e2

Browse files
committed
test(http): address review comments - assertThrows, ASCII punctuation, finally guard
- Replace try/catch/fail pattern with Assert.assertThrows in UtilTest and ArgsTest (4 cases) - Replace non-ASCII punctuation (→, —) with ASCII (->, -) in newly added test comments - Hoist originalLimit before outer try in testJsonRpcSizeLimitIntegration so restore executes even when start() throws
1 parent 7df020e commit 81dd2e2

4 files changed

Lines changed: 30 additions & 46 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class SizeLimitHandlerTest {
5858

5959
/**
6060
* Simulates the real servlet pattern: reads body via getReader(), wraps in
61-
* broad catch(Exception) mirrors what RateLimiterServlet + actual servlets do.
61+
* broad catch(Exception) - mirrors what RateLimiterServlet + actual servlets do.
6262
*/
6363
public static class BroadCatchServlet extends HttpServlet {
6464
@Override
@@ -214,17 +214,17 @@ public void testChunkedBodyExceedsLimit() throws Exception {
214214
String body = EntityUtils.toString(resp.getEntity());
215215
logger.info("Chunked oversized: status={}, body={}", status, body);
216216

217-
// catch(Exception) absorbs BadMessageException 200 + error JSON, not 413.
218-
// Body read IS truncated OOM protection still effective.
217+
// catch(Exception) absorbs BadMessageException -> 200 + error JSON, not 413.
218+
// Body read IS truncated - OOM protection still effective.
219219
Assert.assertEquals(200, status);
220220
Assert.assertTrue("Error should be surfaced in response body",
221221
body.contains("Error"));
222222
}
223223

224224
/**
225225
* When maxRequestSize is 0, SizeLimitHandler treats it as "reject all bodies > 0 bytes".
226-
* Jetty's logic: {@code _requestLimit >= 0 && size > _requestLimit} 0 >= 0 is true,
227-
* so any non-empty body triggers 413. This is NOT "pass all" it is a silent DoS
226+
* Jetty's logic: {@code _requestLimit >= 0 && size > _requestLimit} - 0 >= 0 is true,
227+
* so any non-empty body triggers 413. This is NOT "pass all" - it is a silent DoS
228228
* against the node's own API.
229229
*/
230230
@Test
@@ -248,7 +248,7 @@ public void testZeroLimitRejectsAllBodies() throws Exception {
248248
/**
249249
* For pure ASCII JSON (the normal TRON API case), wire bytes and
250250
* {@code body.getBytes().length} (what {@code Util.checkBodySize()} measures)
251-
* must be identical the two enforcement layers agree exactly.
251+
* must be identical - the two enforcement layers agree exactly.
252252
*/
253253
@Test
254254
public void testWireBytesMatchCheckBodySizeForAsciiJson() throws Exception {
@@ -266,8 +266,8 @@ public void testWireBytesMatchCheckBodySizeForAsciiJson() throws Exception {
266266

267267
/**
268268
* For UTF-8 JSON with multi-byte characters (CJK), wire bytes and
269-
* {@code body.getBytes().length} must still be identical UTF-8 round-trips
270-
* through {@code request.getReader()} {@code String.getBytes()} losslessly.
269+
* {@code body.getBytes().length} must still be identical - UTF-8 round-trips
270+
* through {@code request.getReader()} -> {@code String.getBytes()} losslessly.
271271
*/
272272
@Test
273273
public void testWireBytesMatchCheckBodySizeForUtf8Json() throws Exception {
@@ -285,7 +285,7 @@ public void testWireBytesMatchCheckBodySizeForUtf8Json() throws Exception {
285285
/**
286286
* When the body contains {@code \r\n} line endings, {@code lines().collect()}
287287
* normalizes them to {@code \n} (on Linux) or the platform line separator.
288-
* This makes {@code checkBodySize} measure fewer bytes than the wire
288+
* This makes {@code checkBodySize} measure fewer bytes than the wire -
289289
* a safe direction: checkBodySize never rejects what SizeLimitHandler accepts.
290290
*/
291291
@Test

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,9 @@ public void testRpcMaxMessageSizeExceedsIntMax() {
453453
configMap.put("node.rpc.maxMessageSize", "3g");
454454
Config config = ConfigFactory.defaultOverrides()
455455
.withFallback(ConfigFactory.parseMap(configMap));
456-
try {
457-
Args.applyConfigParams(config);
458-
Assert.fail("expected TronError for rpc maxMessageSize > Integer.MAX_VALUE");
459-
} catch (TronError e) {
460-
Assert.assertTrue(e.getMessage().contains("node.rpc.maxMessageSize must be non-negative"));
461-
}
456+
TronError e = Assert.assertThrows(TronError.class,
457+
() -> Args.applyConfigParams(config));
458+
Assert.assertTrue(e.getMessage().contains("node.rpc.maxMessageSize must be non-negative"));
462459
}
463460

464461
@Test
@@ -468,12 +465,9 @@ public void testMaxMessageSizeNegativeValue() {
468465
configMap.put("node.rpc.maxMessageSize", "-4m");
469466
Config config = ConfigFactory.defaultOverrides()
470467
.withFallback(ConfigFactory.parseMap(configMap));
471-
try {
472-
Args.applyConfigParams(config);
473-
Assert.fail("expected IllegalArgumentException for negative memory size");
474-
} catch (IllegalArgumentException e) {
475-
Assert.assertTrue(e.getMessage().contains("negative"));
476-
}
468+
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class,
469+
() -> Args.applyConfigParams(config));
470+
Assert.assertTrue(e.getMessage().contains("negative"));
477471
}
478472

479473
@Test
@@ -483,12 +477,9 @@ public void testMaxMessageSizeInvalidUnit() {
483477
configMap.put("node.rpc.maxMessageSize", "4x");
484478
Config config = ConfigFactory.defaultOverrides()
485479
.withFallback(ConfigFactory.parseMap(configMap));
486-
try {
487-
Args.applyConfigParams(config);
488-
Assert.fail("expected ConfigException.BadValue for invalid unit");
489-
} catch (ConfigException.BadValue e) {
490-
Assert.assertTrue(e.getMessage().contains("Could not parse size-in-bytes unit"));
491-
}
480+
ConfigException.BadValue e = Assert.assertThrows(ConfigException.BadValue.class,
481+
() -> Args.applyConfigParams(config));
482+
Assert.assertTrue(e.getMessage().contains("Could not parse size-in-bytes unit"));
492483
}
493484

494485
@Test
@@ -498,11 +489,8 @@ public void testMaxMessageSizeNonNumeric() {
498489
configMap.put("node.http.maxMessageSize", "abc");
499490
Config config = ConfigFactory.defaultOverrides()
500491
.withFallback(ConfigFactory.parseMap(configMap));
501-
try {
502-
Args.applyConfigParams(config);
503-
Assert.fail("expected ConfigException.BadValue for non-numeric value");
504-
} catch (ConfigException.BadValue e) {
505-
Assert.assertTrue(e.getMessage().contains("No number in size-in-bytes value"));
506-
}
492+
ConfigException.BadValue e = Assert.assertThrows(ConfigException.BadValue.class,
493+
() -> Args.applyConfigParams(config));
494+
Assert.assertTrue(e.getMessage().contains("No number in size-in-bytes value"));
507495
}
508496
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,8 @@ public void testWeb3ClientVersion() {
11141114
@Test
11151115
public void testJsonRpcSizeLimitIntegration() {
11161116
long testLimit = 1024;
1117+
long originalLimit = fullNodeJsonRpcHttpService.getMaxRequestSize();
11171118
try {
1118-
long originalLimit = fullNodeJsonRpcHttpService.getMaxRequestSize();
11191119
fullNodeJsonRpcHttpService.setMaxRequestSize(testLimit);
11201120

11211121
fullNodeJsonRpcHttpService.start();
@@ -1139,7 +1139,7 @@ public void testJsonRpcSizeLimitIntegration() {
11391139
body.contains("result"));
11401140
resp.close();
11411141

1142-
// Oversized request with Content-Length 413 before JsonRpcServlet
1142+
// Oversized request with Content-Length -> 413 before JsonRpcServlet
11431143
HttpPost overPost = new HttpPost(url);
11441144
overPost.addHeader("Content-Type", "application/json");
11451145
overPost.setEntity(new StringEntity(
@@ -1148,9 +1148,9 @@ public void testJsonRpcSizeLimitIntegration() {
11481148
Assert.assertEquals(413, resp.getStatusLine().getStatusCode());
11491149
resp.close();
11501150

1151-
// Chunked oversized BadMessageException thrown during body read,
1152-
// absorbed by jsonrpc4j catch(Exception) 200 with empty body.
1153-
// Body read IS truncated at the limit OOM protection effective.
1151+
// Chunked oversized -> BadMessageException thrown during body read,
1152+
// absorbed by jsonrpc4j catch(Exception) -> 200 with empty body.
1153+
// Body read IS truncated at the limit - OOM protection effective.
11541154
byte[] chunkedData = new String(new char[(int) testLimit * 2])
11551155
.replace('\0', 'x').getBytes("UTF-8");
11561156
HttpPost chunkedPost = new HttpPost(url);
@@ -1162,12 +1162,11 @@ public void testJsonRpcSizeLimitIntegration() {
11621162
Assert.assertTrue("Chunked oversized should return empty body"
11631163
+ " (jsonrpc4j absorbs BadMessageException)", body.isEmpty());
11641164
resp.close();
1165-
} finally {
1166-
fullNodeJsonRpcHttpService.setMaxRequestSize(originalLimit);
11671165
}
11681166
} catch (Exception e) {
11691167
Assert.fail(e.getMessage());
11701168
} finally {
1169+
fullNodeJsonRpcHttpService.setMaxRequestSize(originalLimit);
11711170
fullNodeJsonRpcHttpService.stop();
11721171
}
11731172
}

framework/src/test/java/org/tron/core/services/http/UtilTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,9 @@ public void testCheckBodySizeUsesHttpLimit() throws Exception {
143143
Util.checkBodySize(withinHttpLimit);
144144

145145
String exceedsHttpLimit = new String(new char[201]).replace('\0', 'b');
146-
try {
147-
Util.checkBodySize(exceedsHttpLimit);
148-
Assert.fail("expected exception for body exceeding httpMaxMessageSize");
149-
} catch (Exception e) {
150-
Assert.assertTrue(e.getMessage().contains("200"));
151-
}
146+
Exception e = Assert.assertThrows(Exception.class,
147+
() -> Util.checkBodySize(exceedsHttpLimit));
148+
Assert.assertTrue(e.getMessage().contains("200"));
152149
} finally {
153150
Args.getInstance().setHttpMaxMessageSize(originalHttpMax);
154151
Args.getInstance().setMaxMessageSize(originalRpcMax);

0 commit comments

Comments
 (0)