[java-opensaml COMMIT] in /trunk/opensaml-util/src: main/java/org/opensaml/util/net/IPRange.java test/java/org/opensa...
noreply at shibboleth.net
noreply at shibboleth.net
Wed Sep 21 14:00:29 BST 2011
Author: iay
Date: Wed Sep 21 14:00:29 2011
New Revision: 2897
URL: http://svn.shibboleth.net/view/java-opensaml?rev=2897&view=rev
Log:
After discussion with Chad, clarify that the intention is to permit parsing of either host addresses or network addresses.
Clarify this in Javadoc and in parameter and variable names.
Correctly handle the case where the CIDR notation is for a host address (e.g., 192.168.117.99/24) by masking the network address properly.
Modified:
trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java
trunk/opensaml-util/src/test/java/org/opensaml/util/net/IPRangeTest.java
Modified: trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java?rev=2897&r1=2896&r2=2897&view=diff
==============================================================================
--- trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java (original)
+++ trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java Wed Sep 21 14:00:29 2011
@@ -38,32 +38,36 @@
/**
* Constructor.
*
- * @param networkAddress the network address for the range
+ * @param address address to base the range on; may be the network address or the
+ * address of a host within the network
* @param maskSize the number of bits in the netmask
*/
- public IPRange(InetAddress networkAddress, int maskSize) {
- this(networkAddress.getAddress(), maskSize);
+ public IPRange(InetAddress address, int maskSize) {
+ this(address.getAddress(), maskSize);
}
/**
* Constructor.
*
- * @param networkAddress the network address for the range
+ * @param address address to base the range on; may be the network address or the
+ * address of a host within the network
* @param maskSize the number of bits in the netmask
*/
- public IPRange(byte[] networkAddress, int maskSize) {
- addressLength = networkAddress.length * 8;
+ public IPRange(byte[] address, int maskSize) {
+ addressLength = address.length * 8;
if (addressLength != 32 && addressLength != 128) {
- throw new IllegalArgumentException("Network address was neither an IPv4 or IPv6 address");
+ throw new IllegalArgumentException("address was neither an IPv4 or IPv6 address");
}
if (maskSize < 0 || maskSize > addressLength) {
throw new IllegalArgumentException("prefix length must be in range 0 to " + addressLength);
}
- network = toBitSet(networkAddress);
mask = new BitSet(addressLength);
mask.set(addressLength - maskSize, addressLength, true);
+
+ network = toBitSet(address);
+ network.and(mask);
}
/**
@@ -140,9 +144,9 @@
String[] blockParts = block.split("/");
try {
validateIPAddress(blockParts[0]);
- InetAddress networkAddress = InetAddress.getByName(blockParts[0]);
+ InetAddress address = InetAddress.getByName(blockParts[0]);
int maskSize = Integer.parseInt(blockParts[1]);
- return new IPRange(networkAddress, maskSize);
+ return new IPRange(address, maskSize);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Invalid IP address");
} catch (NumberFormatException e) {
Modified: trunk/opensaml-util/src/test/java/org/opensaml/util/net/IPRangeTest.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-util/src/test/java/org/opensaml/util/net/IPRangeTest.java?rev=2897&r1=2896&r2=2897&view=diff
==============================================================================
--- trunk/opensaml-util/src/test/java/org/opensaml/util/net/IPRangeTest.java (original)
+++ trunk/opensaml-util/src/test/java/org/opensaml/util/net/IPRangeTest.java Wed Sep 21 14:00:29 2011
@@ -59,4 +59,28 @@
testInvalid("1:2:3:4:5:6:7:8/wrong");
}
+ @Test
+ public void contains() {
+ // IPRange given a network address
+ IPRange networkRange = IPRange.parseCIDRBlock("192.168.117.192/28");
+
+ // IPRange given a host address
+ IPRange hostRange = IPRange.parseCIDRBlock("192.168.117.199/28");
+
+ // test for contain
+ byte[] bytes = new byte[]{(byte)192, (byte)168, 117, (byte)191};
+ Assert.assertFalse(networkRange.contains(bytes));
+ Assert.assertFalse(hostRange.contains(bytes));
+
+ for (int host = 0; host < 16; host++) {
+ bytes[3] = (byte)(192+host);
+ Assert.assertTrue(networkRange.contains(bytes));
+ Assert.assertTrue(hostRange.contains(bytes));
+ }
+
+ bytes[3] = (byte)(192+16);
+ Assert.assertFalse(networkRange.contains(bytes));
+ Assert.assertFalse(hostRange.contains(bytes));
+ }
+
}
More information about the commits
mailing list