IPv4Ranges { public boolean matches(byte[] address) { if (ranges.length == 0) return false; int comparableIP = toInteger(address) ^ Integer.MIN_VALUE; return withinRanges(ranges, comparableIP); } IPv4Ranges(List<String> cidrs); boolean matches(byte[] address);  }
@Test void matches() { var ranges = new IPv4Ranges(List.of("192.168.1.0/24")); assertThat(ranges.matches(address("192.168.1.1"))).isTrue(); assertThat(ranges.matches(address("192.168.1.254"))).isTrue(); assertThat(ranges.matches(address("192.168.2.1"))).isFalse(); assertThat(ranges.matches(address("192.168.0.1"))).isFalse(); ranges = new IPv4Ranges(List.of("192.168.1.1/32")); assertThat(ranges.matches(address("192.168.1.1"))).isTrue(); assertThat(ranges.matches(address("192.168.1.2"))).isFalse(); assertThat(ranges.matches(address("192.168.1.3"))).isFalse(); ranges = new IPv4Ranges(List.of("192.168.1.1/31")); assertThat(ranges.matches(address("192.168.1.0"))).isTrue(); assertThat(ranges.matches(address("192.168.1.1"))).isTrue(); assertThat(ranges.matches(address("192.168.1.2"))).isFalse(); ranges = new IPv4Ranges(List.of("192.168.1.1/30")); assertThat(ranges.matches(address("192.168.1.0"))).isTrue(); assertThat(ranges.matches(address("192.168.1.1"))).isTrue(); assertThat(ranges.matches(address("192.168.1.2"))).isTrue(); assertThat(ranges.matches(address("192.168.1.3"))).isTrue(); assertThat(ranges.matches(address("192.168.1.4"))).isFalse(); ranges = new IPv4Ranges(List.of("119.137.52.0/22")); assertThat(ranges.matches(address("119.137.52.1"))).isTrue(); assertThat(ranges.matches(address("119.137.53.1"))).isTrue(); assertThat(ranges.matches(address("119.137.53.254"))).isTrue(); assertThat(ranges.matches(address("119.137.54.254"))).isTrue(); ranges = new IPv4Ranges(List.of("42.200.0.0/16", "43.224.4.0/22", "43.224.28.0/22")); assertThat(ranges.matches(address("42.119.0.1"))).isFalse(); assertThat(ranges.matches(address("42.200.218.1"))).isTrue(); assertThat(ranges.matches(address("42.201.218.1"))).isFalse(); assertThat(ranges.matches(address("43.224.32.1"))).isFalse(); }