mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-10 03:23:13 +00:00
92917: Support implicit ranges & update unit tests
This commit is contained in:
@@ -13,6 +13,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@@ -98,7 +99,17 @@ public class IPTable {
|
|||||||
throw new IPFormatException(ip + " - Range format should be similar to 1.2.3.0-1.2.3.255");
|
throw new IPFormatException(ip + " - Range format should be similar to 1.2.3.0-1.2.3.255");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (ip.contains("/")) {
|
} else {
|
||||||
|
// Convert implicit ranges to netmask format
|
||||||
|
// 192 -> 192.0.0.0/8
|
||||||
|
// 192.168 -> 192.168.0.0/16
|
||||||
|
// 192.168.1 -> 192.168.1.0/24
|
||||||
|
int periods = StringUtils.countMatches(ip, '.');
|
||||||
|
if (periods < 3) {
|
||||||
|
ip = StringUtils.join(ip, StringUtils.repeat(".0", 4 - periods - 1), "/", (periods + 1) * 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip.contains("/")) {
|
||||||
String[] parts = ip.split("/");
|
String[] parts = ip.split("/");
|
||||||
try {
|
try {
|
||||||
byte[] octets = InetAddress.getByName(parts[0]).getAddress();
|
byte[] octets = InetAddress.getByName(parts[0]).getAddress();
|
||||||
@@ -115,7 +126,6 @@ public class IPTable {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IPFormatException(ip + " - Range format should be similar to 172.16.0.0/12");
|
throw new IPFormatException(ip + " - Range format should be similar to 172.16.0.0/12");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
long ipLo = ipToLong(InetAddress.getByName(ip));
|
long ipLo = ipToLong(InetAddress.getByName(ip));
|
||||||
@@ -126,6 +136,7 @@ public class IPTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an IP address to a long integer
|
* Convert an IP address to a long integer
|
||||||
|
@@ -56,14 +56,15 @@ public class IPTableTest {
|
|||||||
IPTable instance = new IPTable();
|
IPTable instance = new IPTable();
|
||||||
// Add IP address
|
// Add IP address
|
||||||
instance.add(LOCALHOST);
|
instance.add(LOCALHOST);
|
||||||
// Add IP range
|
// Add IP range (contains 256 addresses)
|
||||||
instance.add("192.168.1");
|
instance.add("192.168.1");
|
||||||
|
|
||||||
// Make sure both exist
|
// Make sure it returns the addresses for all ranges
|
||||||
Set<String> ipSet = instance.toSet();
|
Set<String> ipSet = instance.toSet();
|
||||||
assertEquals(2, ipSet.size());
|
assertEquals(257, ipSet.size());
|
||||||
assertTrue(ipSet.contains(LOCALHOST));
|
assertTrue(ipSet.contains(LOCALHOST));
|
||||||
assertTrue(ipSet.contains("192.168.1"));
|
assertTrue(ipSet.contains("192.168.1.0"));
|
||||||
|
assertTrue(ipSet.contains("192.168.1.255"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -76,13 +77,13 @@ public class IPTableTest {
|
|||||||
assertEquals(1, instance.toSet().size());
|
assertEquals(1, instance.toSet().size());
|
||||||
|
|
||||||
instance = new IPTable();
|
instance = new IPTable();
|
||||||
// Add IP range & then add an IP from within that range
|
// Add IP range w/ 256 addresses & then add an IP from within that range
|
||||||
instance.add("192.168.1");
|
instance.add("192.168.1");
|
||||||
instance.add("192.168.1.1");
|
instance.add("192.168.1.1");
|
||||||
// Verify only the range exists
|
// Verify only the range exists
|
||||||
Set<String> ipSet = instance.toSet();
|
Set<String> ipSet = instance.toSet();
|
||||||
assertEquals(1, ipSet.size());
|
assertEquals(256, ipSet.size());
|
||||||
assertTrue(ipSet.contains("192.168.1"));
|
assertTrue(ipSet.contains("192.168.1.1"));
|
||||||
|
|
||||||
instance = new IPTable();
|
instance = new IPTable();
|
||||||
// Now, switch order. Add IP address, then add a range encompassing that IP
|
// Now, switch order. Add IP address, then add a range encompassing that IP
|
||||||
@@ -90,8 +91,8 @@ public class IPTableTest {
|
|||||||
instance.add("192.168.1");
|
instance.add("192.168.1");
|
||||||
// Verify only the range exists
|
// Verify only the range exists
|
||||||
ipSet = instance.toSet();
|
ipSet = instance.toSet();
|
||||||
assertEquals(1, ipSet.size());
|
assertEquals(256, ipSet.size());
|
||||||
assertTrue(ipSet.contains("192.168.1"));
|
assertTrue(ipSet.contains("192.168.1.1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,6 +121,48 @@ public class IPTableTest {
|
|||||||
assertTrue("IP within an add()ed range should match", contains);
|
assertTrue("IP within an add()ed range should match", contains);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDashRangeContains() throws Exception {
|
||||||
|
IPTable instance = new IPTable();
|
||||||
|
instance.add("192.168.0.0 - 192.168.0.245");
|
||||||
|
|
||||||
|
assertTrue("Range should contain lower limit", instance.contains("192.168.0.0"));
|
||||||
|
assertTrue("Range should contain upper limit", instance.contains("192.168.0.245"));
|
||||||
|
assertTrue("Range should contain value in between limits", instance.contains("192.168.0.123"));
|
||||||
|
assertTrue("Range should contain value in between limits", instance.contains("192.168.0.234"));
|
||||||
|
|
||||||
|
assertFalse("Range should not contain value below lower limit", instance.contains("192.167.255.255"));
|
||||||
|
assertFalse("Range should not contain value above upper limit", instance.contains("192.168.0.246"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubnetRangeContains() throws Exception {
|
||||||
|
IPTable instance = new IPTable();
|
||||||
|
instance.add("192.168.0.0/30"); // translates to 192.168.0.0 - 192.168.0.3
|
||||||
|
|
||||||
|
assertTrue("Range should contain lower limit", instance.contains("192.168.0.0"));
|
||||||
|
assertTrue("Range should contain upper limit", instance.contains("192.168.0.3"));
|
||||||
|
assertTrue("Range should contain values in between limits", instance.contains("192.168.0.1"));
|
||||||
|
assertTrue("Range should contain values in between limits", instance.contains("192.168.0.2"));
|
||||||
|
|
||||||
|
assertFalse("Range should not contain value below lower limit", instance.contains("192.167.255.255"));
|
||||||
|
assertFalse("Range should not contain value above upper limit", instance.contains("192.168.0.4"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testImplicitRangeContains() throws Exception {
|
||||||
|
IPTable instance = new IPTable();
|
||||||
|
instance.add("192.168.1");
|
||||||
|
|
||||||
|
assertTrue("Range should contain lower limit", instance.contains("192.168.1.0"));
|
||||||
|
assertTrue("Range should contain upper limit", instance.contains("192.168.1.255"));
|
||||||
|
assertTrue("Range should contain values in between limits", instance.contains("192.168.1.123"));
|
||||||
|
assertTrue("Range should contain values in between limits", instance.contains("192.168.1.234"));
|
||||||
|
|
||||||
|
assertFalse("Range should not contain value below lower limit", instance.contains("192.168.0.0"));
|
||||||
|
assertFalse("Range should not contain value above upper limit", instance.contains("192.168.2.0"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test of isEmpty method, of class IPTable.
|
* Test of isEmpty method, of class IPTable.
|
||||||
* @throws java.lang.Exception passed through.
|
* @throws java.lang.Exception passed through.
|
||||||
|
Reference in New Issue
Block a user