+
+ @Test
+ public void testInetAddressConflict() throws UnknownHostException {
+
+ // test a ipv4 testAddress in the same subnet as the filter
+ // the method should return false as there is no conflict
+ Assert.assertFalse(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.1.1"),
+ InetAddress.getByName("9.9.1.0"), null,
+ InetAddress.getByName("255.255.255.0")));
+
+ // test a ipv4 testAddress not in the same subnet as the filter
+ // the method should return true as there is a conflict
+ Assert.assertTrue(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.2.1"),
+ InetAddress.getByName("9.9.1.0"), null,
+ InetAddress.getByName("255.255.255.0")));
+
+ // test a ipv4 testAddress more generic than the filter
+ // the method should return true as there is a conflict
+ Assert.assertTrue(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.1.1"),
+ InetAddress.getByName("9.9.1.0"),
+ InetAddress.getByName("255.255.0.0"),
+ InetAddress.getByName("255.255.255.0")));
+
+ // test a ipv4 testAddress less generic than the filter and in the same
+ // subnet as the filter
+ // the method should return false as there is no conflict
+ Assert.assertFalse(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.1.0"),
+ InetAddress.getByName("9.9.0.0"),
+ InetAddress.getByName("255.255.255.0"),
+ InetAddress.getByName("255.255.0.0")));
+
+ // test a ipv4 testAddress less generic than the filter and not in the
+ // same subnet as the filter
+ // the method should return true as there is a conflict
+ Assert.assertTrue(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.8.1.0"),
+ InetAddress.getByName("9.9.0.0"),
+ InetAddress.getByName("255.255.255.0"),
+ InetAddress.getByName("255.255.0.0")));
+
+ }
+
+ @Test
+ public void testIPAddressValidity() {
+ Assert.assertFalse(NetUtils.isIPAddressValid(null));
+ Assert.assertFalse(NetUtils.isIPAddressValid("abc"));
+ Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1"));
+ Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1.1/49"));
+
+ Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1"));
+ Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1/32"));
+ Assert.assertTrue(NetUtils
+ .isIPAddressValid("2001:420:281:1004:407a:57f4:4d15:c355"));
+ }
+
+ @Test
+ public void testGetUnsignedByte() {
+ Assert.assertEquals(0, NetUtils.getUnsignedByte((byte) 0x00));
+ Assert.assertEquals(1, NetUtils.getUnsignedByte((byte) 0x01));
+ Assert.assertEquals(127, NetUtils.getUnsignedByte((byte) 0x7f));
+
+ Assert.assertEquals(128, NetUtils.getUnsignedByte((byte) 0x80));
+ Assert.assertEquals(255, NetUtils.getUnsignedByte((byte) 0xff));
+ }
+
+ @Test
+ public void testGetUnsignedShort() {
+ Assert.assertEquals(0, NetUtils.getUnsignedShort((short) 0x0000));
+ Assert.assertEquals(1, NetUtils.getUnsignedShort((short) 0x0001));
+ Assert.assertEquals(32767, NetUtils.getUnsignedShort((short) 0x7fff));
+
+ Assert.assertEquals(32768, NetUtils.getUnsignedShort((short) 0x8000));
+ Assert.assertEquals(65535, NetUtils.getUnsignedShort((short) 0xffff));
+ }
+
+ @Test
+ public void testMulticastMACAddr() {
+ byte[] empty = new byte[0];
+ Assert.assertFalse(NetUtils.isUnicastMACAddr(empty));
+ Assert.assertFalse(NetUtils.isMulticastMACAddr(empty));
+
+ byte[] bcast = {
+ (byte)0xff, (byte)0xff, (byte)0xff,
+ (byte)0xff, (byte)0xff, (byte)0xff,
+ };
+ Assert.assertFalse(NetUtils.isUnicastMACAddr(bcast));
+ Assert.assertFalse(NetUtils.isMulticastMACAddr(bcast));
+
+ byte[] firstOctet = {
+ (byte)0x00, (byte)0x20, (byte)0x80, (byte)0xfe,
+ };
+ for (int len = 1; len <= 10; len++) {
+ byte[] ba = new byte[len];
+ boolean valid = (len == 6);
+ for (byte first: firstOctet) {
+ ba[0] = first;
+ Assert.assertFalse(NetUtils.isMulticastMACAddr(ba));
+ Assert.assertEquals(valid, NetUtils.isUnicastMACAddr(ba));
+
+ ba[0] |= (byte)0x01;
+ Assert.assertEquals(valid, NetUtils.isMulticastMACAddr(ba));
+ Assert.assertFalse(NetUtils.isUnicastMACAddr(ba));
+ }
+ }
+ }