Add unit tests for HexTool
This commit is contained in:
@@ -25,7 +25,9 @@ import constants.string.CharsetConstants;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.HexFormat;
|
||||
|
||||
// TODO: use HexFormat from Java 17
|
||||
public class HexTool {
|
||||
private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
|
||||
@@ -44,12 +46,19 @@ public class HexTool {
|
||||
return hexed.substring(0, hexed.length() - 1);
|
||||
}
|
||||
|
||||
public static String toCompressedString(byte[] bytes) {
|
||||
StringBuilder hexed = new StringBuilder();
|
||||
for (byte aByte : bytes) {
|
||||
hexed.append(toString(aByte));
|
||||
}
|
||||
return hexed.substring(0, hexed.length());
|
||||
/**
|
||||
* Convert a hex string to its byte array representation. Two consecutive hex characters are equivalent to one byte.
|
||||
*
|
||||
* @param hexString Hex string to convert to bytes. Hex character pairs may be delimited by a space or not (compact)
|
||||
* Example: "01 10 7F FF" is converted to {1, 16, 127, -1}.
|
||||
* @return The byte array
|
||||
*/
|
||||
public static byte[] toBytes(String hexString) {
|
||||
return HexFormat.of().parseHex(removeAllSpaces(hexString));
|
||||
}
|
||||
|
||||
private static String removeAllSpaces(String input) {
|
||||
return input.replaceAll("\\s", "");
|
||||
}
|
||||
|
||||
public static byte[] getByteArrayFromHexString(String hex) {
|
||||
@@ -85,6 +94,7 @@ public class HexTool {
|
||||
baos.write(nextb);
|
||||
}
|
||||
}
|
||||
// return toBytes(hex);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,45 @@
|
||||
package tools;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class HexToolTest {
|
||||
|
||||
@Test
|
||||
void bytesToHexString() {
|
||||
byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
|
||||
String expectedHexString = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 7F FF";
|
||||
assertEquals(expectedHexString, HexTool.toString(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hexStringWithSpacesToBytes() {
|
||||
String hexString = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 7F FF";
|
||||
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
|
||||
assertArrayEquals(expectedBytes, HexTool.getByteArrayFromHexString(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
void compactHexStringToBytes() {
|
||||
String hexString = "0102030405060708090A0B0C0D0E0F10117FFF";
|
||||
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 127, -1};
|
||||
assertArrayEquals(expectedBytes, HexTool.getByteArrayFromHexString(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("The original method does not work this way, this test should be removed!")
|
||||
void compactShortHexStringToBytes() {
|
||||
String hexString = "0 2 3 4 5 A F";
|
||||
byte[] expectedBytes = new byte[]{1, 2, 3, 4, 5, 10, 15};
|
||||
System.out.println(Arrays.toString(HexTool.getByteArrayFromHexString(hexString)));
|
||||
assertArrayEquals(expectedBytes, HexTool.getByteArrayFromHexString(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
void upperCaseHexToBytesAndBack() {
|
||||
String hex = "A1B2C3";
|
||||
|
||||
Reference in New Issue
Block a user