Tests for IPAddress and VLANID

Change-Id: If8183366428c9b4fb14f78005922b2229cff1456
diff --git a/utils/misc/src/main/java/org/onlab/packet/IPAddress.java b/utils/misc/src/main/java/org/onlab/packet/IPAddress.java
index 5f4e174..1131ca4 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IPAddress.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IPAddress.java
@@ -40,12 +40,11 @@
      * @return an IP address
      */
     public static IPAddress valueOf(int address) {
-        byte [] bytes = new byte [] {
-                (byte) ((address >> 24) & 0xff),
-                (byte) ((address >> 16) & 0xff),
-                (byte) ((address >> 8) & 0xff),
-                (byte) ((address >> 0) & 0xff)
-        };
+        byte [] bytes = new byte [INET_LEN];
+        for (int i = 0; i < INET_LEN; i++) {
+            bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff);
+        }
+
         return new IPAddress(Version.INET, bytes);
     }
 
@@ -87,12 +86,16 @@
         return Arrays.copyOf(this.octets, INET_LEN);
     }
 
+    /**
+     * Returns the integral value of this IP address.
+     *
+     * @return the IP address's value as an integer
+     */
     public int toInt() {
-        int address =
-                ((octets[0] << 24) |
-                (octets[1] << 16) |
-                (octets[2] << 8) |
-                (octets[3] << 0));
+        int address = 0;
+        for (int i = 0; i < INET_LEN; i++) {
+            address |= octets[i] << ((INET_LEN - (i + 1)) * 8);
+        }
         return address;
     }
 
diff --git a/utils/misc/src/main/java/org/onlab/packet/VLANID.java b/utils/misc/src/main/java/org/onlab/packet/VLANID.java
index c978e52..d2a09fa 100644
--- a/utils/misc/src/main/java/org/onlab/packet/VLANID.java
+++ b/utils/misc/src/main/java/org/onlab/packet/VLANID.java
@@ -7,9 +7,9 @@
 
     private final short value;
     // Based on convention used elsewhere? Check and change if needed
-    private static final short UNTAGGED = (short) 0xffff;
+    public static final short UNTAGGED = (short) 0xffff;
     // A VLAN ID is actually 12 bits of a VLAN tag.
-    private static final short MAX_VLAN = 4095;
+    public static final short MAX_VLAN = 4095;
 
     protected VLANID() {
         this.value = UNTAGGED;
diff --git a/utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java b/utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java
new file mode 100644
index 0000000..35c8271
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java
@@ -0,0 +1,43 @@
+package org.onlab.packet;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.onlab.packet.IPAddress.Version;
+
+import com.google.common.testing.EqualsTester;
+
+public class IPAddressTest {
+
+    private static final byte [] BYTES1 = new byte [] {0x0, 0x0, 0x0, 0xa};
+    private static final byte [] BYTES2 = new byte [] {0x0, 0x0, 0x0, 0xb};
+    private static final int INTVAL1 = 10;
+    private static final int INTVAL2 = 12;
+    private static final String STRVAL = "0.0.0.11";
+
+    @Test
+    public void testEquality() {
+        IPAddress ip1 = IPAddress.valueOf(BYTES1);
+        IPAddress ip2 = IPAddress.valueOf(BYTES2);
+        IPAddress ip3 = IPAddress.valueOf(INTVAL1);
+        IPAddress ip4 = IPAddress.valueOf(INTVAL2);
+        IPAddress ip5 = IPAddress.valueOf(STRVAL);
+
+        new EqualsTester().addEqualityGroup(ip1, ip3)
+        .addEqualityGroup(ip2, ip5)
+        .addEqualityGroup(ip4)
+        .testEquals();
+    }
+
+    @Test
+    public void basics() {
+        IPAddress ip4 = IPAddress.valueOf(BYTES1);
+        assertEquals("incorrect IP Version", Version.INET, ip4.version());
+        assertEquals("faulty toOctets()", Arrays.equals(
+                new byte [] {0x0, 0x0, 0x0, 0xa}, ip4.toOctets()), true);
+        assertEquals("faulty toInt()", INTVAL1, ip4.toInt());
+        assertEquals("faulty toString()", "0.0.0.10", ip4.toString());
+    }
+}
diff --git a/utils/misc/src/test/java/org/onlab/packet/VLANIDTest.java b/utils/misc/src/test/java/org/onlab/packet/VLANIDTest.java
new file mode 100644
index 0000000..88fb9c8
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/VLANIDTest.java
@@ -0,0 +1,38 @@
+package org.onlab.packet;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.google.common.testing.EqualsTester;
+
+public class VLANIDTest {
+
+    @Test
+    public void testEquality() {
+
+        VLANID vlan1 = VLANID.vlanId((short) -1);
+        VLANID vlan2 = VLANID.vlanId((short) 100);
+        VLANID vlan3 = VLANID.vlanId((short) 100);
+
+        new EqualsTester().addEqualityGroup(VLANID.vlanId(), vlan1)
+        .addEqualityGroup(vlan2, vlan3)
+        .addEqualityGroup(VLANID.vlanId((short) 10));
+
+    }
+
+    @Test
+    public void basics() {
+        // purposefully create UNTAGGED VLAN
+        VLANID vlan1 = VLANID.vlanId((short) 10);
+        VLANID vlan2 = VLANID.vlanId((short) -1);
+
+        assertEquals("incorrect VLAN value", 10, vlan1.toShort());
+        assertEquals("invalid untagged value", VLANID.UNTAGGED, vlan2.toShort());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testIllicitVLAN() {
+        VLANID.vlanId((short) 5000);
+    }
+}