android: Add ability to remove a range set from another
authorTobias Brunner <tobias@strongswan.org>
Wed, 21 Jun 2017 14:22:11 +0000 (16:22 +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/IPRangeSet.java
src/frontends/android/app/src/test/java/org/strongswan/android/test/IPRangeSetTest.java

index e6884c6..3749ac0 100644 (file)
@@ -109,6 +109,22 @@ public class IPRangeSet
        }
 
        /**
+        * Remove the given ranges from ranges in this set.
+        */
+       public void remove(IPRangeSet ranges)
+       {
+               if (ranges == this)
+               {
+                       mRanges.clear();
+                       return;
+               }
+               for (IPRange range : ranges.mRanges)
+               {
+                       remove(range);
+               }
+       }
+
+       /**
         * Returns the subnets derived from all the ranges in this set.
         */
        public Enumeration<IPRange> getSubnets()
index 3f0e33e..6c6e159 100644 (file)
@@ -167,6 +167,26 @@ public class IPRangeSetTest
        }
 
        @Test
+       public void testRemoveRangesIdent() throws UnknownHostException
+       {
+               IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 192.168.4.0/24");
+               set.remove(set);
+               assertEquals("size", 0, set.size());
+               assertSubnets(set.getSubnets());
+       }
+
+       @Test
+       public void testRemoveRanges() throws UnknownHostException
+       {
+               IPRangeSet set = IPRangeSet.fromString("192.168.0.0/16");
+               IPRangeSet remove = IPRangeSet.fromString("192.168.1.0/24 192.168.3.0/24 192.168.16.0-192.168.255.255");
+               set.remove(remove);
+               assertEquals("size", 3, set.size());
+               assertSubnets(set.getSubnets(), new IPRange("192.168.0.0/24"), new IPRange("192.168.2.0/24"),
+                                         new IPRange("192.168.4.0/22"), new IPRange("192.168.8.0/21"));
+       }
+
+       @Test
        public void testFromStringSingle() throws UnknownHostException
        {
                IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24");