MachineList { public boolean includes(String ipAddress) { if (all) { return true; } if (ipAddress == null) { throw new IllegalArgumentException("ipAddress is null."); } if ((ipAddresses != null) && ipAddresses.contains(ipAddress)) { return true; } if (cidrAddresses != null) { for(SubnetUtils.SubnetInfo cidrAddress : cidrAddresses) { if(cidrAddress.isInRange(ipAddress)) { return true; } } } if (hostNames != null) { InetAddress hostAddr; try { hostAddr = addressFactory.getByName(ipAddress); if ((hostAddr != null) && hostNames.contains(hostAddr.getCanonicalHostName())) { return true; } } catch (UnknownHostException e) { } for (String host : hostNames) { try { hostAddr = addressFactory.getByName(host); } catch (UnknownHostException e) { continue; } if (hostAddr.getHostAddress().equals(ipAddress)) { return true; } } } return false; } MachineList(String hostEntries);  MachineList(Collection<String> hostEntries);  MachineList(Collection<String> hostEntries, InetAddressFactory addressFactory); boolean includes(String ipAddress); @VisibleForTesting Collection<String> getCollection(); static final Logger LOG; static final String WILDCARD_VALUE; }
@Test public void testWildCard() { MachineList ml = new MachineList("*"); assertTrue(ml.includes("10.119.103.112")); assertTrue(ml.includes("1.2.3.4")); }
@Test public void testIPList() { MachineList ml = new MachineList(IP_LIST); assertTrue(ml.includes("10.119.103.112")); assertFalse(ml.includes("10.119.103.111")); }
@Test public void testIPListSpaces() { MachineList ml = new MachineList(IP_LIST_SPACES); assertTrue(ml.includes("10.119.103.112")); assertFalse(ml.includes("10.119.103.111")); }
@Test public void testStaticIPHostNameList()throws UnknownHostException { InetAddress addressHost1 = InetAddress.getByName("1.2.3.1"); InetAddress addressHost4 = InetAddress.getByName("1.2.3.4"); MachineList.InetAddressFactory addressFactory = Mockito.mock(MachineList.InetAddressFactory.class); Mockito.when(addressFactory.getByName("host1")).thenReturn(addressHost1); Mockito.when(addressFactory.getByName("host4")).thenReturn(addressHost4); MachineList ml = new MachineList( StringUtils.getTrimmedStringCollection(HOST_LIST), addressFactory); assertTrue(ml.includes("1.2.3.4")); assertFalse(ml.includes("1.2.3.5")); }
@Test public void testHostNames() throws UnknownHostException { InetAddress addressHost1 = InetAddress.getByName("1.2.3.1"); InetAddress addressHost4 = InetAddress.getByName("1.2.3.4"); InetAddress addressMockHost4 = Mockito.mock(InetAddress.class); Mockito.when(addressMockHost4.getCanonicalHostName()).thenReturn("differentName"); InetAddress addressMockHost5 = Mockito.mock(InetAddress.class); Mockito.when(addressMockHost5.getCanonicalHostName()).thenReturn("host5"); MachineList.InetAddressFactory addressFactory = Mockito.mock(MachineList.InetAddressFactory.class); Mockito.when(addressFactory.getByName("1.2.3.4")).thenReturn(addressMockHost4); Mockito.when(addressFactory.getByName("1.2.3.5")).thenReturn(addressMockHost5); Mockito.when(addressFactory.getByName("host1")).thenReturn(addressHost1); Mockito.when(addressFactory.getByName("host4")).thenReturn(addressHost4); MachineList ml = new MachineList( StringUtils.getTrimmedStringCollection(HOST_LIST), addressFactory ); assertTrue(ml.includes("1.2.3.4")); assertFalse(ml.includes("1.2.3.5")); }
@Test public void testHostNamesReverserIpMatch() throws UnknownHostException { InetAddress addressHost1 = InetAddress.getByName("1.2.3.1"); InetAddress addressHost4 = InetAddress.getByName("1.2.3.4"); InetAddress addressMockHost4 = Mockito.mock(InetAddress.class); Mockito.when(addressMockHost4.getCanonicalHostName()).thenReturn("host4"); InetAddress addressMockHost5 = Mockito.mock(InetAddress.class); Mockito.when(addressMockHost5.getCanonicalHostName()).thenReturn("host5"); MachineList.InetAddressFactory addressFactory = Mockito.mock(MachineList.InetAddressFactory.class); Mockito.when(addressFactory.getByName("1.2.3.4")).thenReturn(addressMockHost4); Mockito.when(addressFactory.getByName("1.2.3.5")).thenReturn(addressMockHost5); Mockito.when(addressFactory.getByName("host1")).thenReturn(addressHost1); Mockito.when(addressFactory.getByName("host4")).thenReturn(addressHost4); MachineList ml = new MachineList( StringUtils.getTrimmedStringCollection(HOST_LIST), addressFactory ); assertTrue(ml.includes("1.2.3.4")); assertFalse(ml.includes("1.2.3.5")); }
@Test public void testCIDRs() { MachineList ml = new MachineList(CIDR_LIST); assertFalse(ml.includes("10.221.255.255")); assertTrue(ml.includes("10.222.0.0")); assertTrue(ml.includes("10.222.0.1")); assertTrue(ml.includes("10.222.0.255")); assertTrue(ml.includes("10.222.255.0")); assertTrue(ml.includes("10.222.255.254")); assertTrue(ml.includes("10.222.255.255")); assertFalse(ml.includes("10.223.0.0")); assertTrue(ml.includes("10.241.23.0")); assertTrue(ml.includes("10.241.23.1")); assertTrue(ml.includes("10.241.23.254")); assertTrue(ml.includes("10.241.23.255")); assertFalse(ml.includes("10.119.103.111")); }
@Test(expected = IllegalArgumentException.class) public void testNullIpAddress() { MachineList ml = new MachineList(CIDR_LIST); assertFalse(ml.includes(null)); }
@Test public void testCIDRWith16bitmask() { MachineList ml = new MachineList(CIDR_LIST1); assertFalse(ml.includes("10.221.255.255")); assertTrue(ml.includes("10.222.0.0")); assertTrue(ml.includes("10.222.0.1")); assertTrue(ml.includes("10.222.0.255")); assertTrue(ml.includes("10.222.255.0")); assertTrue(ml.includes("10.222.255.254")); assertTrue(ml.includes("10.222.255.255")); assertFalse(ml.includes("10.223.0.0")); assertFalse(ml.includes("10.119.103.111")); }
@Test public void testCIDRWith8BitMask() { MachineList ml = new MachineList(CIDR_LIST2); assertFalse(ml.includes("10.241.22.255")); assertTrue(ml.includes("10.241.23.0")); assertTrue(ml.includes("10.241.23.1")); assertTrue(ml.includes("10.241.23.254")); assertTrue(ml.includes("10.241.23.255")); assertFalse(ml.includes("10.241.24.0")); assertFalse(ml.includes("10.119.103.111")); }
@Test public void testIPandCIDRs() { MachineList ml = new MachineList(IP_CIDR_LIST); assertTrue(ml.includes("10.119.103.112")); assertFalse(ml.includes("10.119.103.111")); assertFalse(ml.includes("10.221.255.255")); assertTrue(ml.includes("10.222.0.0")); assertTrue(ml.includes("10.222.255.255")); assertFalse(ml.includes("10.223.0.0")); assertFalse(ml.includes("10.241.22.255")); assertTrue(ml.includes("10.241.23.0")); assertTrue(ml.includes("10.241.23.255")); assertFalse(ml.includes("10.241.24.0")); }
@Test public void testHostNameIPandCIDRs() { MachineList ml = new MachineList(HOSTNAME_IP_CIDR_LIST); assertTrue(ml.includes("10.119.103.112")); assertFalse(ml.includes("10.119.103.111")); assertFalse(ml.includes("10.221.255.255")); assertTrue(ml.includes("10.222.0.0")); assertTrue(ml.includes("10.222.255.255")); assertFalse(ml.includes("10.223.0.0")); assertFalse(ml.includes("10.241.22.255")); assertTrue(ml.includes("10.241.23.0")); assertTrue(ml.includes("10.241.23.255")); assertFalse(ml.includes("10.241.24.0")); }