|
23 | 23 | import static org.junit.Assert.fail; |
24 | 24 | import static org.tron.common.utils.ByteArray.fromHex; |
25 | 25 | import static org.tron.common.utils.ByteArray.jsonHexToInt; |
26 | | -import static org.tron.common.utils.ByteArray.jsonHexToLong; |
27 | 26 |
|
28 | 27 | import lombok.extern.slf4j.Slf4j; |
29 | 28 | import org.bouncycastle.util.encoders.Hex; |
30 | 29 | import org.junit.Test; |
31 | | -import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; |
32 | 30 |
|
33 | 31 | @Slf4j |
34 | 32 | public class ByteArrayTest { |
@@ -117,130 +115,4 @@ public void testFromHexWithPrefix() { |
117 | 115 | String input1 = "1A3"; |
118 | 116 | assertEquals("01A3", fromHex(input1)); |
119 | 117 | } |
120 | | - |
121 | | - @Test |
122 | | - public void testJsonHexToLong_ValidInputs() { |
123 | | - try { |
124 | | - // Test basic hex conversion |
125 | | - assertEquals(26L, jsonHexToLong("0x1A")); |
126 | | - assertEquals(255L, jsonHexToLong("0xFF")); |
127 | | - assertEquals(0L, jsonHexToLong("0x0")); |
128 | | - assertEquals(1L, jsonHexToLong("0x1")); |
129 | | - |
130 | | - // Test large values |
131 | | - assertEquals(4294967295L, jsonHexToLong("0xFFFFFFFF")); |
132 | | - |
133 | | - // Test maximum long value |
134 | | - assertEquals(Long.MAX_VALUE, jsonHexToLong("0x7FFFFFFFFFFFFFFF")); |
135 | | - } catch (JsonRpcInvalidParamsException e) { |
136 | | - fail("Exception should not have been thrown for valid hex strings: " + e.getMessage()); |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - @Test |
141 | | - public void testJsonHexToLong_InvalidInputs() { |
142 | | - // Test null input |
143 | | - assertThrows(JsonRpcInvalidParamsException.class, () -> jsonHexToLong(null)); |
144 | | - |
145 | | - // Test missing 0x prefix |
146 | | - assertThrows(JsonRpcInvalidParamsException.class, () -> jsonHexToLong("1A")); |
147 | | - |
148 | | - // Test too long input (DDoS protection) |
149 | | - StringBuilder tooLongStr = new StringBuilder("0x"); |
150 | | - for (int i = 0; i < 20; i++) { |
151 | | - tooLongStr.append("F"); |
152 | | - } |
153 | | - String tooLongHex = tooLongStr.toString(); // 22 characters total, exceeds MAX_HEX_LONG_LENGTH |
154 | | - assertThrows(JsonRpcInvalidParamsException.class, () -> jsonHexToLong(tooLongHex)); |
155 | | - |
156 | | - // Test invalid hex characters |
157 | | - assertThrows(NumberFormatException.class, () -> jsonHexToLong("0xGG")); |
158 | | - } |
159 | | - |
160 | | - @Test |
161 | | - public void testJsonHexToInt_ValidInputs() { |
162 | | - try { |
163 | | - // Test basic hex conversion |
164 | | - assertEquals(26, jsonHexToInt("0x1A")); |
165 | | - assertEquals(255, jsonHexToInt("0xFF")); |
166 | | - assertEquals(0, jsonHexToInt("0x0")); |
167 | | - assertEquals(1, jsonHexToInt("0x1")); |
168 | | - |
169 | | - // Test maximum int value |
170 | | - assertEquals(Integer.MAX_VALUE, jsonHexToInt("0x7FFFFFFF")); |
171 | | - |
172 | | - // Test large values |
173 | | - assertEquals(65535, jsonHexToInt("0xFFFF")); |
174 | | - } catch (Exception e) { |
175 | | - fail("Exception should not have been thrown for valid hex strings: " + e.getMessage()); |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - @Test |
180 | | - public void testJsonHexToInt_InvalidInputs() { |
181 | | - // Test null input |
182 | | - assertThrows(Exception.class, () -> jsonHexToInt(null)); |
183 | | - |
184 | | - // Test missing 0x prefix |
185 | | - assertThrows(Exception.class, () -> jsonHexToInt("1A")); |
186 | | - |
187 | | - // Test too long input (DDoS protection) |
188 | | - StringBuilder tooLongStr = new StringBuilder("0x"); |
189 | | - for (int i = 0; i < 12; i++) { |
190 | | - tooLongStr.append("F"); |
191 | | - } |
192 | | - String tooLongHex = tooLongStr.toString(); // 14 characters total, exceeds MAX_HEX_INT_LENGTH |
193 | | - assertThrows(Exception.class, () -> jsonHexToInt(tooLongHex)); |
194 | | - |
195 | | - // Test invalid hex characters |
196 | | - assertThrows(NumberFormatException.class, () -> jsonHexToInt("0xGG")); |
197 | | - } |
198 | | - |
199 | | - @Test |
200 | | - public void testJsonHexToLong_EdgeCases() { |
201 | | - try { |
202 | | - // Test minimum length valid input |
203 | | - assertEquals(0L, jsonHexToLong("0x0")); |
204 | | - |
205 | | - // Test a long hex string that's within limits but doesn't overflow |
206 | | - assertEquals(4095L, jsonHexToLong("0xFFF")); // 3 F's = 4095, safe value |
207 | | - |
208 | | - // Test length validation - this should pass length check |
209 | | - assertEquals(1048575L, jsonHexToLong("0xFFFFF")); // 5 F's = 1048575, safe value |
210 | | - } catch (JsonRpcInvalidParamsException e) { |
211 | | - fail("Exception should not have been thrown for edge case inputs: " + e.getMessage()); |
212 | | - } |
213 | | - } |
214 | | - |
215 | | - @Test |
216 | | - public void testJsonHexToInt_EdgeCases() { |
217 | | - try { |
218 | | - // Test minimum length valid input |
219 | | - assertEquals(0, jsonHexToInt("0x0")); |
220 | | - |
221 | | - // Test a hex string that's within limits but doesn't overflow |
222 | | - assertEquals(4095, jsonHexToInt("0xFFF")); // 3 F's = 4095, safe value |
223 | | - |
224 | | - // Test length validation - this should pass length check |
225 | | - assertEquals(1048575, jsonHexToInt("0xFFFFF")); // 5 F's = 1048575, safe value |
226 | | - } catch (Exception e) { |
227 | | - fail("Exception should not have been thrown for edge case inputs: " + e.getMessage()); |
228 | | - } |
229 | | - } |
230 | | - |
231 | | - @Test |
232 | | - public void testJsonHexToLong_OverflowHandling() { |
233 | | - // Test that Long.parseLong properly handles overflow by throwing NumberFormatException |
234 | | - // This tests values that pass length validation but cause overflow |
235 | | - assertThrows(NumberFormatException.class, |
236 | | - () -> jsonHexToLong("0x8000000000000000")); // Long.MAX_VALUE + 1 |
237 | | - } |
238 | | - |
239 | | - @Test |
240 | | - public void testJsonHexToInt_OverflowHandling() { |
241 | | - // Test that Integer.parseInt properly handles overflow by throwing NumberFormatException |
242 | | - // This tests values that pass length validation but cause overflow |
243 | | - assertThrows(NumberFormatException.class, |
244 | | - () -> jsonHexToInt("0x80000000")); // Integer.MAX_VALUE + 1 |
245 | | - } |
246 | 118 | } |
0 commit comments