CIDRUtils { public boolean isInRange(String ipAddress) throws UnknownHostException { InetAddress address = InetAddress.getByName(ipAddress); BigInteger start = new BigInteger(1, this.startAddress.getAddress()); BigInteger end = new BigInteger(1, this.endAddress.getAddress()); BigInteger target = new BigInteger(1, address.getAddress()); int st = start.compareTo(target); int te = target.compareTo(end); return (st == -1 || st == 0) && (te == -1 || te == 0); } CIDRUtils(String cidr); String getNetworkAddress(); String getBroadcastAddress(); boolean isInRange(String ipAddress);  }
@Test public void testIpv4() throws UnknownHostException { CIDRUtils cidrUtils = new CIDRUtils("192.168.1.0/26"); Assertions.assertTrue(cidrUtils.isInRange("192.168.1.63")); Assertions.assertFalse(cidrUtils.isInRange("192.168.1.65")); cidrUtils = new CIDRUtils("192.168.1.192/26"); Assertions.assertTrue(cidrUtils.isInRange("192.168.1.199")); Assertions.assertFalse(cidrUtils.isInRange("192.168.1.190")); }
@Test public void testIpv6() throws UnknownHostException { CIDRUtils cidrUtils = new CIDRUtils("234e:0:4567::3d/64"); Assertions.assertTrue(cidrUtils.isInRange("234e:0:4567::3e")); Assertions.assertTrue(cidrUtils.isInRange("234e:0:4567::ffff:3e")); Assertions.assertFalse(cidrUtils.isInRange("234e:1:4567::3d")); Assertions.assertFalse(cidrUtils.isInRange("234e:0:4567:1::3d")); cidrUtils = new CIDRUtils("3FFE:FFFF:0:CC00::/54"); Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC00::dd")); Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC00:0000:eeee:0909:dd")); Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC0F:0000:eeee:0909:dd")); Assertions.assertFalse(cidrUtils.isInRange("3EFE:FFFE:0:C107::dd")); Assertions.assertFalse(cidrUtils.isInRange("1FFE:FFFE:0:CC00::dd")); }