Tests for IPAddress and VLANID
Change-Id: If8183366428c9b4fb14f78005922b2229cff1456
Showing
4 changed files
with
97 additions
and
13 deletions
| ... | @@ -40,12 +40,11 @@ public class IPAddress { | ... | @@ -40,12 +40,11 @@ public class IPAddress { |
| 40 | * @return an IP address | 40 | * @return an IP address |
| 41 | */ | 41 | */ |
| 42 | public static IPAddress valueOf(int address) { | 42 | public static IPAddress valueOf(int address) { |
| 43 | - byte [] bytes = new byte [] { | 43 | + byte [] bytes = new byte [INET_LEN]; |
| 44 | - (byte) ((address >> 24) & 0xff), | 44 | + for (int i = 0; i < INET_LEN; i++) { |
| 45 | - (byte) ((address >> 16) & 0xff), | 45 | + bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff); |
| 46 | - (byte) ((address >> 8) & 0xff), | 46 | + } |
| 47 | - (byte) ((address >> 0) & 0xff) | 47 | + |
| 48 | - }; | ||
| 49 | return new IPAddress(Version.INET, bytes); | 48 | return new IPAddress(Version.INET, bytes); |
| 50 | } | 49 | } |
| 51 | 50 | ||
| ... | @@ -87,12 +86,16 @@ public class IPAddress { | ... | @@ -87,12 +86,16 @@ public class IPAddress { |
| 87 | return Arrays.copyOf(this.octets, INET_LEN); | 86 | return Arrays.copyOf(this.octets, INET_LEN); |
| 88 | } | 87 | } |
| 89 | 88 | ||
| 89 | + /** | ||
| 90 | + * Returns the integral value of this IP address. | ||
| 91 | + * | ||
| 92 | + * @return the IP address's value as an integer | ||
| 93 | + */ | ||
| 90 | public int toInt() { | 94 | public int toInt() { |
| 91 | - int address = | 95 | + int address = 0; |
| 92 | - ((octets[0] << 24) | | 96 | + for (int i = 0; i < INET_LEN; i++) { |
| 93 | - (octets[1] << 16) | | 97 | + address |= octets[i] << ((INET_LEN - (i + 1)) * 8); |
| 94 | - (octets[2] << 8) | | 98 | + } |
| 95 | - (octets[3] << 0)); | ||
| 96 | return address; | 99 | return address; |
| 97 | } | 100 | } |
| 98 | 101 | ... | ... |
| ... | @@ -7,9 +7,9 @@ public class VLANID { | ... | @@ -7,9 +7,9 @@ public class VLANID { |
| 7 | 7 | ||
| 8 | private final short value; | 8 | private final short value; |
| 9 | // Based on convention used elsewhere? Check and change if needed | 9 | // Based on convention used elsewhere? Check and change if needed |
| 10 | - private static final short UNTAGGED = (short) 0xffff; | 10 | + public static final short UNTAGGED = (short) 0xffff; |
| 11 | // A VLAN ID is actually 12 bits of a VLAN tag. | 11 | // A VLAN ID is actually 12 bits of a VLAN tag. |
| 12 | - private static final short MAX_VLAN = 4095; | 12 | + public static final short MAX_VLAN = 4095; |
| 13 | 13 | ||
| 14 | protected VLANID() { | 14 | protected VLANID() { |
| 15 | this.value = UNTAGGED; | 15 | this.value = UNTAGGED; | ... | ... |
| 1 | +package org.onlab.packet; | ||
| 2 | + | ||
| 3 | +import static org.junit.Assert.assertEquals; | ||
| 4 | + | ||
| 5 | +import java.util.Arrays; | ||
| 6 | + | ||
| 7 | +import org.junit.Test; | ||
| 8 | +import org.onlab.packet.IPAddress.Version; | ||
| 9 | + | ||
| 10 | +import com.google.common.testing.EqualsTester; | ||
| 11 | + | ||
| 12 | +public class IPAddressTest { | ||
| 13 | + | ||
| 14 | + private static final byte [] BYTES1 = new byte [] {0x0, 0x0, 0x0, 0xa}; | ||
| 15 | + private static final byte [] BYTES2 = new byte [] {0x0, 0x0, 0x0, 0xb}; | ||
| 16 | + private static final int INTVAL1 = 10; | ||
| 17 | + private static final int INTVAL2 = 12; | ||
| 18 | + private static final String STRVAL = "0.0.0.11"; | ||
| 19 | + | ||
| 20 | + @Test | ||
| 21 | + public void testEquality() { | ||
| 22 | + IPAddress ip1 = IPAddress.valueOf(BYTES1); | ||
| 23 | + IPAddress ip2 = IPAddress.valueOf(BYTES2); | ||
| 24 | + IPAddress ip3 = IPAddress.valueOf(INTVAL1); | ||
| 25 | + IPAddress ip4 = IPAddress.valueOf(INTVAL2); | ||
| 26 | + IPAddress ip5 = IPAddress.valueOf(STRVAL); | ||
| 27 | + | ||
| 28 | + new EqualsTester().addEqualityGroup(ip1, ip3) | ||
| 29 | + .addEqualityGroup(ip2, ip5) | ||
| 30 | + .addEqualityGroup(ip4) | ||
| 31 | + .testEquals(); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + @Test | ||
| 35 | + public void basics() { | ||
| 36 | + IPAddress ip4 = IPAddress.valueOf(BYTES1); | ||
| 37 | + assertEquals("incorrect IP Version", Version.INET, ip4.version()); | ||
| 38 | + assertEquals("faulty toOctets()", Arrays.equals( | ||
| 39 | + new byte [] {0x0, 0x0, 0x0, 0xa}, ip4.toOctets()), true); | ||
| 40 | + assertEquals("faulty toInt()", INTVAL1, ip4.toInt()); | ||
| 41 | + assertEquals("faulty toString()", "0.0.0.10", ip4.toString()); | ||
| 42 | + } | ||
| 43 | +} |
| 1 | +package org.onlab.packet; | ||
| 2 | + | ||
| 3 | +import static org.junit.Assert.assertEquals; | ||
| 4 | + | ||
| 5 | +import org.junit.Test; | ||
| 6 | + | ||
| 7 | +import com.google.common.testing.EqualsTester; | ||
| 8 | + | ||
| 9 | +public class VLANIDTest { | ||
| 10 | + | ||
| 11 | + @Test | ||
| 12 | + public void testEquality() { | ||
| 13 | + | ||
| 14 | + VLANID vlan1 = VLANID.vlanId((short) -1); | ||
| 15 | + VLANID vlan2 = VLANID.vlanId((short) 100); | ||
| 16 | + VLANID vlan3 = VLANID.vlanId((short) 100); | ||
| 17 | + | ||
| 18 | + new EqualsTester().addEqualityGroup(VLANID.vlanId(), vlan1) | ||
| 19 | + .addEqualityGroup(vlan2, vlan3) | ||
| 20 | + .addEqualityGroup(VLANID.vlanId((short) 10)); | ||
| 21 | + | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + @Test | ||
| 25 | + public void basics() { | ||
| 26 | + // purposefully create UNTAGGED VLAN | ||
| 27 | + VLANID vlan1 = VLANID.vlanId((short) 10); | ||
| 28 | + VLANID vlan2 = VLANID.vlanId((short) -1); | ||
| 29 | + | ||
| 30 | + assertEquals("incorrect VLAN value", 10, vlan1.toShort()); | ||
| 31 | + assertEquals("invalid untagged value", VLANID.UNTAGGED, vlan2.toShort()); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + @Test(expected = IllegalArgumentException.class) | ||
| 35 | + public void testIllicitVLAN() { | ||
| 36 | + VLANID.vlanId((short) 5000); | ||
| 37 | + } | ||
| 38 | +} |
-
Please register or login to post a comment