Author: sebb Date: Tue Mar 4 19:24:58 2008 New Revision: 633759 URL: http://svn.apache.org/viewvc?rev=633759&view=rev Log: Add eol-style native
Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java (contents, props changed) commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java (contents, props changed) commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java (contents, props changed) Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java?rev=633759&r1=633758&r2=633759&view=diff ============================================================================== --- commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java (original) +++ commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java Tue Mar 4 19:24:58 2008 @@ -1,72 +1,72 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package examples; - -import java.util.Arrays; -import java.util.Scanner; - -import org.apache.commons.net.util.SubnetUtils; -import org.apache.commons.net.util.SubnetUtils.SubnetInfo; - -/** - * Example class that shows how to use the [EMAIL PROTECTED] SubnetUtils} class. - * @author Rory Winston <[EMAIL PROTECTED]> - * - */ -public class SubnetUtilsExample { - - public static void main(String[] args) { - String subnet = "192.168.0.1/29"; - SubnetUtils utils = new SubnetUtils(subnet); - SubnetInfo info = utils.getInfo(); - - System.out.printf("Subnet Information for %s:\n", subnet); - System.out.println("--------------------------------------"); - System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(), - Integer.toBinaryString(info.asInteger(info.getAddress()))); - System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(), - Integer.toBinaryString(info.asInteger(info.getNetmask()))); - System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature()); - - System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress()); - - System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(), - Integer.toBinaryString(info.asInteger(info.getNetworkAddress()))); - System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(), - Integer.toBinaryString(info.asInteger(info.getBroadcastAddress()))); - System.out.printf("First Usable Address:\t\t%s\t[%s]\n", info.getLowAddress(), - Integer.toBinaryString(info.asInteger(info.getLowAddress()))); - System.out.printf("Last Usable Address:\t\t%s\t[%s]\n", info.getHighAddress(), - Integer.toBinaryString(info.asInteger(info.getHighAddress()))); - - System.out.printf("Total usable addresses: \t%d\n", info.getAddressCount()); - System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses())); - - final String prompt ="Enter an IP address (e.g. 192.168.0.10):"; - System.out.println(prompt); - Scanner scanner = new Scanner(System.in); - while (scanner.hasNextLine()) { - String address = scanner.nextLine(); - System.out.println("The IP address [" + address + "] is " - + (info.isInRange(address) ? "" : "not ") - + "within the subnet [" + subnet + "]"); - System.out.println(prompt); - } - - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package examples; + +import java.util.Arrays; +import java.util.Scanner; + +import org.apache.commons.net.util.SubnetUtils; +import org.apache.commons.net.util.SubnetUtils.SubnetInfo; + +/** + * Example class that shows how to use the [EMAIL PROTECTED] SubnetUtils} class. + * @author Rory Winston <[EMAIL PROTECTED]> + * + */ +public class SubnetUtilsExample { + + public static void main(String[] args) { + String subnet = "192.168.0.1/29"; + SubnetUtils utils = new SubnetUtils(subnet); + SubnetInfo info = utils.getInfo(); + + System.out.printf("Subnet Information for %s:\n", subnet); + System.out.println("--------------------------------------"); + System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(), + Integer.toBinaryString(info.asInteger(info.getAddress()))); + System.out.printf("Netmask:\t\t\t%s\t[%s]\n", info.getNetmask(), + Integer.toBinaryString(info.asInteger(info.getNetmask()))); + System.out.printf("CIDR Representation:\t\t%s\n\n", info.getCidrSignature()); + + System.out.printf("Supplied IP Address:\t\t%s\n\n", info.getAddress()); + + System.out.printf("Network Address:\t\t%s\t[%s]\n", info.getNetworkAddress(), + Integer.toBinaryString(info.asInteger(info.getNetworkAddress()))); + System.out.printf("Broadcast Address:\t\t%s\t[%s]\n", info.getBroadcastAddress(), + Integer.toBinaryString(info.asInteger(info.getBroadcastAddress()))); + System.out.printf("First Usable Address:\t\t%s\t[%s]\n", info.getLowAddress(), + Integer.toBinaryString(info.asInteger(info.getLowAddress()))); + System.out.printf("Last Usable Address:\t\t%s\t[%s]\n", info.getHighAddress(), + Integer.toBinaryString(info.asInteger(info.getHighAddress()))); + + System.out.printf("Total usable addresses: \t%d\n", info.getAddressCount()); + System.out.printf("Address List: %s\n\n", Arrays.toString(info.getAllAddresses())); + + final String prompt ="Enter an IP address (e.g. 192.168.0.10):"; + System.out.println(prompt); + Scanner scanner = new Scanner(System.in); + while (scanner.hasNextLine()) { + String address = scanner.nextLine(); + System.out.println("The IP address [" + address + "] is " + + (info.isInRange(address) ? "" : "not ") + + "within the subnet [" + subnet + "]"); + System.out.println(prompt); + } + + } + +} Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/SubnetUtilsExample.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java?rev=633759&r1=633758&r2=633759&view=diff ============================================================================== --- commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java (original) +++ commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java Tue Mar 4 19:24:58 2008 @@ -1,207 +1,207 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.net.util; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * A class that performs some subnet calculations given a network address and a subnet mask. - * @see http://www.faqs.org/rfcs/rfc1519.html - * @author <[EMAIL PROTECTED]> - */ -public class SubnetUtils { - - private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})"; - private static final String SLASH_FORMAT = IP_ADDRESS + "/(\\d{1,3})"; - private static final Pattern addressPattern = Pattern.compile(IP_ADDRESS); - private static final Pattern cidrPattern = Pattern.compile(SLASH_FORMAT); - private static final int NBITS = 32; - - private int netmask = 0; - private int address = 0; - private int network = 0; - private int broadcast = 0; - - /** - * Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16" - * @param cidrNotation A CIDR-notation string, e.g. "192.168.0.1/16" - */ - public SubnetUtils(String cidrNotation) { - calculate(cidrNotation); - } - - /** - * Constructor that takes two dotted decimal addresses. - * @param address An IP address, e.g. "192.168.0.1" - * @param mask A dotted decimal netmask e.g. "255.255.0.0" - */ - public SubnetUtils(String address, String mask) { - calculate(toCidrNotation(address, mask)); - } - - /** - * Convenience container for subnet summary information. - * - */ - public final class SubnetInfo { - private SubnetInfo() {} - - private int netmask() { return netmask; } - private int network() { return network; } - private int address() { return address; } - private int broadcast() { return broadcast; } - private int low() { return network() + 1; } - private int high() { return broadcast() - 1; } - - public boolean isInRange(String address) { return isInRange(toInteger(address)); } - private boolean isInRange(int address) { return ((address-low()) <= (high()-low())); } - - public String getBroadcastAddress() { return format(toArray(broadcast())); } - public String getNetworkAddress() { return format(toArray(network())); } - public String getNetmask() { return format(toArray(netmask())); } - public String getAddress() { return format(toArray(address())); } - public String getLowAddress() { return format(toArray(low())); } - public String getHighAddress() { return format(toArray(high())); } - public int getAddressCount() { return (broadcast() - low()); } - - public int asInteger(String address) { return toInteger(address); } - - public String getCidrSignature() { - return toCidrNotation( - format(toArray(address())), - format(toArray(netmask())) - ); - } - - public String[] getAllAddresses() { - String[] addresses = new String[getAddressCount()]; - for (int add = low(), j=0; add <= high(); ++add, ++j) { - addresses[j] = format(toArray(add)); - } - return addresses; - } - } - - /** - * Return a [EMAIL PROTECTED] SubnetInfo} instance that contains subnet-specific statistics - * @return - */ - public final SubnetInfo getInfo() { return new SubnetInfo(); } - - /* - * Initialize the internal fields from the supplied CIDR mask - */ - private void calculate(String mask) { - Matcher matcher = cidrPattern.matcher(mask); - - if (matcher.matches()) { - address = matchAddress(matcher); - - /* Create a binary netmask from the number of bits specification /x */ - int cidrPart = rangeCheck(Integer.valueOf(matcher.group(5)), 0, NBITS-1); - for (int j = 0; j < cidrPart; ++j) { - netmask |= (1 << 31-j); - } - - /* Calculate base network address */ - network = (address & netmask); - - /* Calculate broadcast address */ - broadcast = network | ~(netmask); - } - else - throw new IllegalArgumentException("Could not parse [" + mask + "]"); - } - - /* - * Convert a dotted decimal format address to a packed integer format - */ - private int toInteger(String address) { - Matcher matcher = addressPattern.matcher(address); - if (matcher.matches()) { - return matchAddress(matcher); - } - else - throw new IllegalArgumentException("Could not parse [" + address + "]"); - } - - /* - * Convenience method to extract the components of a dotted decimal address and - * pack into an integer using a regex match - */ - private int matchAddress(Matcher matcher) { - int addr = 0; - for (int i = 1; i <= 4; ++i) { - int n = (rangeCheck(Integer.valueOf(matcher.group(i)), 0, 255)); - addr |= ((n & 0xff) << 8*(4-i)); - } - return addr; - } - - /* - * Convert a packed integer address into a 4-element array - */ - private int[] toArray(int val) { - int ret[] = new int[4]; - for (int j = 3; j >= 0; --j) - ret[j] |= ((val >>> 8*(3-j)) & (0xff)); - return ret; - } - - /* - * Convert a 4-element array into dotted decimal format - */ - private String format(int[] octets) { - StringBuilder str = new StringBuilder(); - for (int i =0; i < octets.length; ++i) - str.append((i == octets.length - 1) - ? octets[i] : octets[i] + "."); - return str.toString(); - } - - /* - * Convenience function to check integer boundaries - */ - private int rangeCheck(Integer value, int begin, int end) { - if (value >= begin && value <= end) - return value; - - throw new IllegalArgumentException("Value out of range: [" + value + "]"); - } - - /* - * Count the number of 1-bits in a 32-bit integer using a divide-and-conquer strategy - * see Hacker's Delight section 5.1 - */ - int pop(int x) { - x = x - ((x >>> 1) & 0x55555555); - x = (x & 0x33333333) + ((x >>> 2) & 0x33333333); - x = (x + (x >>> 4)) & 0x0F0F0F0F; - x = x + (x >>> 8); - x = x + (x >>> 16); - return x & 0x0000003F; - } - - /* Convert two dotted decimal addresses to a single xxx.xxx.xxx.xxx/yy format - * by counting the 1-bit population in the mask address. (It may be better to count - * NBITS-#trailing zeroes for this case) - */ - private String toCidrNotation(String addr, String mask) { - return addr + "/" + pop(toInteger(mask)); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.net.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A class that performs some subnet calculations given a network address and a subnet mask. + * @see http://www.faqs.org/rfcs/rfc1519.html + * @author <[EMAIL PROTECTED]> + */ +public class SubnetUtils { + + private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})"; + private static final String SLASH_FORMAT = IP_ADDRESS + "/(\\d{1,3})"; + private static final Pattern addressPattern = Pattern.compile(IP_ADDRESS); + private static final Pattern cidrPattern = Pattern.compile(SLASH_FORMAT); + private static final int NBITS = 32; + + private int netmask = 0; + private int address = 0; + private int network = 0; + private int broadcast = 0; + + /** + * Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16" + * @param cidrNotation A CIDR-notation string, e.g. "192.168.0.1/16" + */ + public SubnetUtils(String cidrNotation) { + calculate(cidrNotation); + } + + /** + * Constructor that takes two dotted decimal addresses. + * @param address An IP address, e.g. "192.168.0.1" + * @param mask A dotted decimal netmask e.g. "255.255.0.0" + */ + public SubnetUtils(String address, String mask) { + calculate(toCidrNotation(address, mask)); + } + + /** + * Convenience container for subnet summary information. + * + */ + public final class SubnetInfo { + private SubnetInfo() {} + + private int netmask() { return netmask; } + private int network() { return network; } + private int address() { return address; } + private int broadcast() { return broadcast; } + private int low() { return network() + 1; } + private int high() { return broadcast() - 1; } + + public boolean isInRange(String address) { return isInRange(toInteger(address)); } + private boolean isInRange(int address) { return ((address-low()) <= (high()-low())); } + + public String getBroadcastAddress() { return format(toArray(broadcast())); } + public String getNetworkAddress() { return format(toArray(network())); } + public String getNetmask() { return format(toArray(netmask())); } + public String getAddress() { return format(toArray(address())); } + public String getLowAddress() { return format(toArray(low())); } + public String getHighAddress() { return format(toArray(high())); } + public int getAddressCount() { return (broadcast() - low()); } + + public int asInteger(String address) { return toInteger(address); } + + public String getCidrSignature() { + return toCidrNotation( + format(toArray(address())), + format(toArray(netmask())) + ); + } + + public String[] getAllAddresses() { + String[] addresses = new String[getAddressCount()]; + for (int add = low(), j=0; add <= high(); ++add, ++j) { + addresses[j] = format(toArray(add)); + } + return addresses; + } + } + + /** + * Return a [EMAIL PROTECTED] SubnetInfo} instance that contains subnet-specific statistics + * @return + */ + public final SubnetInfo getInfo() { return new SubnetInfo(); } + + /* + * Initialize the internal fields from the supplied CIDR mask + */ + private void calculate(String mask) { + Matcher matcher = cidrPattern.matcher(mask); + + if (matcher.matches()) { + address = matchAddress(matcher); + + /* Create a binary netmask from the number of bits specification /x */ + int cidrPart = rangeCheck(Integer.valueOf(matcher.group(5)), 0, NBITS-1); + for (int j = 0; j < cidrPart; ++j) { + netmask |= (1 << 31-j); + } + + /* Calculate base network address */ + network = (address & netmask); + + /* Calculate broadcast address */ + broadcast = network | ~(netmask); + } + else + throw new IllegalArgumentException("Could not parse [" + mask + "]"); + } + + /* + * Convert a dotted decimal format address to a packed integer format + */ + private int toInteger(String address) { + Matcher matcher = addressPattern.matcher(address); + if (matcher.matches()) { + return matchAddress(matcher); + } + else + throw new IllegalArgumentException("Could not parse [" + address + "]"); + } + + /* + * Convenience method to extract the components of a dotted decimal address and + * pack into an integer using a regex match + */ + private int matchAddress(Matcher matcher) { + int addr = 0; + for (int i = 1; i <= 4; ++i) { + int n = (rangeCheck(Integer.valueOf(matcher.group(i)), 0, 255)); + addr |= ((n & 0xff) << 8*(4-i)); + } + return addr; + } + + /* + * Convert a packed integer address into a 4-element array + */ + private int[] toArray(int val) { + int ret[] = new int[4]; + for (int j = 3; j >= 0; --j) + ret[j] |= ((val >>> 8*(3-j)) & (0xff)); + return ret; + } + + /* + * Convert a 4-element array into dotted decimal format + */ + private String format(int[] octets) { + StringBuilder str = new StringBuilder(); + for (int i =0; i < octets.length; ++i) + str.append((i == octets.length - 1) + ? octets[i] : octets[i] + "."); + return str.toString(); + } + + /* + * Convenience function to check integer boundaries + */ + private int rangeCheck(Integer value, int begin, int end) { + if (value >= begin && value <= end) + return value; + + throw new IllegalArgumentException("Value out of range: [" + value + "]"); + } + + /* + * Count the number of 1-bits in a 32-bit integer using a divide-and-conquer strategy + * see Hacker's Delight section 5.1 + */ + int pop(int x) { + x = x - ((x >>> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >>> 2) & 0x33333333); + x = (x + (x >>> 4)) & 0x0F0F0F0F; + x = x + (x >>> 8); + x = x + (x >>> 16); + return x & 0x0000003F; + } + + /* Convert two dotted decimal addresses to a single xxx.xxx.xxx.xxx/yy format + * by counting the 1-bit population in the mask address. (It may be better to count + * NBITS-#trailing zeroes for this case) + */ + private String toCidrNotation(String addr, String mask) { + return addr + "/" + pop(toInteger(mask)); + } +} Propchange: commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java?rev=633759&r1=633758&r2=633759&view=diff ============================================================================== --- commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java (original) +++ commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java Tue Mar 4 19:24:58 2008 @@ -1,60 +1,60 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.net; - -import org.apache.commons.net.util.SubnetUtils; -import org.apache.commons.net.util.SubnetUtils.SubnetInfo; - -import junit.framework.TestCase; - -public class SubnetUtilsTest extends TestCase { - - public void testParseSimpleNetmask() { - final String address = "192.168.0.1"; - final String masks[] = new String[] { "255.0.0.0", "255.255.0.0", "255.255.255.0", "255.255.255.248"}; - final String bcastAddresses[] = new String[] { "192.255.255.255", "192.168.255.255", "192.168.0.255", "192.168.0.7"}; - final String lowAddresses[] = new String[] { "192.0.0.1", "192.168.0.1", "192.168.0.1", "192.168.0.1" }; - final String highAddresses[] = new String[] { "192.255.255.254", "192.168.255.254", "192.168.0.254", "192.168.0.6" }; - final String networkAddresses[] = new String[] { "192.0.0.0", "192.168.0.0", "192.168.0.0", "192.168.0.0" }; - final String cidrSignatures[] = new String[] { "192.168.0.1/8", "192.168.0.1/16", "192.168.0.1/24", "192.168.0.1/29" }; - final int usableAddresses[] = new int[] { 16777214, 65534, 254, 6 }; - - for (int i = 0; i < masks.length; ++i) { - SubnetUtils utils = new SubnetUtils(address, masks[i]); - SubnetInfo info = utils.getInfo(); - assertEquals(bcastAddresses[i], info.getBroadcastAddress()); - assertEquals(cidrSignatures[i], info.getCidrSignature()); - assertEquals(lowAddresses[i], info.getLowAddress()); - assertEquals(highAddresses[i], info.getHighAddress()); - assertEquals(networkAddresses[i], info.getNetworkAddress()); - assertEquals(usableAddresses[i], info.getAddressCount()); - } - } - - public void testAddresses() { - SubnetUtils utils = new SubnetUtils("192.168.0.1/29"); - SubnetInfo info = utils.getInfo(); - assertTrue(info.isInRange("192.168.0.1")); - // We dont count the broadcast address as usable - assertFalse(info.isInRange("192.168.0.7")); - assertFalse(info.isInRange("192.168.0.8")); - assertFalse(info.isInRange("10.10.2.1")); - assertFalse(info.isInRange("192.168.1.1")); - assertFalse(info.isInRange("192.168.0.255")); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net; + +import org.apache.commons.net.util.SubnetUtils; +import org.apache.commons.net.util.SubnetUtils.SubnetInfo; + +import junit.framework.TestCase; + +public class SubnetUtilsTest extends TestCase { + + public void testParseSimpleNetmask() { + final String address = "192.168.0.1"; + final String masks[] = new String[] { "255.0.0.0", "255.255.0.0", "255.255.255.0", "255.255.255.248"}; + final String bcastAddresses[] = new String[] { "192.255.255.255", "192.168.255.255", "192.168.0.255", "192.168.0.7"}; + final String lowAddresses[] = new String[] { "192.0.0.1", "192.168.0.1", "192.168.0.1", "192.168.0.1" }; + final String highAddresses[] = new String[] { "192.255.255.254", "192.168.255.254", "192.168.0.254", "192.168.0.6" }; + final String networkAddresses[] = new String[] { "192.0.0.0", "192.168.0.0", "192.168.0.0", "192.168.0.0" }; + final String cidrSignatures[] = new String[] { "192.168.0.1/8", "192.168.0.1/16", "192.168.0.1/24", "192.168.0.1/29" }; + final int usableAddresses[] = new int[] { 16777214, 65534, 254, 6 }; + + for (int i = 0; i < masks.length; ++i) { + SubnetUtils utils = new SubnetUtils(address, masks[i]); + SubnetInfo info = utils.getInfo(); + assertEquals(bcastAddresses[i], info.getBroadcastAddress()); + assertEquals(cidrSignatures[i], info.getCidrSignature()); + assertEquals(lowAddresses[i], info.getLowAddress()); + assertEquals(highAddresses[i], info.getHighAddress()); + assertEquals(networkAddresses[i], info.getNetworkAddress()); + assertEquals(usableAddresses[i], info.getAddressCount()); + } + } + + public void testAddresses() { + SubnetUtils utils = new SubnetUtils("192.168.0.1/29"); + SubnetInfo info = utils.getInfo(); + assertTrue(info.isInRange("192.168.0.1")); + // We dont count the broadcast address as usable + assertFalse(info.isInRange("192.168.0.7")); + assertFalse(info.isInRange("192.168.0.8")); + assertFalse(info.isInRange("10.10.2.1")); + assertFalse(info.isInRange("192.168.1.1")); + assertFalse(info.isInRange("192.168.0.255")); + } +} Propchange: commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java ------------------------------------------------------------------------------ svn:eol-style = native