IPv4AccessControl { boolean allow(byte[] address) { if (address.length > 4) { logger.debug("skip with ipv6 client address"); return true; } if (allow != null && allow.matches(address)) { logger.debug("allow client ip within allowed ranges"); return true; } if (deny == null || deny.matches(address)) { logger.debug("deny client ip within denied ranges"); return false; } logger.debug("allow client ip not within denied ranges"); return true; }  void validate(String clientIP); public IPv4Ranges allow; public IPv4Ranges deny; }
@Test void allowWithBothAllowDenyDefined() { accessControl.deny = new IPv4Ranges(List.of("100.100.100.100/24")); accessControl.allow = new IPv4Ranges(List.of("100.100.100.101/32")); assertThat(accessControl.allow(IPv4Ranges.address("100.100.99.1"))).isTrue(); assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.101"))).isTrue(); assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.1"))).isFalse(); }
@Test void allowWithOnlyAllowDefined() { accessControl.allow = new IPv4Ranges(List.of("100.100.100.101/32")); assertThat(accessControl.allow(IPv4Ranges.address("100.100.99.1"))).isFalse(); assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.101"))).isTrue(); assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.1"))).isFalse(); }
@Test void allowWithOnlyDenyDefined() { accessControl.deny = new IPv4Ranges(List.of("100.100.100.101/32")); assertThat(accessControl.allow(IPv4Ranges.address("100.100.99.1"))).isTrue(); assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.101"))).isFalse(); assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.1"))).isTrue(); }
@Test void denyByDefault() { accessControl.allow = null; accessControl.deny = null; assertThat(accessControl.allow(IPv4Ranges.address("100.100.100.100"))).isFalse(); }
@Test void allowWithIPv6() { assertThat(accessControl.allow(IPv4Ranges.address("2001:0db8:85a3:0000:0000:8a2e:0370:0000"))).isTrue(); }