IPBlock { public boolean ipCheck(long addr) { return ((addr & mask) == subnet); } IPBlock(final String ipBlock); boolean ipCheck(long addr); static long convertIPToLong(final String ipAddress); static long convertMaskToLong(int value);  }
@Test public void testIpCheck() { IPBlock ipBlock = new IPBlock("10.1.0.1/32"); assertTrue(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.0.1"))); assertFalse(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.0.2"))); ipBlock = new IPBlock("10.1.0.0/21"); assertTrue(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.0.0"))); assertTrue(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.7.255"))); assertTrue(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.3.25"))); assertTrue(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.0.24"))); assertFalse(ipBlock.ipCheck(IPBlock.convertIPToLong("10.1.8.0"))); assertFalse(ipBlock.ipCheck(IPBlock.convertIPToLong("10.0.0.0"))); assertFalse(ipBlock.ipCheck(IPBlock.convertIPToLong("10.2.0.0"))); assertFalse(ipBlock.ipCheck(IPBlock.convertIPToLong("10.2.1.255"))); ipBlock = new IPBlock("35.160.0.0/13"); assertTrue(ipBlock.ipCheck(IPBlock.convertIPToLong("35.166.98.147"))); }