FileBasedIPList implements IPList { @Override public boolean isIn(String ipAddress) { if (ipAddress == null || addressList == null) { return false; } return addressList.includes(ipAddress); } FileBasedIPList(String fileName); FileBasedIPList reload(); @Override boolean isIn(String ipAddress);  }
@Test public void testSubnetsAndIPs() throws IOException { String[] ips = {"10.119.103.112", "10.221.102.0/23"}; createFileWithEntries ("ips.txt", ips); IPList ipList = new FileBasedIPList("ips.txt"); assertTrue ("10.119.103.112 is not in the list", ipList.isIn("10.119.103.112")); assertFalse ("10.119.103.113 is in the list", ipList.isIn("10.119.103.113")); assertTrue ("10.221.102.0 is not in the list", ipList.isIn("10.221.102.0")); assertTrue ("10.221.102.1 is not in the list", ipList.isIn("10.221.102.1")); assertTrue ("10.221.103.1 is not in the list", ipList.isIn("10.221.103.1")); assertTrue ("10.221.103.255 is not in the list", ipList.isIn("10.221.103.255")); assertFalse("10.221.104.0 is in the list", ipList.isIn("10.221.104.0")); assertFalse("10.221.104.1 is in the list", ipList.isIn("10.221.104.1")); }
@Test public void testNullIP() throws IOException { String[] ips = {"10.119.103.112", "10.221.102.0/23"}; createFileWithEntries ("ips.txt", ips); IPList ipList = new FileBasedIPList("ips.txt"); assertFalse ("Null Ip is in the list", ipList.isIn(null)); }
@Test public void testWithMultipleSubnetAndIPs() throws IOException { String[] ips = {"10.119.103.112", "10.221.102.0/23", "10.222.0.0/16", "10.113.221.221"}; createFileWithEntries ("ips.txt", ips); IPList ipList = new FileBasedIPList("ips.txt"); assertTrue ("10.119.103.112 is not in the list", ipList.isIn("10.119.103.112")); assertFalse ("10.119.103.113 is in the list", ipList.isIn("10.119.103.113")); assertTrue ("10.221.103.121 is not in the list", ipList.isIn("10.221.103.121")); assertFalse("10.221.104.0 is in the list", ipList.isIn("10.221.104.0")); assertTrue ("10.222.103.121 is not in the list", ipList.isIn("10.222.103.121")); assertFalse("10.223.104.0 is in the list", ipList.isIn("10.223.104.0")); assertTrue ("10.113.221.221 is not in the list", ipList.isIn("10.113.221.221")); assertFalse("10.113.221.222 is in the list", ipList.isIn("10.113.221.222")); }
@Test public void testFileNotSpecified() { IPList ipl = new FileBasedIPList(null); assertFalse("110.113.221.222 is in the list", ipl.isIn("110.113.221.222")); }
@Test public void testFileMissing() { IPList ipl = new FileBasedIPList("missingips.txt"); assertFalse("110.113.221.222 is in the list", ipl.isIn("110.113.221.222")); }
@Test public void testWithEmptyList() throws IOException { String[] ips = {}; createFileWithEntries ("ips.txt", ips); IPList ipl = new FileBasedIPList("ips.txt"); assertFalse("110.113.221.222 is in the list", ipl.isIn("110.113.221.222")); }