From 2d6cbc63cb8f0dc34e3556e0c5a629129d2e7d91 Mon Sep 17 00:00:00 2001 From: Michael Osipov Date: Sat, 9 Dec 2023 00:18:29 +0100 Subject: [PATCH] [SUREFIRE-2220] SurefireForkChannel#getForkNodeConnectionString() returns invalid URI string if localHost resolves to IPv6 address This closes #697 --- .../surefire/extensions/SurefireForkChannel.java | 16 +++++++++++++++- .../surefire/extensions/ForkChannelTest.java | 16 +++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java index 99e6e0304b..c58d3ee16d 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java @@ -25,6 +25,8 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketOption; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousServerSocketChannel; @@ -112,7 +114,19 @@ public void tryConnectToClient() { @Override public String getForkNodeConnectionString() { - return "tcp://" + localHost + ":" + localPort + (isBlank(sessionId) ? "" : "?sessionId=" + sessionId); + try { + URI uri = new URI( + "tcp", + null, + localHost, + localPort, + null, + isBlank(sessionId) ? null : "sessionId=" + sessionId, + null); + return uri.toASCIIString(); + } catch (URISyntaxException e) { + throw new IllegalStateException(e); + } } @Override diff --git a/maven-surefire-common/src/test/java/org/apache/maven/surefire/extensions/ForkChannelTest.java b/maven-surefire-common/src/test/java/org/apache/maven/surefire/extensions/ForkChannelTest.java index 966bcb2397..e1c168b788 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/surefire/extensions/ForkChannelTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/surefire/extensions/ForkChannelTest.java @@ -121,14 +121,16 @@ public Object getConsoleLock() { assertThat(channel.getCountdownCloseablePermits()).isEqualTo(3); String localHost = InetAddress.getLoopbackAddress().getHostAddress(); - assertThat(channel.getForkNodeConnectionString()) - .startsWith("tcp://" + localHost + ":") - .isNotEqualTo("tcp://" + localHost + ":") - .endsWith("?sessionId=" + sessionId); - - URI uri = new URI(channel.getForkNodeConnectionString()); - + String connectionString = channel.getForkNodeConnectionString(); + URI uri = new URI(connectionString); + assertThat(uri.getScheme()).isEqualTo("tcp"); + String uriHost = uri.getHost(); + if (uriHost.startsWith("[") && uriHost.endsWith("]")) { + uriHost = uriHost.substring(1, uriHost.length() - 1); + } + assertThat(uriHost).isEqualTo(localHost); assertThat(uri.getPort()).isPositive(); + assertThat(uri.getQuery()).isEqualTo("sessionId=" + sessionId); final TestLessInputStreamBuilder builder = new TestLessInputStreamBuilder(); TestLessInputStream commandReader = builder.build();