|
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; |
26 | 27 |
|
27 | 28 | import lombok.extern.slf4j.Slf4j; |
28 | 29 | import org.bouncycastle.util.encoders.Hex; |
29 | 30 | import org.junit.Test; |
| 31 | +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; |
30 | 32 |
|
31 | 33 | @Slf4j |
32 | 34 | public class ByteArrayTest { |
@@ -115,4 +117,128 @@ public void testFromHexWithPrefix() { |
115 | 117 | String input1 = "1A3"; |
116 | 118 | assertEquals("01A3", fromHex(input1)); |
117 | 119 | } |
| 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 | + lontooLongStrgStr.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, () -> jsonHexToLong("0x8000000000000000")); // Long.MAX_VALUE + 1 |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void testJsonHexToInt_OverflowHandling() { |
| 240 | + // Test that Integer.parseInt properly handles overflow by throwing NumberFormatException |
| 241 | + // This tests values that pass length validation but cause overflow |
| 242 | + assertThrows(NumberFormatException.class, () -> jsonHexToInt("0x80000000")); // Integer.MAX_VALUE + 1 |
| 243 | + } |
118 | 244 | } |
0 commit comments