| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 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 | * Tests the HexUtil class. |
|---|
| 41 | * @author Albrecht Messner |
|---|
| 42 | */ |
|---|
| 43 | 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 | 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 | * Main method to run test stand-alone. |
|---|
| 54 | * @param args command line arguments |
|---|
| 55 | */ |
|---|
| 56 | public static void main (String[] args) |
|---|
| 57 | { |
|---|
| 58 | junit.textui.TestRunner.run(HexUtilTest.class); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Tests the bytesToHex method of the HexUtil class. |
|---|
| 63 | */ |
|---|
| 64 | public void testBytesToHex () |
|---|
| 65 | { |
|---|
| 66 | for (int i = 0; i <= BYTE_UNSIGNED_MAX; i++) |
|---|
| 67 | { |
|---|
| 68 | final byte[] b = new byte[1]; |
|---|
| 69 | b[0] = (byte) i; |
|---|
| 70 | final String s = HexUtil.bytesToHex(b, 0, b.length); |
|---|
| 71 | assertEquals("Hex dump of a single byte must yield 2 chars", |
|---|
| 72 | s.length(), CHARS_PER_BYTE); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Tests boundaries. |
|---|
| 78 | */ |
|---|
| 79 | public void testVariousBytes () |
|---|
| 80 | { |
|---|
| 81 | final byte[] testMin = {Byte.MIN_VALUE}; |
|---|
| 82 | final String minResult = "80"; |
|---|
| 83 | testB2H(testMin, minResult); |
|---|
| 84 | |
|---|
| 85 | final byte[] testMax = {Byte.MAX_VALUE}; |
|---|
| 86 | final String maxResult = "7F"; |
|---|
| 87 | testB2H(testMax, maxResult); |
|---|
| 88 | |
|---|
| 89 | final byte[] testNull = {0}; |
|---|
| 90 | final String nullResult = "00"; |
|---|
| 91 | testB2H(testNull, nullResult); |
|---|
| 92 | |
|---|
| 93 | final byte[] testMinusOne = {-1}; |
|---|
| 94 | final String minusOneResult = "FF"; |
|---|
| 95 | testB2H(testMinusOne, minusOneResult); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Perform a cross check with the Integer.toHexString functionality. |
|---|
| 100 | */ |
|---|
| 101 | public void testCrossCheck () |
|---|
| 102 | { |
|---|
| 103 | for (int i = 0; i <= BYTE_UNSIGNED_MAX; i++) |
|---|
| 104 | { |
|---|
| 105 | String hex1 = Integer.toHexString(i); |
|---|
| 106 | if (hex1.length() == 1) |
|---|
| 107 | { |
|---|
| 108 | hex1 = "0" + hex1; |
|---|
| 109 | } |
|---|
| 110 | hex1 = hex1.toUpperCase(Constants.SYSTEM_LOCALE); |
|---|
| 111 | final byte[] b = new byte[1]; |
|---|
| 112 | b[0] = (byte) i; |
|---|
| 113 | final String hex2 = HexUtil.bytesToHex(b, 0, b.length); |
|---|
| 114 | assertEquals( |
|---|
| 115 | "HexUtil must return same result as Integer.toHexString", |
|---|
| 116 | hex1, hex2); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Test empty and null byte array. |
|---|
| 122 | */ |
|---|
| 123 | public void testNullAndEmpty () |
|---|
| 124 | { |
|---|
| 125 | final byte[] b = null; |
|---|
| 126 | final String s = HexUtil.bytesToHex(b, 0, 0); |
|---|
| 127 | assertNull("HexUtil must return null for null argument", s); |
|---|
| 128 | |
|---|
| 129 | final byte[] b2 = new byte[0]; |
|---|
| 130 | final String s2 = HexUtil.bytesToHex(b2, 0, 0); |
|---|
| 131 | assertEquals( |
|---|
| 132 | "HexUtil must return an empty string for a 0-byte long argument", |
|---|
| 133 | s2.length(), 0); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Tests a couple of valid hex strings. |
|---|
| 138 | */ |
|---|
| 139 | public void testValidStrings () |
|---|
| 140 | { |
|---|
| 141 | final String[] hex = {"00", "FF", "7F", "80"}; |
|---|
| 142 | final byte[] bin = {0, -1, Byte.MAX_VALUE, Byte.MIN_VALUE}; |
|---|
| 143 | for (int i = 0; i < hex.length; i++) |
|---|
| 144 | { |
|---|
| 145 | final byte[] b = HexUtil.stringToBytes(hex[i]); |
|---|
| 146 | assertEquals("Expected exactly one byte", b.length, 1); |
|---|
| 147 | assertEquals("Expected correct result", bin[i], b[0]); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * Tests a couple of invalid hex strings. |
|---|
| 153 | */ |
|---|
| 154 | public void testInvalidStrings () |
|---|
| 155 | { |
|---|
| 156 | final String[] badHex = {"AX", "A", "BAR", "DROP", |
|---|
| 157 | "\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd", "?\u00ef\u00bf\u00bd"}; |
|---|
| 158 | for (int i = 0; i < badHex.length; i++) |
|---|
| 159 | { |
|---|
| 160 | try |
|---|
| 161 | { |
|---|
| 162 | HexUtil.stringToBytes(badHex[i]); |
|---|
| 163 | fail("Method should throw exception for invalid hex string " |
|---|
| 164 | + badHex[i]); |
|---|
| 165 | } |
|---|
| 166 | catch (IllegalArgumentException x) |
|---|
| 167 | { |
|---|
| 168 | // expected |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | * Test HexUtil with 10k of random data. |
|---|
| 175 | */ |
|---|
| 176 | public void testWithRandomData () |
|---|
| 177 | { |
|---|
| 178 | final byte[] data = new byte[TEN_K]; |
|---|
| 179 | RANDOM.nextBytes(data); |
|---|
| 180 | final String s = HexUtil.bytesToHex(data); |
|---|
| 181 | final byte[] b = HexUtil.stringToBytes(s); |
|---|
| 182 | assertTrue("Input data must equal output data", |
|---|
| 183 | Arrays.equals(data, b)); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Test the hexdump method and prints out the result for visual test. |
|---|
| 188 | */ |
|---|
| 189 | public void testHexDump () |
|---|
| 190 | { |
|---|
| 191 | final byte[] b = new byte[BYTE_UNSIGNED_MAX - 1]; |
|---|
| 192 | for (int i = 0; i < b.length; i++) |
|---|
| 193 | { |
|---|
| 194 | b[i] = (byte) i; |
|---|
| 195 | } |
|---|
| 196 | final String dump = HexUtil.dump(b); |
|---|
| 197 | System.out.println(dump); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * Test hexdump with sixteen bytes from 'a' to 'p'. |
|---|
| 202 | */ |
|---|
| 203 | public void testHexDump2 () |
|---|
| 204 | { |
|---|
| 205 | 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 | final byte[] data = new byte[DUMP_TEST_LENGTH]; |
|---|
| 210 | for (int i = 0; i < data.length; i++) |
|---|
| 211 | { |
|---|
| 212 | data[i] = (byte) (i + 'a'); |
|---|
| 213 | } |
|---|
| 214 | final String dump = HexUtil.dump(data); |
|---|
| 215 | assertEquals("Dump must equal expected format", expectedResult, dump); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | * Test hexdump with a zero-length byte array. |
|---|
| 220 | */ |
|---|
| 221 | public void testHexDumpWithEmptyArray () |
|---|
| 222 | { |
|---|
| 223 | final byte[] b = new byte[0]; |
|---|
| 224 | final String dump = HexUtil.dump(b); |
|---|
| 225 | assertEquals("Dump of empty byte array must yield empty string", |
|---|
| 226 | dump.length(), 0); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | /** |
|---|
| 230 | * Test hexdump with a null byte array. |
|---|
| 231 | */ |
|---|
| 232 | public void testHexDumpWithNull () |
|---|
| 233 | { |
|---|
| 234 | final byte[] b = null; |
|---|
| 235 | final String dump = HexUtil.dump(b); |
|---|
| 236 | assertNull("Hexdump of null byte array must return null", dump); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | private String testB2H (final byte[] b, final String expectedResult) |
|---|
| 240 | { |
|---|
| 241 | final String result = HexUtil.bytesToHex(b, 0, b.length); |
|---|
| 242 | assertEquals("Invalid hex representation", result, expectedResult); |
|---|
| 243 | return result; |
|---|
| 244 | } |
|---|
| 245 | } |
|---|