Fixed bug in NetUtils.isMulticastMACAddr() caused by sign extension.
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / utils / NetUtilsTest.java
index 599f97448a4ce4e4d504542f9ae7df6347442789..e5e0a0941b7db0ccb6b647b313096f64def5fa90 100644 (file)
@@ -1,6 +1,5 @@
-
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -65,6 +64,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 +287,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
@@ -331,4 +448,55 @@ public class NetUtilsTest {
         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));
+            }
+        }
+    }
 }