java_gen: Fixed bug in Ipv[46]AddressWithMask when mask == /0
Both IP address parsers treated the case mask == /0 incorrectly.
Fixed the behavior and added to the unit tests.
Clean up the IPv6 unit test.
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 67bbce4..f30fcbb 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
@@ -46,7 +46,7 @@
public static IPv4AddressWithMask of(final String string) {
int slashPos;
String ip = string;
- int maskBits = 0;
+ int maskBits = 32;
IPv4Address maskAddress = null;
// Read mask suffix
@@ -77,9 +77,12 @@
if (maskAddress != null) {
// Full address mask
return IPv4AddressWithMask.of(ipv4, maskAddress);
- } else if (maskBits == 0) {
+ } else if (maskBits == 32) {
// No mask
return IPv4AddressWithMask.of(ipv4, IPv4Address.NO_MASK);
+ } else if (maskBits == 0) {
+ // No mask
+ return IPv4AddressWithMask.of(ipv4, IPv4Address.FULL_MASK);
} else {
// With mask
int mask = (-1) << (32 - maskBits);
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 727daf6..6faf0b8 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
@@ -40,7 +40,7 @@
public static IPv6AddressWithMask of(final String string) {
int slashPos;
String ip = string;
- int maskBits = 0;
+ int maskBits = 128;
IPv6Address maskAddress = null;
// Read mask suffix
@@ -71,10 +71,13 @@
if (maskAddress != null) {
// Full address mask
return IPv6AddressWithMask.of(ipv6, maskAddress);
- } else if (maskBits == 0) {
+ } else if (maskBits == 128) {
// No mask
return IPv6AddressWithMask.of(ipv6, IPv6Address.NO_MASK);
- } else {
+ } else if (maskBits == 0) {
+ // Entirely masked out
+ return IPv6AddressWithMask.of(ipv6, IPv6Address.FULL_MASK);
+ }else {
// With mask
BigInteger mask = BigInteger.ONE.negate().shiftLeft(128 - maskBits);
byte[] maskBytesTemp = mask.toByteArray();
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 4f3f968..38d60b3 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
@@ -2,8 +2,10 @@
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 org.hamcrest.CoreMatchers;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Test;
import org.projectfloodlight.openflow.exceptions.OFParseError;
@@ -47,20 +49,23 @@
"192.168.130.140/255.255.192.0",
"127.0.0.1/8",
"8.8.8.8",
+ "0.0.0.0/0"
};
boolean[] hasMask = {
true,
true,
true,
- false
+ false,
+ true
};
byte[][][] ipsWithMaskValues = {
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)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 } }
};
@@ -123,7 +128,8 @@
}
assertArrayEquals(ipBytes, value.getValue().getBytes());
- assertArrayEquals(ipsWithMaskValues[i][1], value.getMask().getBytes());
+ assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
+ value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
}
}
}
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 3fcfe23..672a0e1 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
@@ -2,16 +2,20 @@
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 java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import org.hamcrest.CoreMatchers;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Test;
import org.projectfloodlight.openflow.exceptions.OFParseError;
+import com.google.common.io.BaseEncoding;
+
public class IPv6AddressTest {
String[] testStrings = {
@@ -21,61 +25,83 @@
"1:2:3:4:5:6:7:8"
};
- String[] ipsWithMask = {
- "1::1/80",
- "1:2:3:4::/ffff:ffff:ffff:ff00::",
- "ffff:ffee:1::/ff00:ff00:ff00:ff00::",
- "8:8:8:8:8:8:8:8",
- };
- byte[][] masks = {
- new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
- (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
- (byte)0xff, (byte)0xff, (byte)0x00, (byte)0x00,
- (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
- new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
- (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x00,
- (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
- (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
- new byte[] { (byte)0xff, (byte)0x00, (byte)0xff, (byte)0x00,
- (byte)0xff, (byte)0x00, (byte)0xff, (byte)0x00,
- (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
- (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
- new byte[] { (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 }
- };
+ private final BaseEncoding hex = BaseEncoding.base16().omitPadding().lowerCase();
- boolean[] hasMask = {
- true,
- true,
- true,
- false
+ private class WithMaskTaskCase {
+ final String input;
+ boolean hasMask;
+ 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) {
+ super();
+ this.input = input;
+ }
+
+ public WithMaskTaskCase hasMask() {
+ this.hasMask = true;
+ return this;
+ }
+
+ public WithMaskTaskCase maskLength(int l) {
+ this.hasMask = l < 128;
+ for(int j=0; j < 8; j++) {
+ int pos = j * 8;
+ int index = Math.max(0, Math.min(7, pos - l));
+
+ if(index == 0) {
+ expectedMask[j] = 0;
+ } else {
+ expectedMask[j] = (byte) (-1 << index);
+ }
+ }
+ return this;
+ }
+
+ public WithMaskTaskCase maskHex(String string) {
+ string = string.replaceAll(" ", "");
+ this.hasMask = true;
+ expectedMask = hex.decode(string);
+ 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"),
+
+ 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"),
+ 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")
};
@Test
public void testMasked() throws UnknownHostException {
- for(int i=0; i < ipsWithMask.length; i++ ) {
- IPv6AddressWithMask value = IPv6AddressWithMask.of(ipsWithMask[i]);
- if (!hasMask[i]) {
+ for(WithMaskTaskCase w: withMasks) {
+ IPv6AddressWithMask value = IPv6AddressWithMask.of(w.input);
+ if (!w.hasMask) {
IPv6Address ip = value.getValue();
- InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i]);
+ InetAddress inetAddress = InetAddress.getByName(w.input.split("/")[0]);
assertArrayEquals(ip.getBytes(), inetAddress.getAddress());
- assertEquals(ipsWithMask[i], ip.toString());
- } else if (value instanceof IPv6AddressWithMask && hasMask[i]) {
- InetAddress inetAddress = InetAddress.getByName(ipsWithMask[i].substring(0, ipsWithMask[i].indexOf('/')));
+ 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] &= masks[i][j];
+ address[j] &= w.expectedMask[j];
}
- assertArrayEquals(value.getValue().getBytes(), address);
- assertArrayEquals(masks[i], value.getMask().getBytes());
+ 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));
}
}
}