X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fapi%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Futils%2FNetUtilsTest.java;h=b8bc6fb4470901c0761df18e51482489c7e4325b;hb=405ea7ce68d22bd3d2501857c5253793b581b086;hp=77d1fe216413d38477eaffabdc5f8f1dea9bfea4;hpb=42210c03b0a4c54706320ba9f55794c0abd4d201;p=controller.git diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java index 77d1fe2164..b8bc6fb447 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java @@ -15,7 +15,6 @@ import java.util.Arrays; import org.junit.Assert; import org.junit.Test; -import org.opendaylight.controller.sal.utils.NetUtils; public class NetUtilsTest { @@ -66,6 +65,94 @@ public class NetUtilsTest { .byteArray4ToInt(ba4)))); } + @Test + public void testByteArrayMethodsForLong() { + // Test of longToByteArray6 method. + byte ba[] = { + (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, + (byte) 0x55, (byte) 0x66 + }; + long mac = 0x112233445566L; + Assert.assertTrue(Arrays.equals(ba, NetUtils.longToByteArray6(mac))); + + byte ba1[] = { + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff + }; + long mac1 = 0xffffffffffffL; + Assert.assertTrue(Arrays.equals(ba1, NetUtils.longToByteArray6(mac1))); + + byte ba2[] = { + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00 + }; + long mac2 = 0x000000000000L; + Assert.assertTrue(Arrays.equals(ba2, NetUtils.longToByteArray6(mac2))); + + byte ba3[] = { + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, + (byte) 0x00, (byte) 0x00 + }; + long mac3 = 0xffffff000000L; + Assert.assertTrue(Arrays.equals(ba3, NetUtils.longToByteArray6(mac3))); + + byte ba4[] = { + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff, + (byte) 0xff, (byte) 0xff + }; + long mac4 = 0x000000ffffffL; + Assert.assertTrue(Arrays.equals(ba4, NetUtils.longToByteArray6(mac4))); + + // Convert a long number to a byte array, + // and revert it to the long number again. + Assert.assertTrue(NetUtils + .byteArray6ToLong(NetUtils.longToByteArray6(mac)) == mac); + + Assert.assertTrue(NetUtils + .byteArray6ToLong(NetUtils.longToByteArray6(mac1)) == mac1); + + Assert.assertTrue(NetUtils + .byteArray6ToLong(NetUtils.longToByteArray6(mac2)) == mac2); + + Assert.assertTrue(NetUtils + .byteArray6ToLong(NetUtils.longToByteArray6(mac3)) == mac3); + + Assert.assertTrue(NetUtils + .byteArray6ToLong(NetUtils.longToByteArray6(mac4)) == mac4); + + // Convert a byte array to a long nubmer, + // and revert it to the byte array again. + Assert.assertTrue(Arrays.equals(ba, + NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba)))); + + Assert.assertTrue(Arrays.equals(ba1, + NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba1)))); + + Assert.assertTrue(Arrays.equals(ba2, + NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba2)))); + + Assert.assertTrue(Arrays.equals(ba3, + NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba3)))); + + Assert.assertTrue(Arrays.equals(ba4, + NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba4)))); + + // Test of paramter validation of byteArray6ToLong method. + byte array5[] = { + (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 + }; + Assert.assertEquals(0, NetUtils.byteArray6ToLong(array5)); + + byte array7[] = { + (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, + (byte) 0x55, (byte) 0x66, (byte) 0x77 + }; + Assert.assertEquals(0, NetUtils.byteArray6ToLong(array7)); + + byte arrayNull[] = null; + Assert.assertEquals(0, NetUtils.byteArray6ToLong(arrayNull)); + } + @Test public void testInetMethods() throws UnknownHostException { int ip = 0xfffffff0; @@ -201,16 +288,46 @@ public class NetUtilsTest { public void testGetSubnetLen() { byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 }; - Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 31); + Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 1); byte address1[] = { (byte) 255, 0, 0, 0 }; - Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 24); + Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 8); byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 }; - Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 11); + Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 21); byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 }; - Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 1); + Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 31); + + byte address5[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, + (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, + (byte) 255 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address5) == 128); + + byte address6[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, + (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, + (byte) 254 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address6) == 127); + + byte address7[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, + (byte) 255, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address7) == 64); + + byte address8[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, + (byte) 254, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address8) == 63); + + byte address9[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 128, + (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address9) == 49); + + byte address10[] = { (byte) 128, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, + (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address10) == 1); + + byte address11[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, + (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; + Assert.assertTrue(NetUtils.getSubnetMaskLength(address11) == 0); } @Test @@ -274,4 +391,62 @@ public class NetUtilsTest { .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); //not valid both } + + @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")); + } }