package uk.ac.cam.db538.cryptosms.utils; import junit.framework.TestCase; import uk.ac.cam.db538.cryptosms.CustomAsserts; public class LowLevel_Test extends TestCase { public void testGetInt() { byte[] data; long result; // CONVERSION TESTS data = new byte[] { 0x01, 0x00, 0x00, 0x00 }; result = LowLevel.getUnsignedInt(data); assertEquals(16777216L, result); data = new byte[] { 0x00, 0x00, 0x00, 0x01 }; result = LowLevel.getUnsignedInt(data); assertEquals(1L, result); data = new byte[] { (byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x89 }; result = LowLevel.getUnsignedInt(data); assertEquals(2882400137L, result); data = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; result = LowLevel.getUnsignedInt(data); assertEquals(4294967295L, result); // OFFSET TESTS // data somewhere in the bytes data = new byte[] { (byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x89, 0x00, 0x00, 0x00, 0x01 }; result = LowLevel.getUnsignedInt(data, 4); assertEquals(1L, result); // index greater than limit try { data = new byte[] { (byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x89, 0x00, 0x00, 0x00, 0x01 }; result = LowLevel.getUnsignedInt(data, 5); assertTrue(false); } catch (IndexOutOfBoundsException ex) { } // negative index try { data = new byte[] { (byte) 0xAB, (byte) 0xCD, (byte) 0xEF, (byte) 0x89, 0x00, 0x00, 0x00, 0x01 }; result = LowLevel.getUnsignedInt(data, -1); assertTrue(false); } catch (IndexOutOfBoundsException ex) { } } public void testGetBytes() { long number; byte[] result, expected; // CONVERSION TESTS expected = new byte[] { (byte) 0x84, (byte) 0xD2, (byte) 0xC3, (byte) 0x6E }; number = 2228405102L; result = LowLevel.getBytesUnsignedInt(number); CustomAsserts.assertArrayEquals(expected, result); expected = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; number = 4294967295L; result = LowLevel.getBytesUnsignedInt(number); CustomAsserts.assertArrayEquals(expected, result); // unsigned byte assertEquals(145, LowLevel.getUnsignedByte((byte)0x91)); assertEquals(21, LowLevel.getUnsignedByte((byte)0x15)); assertEquals((byte)0x91, LowLevel.getBytesUnsignedByte(145)); assertEquals((byte)0x15, LowLevel.getBytesUnsignedByte(21)); } }