You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/06 12:34:59 UTC

[commons-net] 04/04: Add missing tests

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git

commit 1efbde46baab4223241635c484f0fd5447b9f1bb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 6 07:34:50 2022 -0500

    Add missing tests
    
    - Format tweaks
    - Clean up SubnetUtils.SubnetInfo.toString()
---
 pom.xml                                            |  5 ++
 .../org/apache/commons/net/util/SubnetUtils.java   | 48 +++++++-------
 .../org/apache/commons/net/SubnetUtilsTest.java    | 77 +++++++++++++++-------
 3 files changed, 82 insertions(+), 48 deletions(-)

diff --git a/pom.xml b/pom.xml
index db05d984..0db5107f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -100,6 +100,11 @@ Supported protocols include: Echo, Finger, FTP, NNTP, NTP, POP3(S), SMTP(S), Tel
             <artifactId>junit-vintage-engine</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.ftpserver</groupId>
             <artifactId>ftpserver-core</artifactId>
diff --git a/src/main/java/org/apache/commons/net/util/SubnetUtils.java b/src/main/java/org/apache/commons/net/util/SubnetUtils.java
index 1a18afd5..f1014b1e 100644
--- a/src/main/java/org/apache/commons/net/util/SubnetUtils.java
+++ b/src/main/java/org/apache/commons/net/util/SubnetUtils.java
@@ -207,13 +207,13 @@ public class SubnetUtils {
         @Override
         public String toString() {
             final StringBuilder buf = new StringBuilder();
-            buf.append("CIDR Signature:\t[").append(getCidrSignature()).append("]")
-                .append(" Netmask: [").append(getNetmask()).append("]\n")
-                .append("Network:\t[").append(getNetworkAddress()).append("]\n")
-                .append("Broadcast:\t[").append(getBroadcastAddress()).append("]\n")
-                 .append("First Address:\t[").append(getLowAddress()).append("]\n")
-                 .append("Last Address:\t[").append(getHighAddress()).append("]\n")
-                 .append("# Addresses:\t[").append(getAddressCount()).append("]\n");
+            buf.append("CIDR Signature:\t[").append(getCidrSignature()).append("]\n")
+                .append("  Netmask: [").append(getNetmask()).append("]\n")
+                .append("  Network: [").append(getNetworkAddress()).append("]\n")
+                .append("  Broadcast: [").append(getBroadcastAddress()).append("]\n")
+                .append("  First address: [").append(getLowAddress()).append("]\n")
+                .append("  Last address: [").append(getHighAddress()).append("]\n")
+                .append("  Address Count: [").append(getAddressCountLong()).append("]\n");
             return buf.toString();
         }
     }
@@ -249,6 +249,7 @@ public class SubnetUtils {
 
         throw new IllegalArgumentException("Value [" + value + "] not in range ["+begin+","+end+"]");
     }
+
     /*
      * Convert a dotted decimal format address to a packed integer format
      */
@@ -262,19 +263,15 @@ public class SubnetUtils {
 
     private final int netmask;
 
-
     private final int address;
 
     private final int network;
 
-
     private final int broadcast;
 
     /** Whether the broadcast/network address are included in host count */
     private boolean inclusiveHostCount;
 
-
-
     /**
      * 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"
@@ -287,25 +284,26 @@ public class SubnetUtils {
       if (!matcher.matches()) {
           throw new IllegalArgumentException(String.format(PARSE_FAIL, cidrNotation));
       }
-    this.address = matchAddress(matcher);
+      this.address = matchAddress(matcher);
 
-      /* Create a binary netmask from the number of bits specification /x */
+      // Create a binary netmask from the number of bits specification /x 
 
       final int trailingZeroes = NBITS - rangeCheck(Integer.parseInt(matcher.group(5)), 0, NBITS);
-      /*
-       * An IPv4 netmask consists of 32 bits, a contiguous sequence
-       * of the specified number of ones followed by all zeros.
-       * So, it can be obtained by shifting an unsigned integer (32 bits) to the left by
-       * the number of trailing zeros which is (32 - the # bits specification).
-       * Note that there is no unsigned left shift operator, so we have to use
-       * a long to ensure that the left-most bit is shifted out correctly.
-       */
+      
+      //
+      // An IPv4 netmask consists of 32 bits, a contiguous sequence
+      // of the specified number of ones followed by all zeros.
+      // So, it can be obtained by shifting an unsigned integer (32 bits) to the left by
+      // the number of trailing zeros which is (32 - the # bits specification).
+      // Note that there is no unsigned left shift operator, so we have to use
+      // a long to ensure that the left-most bit is shifted out correctly.
+      //
       this.netmask = (int) (0x0FFFFFFFFL << trailingZeroes );
 
-      /* Calculate base network address */
+      // Calculate base network address
       this.network = address & netmask;
 
-      /* Calculate broadcast address */
+      // Calculate broadcast address
       this.broadcast = network | ~netmask;
     }
 
