Skip to content

Commit

Permalink
Improve IpAddressHelper for public address determining (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanchyk authored Aug 12, 2020
1 parent c83316e commit b341d78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/prebid/server/auction/IpAddressHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ private boolean isIpPublic(IPAddress ipAddress) {
return ipAddress != null
&& !ipAddress.isLocal()
&& !ipAddress.isLoopback()
&& !ipAddress.isMulticast()
&& !ipAddress.isMax()
&& ipv6LocalNetworkMaskAddresses.stream().noneMatch(network -> network.contains(ipAddress));
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/prebid/server/auction/IpAddressHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,41 @@ public void toIpAddressShouldReturnNullIfIpIsV4AndSiteLocal() {
assertThat(ipAddressHelper.toIpAddress("192.168.0.1")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV4AndMulticast() {
assertThat(ipAddressHelper.toIpAddress("224.0.0.0/4")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV4AndZero() {
assertThat(ipAddressHelper.toIpAddress("0.0.0.0")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV4AndMax() {
assertThat(ipAddressHelper.toIpAddress("255.255.255.255")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV6AndLocal() {
assertThat(ipAddressHelper.toIpAddress("fc00:0000:0000:0000:0000:0000:0000:0001")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV6AndMulticast() {
assertThat(ipAddressHelper.toIpAddress("ff00::/64")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV6AndZero() {
assertThat(ipAddressHelper.toIpAddress("0000:0000:0000:0000:0000:0000:0000:0000")).isNull();
}

@Test
public void toIpAddressShouldReturnNullIfIpIsV6AndMax() {
assertThat(ipAddressHelper.toIpAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")).isNull();
}

@Test
public void toIpAddressShouldReturnIpV4Address() {
assertThat(ipAddressHelper.toIpAddress("12.34.56.78")).isEqualTo(IpAddress.of("12.34.56.78", IpAddress.IP.v4));
Expand Down

0 comments on commit b341d78

Please sign in to comment.