android: Parse two addresses separated by - as range
authorTobias Brunner <tobias@strongswan.org>
Wed, 21 Jun 2017 13:53:39 +0000 (15:53 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 3 Jul 2017 08:27:51 +0000 (10:27 +0200)
src/frontends/android/app/src/main/java/org/strongswan/android/utils/IPRange.java
src/frontends/android/app/src/main/java/org/strongswan/android/utils/IPRangeSet.java
src/frontends/android/app/src/test/java/org/strongswan/android/test/IPRangeSetTest.java
src/frontends/android/app/src/test/java/org/strongswan/android/test/IPRangeTest.java

index 79342fc..621dfd6 100644 (file)
@@ -81,6 +81,11 @@ public class IPRange implements Comparable<IPRange>
 
        public IPRange(InetAddress from, InetAddress to)
        {
+               initializeFromRange(from, to);
+       }
+
+       private void initializeFromRange(InetAddress from, InetAddress to)
+       {
                byte[] fa = from.getAddress(), ta = to.getAddress();
                if (fa.length != ta.length)
                {
@@ -143,19 +148,29 @@ public class IPRange implements Comparable<IPRange>
        public IPRange(String cidr) throws UnknownHostException
        {
                /* only verify the basic structure */
-               if (!cidr.matches("^(([0-9.]+)|([0-9a-f:]+))(/\\d+)?$"))
+               if (!cidr.matches("(?i)^(([0-9.]+)|([0-9a-f:]+))(-(([0-9.]+)|([0-9a-f:]+))|(/\\d+))?$"))
+               {
+                       throw new IllegalArgumentException("Invalid CIDR or range notation");
+               }
+               if (cidr.contains("-"))
                {
-                       throw new IllegalArgumentException("Invalid CIDR notation");
+                       String[] parts = cidr.split("-");
+                       InetAddress from = InetAddress.getByName(parts[0]);
+                       InetAddress to = InetAddress.getByName(parts[1]);
+                       initializeFromRange(from, to);
                }
-               String[] parts = cidr.split("/");
-               InetAddress addr = InetAddress.getByName(parts[0]);
-               byte[] base = addr.getAddress();
-               int prefix = base.length * 8;
-               if (parts.length > 1)
+               else
                {
-                       prefix = Integer.parseInt(parts[1]);
+                       String[] parts = cidr.split("/");
+                       InetAddress addr = InetAddress.getByName(parts[0]);
+                       byte[] base = addr.getAddress();
+                       int prefix = base.length * 8;
+                       if (parts.length > 1)
+                       {
+                               prefix = Integer.parseInt(parts[1]);
+                       }
+                       initializeFromCIDR(base, prefix);
                }
-               initializeFromCIDR(base, prefix);
        }
 
        /**
index 60e468b..e6884c6 100644 (file)
@@ -30,8 +30,8 @@ public class IPRangeSet
        private TreeSet<IPRange> mRanges = new TreeSet<>();
 
        /**
-        * Parse the given string (space separated subnets in CIDR notation) and return the resulting
-        * set or {@code null} if the string was invalid. And empty set is returned if the given string
+        * Parse the given string (space separated ranges in CIDR or range notation) and return the
+        * resulting set or {@code null} if the string was invalid. An empty set is returned if the given string
         * is {@code null}.
         */
        public static IPRangeSet fromString(String ranges)
index d53d127..3f0e33e 100644 (file)
@@ -184,9 +184,25 @@ public class IPRangeSetTest
        }
 
        @Test
-       public void testFromStringInvalidRange() throws UnknownHostException
+       public void testFromStringRange() throws UnknownHostException
+       {
+               IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 10.0.1.0-10.0.1.16");
+               assertEquals("size", 2, set.size());
+               assertSubnets(set.getSubnets(), new IPRange("10.0.1.0/28"), new IPRange("10.0.1.16/32"),
+                                         new IPRange("192.168.1.0/24"));
+       }
+
+       @Test
+       public void testFromStringInvalidPrefix() throws UnknownHostException
        {
                IPRangeSet set = IPRangeSet.fromString("192.168.1.0/65");
                assertEquals("failed", null, set);
        }
+
+       @Test
+       public void testFromStringInvalidRange() throws UnknownHostException
+       {
+               IPRangeSet set = IPRangeSet.fromString("192.168.1.1 - 192.168.1.10");
+               assertEquals("failed", null, set);
+       }
 }
index da6f336..15d710e 100644 (file)
@@ -159,6 +159,13 @@ public class IPRangeTest
                assertEquals("not reached", null, test);
        }
 
+       @Test(expected = IllegalArgumentException.class)
+       public void testRangeMixed() throws UnknownHostException
+       {
+               IPRange test = new IPRange("192.168.1.1-fec1::1");
+               assertEquals("not reached", null, test);
+       }
+
        private void testCIDR(String cidr, IPRange exp) throws UnknownHostException
        {
                IPRange test = new IPRange(cidr);
@@ -173,11 +180,13 @@ public class IPRangeTest
                testCIDR("192.168.1.10/24", new IPRange("192.168.1.0", 24));
                testCIDR("192.168.1.1/32", new IPRange("192.168.1.1", 32));
                testCIDR("192.168.1.1", new IPRange("192.168.1.1", 32));
+               testCIDR("192.168.1.1-192.168.1.10", new IPRange("192.168.1.1", "192.168.1.10"));
                testCIDR("::/0", new IPRange("::", 0));
                testCIDR("fec1::/64", new IPRange("fec1::", 64));
                testCIDR("fec1::10/64", new IPRange("fec1::", 64));
                testCIDR("fec1::1/128", new IPRange("fec1::1", 128));
                testCIDR("fec1::1", new IPRange("fec1::1", 128));
+               testCIDR("fec1::1-fec1::5", new IPRange("fec1::1", "fec1::5"));
        }
 
        private void testToString(String f, String t, String exp) throws UnknownHostException