| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.util.Arrays; |
| 36 | | | import java.util.Random; |
| 37 | | | import junit.framework.TestCase; |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | @author |
| 42 | | | |
| 43 | 100 | | public class HexUtilTest |
| 44 | | | extends TestCase |
| 45 | | | { |
| 46 | | | private static final int CHARS_PER_BYTE = 2; |
| 47 | | | private static final int BYTE_UNSIGNED_MAX = 255; |
| 48 | 100 | | private static final Random RANDOM = new Random(); |
| 49 | | | private static final int TEN_K = 10240; |
| 50 | | | private static final int DUMP_TEST_LENGTH = 16; |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | @param |
| 55 | | | |
| 56 | | | public static void main (String[] args) |
| 57 | | | { |
| 58 | 0 | | junit.textui.TestRunner.run(HexUtilTest.class); |
| 59 | 0 | | } |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | public void testBytesToHex () |
| 65 | | | { |
| 66 | 100 | | for (int i = 0; i <= BYTE_UNSIGNED_MAX; i++) |
| 67 | | | { |
| 68 | 100 | | final byte[] b = new byte[1]; |
| 69 | 100 | | b[0] = (byte) i; |
| 70 | 100 | | final String s = HexUtil.bytesToHex(b, 0, b.length); |
| 71 | 100 | | assertEquals("Hex dump of a single byte must yield 2 chars", |
| 72 | | | s.length(), CHARS_PER_BYTE); |
| 73 | | | } |
| 74 | 100 | | } |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | | | |
| 79 | | | public void testVariousBytes () |
| 80 | | | { |
| 81 | 100 | | final byte[] testMin = {Byte.MIN_VALUE}; |
| 82 | 100 | | final String minResult = "80"; |
| 83 | 100 | | testB2H(testMin, minResult); |
| 84 | | | |
| 85 | 100 | | final byte[] testMax = {Byte.MAX_VALUE}; |
| 86 | 100 | | final String maxResult = "7F"; |
| 87 | 100 | | testB2H(testMax, maxResult); |
| 88 | | | |
| 89 | 100 | | final byte[] testNull = {0}; |
| 90 | 100 | | final String nullResult = "00"; |
| 91 | 100 | | testB2H(testNull, nullResult); |
| 92 | | | |
| 93 | 100 | | final byte[] testMinusOne = {-1}; |
| 94 | 100 | | final String minusOneResult = "FF"; |
| 95 | 100 | | testB2H(testMinusOne, minusOneResult); |
| 96 | 100 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | public void testCrossCheck () |
| 102 | | | { |
| 103 | 100 | | for (int i = 0; i <= BYTE_UNSIGNED_MAX; i++) |
| 104 | | | { |
| 105 | 100 | | String hex1 = Integer.toHexString(i); |
| 106 | 100 | | if (hex1.length() == 1) |
| 107 | | | { |
| 108 | 100 | (1) | hex1 = "0" + hex1; |
| 109 | | | } |
| 110 | 100 | | hex1 = hex1.toUpperCase(Constants.SYSTEM_LOCALE); |
| 111 | 100 | | final byte[] b = new byte[1]; |
| 112 | 100 | | b[0] = (byte) i; |
| 113 | 100 | | final String hex2 = HexUtil.bytesToHex(b, 0, b.length); |
| 114 | 100 | | assertEquals( |
| 115 | | | "HexUtil must return same result as Integer.toHexString", |
| 116 | | | hex1, hex2); |
| 117 | | | } |
| 118 | 100 | | } |
| 119 | | | |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | public void testNullAndEmpty () |
| 124 | | | { |
| 125 | 100 | | final byte[] b = null; |
| 126 | 100 | | final String s = HexUtil.bytesToHex(b, 0, 0); |
| 127 | 100 | | assertNull("HexUtil must return null for null argument", s); |
| 128 | | | |
| 129 | 100 | | final byte[] b2 = new byte[0]; |
| 130 | 100 | | final String s2 = HexUtil.bytesToHex(b2, 0, 0); |
| 131 | 100 | | assertEquals( |
| 132 | | | "HexUtil must return an empty string for a 0-byte long argument", |
| 133 | | | s2.length(), 0); |
| 134 | 100 | | } |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | |
| 139 | | | public void testValidStrings () |
| 140 | | | { |
| 141 | 100 | (2) | final String[] hex = {"00", "FF", "7F", "80"}; |
| 142 | 100 | | final byte[] bin = {0, -1, Byte.MAX_VALUE, Byte.MIN_VALUE}; |
| 143 | 100 | | for (int i = 0; i < hex.length; i++) |
| 144 | | | { |
| 145 | 100 | | final byte[] b = HexUtil.stringToBytes(hex[i]); |
| 146 | 100 | | assertEquals("Expected exactly one byte", b.length, 1); |
| 147 | 100 | | assertEquals("Expected correct result", bin[i], b[0]); |
| 148 | | | } |
| 149 | 100 | | } |
| 150 | | | |
| 151 | | | |
| 152 | | | |
| 153 | | | |
| 154 | | | public void testInvalidStrings () |
| 155 | | | { |
| 156 | 100 | (3) | final String[] badHex = {"AX", "A", "BAR", "DROP", |
| 157 | | | "\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd", "?\u00ef\u00bf\u00bd"}; |
| 158 | 100 | | for (int i = 0; i < badHex.length; i++) |
| 159 | | | { |
| 160 | | | try |
| 161 | | | { |
| 162 | 0 | | HexUtil.stringToBytes(badHex[i]); |
| 163 | 0 | | fail("Method should throw exception for invalid hex string " |
| 164 | | | + badHex[i]); |
| 165 | | | } |
| 166 | 100 | | catch (IllegalArgumentException x) |
| 167 | | | { |
| 168 | | | |
| 169 | 0 | | } |
| 170 | | | } |
| 171 | 100 | | } |
| 172 | | | |
| 173 | | | |
| 174 | | | |
| 175 | | | |
| 176 | | | public void testWithRandomData () |
| 177 | | | { |
| 178 | 100 | | final byte[] data = new byte[TEN_K]; |
| 179 | 100 | | RANDOM.nextBytes(data); |
| 180 | 100 | | final String s = HexUtil.bytesToHex(data); |
| 181 | 100 | | final byte[] b = HexUtil.stringToBytes(s); |
| 182 | 100 | (4) | assertTrue("Input data must equal output data", |
| 183 | | | Arrays.equals(data, b)); |
| 184 | 100 | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | public void testHexDump () |
| 190 | | | { |
| 191 | 100 | | final byte[] b = new byte[BYTE_UNSIGNED_MAX - 1]; |
| 192 | 100 | | for (int i = 0; i < b.length; i++) |
| 193 | | | { |
| 194 | 100 | | b[i] = (byte) i; |
| 195 | | | } |
| 196 | 100 | | final String dump = HexUtil.dump(b); |
| 197 | 100 | (5) | System.out.println(dump); |
| 198 | 100 | | } |
| 199 | | | |
| 200 | | | |
| 201 | | | |
| 202 | | | |
| 203 | | | public void testHexDump2 () |
| 204 | | | { |
| 205 | 100 | | final String expectedResult |
| 206 | | | = "00000000 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 " |
| 207 | | | + "|abcdefghijklmnop|" |
| 208 | | | + Constants.LINE_SEPARATOR; |
| 209 | 100 | | final byte[] data = new byte[DUMP_TEST_LENGTH]; |
| 210 | 100 | | for (int i = 0; i < data.length; i++) |
| 211 | | | { |
| 212 | 100 | | data[i] = (byte) (i + 'a'); |
| 213 | | | } |
| 214 | 100 | | final String dump = HexUtil.dump(data); |
| 215 | 100 | | assertEquals("Dump must equal expected format", expectedResult, dump); |
| 216 | 100 | | } |
| 217 | | | |
| 218 | | | |
| 219 | | | |
| 220 | | | |
| 221 | | | public void testHexDumpWithEmptyArray () |
| 222 | | | { |
| 223 | 100 | | final byte[] b = new byte[0]; |
| 224 | 100 | | final String dump = HexUtil.dump(b); |
| 225 | 100 | | assertEquals("Dump of empty byte array must yield empty string", |
| 226 | | | dump.length(), 0); |
| 227 | 100 | | } |
| 228 | | | |
| 229 | | | |
| 230 | | | |
| 231 | | | |
| 232 | | | public void testHexDumpWithNull () |
| 233 | | | { |
| 234 | 100 | | final byte[] b = null; |
| 235 | 100 | | final String dump = HexUtil.dump(b); |
| 236 | 100 | | assertNull("Hexdump of null byte array must return null", dump); |
| 237 | 100 | | } |
| 238 | | | |
| 239 | | | private String testB2H (final byte[] b, final String expectedResult) |
| 240 | | | { |
| 241 | 100 | | final String result = HexUtil.bytesToHex(b, 0, b.length); |
| 242 | 100 | | assertEquals("Invalid hex representation", result, expectedResult); |
| 243 | 100 | | return result; |
| 244 | | | } |
| 245 | | | } |