import java.io.*; import java.util.*; import junit.framework.*; import org.noreply.fancydress.misc.Util; public class UtilTest extends TestCase { public UtilTest(String name) { super(name); } /** * Test if equal returns an array of the same size as the input arrays. */ public void testEqual() { byte[] a0 = new byte[30]; byte[] a1 = { }; byte[] a2 = { (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }; byte[] a3 = { (byte)0x10, (byte)0x20, (byte)0xf0, (byte)0xF0, (byte)0xab }; assertTrue(Util.equal(a0,a0)); assertTrue(Util.equal(a1,a1)); assertTrue(Util.equal(a2,a2)); assertTrue(Util.equal(a3,a3)); assertTrue(!Util.equal(a0,a1)); assertTrue(!Util.equal(a0,a2)); assertTrue(!Util.equal(a0,a3)); assertTrue(!Util.equal(a1,a0)); assertTrue(!Util.equal(a1,a2)); assertTrue(!Util.equal(a1,a3)); assertTrue(!Util.equal(a2,a0)); assertTrue(!Util.equal(a2,a1)); assertTrue(!Util.equal(a2,a3)); assertTrue(!Util.equal(a3,a0)); assertTrue(!Util.equal(a3,a1)); assertTrue(!Util.equal(a3,a2)); } /** * Test if concat returns n+m bytes */ public void testConcatLength() { byte[] a; byte[] b; byte[] r; a = new byte[0]; b = new byte[30]; r = Util.concat(a,b); assertEquals(a.length + b.length, r.length); a = new byte[30]; b = new byte[0]; r = Util.concat(a,b); assertEquals(a.length + b.length, r.length); a = new byte[30]; b = new byte[30]; r = Util.concat(a,b); assertEquals(a.length + b.length, r.length); } /** * Test that slice returns the correct length */ public void testSliceLength() { byte[] b = new byte[300]; byte[] r; r = Util.slice(b, 0, 0); assertEquals(0, r.length); r = Util.slice(b, 30, 0); assertEquals(0, r.length); r = Util.slice(b, 0, 30); assertEquals(30, r.length); r = Util.slice(b, 30, 30); assertEquals(30, r.length); } /** * Test that slice returns the correct length */ public void testSlice() { byte[] b0 = Util.fromHex("00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF"); byte[] b1 = Util.fromHex("00 11 22 33 44 55 66 77 "); byte[] b2 = Util.fromHex(" 55 66 77 88 99 "); byte[] b3 = Util.fromHex(" DD EE FF"); byte[] b4 = Util.fromHex(" "); assertTrue(Util.equal(Util.slice(b0, 0, b0.length), b0)); assertTrue(Util.equal(Util.slice(b0, 0, 8), b1)); assertTrue(Util.equal(Util.slice(b0, 5, 5), b2)); assertTrue(Util.equal(Util.slice(b0, 13, 3), b3)); assertTrue(Util.equal(Util.slice(b0, 0, 0), b4)); assertTrue(Util.equal(Util.slice(b0, 5, 0), b4)); assertTrue(Util.equal(Util.slice(b0, 15, 0), b4)); } /** * Test if fromHex returns the right amount if bytes. */ public void testFromHexLength() { String s; byte[] r; s = ""; r = Util.fromHex(s); assertEquals(0, r.length); s = " "; r = Util.fromHex(s); assertEquals(0, r.length); s = "00 01 02 03 04"; r = Util.fromHex(s); assertEquals(5, r.length); s = "00 01 02 0 3 0 4"; r = Util.fromHex(s); assertEquals(5, r.length); } /** * Test if fromHex is sane. */ public void testFromHexSaneness() { String s1 = ""; String s2 = "00 01 02 03 04"; String s2_2 = "00 01 02 0 3 04"; String s3 = "10 20 f0 F0 Ab"; byte[] a1 = { }; byte[] a2 = { (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }; byte[] a3 = { (byte)0x10, (byte)0x20, (byte)0xf0, (byte)0xF0, (byte)0xab }; byte[] r; r = Util.fromHex(s1); assertTrue(Util.equal(a1, r)); r = Util.fromHex(s2); assertTrue(Util.equal(a2, r)); r = Util.fromHex(s2_2); assertTrue(Util.equal(a2, r)); r = Util.fromHex(s3); assertTrue(Util.equal(a3, r)); } /** * Test if fromHex(asHex()) is sane. */ public void testAsHexFromHex() { String s = "CA F3 E8 F6 23 B9 87 20 D2 F7 A8 66 9C B6 DE 01 71" + "31 CC 3E 74 20 80 99 62 2D 7D DF 98 59 D7 5B A6 77 78 FE 3C 22 C1 B5 AE 1F 8E" + "79 78 72 3D 0F 51 B7 EA 19 F7 93 7F F6 DC 21 EC 2C 13 54 DD 98"; byte[] r, r1; String s1; r = Util.fromHex(s); s1 = Util.asHex(r); r1 = Util.fromHex(s1); assertTrue(Util.equal(r, r1)); } /** * Test if toOctets() works. */ public void testToOctets() { String s0 = "Don't you wish you had more energy... or less ambition?"; byte[] o0 = Util.fromHex("44 6f 6e 27 74 20 79 6f 75 20 77 69 73 68 20 79" + "6f 75 20 68 61 64 20 6d 6f 72 65 20 65 6e 65 72" + "67 79 2e 2e 2e 20 6f 72 20 6c 65 73 73 20 61 6d" + "62 69 74 69 6f 6e 3f" ); String s1 = ""; byte[] o1 = {}; byte[] r; r = Util.toOctets(s0); assertTrue(Util.equal(r, o0)); r = Util.toOctets(s1); assertTrue(Util.equal(r, o1)); } public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new UtilTest("testEqual")); suite.addTest(new UtilTest("testConcatLength")); suite.addTest(new UtilTest("testSlice")); suite.addTest(new UtilTest("testSliceLength")); suite.addTest(new UtilTest("testFromHexLength")); suite.addTest(new UtilTest("testFromHexSaneness")); suite.addTest(new UtilTest("testAsHexFromHex")); suite.addTest(new UtilTest("testToOctets")); return suite; } public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } }