Author: sebb
Date: Thu Mar 31 01:36:51 2011
New Revision: 1087155

URL: http://svn.apache.org/viewvc?rev=1087155&view=rev
Log:
NET-268 Better handling of CIDR/31 and CIDR/32 where isInclusive = false.
Return 0 for address count, and 0.0.0.0 for each of the addresses

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
    
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1087155&r1=1087154&r2=1087155&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml (original)
+++ commons/proper/net/trunk/src/changes/changes.xml Thu Mar 31 01:36:51 2011
@@ -57,6 +57,10 @@ The <action> type attribute can be add,u
 
     <body>
         <release version="3.0" date="TBA" description="TBA">
+            <action issue="NET-268" dev="sebb" type="fix">
+            Better handling of CIDR/31 and CIDR/32 where isInclusive = false.
+            Return 0 for address count, and 0.0.0.0 for each of the addresses
+            </action>
             <action issue="NET-395" dev="sebb" type="update">
             Move ProtocolCommandSupport to SocketClient.
             </action>

Modified: 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java?rev=1087155&r1=1087154&r2=1087155&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
 (original)
+++ 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
 Thu Mar 31 01:36:51 2011
@@ -96,8 +96,15 @@ public class SubnetUtils {
         private int network()       { return network; }
         private int address()       { return address; }
         private int broadcast()     { return broadcast; }
-        private int low()           { return network() + 
(isInclusiveHostCount() ? 0 : 1); }
-        private int high()          { return broadcast() - 
(isInclusiveHostCount() ? 0 : 1); }
+
+        private int low() {
+            return (isInclusiveHostCount() ? network() :
+                broadcast() - network() > 1 ? network() + 1 : 0); 
+        }
+        private int high() { 
+            return (isInclusiveHostCount() ? broadcast() :
+                broadcast() - network() > 1 ? broadcast() -1  : 0); 
+        }
 
         /**
          * Returns true if the parameter <code>address</code> is in the
@@ -117,9 +124,32 @@ public class SubnetUtils {
         public String getNetworkAddress()           { return 
format(toArray(network())); }
         public String getNetmask()                  { return 
format(toArray(netmask())); }
         public String getAddress()                  { return 
format(toArray(address())); }
+
+        /**
+         * Return the low address as a dotted IP address.
+         * Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.
+         * 
+         * @return the IP address in dotted format, may be "0.0.0.0" if there 
is no valid address
+         */
         public String getLowAddress()               { return 
format(toArray(low())); }
+
+        /**
+         * Return the high address as a dotted IP address.
+         * Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.
+         * 
+         * @return the IP address in dotted format, may be "0.0.0.0" if there 
is no valid address
+         */
         public String getHighAddress()              { return 
format(toArray(high())); }
-        public int getAddressCount()                { return (broadcast() - 
low() + (isInclusiveHostCount() ? 1 : 0)); }
+        
+        /**
+         * Get the count of available addresses.
+         * Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.
+         * @return the count of addresses, may be zero.
+         */
+        public int getAddressCount()                { 
+            int count = broadcast() - network() + (isInclusiveHostCount() ? 1 
: -1);
+            return count < 0 ? 0 : count;
+        }
 
         public int asInteger(String address)        { return 
toInteger(address); }
 

Modified: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java?rev=1087155&r1=1087154&r2=1087155&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
 (original)
+++ 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
 Thu Mar 31 01:36:51 2011
@@ -46,6 +46,53 @@ public class SubnetUtilsTest extends Tes
         }
     }
 
+    public void testParseSimpleNetmaskInclusive() {
+         String address = "192.168.15.7";
+         String masks[]=new String[]{ "255.255.255.252",  "255.255.255.254",  
"255.255.255.255"};
+         String bcast[]=new String[]{ "192.168.15.7",     "192.168.15.7",     
"192.168.15.7"};
+         String netwk[]=new String[]{ "192.168.15.4",     "192.168.15.6",     
"192.168.15.7" };
+         String lowAd[]=new String[]{ "192.168.15.4",     "192.168.15.6",     
"192.168.15.7" };
+         String highA[]=new String[]{ "192.168.15.7",     "192.168.15.7",     
"192.168.15.7" };
+         String cidrS[]=new String[]{ "192.168.15.7/30",  "192.168.15.7/31",  
"192.168.15.7/32"};
+         int usableAd[]=new int[]   { 4 ,                 2,                  
1};
+
+         for (int i = 0; i < masks.length; ++i) {
+             SubnetUtils utils = new SubnetUtils(address, masks[i]);
+             utils.setInclusiveHostCount(true);
+             SubnetInfo info = utils.getInfo();
+             assertEquals("ci "+masks[i], cidrS[i], info.getCidrSignature());
+             assertEquals("bc "+masks[i], bcast[i], 
info.getBroadcastAddress());
+             assertEquals("ac "+masks[i], usableAd[i], info.getAddressCount());
+             assertEquals("nw "+masks[i], netwk[i], info.getNetworkAddress());
+             assertEquals("lo "+masks[i], lowAd[i], info.getLowAddress());
+             assertEquals("hi "+masks[i], highA[i], info.getHighAddress());
+         }
+     }
+
+    public void testParseSimpleNetmaskExclusive() {
+        String address = "192.168.15.7";
+        String masks[]=new String[]{ "255.255.255.252",  "255.255.255.254",  
"255.255.255.255"};
+        String bcast[]=new String[]{ "192.168.15.7",     "192.168.15.7",     
"192.168.15.7"};
+        String netwk[]=new String[]{ "192.168.15.4",     "192.168.15.6",     
"192.168.15.7" };
+        String lowAd[]=new String[]{ "192.168.15.5",     "0.0.0.0",          
"0.0.0.0"    };
+        String highA[]=new String[]{ "192.168.15.6",     "0.0.0.0",          
"0.0.0.0"    };
+        String cidrS[]=new String[]{ "192.168.15.7/30",  "192.168.15.7/31",  
"192.168.15.7/32"};
+        int usableAd[]=new int[]   { 2 ,                 0,                  
0};
+       // low and high addresses don't exist
+
+        for (int i = 0; i < masks.length; ++i) {
+            SubnetUtils utils = new SubnetUtils(address, masks[i]);
+            utils.setInclusiveHostCount(false);
+            SubnetInfo info = utils.getInfo();
+            assertEquals("ci "+masks[i], cidrS[i], info.getCidrSignature());
+            assertEquals("bc "+masks[i], bcast[i], info.getBroadcastAddress());
+            assertEquals("nw "+masks[i], netwk[i], info.getNetworkAddress());
+            assertEquals("ac "+masks[i], usableAd[i], info.getAddressCount());
+            assertEquals("lo "+masks[i], lowAd[i], info.getLowAddress());
+            assertEquals("hi "+masks[i], highA[i], info.getHighAddress());
+        }
+    }
+
     // TODO Lower address test
     public void testAddresses() {
         SubnetUtils utils = new SubnetUtils("192.168.0.1/29");


Reply via email to