Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* strategies for IP addresses.
*
* @author Rob Winch
* @author Andrey Litvitski
* @since 7.1
*/
public final class InetAddressMatchers {
Expand Down Expand Up @@ -256,6 +257,9 @@ public boolean matches(@Nullable InetAddress address) {
if (address == null) {
return false;
}
if (address.isAnyLocalAddress()) {
return true;
}
if (address.isLoopbackAddress() || address.isLinkLocalAddress() || address.isSiteLocalAddress()) {
Comment on lines 259 to 263
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably be more elegant than adding the same check but on matchExternal, since the logic of matchExternal is to return the response back from matchInternal.

return true;
}
Expand Down Expand Up @@ -335,6 +339,9 @@ private ExternalInetAddressMatcher() {

@Override
public boolean matches(@Nullable InetAddress address) {
if (address == null) {
return false;
}
return !this.internalMatcher.matches(address);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Tests for {@link InetAddressMatchers}.
*
* @author Rob Winch
* @author Andrey Litvitski
*/
class InetAddressMatchersTests {

Expand All @@ -51,6 +52,12 @@ void matchInternalWhenInvokedThenReturnsBuilder() {
assertThat(builder).isNotNull();
}

@Test
void matchesWhenInetAddressNullThenReturnsFalse() {
InetAddressMatcher matcher = InetAddressMatchers.matchExternal().build();
assertThat(matcher.matches((InetAddress) null)).isFalse();
}

@Nested
class BuilderTests {

Expand Down Expand Up @@ -410,6 +417,13 @@ void matchesWhenIpv6PublicThenReturnsFalse() throws Exception {
assertThat(matcher.matches(InetAddress.getByName("2001:4860:4860::8888"))).isFalse();
}

@ParameterizedTest
@ValueSource(strings = { "0.0.0.0", "::" })
void matchesWhenWildcardAddressThenReturnsFalse(String address) throws Exception {
InetAddressMatcher matcher = InetAddressMatchers.matchExternal().build();
assertThat(matcher.matches(InetAddress.getByName(address))).isFalse();
}

}

@Nested
Expand Down
Loading