public class ByteBufUtilsTest {
private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
-
+
/**
* Test of {@link org.opendaylight.openflowjava.util.ByteBufUtils#hexStringToBytes(String)}
*/
@Test
public void testHexStringToByteBuf() {
ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
-
+
Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
}
-
+
/**
* Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
*/
Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
}
-
+
private static byte[] byteBufToByteArray(ByteBuf bb) {
byte[] result = new byte[bb.readableBytes()];
bb.readBytes(result);
return result;
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
*/
Map<Integer, Boolean> emptyMap = new HashMap<>();
String expectedBinaryString = "00000000000000000000000000000000";
String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
-
+
Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
}
private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
- String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
+ String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
*/
bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
*/
bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
*/
bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
*/
emptyList.add(null);
String expectedBinaryString = "00000000000000000000000000000000";
String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
-
+
Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
}
private static String listToBinaryString(List<Boolean> emptyList, int length) {
int[] bitMaskArray;
bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
- String binaryString = Integer.toBinaryString(bitMaskArray[0]);
+ String binaryString = Integer.toBinaryString(bitMaskArray[0]);
return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
*/
bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
*/
bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
}
-
+
/**
* Test of {@link ByteBufUtils#fillBitMaskFromList(List)}
*/
public void testMacToBytes() {
Assert.assertArrayEquals("Wrong byte array", new byte[]{0, 1, 2, 3, (byte) 255, 5},
ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05"));
- Assert.assertArrayEquals("Wrong byte array", new byte[]{11, 1, 2, 3, (byte) 255, 10},
- ByteBufUtils.macAddressToBytes("0b:01:02:03:FF:0a"));
+ Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, (byte) 255, 5},
+ ByteBufUtils.macAddressToBytes("01:02:03:04:FF:05"));
+ Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, (byte) 255, 5},
+ ByteBufUtils.macAddressToBytes("1:2:3:4:FF:5"));
+ Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 2, 3, 4, 5, (byte) 255},
+ ByteBufUtils.macAddressToBytes("1:2:3:4:5:FF"));
+ Assert.assertArrayEquals("Wrong byte array", new byte[]{1, 15, 3, 4, 5, 6},
+ ByteBufUtils.macAddressToBytes("1:F:3:4:5:6"));
}
/**
ByteBufUtils.macAddressToBytes("00:01:02:03:FF:0G"));
}
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testMacToBytesTooShort() {
+ ByteBufUtils.macAddressToBytes("00:01:02:03:FF");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testMacToBytesTooShort2() {
+ ByteBufUtils.macAddressToBytes("00:01:02:03:FF:");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testIncorrectMacToBytes() {
+ ByteBufUtils.macAddressToBytes("00:01:02:03:FF::");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testIncorrectMacToBytes2() {
+ ByteBufUtils.macAddressToBytes("00:01:02:03:FF:::");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testMacToBytesTooLong() {
+ ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05:85");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testMacToBytesInvalidOctet() {
+ ByteBufUtils.macAddressToBytes("00:01:02:03:FF:05d");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testMacToBytesInvalidOctet2() {
+ ByteBufUtils.macAddressToBytes("00:01:rr:03:FF:05");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#macAddressToBytes(String)}
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testMacToBytesInvalidOctet3() {
+ ByteBufUtils.macAddressToBytes("00:01:05d:03:FF:02");
+ }
+
/**
* Test of {@link ByteBufUtils#macAddressToString(byte[])}
*/
public void testDecodeString() {
ByteBuf buf = ByteBufUtils.hexStringToByteBuf("4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00");
Assert.assertEquals("Wrong string decoded", "JAMES BOND", ByteBufUtils.decodeNullTerminatedString(buf, 16));
-
+
ByteBuf buf2 = ByteBufUtils.hexStringToByteBuf("53 50 49 44 45 52 4D 41 4E 00 00 00 00 00 00");
Assert.assertEquals("Wrong string decoded", "SPIDERMAN", ByteBufUtils.decodeNullTerminatedString(buf2, 15));
}
String ipv4Address = ByteBufUtils.readIpv4Address(buffer);
Assert.assertEquals("Wrong conversion", "10.20.30.40", ipv4Address);
Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
-
+
ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
buffer.writeByte(10);
ipv4Address = ByteBufUtils.readIpv4Address(buffer2);
String ipv4Address = ByteBufUtils.readIpv6Address(buffer);
Assert.assertEquals("Wrong conversion", "000A:FFFF:1000:0000:0400:002A:0A08:B279", ipv4Address);
Assert.assertTrue("Unexpected data", buffer.readableBytes() == 0);
-
+
ByteBuf buffer2 = PooledByteBufAllocator.DEFAULT.buffer();
buffer.writeShort(10);
ipv4Address = ByteBufUtils.readIpv6Address(buffer2);