Merge "Cleanup imports/whitespace in MD-SAL"
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / utils / NetUtilsTest.java
index e16612759caebe9083efb17d38495b4a462de662..b8bc6fb4470901c0761df18e51482489c7e4325b 100644 (file)
@@ -65,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;
@@ -200,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
@@ -318,4 +436,17 @@ public class NetUtilsTest {
                 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"));
+    }
 }