@@ -324,10 +322,10 @@ public class SubnetUtils {
             throw new IllegalArgumentException(String.format(PARSE_FAIL, mask));
         }
 
-        /* Calculate base network address */
+        // Calculate base network address
         this.network = this.address & this.netmask;
 
-        /* Calculate broadcast address */
+        // Calculate broadcast address
         this.broadcast = this.network | ~this.netmask;
     }
 
diff --git a/src/test/java/org/apache/commons/net/SubnetUtilsTest.java b/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
index 4b0769ea..a4715989 100644
--- a/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
+++ b/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
@@ -17,6 +17,10 @@
 
 package org.apache.commons.net;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
 import org.apache.commons.net.util.SubnetUtils;
 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
 
@@ -40,6 +44,15 @@ public class SubnetUtilsTest extends TestCase {
         assertFalse(info.isInRange("10.10.2.1"));
         assertFalse(info.isInRange("192.168.1.1"));
         assertFalse(info.isInRange("192.168.0.255"));
+        //
+        assertEquals(-1062731775, info.asInteger("192.168.0.1"));
+        assertThrows(IllegalArgumentException.class, () -> info.asInteger("bad"));
+        //
+        assertArrayEquals(new String[] { "192.168.0.1", "192.168.0.2", "192.168.0.3", "192.168.0.4", "192.168.0.5", "192.168.0.6" }, info.getAllAddresses());
+    }
+
+    public void testAddressIllegalArgument() {
+        assertThrows(IllegalArgumentException.class, () -> new SubnetUtils("bad"));
     }
 
     /**
@@ -318,26 +331,32 @@ public class SubnetUtilsTest extends TestCase {
         assertTrue(info.isInRange("10.213.255.255"));
     }
 
+    public void testNext() {
+        final SubnetUtils utils = new SubnetUtils("192.168.0.1/29");
+        assertEquals("192.168.0.2", utils.getNext().getInfo().getAddress());
+    }
+
     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",
+        final String masks[] = { "255.0.0.0", "255.255.0.0", "255.255.255.0", "255.255.255.248" };
+        final String bcastAddresses[] = { "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",
+        final String lowAddresses[] = { "192.0.0.1", "192.168.0.1", "192.168.0.1", "192.168.0.1" };
+        final String highAddresses[] = { "192.255.255.254", "192.168.255.254", "192.168.0.254",
                 "192.168.0.6" };
-        final String nextAddresses[] = new String[] { "192.168.0.2", "192.168.0.2", "192.168.0.2",
+        final String nextAddresses[] = { "192.168.0.2", "192.168.0.2", "192.168.0.2",
                 "192.168.0.2" };
-        final String previousAddresses[] = new String[] { "192.168.0.0", "192.168.0.0", "192.168.0.0",
+        final String previousAddresses[] = { "192.168.0.0", "192.168.0.0", "192.168.0.0",
                 "192.168.0.0" };
-        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",
+        final String networkAddresses[] = { "192.0.0.0", "192.168.0.0", "192.168.0.0", "192.168.0.0" };
+        final String cidrSignatures[] = { "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 };
+        final int usableAddresses[] = { 16777214, 65534, 254, 6 };
 
         for (int i = 0; i < masks.length; ++i) {
             final SubnetUtils utils = new SubnetUtils(address, masks[i]);
             final SubnetInfo info = utils.getInfo();
+            assertEquals(address, info.getAddress());
             assertEquals(bcastAddresses[i], info.getBroadcastAddress());
             assertEquals(cidrSignatures[i], info.getCidrSignature());
             assertEquals(lowAddresses[i], info.getLowAddress());
@@ -351,13 +370,13 @@ public class SubnetUtilsTest extends TestCase {
 
     public void testParseSimpleNetmaskExclusive() {
         final String address = "192.168.15.7";
-        final String masks[] = new String[] { "255.255.255.252", "255.255.255.254", "255.255.255.255" };
-        final String bcast[] = new String[] { "192.168.15.7", "192.168.15.7", "192.168.15.7" };
-        final String netwk[] = new String[] { "192.168.15.4", "192.168.15.6", "192.168.15.7" };
-        final String lowAd[] = new String[] { "192.168.15.5", "0.0.0.0", "0.0.0.0" };
-        final String highA[] = new String[] { "192.168.15.6", "0.0.0.0", "0.0.0.0" };
-        final String cidrS[] = new String[] { "192.168.15.7/30", "192.168.15.7/31", "192.168.15.7/32" };
-        final int usableAd[] = new int[] { 2, 0, 0 };
+        final String masks[] = { "255.255.255.252", "255.255.255.254", "255.255.255.255" };
+        final String bcast[] = { "192.168.15.7", "192.168.15.7", "192.168.15.7" };
+        final String netwk[] = { "192.168.15.4", "192.168.15.6", "192.168.15.7" };
+        final String lowAd[] = { "192.168.15.5", "0.0.0.0", "0.0.0.0" };
+        final String highA[] = { "192.168.15.6", "0.0.0.0", "0.0.0.0" };
+        final String cidrS[] = { "192.168.15.7/30", "192.168.15.7/31", "192.168.15.7/32" };
+        final int usableAd[] = { 2, 0, 0 };
         // low and high addresses don't exist
 
         for (int i = 0; i < masks.length; ++i) {
@@ -375,13 +394,13 @@ public class SubnetUtilsTest extends TestCase {
 
     public void testParseSimpleNetmaskInclusive() {
         final String address = "192.168.15.7";
-        final String masks[] = new String[] { "255.255.255.252", "255.255.255.254", "255.255.255.255" };
-        final String bcast[] = new String[] { "192.168.15.7", "192.168.15.7", "192.168.15.7" };
-        final String netwk[] = new String[] { "192.168.15.4", "192.168.15.6", "192.168.15.7" };
-        final String lowAd[] = new String[] { "192.168.15.4", "192.168.15.6", "192.168.15.7" };
-        final String highA[] = new String[] { "192.168.15.7", "192.168.15.7", "192.168.15.7" };
-        final String cidrS[] = new String[] { "192.168.15.7/30", "192.168.15.7/31", "192.168.15.7/32" };
-        final int usableAd[] = new int[] { 4, 2, 1 };
+        final String masks[] = { "255.255.255.252", "255.255.255.254", "255.255.255.255" };
+        final String bcast[] = { "192.168.15.7", "192.168.15.7", "192.168.15.7" };
+        final String netwk[] = { "192.168.15.4", "192.168.15.6", "192.168.15.7" };
+        final String lowAd[] = { "192.168.15.4", "192.168.15.6", "192.168.15.7" };
+        final String highA[] = { "192.168.15.7", "192.168.15.7", "192.168.15.7" };
+        final String cidrS[] = { "192.168.15.7/30", "192.168.15.7/31", "192.168.15.7/32" };
+        final int usableAd[] = { 4, 2, 1 };
 
         for (int i = 0; i < masks.length; ++i) {
             final SubnetUtils utils = new SubnetUtils(address, masks[i]);
@@ -396,6 +415,18 @@ public class SubnetUtilsTest extends TestCase {
         }
     }
 
+    public void testPrevious() {
+        final SubnetUtils utils = new SubnetUtils("192.168.0.1/29");
+        assertEquals("192.168.0.0", utils.getPrevious().getInfo().getAddress());
+    }
+
+    public void testToString() {
+        final SubnetUtils utils = new SubnetUtils("192.168.0.1/29");
+        assertDoesNotThrow(() -> utils.toString());
+        final SubnetInfo info = utils.getInfo();
+        assertDoesNotThrow(() -> info.toString());
+    }
+
     public void testZeroAddressAndCidr() {
         final SubnetUtils snu = new SubnetUtils("0.0.0.0/0");
         assertNotNull(snu);