mirror of
https://github.com/strongswan/strongswan.git
synced 2025-11-09 00:01:52 -05:00
android: Parse two addresses separated by - as range
This commit is contained in:
parent
13ead876ad
commit
78b20efb29
@ -80,6 +80,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,10 +148,19 @@ 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 notation");
|
||||
throw new IllegalArgumentException("Invalid CIDR or range notation");
|
||||
}
|
||||
if (cidr.contains("-"))
|
||||
{
|
||||
String[] parts = cidr.split("-");
|
||||
InetAddress from = InetAddress.getByName(parts[0]);
|
||||
InetAddress to = InetAddress.getByName(parts[1]);
|
||||
initializeFromRange(from, to);
|
||||
}
|
||||
else
|
||||
{
|
||||
String[] parts = cidr.split("/");
|
||||
InetAddress addr = InetAddress.getByName(parts[0]);
|
||||
byte[] base = addr.getAddress();
|
||||
@ -157,6 +171,7 @@ public class IPRange implements Comparable<IPRange>
|
||||
}
|
||||
initializeFromCIDR(base, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first address of the range. The network ID in case this is a proper subnet.
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user