11package org .tron .core .services .http .servlets ;
22
33import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertFalse ;
45import static org .junit .Assert .assertThrows ;
56import static org .junit .Assert .assertTrue ;
67
1920import org .tron .core .services .ratelimiter .RateLimiterContainer ;
2021import org .tron .json .JSONObject ;
2122import org .tron .protos .Protocol .Account ;
23+ import org .tron .protos .contract .SmartContractOuterClass .SmartContract ;
2224
2325public class JsonFormatUnicodeErrorTest {
2426
2527 private static final String EMOJI = new String (Character .toChars (0x1F600 ));
28+ private static final String NAME_SURROGATE_ERROR =
29+ "1:9: String field protocol.SmartContract.name contains an unpaired surrogate" ;
30+
31+ @ Test
32+ public void testUnpairedSurrogateDetection () {
33+ assertTrue (JsonFormat .hasUnpairedSurrogate (String .valueOf ((char ) 0xD800 )));
34+ assertTrue (JsonFormat .hasUnpairedSurrogate (String .valueOf ((char ) 0xDC00 )));
35+ assertTrue (JsonFormat .hasUnpairedSurrogate ("" + (char ) 0xDC00 + (char ) 0xD800 ));
36+ assertTrue (JsonFormat .hasUnpairedSurrogate ("a" + (char ) 0xD83D ));
37+ assertFalse (JsonFormat .hasUnpairedSurrogate ("" ));
38+ assertFalse (JsonFormat .hasUnpairedSurrogate (EMOJI ));
39+ assertFalse (JsonFormat .hasUnpairedSurrogate ("" + (char ) 0x4E2D + (char ) 0xFFFF ));
40+ }
41+
42+ @ Test
43+ public void testStringFieldRejectsUnpairedSurrogates () {
44+ assertNameRejected ("\\ ud800" );
45+ assertNameRejected ("\\ udc00" );
46+ assertNameRejected ("a\\ udc00\\ ud800" );
47+ assertNameRejected ("x\\ uD83D" );
48+ assertNameRejected (String .valueOf ((char ) 0xD800 ));
49+ }
50+
51+ @ Test
52+ public void testStringFieldKeepsWellFormedText () throws Exception {
53+ SmartContract .Builder builder = SmartContract .newBuilder ();
54+ JsonFormat .merge ("{\" name\" :\" \\ ud83d\\ ude00\\ u4e2d\\ uffff" + EMOJI + "\" }" , builder , false );
55+
56+ assertEquals (EMOJI + (char ) 0x4E2D + (char ) 0xFFFF + EMOJI , builder .getName ());
57+ }
58+
59+ @ Test
60+ public void testDeployContractRejectsUnpairedSurrogateInAbi () throws Exception {
61+ String body = "{\" owner_address\" :\" \" ,\" abi\" :\" [{\\ \" name\\ \" :\\ \" \\ \\ ud800\\ \" }]\" }" ;
62+
63+ String error = requestError (new DeployContractServlet (), "/wallet/deploycontract" , body );
64+
65+ assertEquals ("1:20: String field protocol.SmartContract.ABI.Entry.name"
66+ + " contains an unpaired surrogate" , error );
67+ }
68+
69+ @ Test
70+ public void testDeployContractRejectsUnpairedSurrogateInName () throws Exception {
71+ String body = "{\" owner_address\" :\" \" ,\" bytecode\" :\" 00\" ,\" name\" :\" \\ ud800\" }" ;
72+
73+ String error = requestError (new DeployContractServlet (), "/wallet/deploycontract" , body );
74+
75+ assertEquals ("Invalid name: contains an unpaired surrogate" , error );
76+ }
2677
2778 @ Test
2879 public void testInvalidSupplementaryEscapeKeepsLegacyReplacement () {
@@ -129,6 +180,11 @@ public void testOrdinaryIntegerErrorsKeepTheirMessages() {
129180
130181 private static String requestError (RateLimiterServlet servlet , String path , JSONObject input )
131182 throws Exception {
183+ return requestError (servlet , path , input .toJSONString ());
184+ }
185+
186+ private static String requestError (RateLimiterServlet servlet , String path , String requestBody )
187+ throws Exception {
132188 CommonParameter args = Args .getInstance ();
133189 long originalMaxSize = args .getHttpMaxMessageSize ();
134190 boolean originalNonBlocking = args .isRateLimiterApiNonBlocking ();
@@ -145,7 +201,7 @@ private static String requestError(RateLimiterServlet servlet, String path, JSON
145201 args .setHttpMaxMessageSize (1_000_000L );
146202 args .setRateLimiterApiNonBlocking (false );
147203 server .start ();
148- byte [] body = input . toJSONString () .getBytes (StandardCharsets .UTF_8 );
204+ byte [] body = requestBody .getBytes (StandardCharsets .UTF_8 );
149205 byte [] headers = ("POST " + path + " HTTP/1.1\r \n "
150206 + "Host: localhost\r \n "
151207 + "Connection: close\r \n "
@@ -177,6 +233,13 @@ private static String requestError(RateLimiterServlet servlet, String path, JSON
177233 }
178234 }
179235
236+ private static void assertNameRejected (String text ) {
237+ JsonFormat .ParseException error = assertThrows (JsonFormat .ParseException .class ,
238+ () -> JsonFormat .merge ("{\" name\" :\" " + text + "\" }" , SmartContract .newBuilder (), false ));
239+
240+ assertEquals (NAME_SURROGATE_ERROR , error .getMessage ());
241+ }
242+
180243 private static void assertEscapeError (String input , String expectedCharacter ) {
181244 JsonFormat .InvalidEscapeSequence error = assertThrows (JsonFormat .InvalidEscapeSequence .class ,
182245 () -> JsonFormat .unescapeText (input ));
0 commit comments