android: Parse two addresses separated by - as range

This commit is contained in:
Tobias Brunner 2017-06-21 15:53:39 +02:00
parent 13ead876ad
commit 78b20efb29
4 changed files with 52 additions and 12 deletions

View File

@ -80,6 +80,11 @@ public class IPRange implements Comparable<IPRange>
} }
public IPRange(InetAddress from, InetAddress to) public IPRange(InetAddress from, InetAddress to)
{
initializeFromRange(from, to);
}
private void initializeFromRange(InetAddress from, InetAddress to)
{ {
byte[] fa = from.getAddress(), ta = to.getAddress(); byte[] fa = from.getAddress(), ta = to.getAddress();
if (fa.length != ta.length) if (fa.length != ta.length)
@ -143,19 +148,29 @@ public class IPRange implements Comparable<IPRange>
public IPRange(String cidr) throws UnknownHostException public IPRange(String cidr) throws UnknownHostException
{ {
/* only verify the basic structure */ /* 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");
} }
String[] parts = cidr.split("/"); if (cidr.contains("-"))
InetAddress addr = InetAddress.getByName(parts[0]);
byte[] base = addr.getAddress();
int prefix = base.length * 8;
if (parts.length > 1)
{ {
prefix = Integer.parseInt(parts[1]); 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();
int prefix = base.length * 8;
if (parts.length > 1)
{
prefix = Integer.parseInt(parts[1]);
}
initializeFromCIDR(base, prefix);
} }
initializeFromCIDR(base, prefix);
} }
/** /**

View File

@ -30,8 +30,8 @@ public class IPRangeSet
private TreeSet<IPRange> mRanges = new TreeSet<>(); private TreeSet<IPRange> mRanges = new TreeSet<>();
/** /**
* Parse the given string (space separated subnets in CIDR notation) and return the resulting * Parse the given string (space separated ranges in CIDR or range notation) and return the
* set or {@code null} if the string was invalid. And empty set is returned if the given string * resulting set or {@code null} if the string was invalid. An empty set is returned if the given string
* is {@code null}. * is {@code null}.
*/ */
public static IPRangeSet fromString(String ranges) public static IPRangeSet fromString(String ranges)

View File

@ -184,9 +184,25 @@ public class IPRangeSetTest
} }
@Test @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"); IPRangeSet set = IPRangeSet.fromString("192.168.1.0/65");
assertEquals("failed", null, set); 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);
}
} }

View File

@ -159,6 +159,13 @@ public class IPRangeTest
assertEquals("not reached", null, test); 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 private void testCIDR(String cidr, IPRange exp) throws UnknownHostException
{ {
IPRange test = new IPRange(cidr); 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.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/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", 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("::/0", new IPRange("::", 0));
testCIDR("fec1::/64", new IPRange("fec1::", 64)); testCIDR("fec1::/64", new IPRange("fec1::", 64));
testCIDR("fec1::10/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/128", new IPRange("fec1::1", 128));
testCIDR("fec1::1", 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 private void testToString(String f, String t, String exp) throws UnknownHostException