Skip to content

Commit

Permalink
Additional pattern for detecting IPv6 addresses formatted as [2001:db…
Browse files Browse the repository at this point in the history
…8::1]
  • Loading branch information
brenuart committed Aug 30, 2022
1 parent f7d231a commit 472b362
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
*/
public class DestinationParser {

private static final Pattern DESTINATION_PATTERN = Pattern.compile("^\\s*(\\S+?)\\s*(:\\s*(\\S+)\\s*)?$");
private static final Pattern DESTINATION_PATTERN = Pattern.compile("^([^:]+)(:(.+))?$");
private static final Pattern DESTINATION_IPV6_PATTERN = Pattern.compile("^\\[(.+)\\](:(.+))?$");

private static final int HOSTNAME_GROUP = 1;
private static final int PORT_GROUP = 3;

Expand Down Expand Up @@ -56,8 +58,8 @@ public static List<InetSocketAddress> parse(String destinations, int defaultPort
/*
* Multiple destinations can be specified on one single line, separated by comma
*/
String[] destinationStrings = (destinations == null ? "" : destinations.trim()).split("\\s*,\\s*");

String[] destinationStrings = (destinations == null ? "" : destinations.replace(" ", "")).split(",");
List<InetSocketAddress> destinationList = new ArrayList<>(destinationStrings.length);

for (String entry: destinationStrings) {
Expand All @@ -70,7 +72,10 @@ public static List<InetSocketAddress> parse(String destinations, int defaultPort
throw new IllegalArgumentException("Invalid destination '" + entry + "': unparseable value (expected format 'host[:port]').");
}

Matcher matcher = DESTINATION_PATTERN.matcher(entry);
Matcher matcher = DESTINATION_IPV6_PATTERN.matcher(entry);
if (!matcher.matches()) {
matcher = DESTINATION_PATTERN.matcher(entry);
}
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid destination '" + entry + "': unparseable value (expected format 'host[:port]').");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public void testParse_Single_NegativePort() {
DestinationParser.parse("localhost:-1", 1);
});
}

@Test
public void testParse_Single_MissingPortAfterColon() {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
DestinationParser.parse(" localhost: ", 1);
});
}

@Test
public void testParse_Single_UndefinedProperty() {
Expand Down Expand Up @@ -105,4 +112,40 @@ public void testParse_Multiple_Empty() {
DestinationParser.parse("localhost:1000, , localhost:1001", 1);
});
}

@Test
public void testIPv4_WithPort() {
List<InetSocketAddress> destinations = DestinationParser.parse(" 192.168.1.1:8080 ", 1);

assertThat(destinations).containsExactly(
InetSocketAddress.createUnresolved("192.168.1.1", 8080)
);
}

@Test
public void testIPv4_DefaultPort() {
List<InetSocketAddress> destinations = DestinationParser.parse(" 192.168.1.1 ", 1);

assertThat(destinations).containsExactly(
InetSocketAddress.createUnresolved("192.168.1.1", 1)
);
}

@Test
public void testIPv6_WithPort() {
List<InetSocketAddress> destinations = DestinationParser.parse(" [2001:db8::1]:8080 ", 1);

assertThat(destinations).containsExactly(
InetSocketAddress.createUnresolved("2001:db8::1", 8080)
);
}

@Test
public void testIPv6_DefaultPort() {
List<InetSocketAddress> destinations = DestinationParser.parse(" [2001:db8::1] ", 1);

assertThat(destinations).containsExactly(
InetSocketAddress.createUnresolved("2001:db8::1", 1)
);
}
}

0 comments on commit 472b362

Please sign in to comment.