Fixed bug in NetUtils.isMulticastMACAddr() caused by sign extension. 03/5603/2
authorShigeru Yasuda <s-yasuda@da.jp.nec.com>
Tue, 11 Mar 2014 16:41:55 +0000 (01:41 +0900)
committerShigeru Yasuda <s-yasuda@da.jp.nec.com>
Tue, 11 Mar 2014 19:20:06 +0000 (04:20 +0900)
This patch fixes a bug that returned false incorrectly when the MSB was set
in the MAC address.

Change-Id: I8d04534962b0a1a585cf0217969a6415f360140b
Signed-off-by: Shigeru Yasuda <s-yasuda@da.jp.nec.com>
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java
opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java

index dc34162..6b303f0 100644 (file)
@@ -355,9 +355,7 @@ public abstract class NetUtils {
      */
     public static boolean isMulticastMACAddr(byte[] MACAddress) {
         if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
-            if (MACAddress[0] % 2 == 1) {
-                return true;
-            }
+            return (MACAddress[0] & 1) != 0;
         }
         return false;
     }
index a2b1278..e5e0a09 100644 (file)
@@ -468,4 +468,35 @@ public class NetUtilsTest {
         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));
+            }
+        }
+    }
 }