Skip to content

Commit

Permalink
Added IPv6Address.ofCidrMaskLength() and the corresponding unit tests
Browse files Browse the repository at this point in the history
Reference:
IPv6AddressWithMask.of(String)
  • Loading branch information
ronaldchl committed Jul 4, 2014
1 parent a780c37 commit fea1789
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.projectfloodlight.openflow.types;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -252,6 +253,36 @@ public static IPv6Address of(final long raw1, final long raw2) {
return new IPv6Address(raw1, raw2);
}

public static IPv6Address ofCidrMaskLength(int cidrMaskLength) {
Preconditions.checkArgument(
cidrMaskLength >= 0 && cidrMaskLength <= 128,
"Invalid IPv6 CIDR mask length: %s", cidrMaskLength);

if (cidrMaskLength == 128) {
return IPv6Address.NO_MASK;
} else if (cidrMaskLength == 0) {
return IPv6Address.FULL_MASK;
} else {
BigInteger mask

This comment has been minimized.

Copy link
@andi-bigswitch

andi-bigswitch Jul 8, 2014

Actually, somehow I missed this code in my previous review... This seems like a really inefficient way to calculate the two long words you need to construct the IPv6Address.
I'm thinking:

 int shift1 = Math.min(cidrLength, 64);
 long raw1 = shift1 == 0 ? 0 :  -1 << (64 - shift1);
 int shift2 = Math.max(cidrLength - 64, 0);
 long raw2 = shift2 == 0 ? 0 :  -1 << (64 - shift2);
 return IPv6Address.of(raw1,raw2);

This comment has been minimized.

Copy link
@andi-bigswitch

andi-bigswitch Jul 8, 2014

(EDIT: corrected corner case)

This comment has been minimized.

Copy link
@ronaldchl

ronaldchl Jul 8, 2014

Author Owner

Done f937760. Needed to cast -1 to long as well.

= BigInteger.ONE.negate().shiftLeft(128 - cidrMaskLength);
byte[] maskBytesTemp = mask.toByteArray();
byte[] maskBytes;
if (maskBytesTemp.length < 16) {
maskBytes = new byte[16];
System.arraycopy(maskBytesTemp, 0,
maskBytes, 16 - maskBytesTemp.length,
maskBytesTemp.length);
Arrays.fill(maskBytes, 0, 16 - maskBytesTemp.length, (byte)(0xFF));
} else if (maskBytesTemp.length > 16) {
maskBytes = new byte[16];
System.arraycopy(maskBytesTemp, 0, maskBytes, 0, maskBytes.length);
} else {
maskBytes = maskBytesTemp;
}
return IPv6Address.of(maskBytes);
}
}

private volatile byte[] bytesCache = null;

public byte[] getBytes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,44 @@ public void testOfByteArray() throws UnknownHostException {
}
}

private static void testOfCidrMaskLengthHelper(
int cidrMaskLength, String ipStr) throws UnknownHostException {
byte[] ba0 = IPv6Address.ofCidrMaskLength(cidrMaskLength).getBytes();
byte[] ba1 = Inet6Address.getByName(ipStr).getAddress();
assertArrayEquals(ba0, ba1);
}

@Test
public void testOfCidrMaskLength() throws UnknownHostException {
for (int i = 0; i <= 128; i++) {
assertTrue(IPv6Address.ofCidrMaskLength(i).isCidrMask());
assertEquals(IPv6Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
}
testOfCidrMaskLengthHelper(0, "::");
testOfCidrMaskLengthHelper(1, "8000::");
testOfCidrMaskLengthHelper(2, "c000::");
testOfCidrMaskLengthHelper(8, "ff00::");
testOfCidrMaskLengthHelper(16, "ffff::");
testOfCidrMaskLengthHelper(17, "ffff:8000::");
testOfCidrMaskLengthHelper(31, "ffff:fffe::");
testOfCidrMaskLengthHelper(32, "ffff:ffff::");
testOfCidrMaskLengthHelper(33, "ffff:ffff:8000::");
testOfCidrMaskLengthHelper(46, "ffff:ffff:fffc::");
testOfCidrMaskLengthHelper(48, "ffff:ffff:ffff::");
testOfCidrMaskLengthHelper(55, "ffff:ffff:ffff:fe00::");
testOfCidrMaskLengthHelper(56, "ffff:ffff:ffff:ff00::");
testOfCidrMaskLengthHelper(59, "ffff:ffff:ffff:ffe0::");
testOfCidrMaskLengthHelper(63, "ffff:ffff:ffff:fffe::");
testOfCidrMaskLengthHelper(64, "ffff:ffff:ffff:ffff::");
testOfCidrMaskLengthHelper(65, "ffff:ffff:ffff:ffff:8000::");
testOfCidrMaskLengthHelper(67, "ffff:ffff:ffff:ffff:e000::");
testOfCidrMaskLengthHelper(100, "ffff:ffff:ffff:ffff:ffff:ffff:f000::");
testOfCidrMaskLengthHelper(101, "ffff:ffff:ffff:ffff:ffff:ffff:f800::");
testOfCidrMaskLengthHelper(126, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc");
testOfCidrMaskLengthHelper(127, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
testOfCidrMaskLengthHelper(128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
}

@Test
public void testReadFrom() throws OFParseError, UnknownHostException {
for(int i=0; i < testStrings.length; i++ ) {
Expand Down Expand Up @@ -313,5 +351,17 @@ public void testOfExceptions() throws Exception {
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
}
try {
IPv6Address.ofCidrMaskLength(-1);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
}
try {
IPv6Address.ofCidrMaskLength(129);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
}
}
}

0 comments on commit fea1789

Please sign in to comment.