Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.HexUtilTest

LineHitsNoteSource
1  /*
2   * $Id: HexUtilTest.java 1011 2008-06-16 17:57:36Z amandel $
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   */
43100 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;
48100    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     {
580       junit.textui.TestRunner.run(HexUtilTest.class);
590    }
60  
61     /**
62      * Tests the bytesToHex method of the HexUtil class.
63      */
64     public void testBytesToHex ()
65     {
66100       for (int i = 0; i <= BYTE_UNSIGNED_MAX; i++)
67        {
68100          final byte[] b = new byte[1];
69100          b[0] = (byte) i;
70100          final String s = HexUtil.bytesToHex(b, 0, b.length);
71100          assertEquals("Hex dump of a single byte must yield 2 chars",
72                 s.length(), CHARS_PER_BYTE);
73        }
74100    }
75  
76     /**
77      * Tests boundaries.
78      */
79     public void testVariousBytes ()
80     {
81100       final byte[] testMin = {Byte.MIN_VALUE};
82100       final String minResult = "80";
83100       testB2H(testMin, minResult);
84  
85100       final byte[] testMax = {Byte.MAX_VALUE};
86100       final String maxResult = "7F";
87100       testB2H(testMax, maxResult);
88  
89100       final byte[] testNull = {0};
90100       final String nullResult = "00";
91100       testB2H(testNull, nullResult);
92  
93100       final byte[] testMinusOne = {-1};
94100       final String minusOneResult = "FF";
95100       testB2H(testMinusOne, minusOneResult);
96100    }
97  
98     /**
99      * Perform a cross check with the Integer.toHexString functionality.
100      */
101     public void testCrossCheck ()
102     {
103100       for (int i = 0; i <= BYTE_UNSIGNED_MAX; i++)
104        {
105100          String hex1 = Integer.toHexString(i);
106100          if (hex1.length() == 1)
107           {
108100(1)            hex1 = "0" + hex1;
109           }
110100          hex1 = hex1.toUpperCase(Constants.SYSTEM_LOCALE);
111100          final byte[] b = new byte[1];
112100          b[0] = (byte) i;
113100          final String hex2 = HexUtil.bytesToHex(b, 0, b.length);
114100          assertEquals(
115                 "HexUtil must return same result as Integer.toHexString",
116                 hex1, hex2);
117        }
118100    }
119  
120     /**
121      * Test empty and null byte array.
122      */
123     public void testNullAndEmpty ()
124     {
125100       final byte[] b = null;
126100       final String s = HexUtil.bytesToHex(b, 0, 0);
127100       assertNull("HexUtil must return null for null argument", s);
128  
129100       final byte[] b2 = new byte[0];
130100       final String s2 = HexUtil.bytesToHex(b2, 0, 0);
131100       assertEquals(
132              "HexUtil must return an empty string for a 0-byte long argument",
133              s2.length(), 0);
134100    }
135  
136     /**
137      * Tests a couple of valid hex strings.
138      */
139     public void testValidStrings ()
140     {
141100(2)      final String[] hex = {"00", "FF", "7F", "80"};
142100       final byte[] bin = {0, -1, Byte.MAX_VALUE, Byte.MIN_VALUE};
143100       for (int i = 0; i < hex.length; i++)
144        {
145100          final byte[] b = HexUtil.stringToBytes(hex[i]);
146100          assertEquals("Expected exactly one byte", b.length, 1);
147100          assertEquals("Expected correct result", bin[i], b[0]);
148        }
149100    }
150  
151     /**
152      * Tests a couple of invalid hex strings.
153      */
154     public void testInvalidStrings ()
155     {
156100(3)      final String[] badHex = {"AX", "A", "BAR", "DROP",
157                "\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd", "?\u00ef\u00bf\u00bd"};
158100       for (int i = 0; i < badHex.length; i++)
159        {
160           try
161           {
1620             HexUtil.stringToBytes(badHex[i]);
1630             fail("Method should throw exception for invalid hex string "
164                    + badHex[i]);
165           }
166100          catch (IllegalArgumentException x)
167           {
168              // expected
1690          }
170        }
171100    }
172  
173     /**
174      * Test HexUtil with 10k of random data.
175      */
176     public void testWithRandomData ()
177     {
178100       final byte[] data = new byte[TEN_K];
179100       RANDOM.nextBytes(data);
180100       final String s = HexUtil.bytesToHex(data);
181100       final byte[] b = HexUtil.stringToBytes(s);
182100(4)      assertTrue("Input data must equal output data",
183              Arrays.equals(data, b));
184100    }
185  
186     /**
187      * Test the hexdump method and prints out the result for visual test.
188      */
189     public void testHexDump ()
190     {
191100       final byte[] b = new byte[BYTE_UNSIGNED_MAX - 1];
192100       for (int i = 0; i < b.length; i++)
193        {
194100          b[i] = (byte) i;
195        }
196100       final String dump = HexUtil.dump(b);
197100(5)      System.out.println(dump);
198100    }
199  
200     /**
201      * Test hexdump with sixteen bytes from 'a' to 'p'.
202      */
203     public void testHexDump2 ()
204     {
205100       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;
209100       final byte[] data = new byte[DUMP_TEST_LENGTH];
210100       for (int i = 0; i < data.length; i++)
211        {
212100          data[i] = (byte) (i + 'a');
213        }
214100       final String dump = HexUtil.dump(data);
215100       assertEquals("Dump must equal expected format", expectedResult, dump);
216100    }
217  
218     /**
219      * Test hexdump with a zero-length byte array.
220      */
221     public void testHexDumpWithEmptyArray ()
222     {
223100       final byte[] b = new byte[0];
224100       final String dump = HexUtil.dump(b);
225100       assertEquals("Dump of empty byte array must yield empty string",
226              dump.length(), 0);
227100    }
228  
229     /**
230      * Test hexdump with a null byte array.
231      */
232     public void testHexDumpWithNull ()
233     {
234100       final byte[] b = null;
235100       final String dump = HexUtil.dump(b);
236100       assertNull("Hexdump of null byte array must return null", dump);
237100    }
238  
239     private String testB2H (final byte[] b, final String expectedResult)
240     {
241100       final String result = HexUtil.bytesToHex(b, 0, b.length);
242100       assertEquals("Invalid hex representation", result, expectedResult);
243100       return result;
244     }
245  }

Findings in this File

c (1) 108 : 13 Prefer StringBuffer over += for concatenating strings
i (2) 141 : 0 Method org.jcoderz.commons.util.HexUtilTest.testValidStrings() creates array using constants (test code) Decreased severity from 'warning' for testcode.
i (3) 156 : 0 Method org.jcoderz.commons.util.HexUtilTest.testInvalidStrings() creates array using constants (test code) Decreased severity from 'warning' for testcode.
c (4) 182 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
d (5) 197 : 7 System.out.print is used