IPMatcher { public boolean match(String ipIn) throws IPMatcherException { log.debug("ipIn: "+ipIn); byte[] candidate; if (ipIn.indexOf(':') < 0) { candidate = new byte[4]; ipToBytes(ipIn, candidate, true); candidate = ip4ToIp6(candidate); } else try { candidate = Inet6Address.getByName(ipIn).getAddress(); } catch (UnknownHostException e) { throw new IPMatcherException("Malformed IPv6 address ",e); } for (int i = 0; i < netmask.length; i++) { if ((candidate[i] & netmask[i]) != (network[i] & netmask[i])) { if (log.isDebugEnabled()) { log.debug("candidate[i]: "+candidate[i]); log.debug("netmask[i]: "+netmask[i]); log.debug("candidate[i] & netmask[i]: "+(candidate[i] & netmask[i])); log.debug("network[i]: "+network[i]); log.debug("network[i] & netmask[i]: "+(network[i] & netmask[i])); } return false; } } return true; } IPMatcher(String ipSpec); boolean match(String ipIn);  }
@Test public void testIp6FullMatch() throws IPMatcherException { assertTrue("IPv6 full match fails", ip6FullMatcher .match(IP6_FULL_ADDRESS1)); }
@Test public void testIp6MisMatch() throws IPMatcherException { assertFalse("IPv6 full nonmatch succeeds", ip6FullMatcher .match(IP6_FULL_ADDRESS2)); }
@Test public void testIp6MaskedMatch() throws IPMatcherException { assertTrue("IPv6 masked match fails", ip6MaskedMatcher .match(IP6_FULL_ADDRESS2)); }
@Test public void testIPv4MatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("1.1.1.1"); assertTrue(ipMatcher.match("1.1.1.1")); ArrayList<String> exceptions = new ArrayList<String>(); exceptions.add("1.1.1.1"); verifyAllIp4Except(exceptions, false, ipMatcher); }
@Test public void testIPv4MatchingFailure() throws Exception { final IPMatcher ipMatcher = new IPMatcher("1.1.1.1"); assertFalse(ipMatcher.match("1.1.1.0")); }
@Test public void testIPv6MatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("::2"); assertTrue(ipMatcher.match("0:0:0:0:0:0:0:2")); }
@Test public void testShortFormIPv6MatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("::2"); assertTrue(ipMatcher.match("::2")); }
@Test public void testIPv6MatchingFailure() throws Exception { final IPMatcher ipMatcher = new IPMatcher("::2"); assertFalse(ipMatcher.match("0:0:0:0:0:0:0:1")); }
@Test public void testAsteriskMatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("172.16"); assertTrue(ipMatcher.match("172.16.1.1")); }
@Test public void testAsteriskMatchingFailure() throws Exception { final IPMatcher ipMatcher = new IPMatcher("172.16"); assertFalse(ipMatcher.match("172.15.255.255")); }
@Test public void testIPv4CIDRMatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("192.1.2.3/8"); assertTrue(ipMatcher.match("192.1.1.1")); }
@Test public void testIPv4CIDRMatchingFailure() throws Exception { final IPMatcher ipMatcher = new IPMatcher("192.1.2.3/8"); assertTrue(ipMatcher.match("192.2.0.0")); }
@Test public void test2IPv4CIDRMatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("192.86.100.72/29"); assertTrue(ipMatcher.match("192.86.100.75")); assertFalse(ipMatcher.match("192.86.100.71")); assertFalse(ipMatcher.match("192.86.100.80")); ArrayList<String> exceptions = new ArrayList<String>(); exceptions.add("192.86.100.72"); exceptions.add("192.86.100.73"); exceptions.add("192.86.100.74"); exceptions.add("192.86.100.75"); exceptions.add("192.86.100.76"); exceptions.add("192.86.100.77"); exceptions.add("192.86.100.78"); exceptions.add("192.86.100.79"); verifyAllIp4Except(exceptions, false, ipMatcher); }
@Test public void test3IPv4CIDRMatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("192.86.100.72/255.255.255.248"); assertTrue(ipMatcher.match("192.86.100.75")); assertFalse(ipMatcher.match("192.86.100.71")); assertFalse(ipMatcher.match("192.86.100.80")); ArrayList<String> exceptions = new ArrayList<String>(); exceptions.add("192.86.100.72"); exceptions.add("192.86.100.73"); exceptions.add("192.86.100.74"); exceptions.add("192.86.100.75"); exceptions.add("192.86.100.76"); exceptions.add("192.86.100.77"); exceptions.add("192.86.100.78"); exceptions.add("192.86.100.79"); verifyAllIp4Except(exceptions, false, ipMatcher); }
@Test public void testIPv6CIDRMatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("0:0:0:1::/64"); assertTrue(ipMatcher.match("0:0:0:1:ffff:ffff:ffff:ffff")); }
@Test public void testIPv6CIDRMatchingFailure() throws Exception { final IPMatcher ipMatcher = new IPMatcher("0:0:0:1::/64"); assertFalse(ipMatcher.match("0:0:0:2::")); }
@Test public void testIPv4IPv6Matching() throws Exception { final IPMatcher ipMatcher = new IPMatcher("0.0.0.1"); assertTrue(ipMatcher.match("::1")); }
@Test public void testSubnetZeroIPv6CIDRMatching() throws Exception { final IPMatcher ipMatcher = new IPMatcher("::1/0"); assertTrue(ipMatcher.match("::2")); }
@Test public void testAllOnesSubnetIPv4CIDRMatchingSuccess() throws Exception { final IPMatcher ipMatcher = new IPMatcher("192.1.2.3/32"); assertTrue(ipMatcher.match("192.1.2.3")); }
@Test public void testAllOnesSubnetIPv4CIDRMatchingFailure() throws Exception { final IPMatcher ipMatcher = new IPMatcher("192.1.2.3/32"); assertFalse(ipMatcher.match("192.1.2.2")); }