|
1 | 1 | package org.tron.core.services.jsonrpc; |
2 | 2 |
|
| 3 | +import static com.google.common.primitives.Bytes.concat; |
3 | 4 | import static org.junit.Assert.assertArrayEquals; |
4 | 5 | import static org.junit.Assert.assertEquals; |
5 | 6 | import static org.junit.Assert.assertFalse; |
|
34 | 35 | import com.fasterxml.jackson.databind.node.ObjectNode; |
35 | 36 | import com.googlecode.jsonrpc4j.JsonRpcMethod; |
36 | 37 | import com.googlecode.jsonrpc4j.JsonRpcServer; |
37 | | -import java.io.ByteArrayOutputStream; |
38 | 38 | import java.io.IOException; |
39 | 39 | import java.io.InputStream; |
40 | 40 | import java.io.OutputStream; |
@@ -110,7 +110,6 @@ public void emptyBody_returnsParseError() throws Exception { |
110 | 110 | @Test |
111 | 111 | public void malformedUtf8_isRejectedBeforeDispatch() throws Exception { |
112 | 112 | TronJsonRpc rpc = useRealRpcServer(); |
113 | | - when(rpc.web3ClientVersion()).thenReturn("client"); |
114 | 113 | // Each sequence replaces the leading 'w' of web3_clientVersion. The overlong forms decode |
115 | 114 | // to 'w' in a lenient parser, so they reach the real method unless rejected up front. |
116 | 115 | byte[][] sequences = { |
@@ -165,15 +164,22 @@ public void utf8ByteOrderMark_isAccepted() throws Exception { |
165 | 164 | } |
166 | 165 |
|
167 | 166 | @Test |
168 | | - public void utf16AndUtf32WithoutByteOrderMark_areRejected() throws Exception { |
| 167 | + public void utf16AndUtf32Bodies_areRejected() throws Exception { |
169 | 168 | TronJsonRpc rpc = useRealRpcServer(); |
170 | 169 | String request = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}"; |
171 | 170 | String chinese = "{\"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"中\"],\"id\":1}"; |
| 171 | + String accented = "{\"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"é\"],\"id\":1}"; |
172 | 172 | for (String charset : Arrays.asList("UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE")) { |
173 | | - for (String body : Arrays.asList(request, chinese)) { |
| 173 | + for (String body : Arrays.asList(request, chinese, accented)) { |
174 | 174 | assertParseError(charset + " " + body, doPost(body.getBytes(Charset.forName(charset)))); |
175 | 175 | } |
176 | 176 | } |
| 177 | + List<byte[]> withByteOrderMark = Arrays.asList( |
| 178 | + request.getBytes(StandardCharsets.UTF_16), |
| 179 | + concat(new byte[] {(byte) 0xFF, (byte) 0xFE}, request.getBytes(StandardCharsets.UTF_16LE))); |
| 180 | + for (byte[] body : withByteOrderMark) { |
| 181 | + assertParseError(Arrays.toString(body), doPost(body)); |
| 182 | + } |
177 | 183 | verify(realRpcServer, never()).handleRequest(any(InputStream.class), any(OutputStream.class)); |
178 | 184 | verify(rpc, never()).getLatestBlockNum(); |
179 | 185 | verify(rpc, never()).web3Sha3(any()); |
@@ -208,25 +214,6 @@ public void escapedNulCharacter_isDispatched() throws Exception { |
208 | 214 | verify(rpc).web3Sha3("a" + (char) 0 + "b"); |
209 | 215 | } |
210 | 216 |
|
211 | | - @Test |
212 | | - public void utf16BodyThatIsNotWellFormedUtf8_isRejected() throws Exception { |
213 | | - TronJsonRpc rpc = useRealRpcServer(); |
214 | | - when(rpc.web3Sha3(any())).thenReturn("0x1"); |
215 | | - String request = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}"; |
216 | | - String nonAscii = "{\"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"é\"],\"id\":1}"; |
217 | | - List<byte[]> bodies = Arrays.asList( |
218 | | - request.getBytes(StandardCharsets.UTF_16), |
219 | | - concat(new byte[] {(byte) 0xFF, (byte) 0xFE}, request.getBytes(StandardCharsets.UTF_16LE)), |
220 | | - nonAscii.getBytes(StandardCharsets.UTF_16LE), |
221 | | - nonAscii.getBytes(StandardCharsets.UTF_16BE)); |
222 | | - for (byte[] body : bodies) { |
223 | | - assertParseError(Arrays.toString(body), doPost(body)); |
224 | | - } |
225 | | - verify(realRpcServer, never()).handleRequest(any(InputStream.class), any(OutputStream.class)); |
226 | | - verify(rpc, never()).getLatestBlockNum(); |
227 | | - verify(rpc, never()).web3Sha3(any()); |
228 | | - } |
229 | | - |
230 | 217 | @Test |
231 | 218 | public void trailingContent_isRejectedBeforeDispatch() throws Exception { |
232 | 219 | TronJsonRpc rpc = useRealRpcServer(); |
@@ -270,28 +257,6 @@ public void trailingWhitespace_isAccepted() throws Exception { |
270 | 257 | verify(rpc, times(7)).getLatestBlockNum(); |
271 | 258 | } |
272 | 259 |
|
273 | | - @Test |
274 | | - public void undecodableUtf32Body_returnsParseError() throws Exception { |
275 | | - TronJsonRpc rpc = useRealRpcServer(); |
276 | | - byte[] request = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}" |
277 | | - .getBytes(Charset.forName("UTF-32BE")); |
278 | | - byte[] padding = new byte[4096]; |
279 | | - for (int i = 3; i < padding.length; i += 4) { |
280 | | - padding[i] = ' '; |
281 | | - } |
282 | | - // Jackson's UTF-32 decoder reports these as a CharConversionException, an IOException that |
283 | | - // is not a JsonProcessingException. |
284 | | - List<byte[]> bodies = Arrays.asList( |
285 | | - concat(request, new byte[] {'\n'}), |
286 | | - concat(request, new byte[] {0, 0, ' '}), |
287 | | - concat(request, padding, new byte[] {0, 0x11, 0, 0})); |
288 | | - for (byte[] body : bodies) { |
289 | | - assertParseError(Arrays.toString(body), doPost(body)); |
290 | | - } |
291 | | - verify(realRpcServer, never()).handleRequest(any(InputStream.class), any(OutputStream.class)); |
292 | | - verify(rpc, never()).getLatestBlockNum(); |
293 | | - } |
294 | | - |
295 | 260 | // --- batch size limit --- |
296 | 261 |
|
297 | 262 | @Test |
@@ -2073,14 +2038,6 @@ private static byte[] utf8(String text) { |
2073 | 2038 | return text.getBytes(StandardCharsets.UTF_8); |
2074 | 2039 | } |
2075 | 2040 |
|
2076 | | - private static byte[] concat(byte[]... parts) { |
2077 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
2078 | | - for (byte[] part : parts) { |
2079 | | - out.write(part, 0, part.length); |
2080 | | - } |
2081 | | - return out.toByteArray(); |
2082 | | - } |
2083 | | - |
2084 | 2041 | private void assertCleanupFailureDoesNotReplaceFatal(HttpServletResponse response) |
2085 | 2042 | throws Exception { |
2086 | 2043 | StackOverflowError fatal = new StackOverflowError("original-fatal-marker"); |
|
0 commit comments