Merge remote-tracking branch 'floodlight/master'
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
index 7c50aed..c96be83 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddress.java
@@ -4,7 +4,32 @@
public abstract IPVersion getIpVersion();
+ /**
+ * Checks if this IPAddress represents a valid CIDR style netmask, i.e.,
+ * it has a set of leading "1" bits followed by only "0" bits
+ * @return true if this represents a valid CIDR style netmask, false
+ * otherwise
+ */
+ public abstract boolean isCidrMask();
+
+ /**
+ * If this IPAddress represents a valid CIDR style netmask (see
+ * isCidrMask()) returns the length of the prefix (the number of "1" bits).
+ * @return length of CIDR mask if this represents a valid CIDR mask
+ * @throws IllegalStateException if isCidrMask() == false
+ */
+ public abstract int asCidrMaskLength();
+
+ @Override
+ public abstract boolean equals(Object other);
+
+ @Override
+ public abstract int hashCode();
+
public static IPAddress<?> of(String ip) {
+ if (ip == null) {
+ throw new NullPointerException("String ip must not be null");
+ }
if (ip.indexOf('.') != -1)
return IPv4Address.of(ip);
else if (ip.indexOf(':') != -1)
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
index 11ef103..2087ab4 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPAddressWithMask.java
@@ -10,6 +10,9 @@
public abstract IPVersion getIpVersion();
public static IPAddressWithMask<?> of(String ip) {
+ if (ip == null) {
+ throw new NullPointerException("String ip must not be null");
+ }
if (ip.indexOf('.') != -1)
return IPv4AddressWithMask.of(ip);
else if (ip.indexOf(':') != -1)
@@ -18,4 +21,21 @@
throw new IllegalArgumentException("IP Address not well formed: " + ip);
}
+ @Override
+ public String toString() {
+ StringBuilder res = new StringBuilder();
+ res.append(value.toString());
+
+ res.append('/');
+ if (mask.isCidrMask()) {
+ // CIDR notation
+ res.append(mask.asCidrMaskLength());
+ } else {
+ // Full address mask
+ res.append(mask.toString());
+ }
+
+ return res.toString();
+ }
+
}
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
index 51d10f3..2505d56 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
@@ -18,6 +18,11 @@
static final int LENGTH = 4;
private final int rawValue;
+ private static int NOT_A_CIDR_MASK = -1;
+ private static int CIDR_MASK_CACHE_UNSET = -2;
+ // Must appear before the static IPv4Address constant assignments
+ private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET;
+
private final static int NONE_VAL = 0x0;
public final static IPv4Address NONE = new IPv4Address(NONE_VAL);
@@ -33,7 +38,41 @@
return IPVersion.IPv4;
}
+ private int asCidrMaskLengthInternal() {
+ if (cidrMaskLengthCache == CIDR_MASK_CACHE_UNSET) {
+ // No lock required. We only write cidrMaskLengthCache once
+ int maskint = getInt();
+ if (maskint == 0) {
+ cidrMaskLengthCache = 0;
+ } else if (Integer.bitCount((~maskint) + 1) == 1) {
+ // IP represents a true CIDR prefix length
+ cidrMaskLengthCache = Integer.bitCount(maskint);
+ } else {
+ cidrMaskLengthCache = NOT_A_CIDR_MASK;
+ }
+ }
+ return cidrMaskLengthCache;
+ }
+
+ @Override
+ public boolean isCidrMask() {
+ return asCidrMaskLengthInternal() != NOT_A_CIDR_MASK;
+ }
+
+ @Override
+ public int asCidrMaskLength() {
+ if (!isCidrMask()) {
+ throw new IllegalStateException("IP is not a valid CIDR prefix " +
+ "mask " + toString());
+ } else {
+ return asCidrMaskLengthInternal();
+ }
+ }
+
public static IPv4Address of(final byte[] address) {
+ if (address == null) {
+ throw new NullPointerException("Address must not be null");
+ }
if (address.length != LENGTH) {
throw new IllegalArgumentException(
"Invalid byte array length for IPv4Address address: " + address.length);
@@ -52,6 +91,9 @@
}
public static IPv4Address of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
int start = 0;
int shift = 24;
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
index f30fcbb..9b60c6a 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4AddressWithMask.java
@@ -22,28 +22,19 @@
}
public static IPv4AddressWithMask of(IPv4Address value, IPv4Address mask) {
+ if (value == null) {
+ throw new NullPointerException("Value must not be null");
+ }
+ if (mask == null) {
+ throw new NullPointerException("Mask must not be null");
+ }
return new IPv4AddressWithMask(value, mask);
}
- @Override
- public String toString() {
- StringBuilder res = new StringBuilder();
- res.append(value.toString());
-
- int maskint = mask.getInt();
- res.append('/');
- if (Integer.bitCount((~maskint) + 1) == 1) {
- // CIDR notation
- res.append(Integer.bitCount(maskint));
- } else {
- // Full address mask
- res.append(mask.toString());
- }
-
- return res.toString();
- }
-
public static IPv4AddressWithMask of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
int slashPos;
String ip = string;
int maskBits = 32;
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
index 1aad85b..b69035c 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6Address.java
@@ -4,7 +4,6 @@
import org.jboss.netty.buffer.ChannelBuffer;
import org.projectfloodlight.openflow.exceptions.OFParseError;
-
import com.google.common.hash.PrimitiveSink;
import com.google.common.primitives.Longs;
@@ -19,10 +18,16 @@
private final long raw1;
private final long raw2;
+ private static int NOT_A_CIDR_MASK = -1;
+ private static int CIDR_MASK_CACHE_UNSET = -2;
+ // Must appear before the static IPv4Address constant assignments
+ private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET;
+
private final static long NONE_VAL1 = 0x0L;
private final static long NONE_VAL2 = 0x0L;
public static final IPv6Address NONE = new IPv6Address(NONE_VAL1, NONE_VAL2);
+
public static final IPv6Address NO_MASK = IPv6Address.of(0xFFFFFFFFFFFFFFFFl, 0xFFFFFFFFFFFFFFFFl);
public static final IPv6Address FULL_MASK = IPv6Address.of(0x0, 0x0);
@@ -36,7 +41,60 @@
return IPVersion.IPv6;
}
+
+ private int computeCidrMask64(long raw) {
+ long mask = raw;
+ if (raw == 0)
+ return 0;
+ else if (Long.bitCount((~mask) + 1L) == 1L) {
+ // represent a true CIDR prefix length
+ return Long.bitCount(mask);
+ }
+ else {
+ // Not a true prefix
+ return NOT_A_CIDR_MASK;
+ }
+ }
+
+ private int asCidrMaskLengthInternal() {
+ if (cidrMaskLengthCache == CIDR_MASK_CACHE_UNSET) {
+ synchronized (this) {
+ if (raw1 == 0 && raw2 == 0) {
+ cidrMaskLengthCache = 0;
+ } else if (raw1 == -1) {
+ // top half is all 1 bits
+ cidrMaskLengthCache = computeCidrMask64(raw2);
+ if (cidrMaskLengthCache != NOT_A_CIDR_MASK)
+ cidrMaskLengthCache += 64;
+ } else if (raw2 == 0) {
+ cidrMaskLengthCache = computeCidrMask64(raw1);
+ } else {
+ cidrMaskLengthCache = NOT_A_CIDR_MASK;
+ }
+ }
+ }
+ return cidrMaskLengthCache;
+ }
+
+ @Override
+ public boolean isCidrMask() {
+ return asCidrMaskLengthInternal() != NOT_A_CIDR_MASK;
+ }
+
+ @Override
+ public int asCidrMaskLength() {
+ if (!isCidrMask()) {
+ throw new IllegalStateException("IP is not a valid CIDR prefix " +
+ "mask " + toString());
+ } else {
+ return asCidrMaskLengthInternal();
+ }
+ }
+
public static IPv6Address of(final byte[] address) {
+ if (address == null) {
+ throw new NullPointerException("Address must not be null");
+ }
if (address.length != LENGTH) {
throw new IllegalArgumentException(
"Invalid byte array length for IPv6 address: " + address.length);
@@ -82,6 +140,9 @@
private final static Pattern colonPattern = Pattern.compile(":");
public static IPv6Address of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
IPv6Builder builder = new IPv6Builder();
String[] parts = colonPattern.split(string, -1);
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
index 6faf0b8..7259c7f 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv6AddressWithMask.java
@@ -16,28 +16,20 @@
}
public static IPv6AddressWithMask of(IPv6Address value, IPv6Address mask) {
+ if (value == null) {
+ throw new NullPointerException("Value must not be null");
+ }
+ if (mask == null) {
+ throw new NullPointerException("Mask must not be null");
+ }
return new IPv6AddressWithMask(value, mask);
}
- @Override
- public String toString() {
- StringBuilder res = new StringBuilder();
- res.append(value.toString());
- res.append('/');
-
- BigInteger maskint = new BigInteger(mask.getBytes());
- if (maskint.not().add(BigInteger.ONE).bitCount() == 1) {
- // CIDR notation
- res.append(maskint.bitCount());
- } else {
- // Full address mask
- res.append(mask.toString());
- }
-
- return res.toString();
- }
public static IPv6AddressWithMask of(final String string) {
+ if (string == null) {
+ throw new NullPointerException("String must not be null");
+ }
int slashPos;
String ip = string;
int maskBits = 128;
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
new file mode 100644
index 0000000..25fc943
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPAddressTest.java
@@ -0,0 +1,54 @@
+package org.projectfloodlight.openflow.types;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Most tests are in IPv4AddressTest and IPv6AddressTest
+ * Just exception testing here
+ * @author gregor
+ *
+ */
+public class IPAddressTest {
+ @Test
+ public void testOfException() {
+ try {
+ IPAddress.of("Foobar");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPAddressWithMask.of("Foobar");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPAddress.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPAddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPAddress.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPAddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ }
+
+}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
index 38d60b3..065331f 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
@@ -1,9 +1,6 @@
package org.projectfloodlight.openflow.types;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
import org.hamcrest.CoreMatchers;
import org.jboss.netty.buffer.ChannelBuffers;
@@ -38,6 +35,7 @@
"1.2..3.4",
"1.2.3.4.",
"257.11.225.1",
+ "256.11.225.1",
"-1.2.3.4",
"1.2.3.4.5",
"1.x.3.4",
@@ -49,7 +47,10 @@
"192.168.130.140/255.255.192.0",
"127.0.0.1/8",
"8.8.8.8",
- "0.0.0.0/0"
+ "8.8.8.8/32",
+ "0.0.0.0/0",
+ "192.168.130.140/255.0.255.0",
+ "1.2.3.4/0.127.0.255"
};
boolean[] hasMask = {
@@ -57,6 +58,9 @@
true,
true,
false,
+ false,
+ true,
+ true,
true
};
@@ -64,10 +68,63 @@
new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
- new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null },
- new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } }
+ new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
+ new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
+ new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
+ new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
+ new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
};
+ int[] ipsWithMaskLengths = {
+ 24,
+ 18,
+ 8,
+ 32,
+ 32,
+ 0,
+ -1,
+ -1
+ };
+
+ String[] invalidIpsWithMask = {
+ "asdf",
+ "1.2.3.4/33",
+ "1.2.3.4/34",
+ "1.2.3.4/-1",
+ "1.2.3.4/256.0.0.0",
+ "1.256.3.4/255.255.0.0",
+ "1.2.3.4/255.255.0.0.0",
+ };
+
+
+ @Test
+ public void testConstants() {
+ byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
+ byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
+ // Make sure class initializtation and static assignment don't get
+ // messed up. Test everything twice for cached values
+ assertTrue(IPv4Address.NONE.isCidrMask());
+ assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
+ assertTrue(IPv4Address.NONE.isCidrMask());
+ assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
+
+ assertTrue(IPv4Address.NO_MASK.isCidrMask());
+ assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
+ assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
+ assertTrue(IPv4Address.NO_MASK.isCidrMask());
+ assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
+ assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
+
+ assertTrue(IPv4Address.FULL_MASK.isCidrMask());
+ assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
+ assertTrue(IPv4Address.FULL_MASK.isCidrMask());
+ assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
+ }
+
@Test
public void testOfString() {
@@ -119,18 +176,111 @@
if (!hasMask[i]) {
IPv4Address ip = value.getValue();
assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
- } else if (hasMask[i]) {
- byte[] ipBytes = new byte[4];
- System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
- assertEquals(ipBytes.length, value.getValue().getBytes().length);
- for (int j = 0; j < ipBytes.length; j++) {
- ipBytes[j] &= ipsWithMaskValues[i][1][j];
- }
-
- assertArrayEquals(ipBytes, value.getValue().getBytes());
- assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
- value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
}
+ IPv4Address mask = value.getMask();
+ if (ipsWithMaskLengths[i] == -1) {
+ assertFalse(mask.isCidrMask());
+ try {
+ mask.asCidrMaskLength();
+ fail("Expected IllegalStateException not thrown");
+ } catch(IllegalStateException e) {
+ //expected
+ }
+ } else {
+ assertTrue(mask.isCidrMask());
+ assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength());
+ }
+ assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
+ byte[] ipBytes = new byte[4];
+ System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
+ assertEquals(ipBytes.length, value.getValue().getBytes().length);
+ for (int j = 0; j < ipBytes.length; j++) {
+ ipBytes[j] &= ipsWithMaskValues[i][1][j];
+ }
+
+ assertArrayEquals(ipBytes, value.getValue().getBytes());
+ assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
+ value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
+ }
+ }
+
+ @Test
+ public void testOfMaskedInvalid() throws Exception {
+ for(String invalid : invalidIpsWithMask) {
+ try {
+ IPv4Address.of(invalid);
+ fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
+ } catch(IllegalArgumentException e) {
+ // ok
+ }
+ }
+ }
+
+ @Test
+ public void testSuperclass() throws Exception {
+ for(String ipString: testStrings) {
+ IPAddress<?> superIp = IPAddress.of(ipString);
+ assertEquals(IPVersion.IPv4, superIp.getIpVersion());
+ assertEquals(IPv4Address.of(ipString), superIp);
+ }
+
+ for(String ipMaskedString: ipsWithMask) {
+ IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
+ assertEquals(IPVersion.IPv4, superIp.getIpVersion());
+ assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
+ }
+ }
+
+ @Test
+ public void testOfExceptions() {
+ // We check if the message of a caught NPE is set to a useful message
+ // as a hacky way of verifying that we got an NPE thrown by use rather
+ // than one the JVM created for a null access.
+ try {
+ String s = null;
+ IPv4Address.of(s);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = null;
+ IPv4Address.of(b);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = new byte[3];
+ IPv4Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ byte[] b = new byte[5];
+ IPv4Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPv4AddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0"));
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
}
}
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
index 6eb5743..a008070 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv6AddressTest.java
@@ -1,9 +1,6 @@
package org.projectfloodlight.openflow.types;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
import java.net.Inet6Address;
import java.net.InetAddress;
@@ -31,6 +28,7 @@
private class WithMaskTaskCase {
final String input;
boolean hasMask;
+ int expectedMaskLength = 128;
byte[] expectedMask = hex.decode("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff".replaceAll(" ", ""));
public WithMaskTaskCase(String input) {
@@ -45,22 +43,64 @@
return this;
}
+ public WithMaskTaskCase expectedMaskLength(int expectedLength) {
+ this.expectedMaskLength = expectedLength;
+ return this;
+ }
+
}
WithMaskTaskCase[] withMasks = new WithMaskTaskCase[] {
new WithMaskTaskCase("1::1/80")
- .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00"),
+ .maskHex("ff ff ff ff ff ff ff ff ff ff 00 00 00 00 00 00")
+ .expectedMaskLength(80),
new WithMaskTaskCase("ffff:ffee:1::/ff00:ff00:ff00:ff00::")
- .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00"),
+ .maskHex("ff 00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00")
+ .expectedMaskLength(-1),
new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
new WithMaskTaskCase("8:8:8:8:8:8:8:8"),
new WithMaskTaskCase("1:2:3:4:5:6:7:8/128"),
new WithMaskTaskCase("::/0")
.maskHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")
+ .expectedMaskLength(0),
};
@Test
+ public void testConstants() {
+ byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
+ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
+ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
+ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
+ byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
+ (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
+ (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
+ (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
+ // Make sure class initializtation and static assignment don't get
+ // messed up. Test everything twice for cached values
+ assertTrue(IPv6Address.NONE.isCidrMask());
+ assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
+ assertTrue(IPv6Address.NONE.isCidrMask());
+ assertEquals(0, IPv6Address.NONE.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv6Address.NONE.getBytes());
+
+ assertTrue(IPv6Address.NO_MASK.isCidrMask());
+ assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
+ assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
+ assertTrue(IPv6Address.NO_MASK.isCidrMask());
+ assertEquals(128, IPv6Address.NO_MASK.asCidrMaskLength());
+ assertArrayEquals(ones, IPv6Address.NO_MASK.getBytes());
+
+ assertTrue(IPv6Address.FULL_MASK.isCidrMask());
+ assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
+ assertTrue(IPv6Address.FULL_MASK.isCidrMask());
+ assertEquals(0, IPv6Address.FULL_MASK.asCidrMaskLength());
+ assertArrayEquals(zeros, IPv6Address.FULL_MASK.getBytes());
+ }
+
+ @Test
public void testMasked() throws UnknownHostException {
for(WithMaskTaskCase w: withMasks) {
IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
@@ -70,19 +110,37 @@
assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
assertEquals(w.input.split("/")[0], ip.toString());
- } else {
- InetAddress inetAddress = InetAddress.getByName(w.input.substring(0, w.input.indexOf('/')));
-
- byte[] address = inetAddress.getAddress();
- assertEquals(address.length, value.getValue().getBytes().length);
-
- for (int j = 0; j < address.length; j++) {
- address[j] &= w.expectedMask[j];
- }
-
- assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
- assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
}
+ InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
+
+ if (w.expectedMaskLength == -1) {
+ assertFalse(value.getMask().isCidrMask());
+ try {
+ value.getMask().asCidrMaskLength();
+ fail("Expected IllegalStateException not thrown");
+ } catch(IllegalStateException e) {
+ //expected
+ }
+ } else {
+ assertTrue(value.getMask().isCidrMask());
+ assertEquals("Input " + w.input, w.expectedMaskLength,
+ value.getMask().asCidrMaskLength());
+ }
+
+ byte[] address = inetAddress.getAddress();
+ assertEquals(address.length, value.getValue().getBytes().length);
+
+ for (int j = 0; j < address.length; j++) {
+ address[j] &= w.expectedMask[j];
+ }
+
+ assertThat("Address bytes for input " + w.input + ", value=" + value, value.getValue().getBytes(), CoreMatchers.equalTo(address));
+ assertThat("mask check for input " + w.input + ", value=" + value, value.getMask().getBytes(), CoreMatchers.equalTo(w.expectedMask));
+ }
+ for (int i = 0; i <= 128; i++) {
+ String ipString = String.format("8001:2::1/%d", i);
+ IPv6AddressWithMask value = IPv6AddressWithMask.of(ipString);
+ assertEquals("Input " + ipString, i, value.getMask().asCidrMaskLength());
}
}
@@ -150,4 +208,70 @@
assertEquals("1::4:5:6:0:8", IPv6Address.of("1:0:0:4:5:6:0:8").toString(true, false));
assertEquals("1:0:0:4::8", IPv6Address.of("1:0:0:4:0:0:0:8").toString(true, false));
}
+
+ @Test
+ public void testSuperclass() throws Exception {
+ for(String ipString: testStrings) {
+ IPAddress<?> superIp = IPAddress.of(ipString);
+ assertEquals(IPVersion.IPv6, superIp.getIpVersion());
+ assertEquals(IPv6Address.of(ipString), superIp);
+ }
+
+ for(WithMaskTaskCase w: withMasks) {
+ String ipMaskedString = w.input;
+ IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
+ assertEquals(IPVersion.IPv6, superIp.getIpVersion());
+ assertEquals(IPv6AddressWithMask.of(ipMaskedString), superIp);
+ }
+ }
+
+ @Test
+ public void testOfExceptions() throws Exception {
+ try {
+ IPv6AddressWithMask.of(null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ String s = null;
+ IPv6Address.of(s);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = null;
+ IPv6Address.of(b);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ byte[] b = new byte[7];
+ IPv6Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ byte[] b = new byte[9];
+ IPv6Address.of(b);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ IPv6AddressWithMask.of(IPv6Address.of("1::"), null);
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv6AddressWithMask.of(null, IPv6Address.of("255::"));
+ fail("Should have thrown NullPointerException");
+ } catch (NullPointerException e) {
+ assertNotNull(e.getMessage());
+ }
+ }
}