You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2020/06/07 22:02:22 UTC
[maven-surefire] branch SUREFIRE-1796 updated (8559694 -> ea40207)
This is an automated email from the ASF dual-hosted git repository.
tibordigana pushed a change to branch SUREFIRE-1796
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git.
discard 8559694 [SUREFIRE-1796] The session of TCP channel should be authenticated
add 5f492fa Using localhost (not local loopback) in SurefireForkChannel.java
add 2e14ba6 updated the road map for 3.0.0-M5 and next versions
new ea40207 [SUREFIRE-1796] The session of TCP channel should be authenticated
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (8559694)
\
N -- N -- N refs/heads/SUREFIRE-1796 (ea40207)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../surefire/extensions/SurefireForkChannel.java | 4 ++--
.../BooterDeserializerStartupConfigurationTest.java | 4 ++--
.../maven/plugin/surefire/extensions/E2ETest.java | 4 ++--
.../maven/surefire/extensions/ForkChannelTest.java | 10 ++++++----
maven-surefire-plugin/src/site/apt/index.apt.vm | 21 ++++++++++++---------
.../surefire/api/util/internal/AsyncSocketTest.java | 6 ++++--
...urefireMasterProcessChannelProcessorFactory.java | 8 ++++----
.../maven/surefire/booter/ForkedBooterMockTest.java | 12 ++++++------
.../spi/MasterProcessChannelProcessorFactory.java | 2 +-
9 files changed, 39 insertions(+), 32 deletions(-)
[maven-surefire] 01/01: [SUREFIRE-1796] The session of TCP channel
should be authenticated
Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tibordigana pushed a commit to branch SUREFIRE-1796
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
commit ea4020701cb2c53545a0ae3a2820938195f4077f
Author: tibordigana <ti...@apache.org>
AuthorDate: Fri Jun 5 00:28:20 2020 +0200
[SUREFIRE-1796] The session of TCP channel should be authenticated
---
.../plugin/surefire/booterclient/ForkStarter.java | 16 ++-
.../surefire/extensions/SurefireForkChannel.java | 32 +++++-
...BooterDeserializerStartupConfigurationTest.java | 4 +-
.../maven/plugin/surefire/extensions/E2ETest.java | 84 ++++++++++++++-
.../maven/surefire/extensions/ForkChannelTest.java | 19 +++-
.../api/util/internal/AsyncSocketTest.java | 4 +-
...refireMasterProcessChannelProcessorFactory.java | 29 +++++
.../surefire/booter/ForkedBooterMockTest.java | 117 ++++++++++++++++++++-
.../surefire/extensions/ForkNodeArguments.java | 4 +
.../spi/MasterProcessChannelProcessorFactory.java | 2 +-
10 files changed, 294 insertions(+), 17 deletions(-)
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
index 3a8cd4f..19b6562 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/ForkStarter.java
@@ -65,6 +65,7 @@ import java.util.Collection;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
+import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -83,6 +84,7 @@ import static java.lang.StrictMath.min;
import static java.lang.System.currentTimeMillis;
import static java.lang.Thread.currentThread;
import static java.util.Collections.addAll;
+import static java.util.UUID.randomUUID;
import static java.util.concurrent.Executors.newScheduledThreadPool;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -573,7 +575,8 @@ public class ForkStarter
File dumpLogDir = replaceForkThreadsInPath( startupReportConfiguration.getReportsDirectory(), forkNumber );
try
{
- forkChannel = forkNodeFactory.createForkChannel( new ForkedNodeArg( forkNumber, dumpLogDir ) );
+ ForkNodeArguments forkNodeArguments = new ForkedNodeArg( forkNumber, dumpLogDir, randomUUID() );
+ forkChannel = forkNodeFactory.createForkChannel( forkNodeArguments );
closer.addCloseable( forkChannel );
tempDir = forkConfiguration.getTempDirectory().getCanonicalPath();
BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration );
@@ -881,11 +884,20 @@ public class ForkStarter
{
private final int forkChannelId;
private final File dumpLogDir;
+ private final UUID sessionId;
- ForkedNodeArg( int forkChannelId, File dumpLogDir )
+ ForkedNodeArg( int forkChannelId, File dumpLogDir, UUID sessionId )
{
this.forkChannelId = forkChannelId;
this.dumpLogDir = dumpLogDir;
+ this.sessionId = sessionId;
+ }
+
+ @Nonnull
+ @Override
+ public UUID getSessionId()
+ {
+ return sessionId;
}
@Override
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 cf522e8..590a65c 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
@@ -36,10 +36,12 @@ import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketOption;
+import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
+import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -49,6 +51,7 @@ import static java.net.StandardSocketOptions.SO_REUSEADDR;
import static java.net.StandardSocketOptions.TCP_NODELAY;
import static java.nio.channels.AsynchronousChannelGroup.withThreadPool;
import static java.nio.channels.AsynchronousServerSocketChannel.open;
+import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newInputStream;
@@ -71,11 +74,14 @@ import static org.apache.maven.surefire.api.util.internal.DaemonThreadFactory.ne
*/
final class SurefireForkChannel extends ForkChannel
{
+ private static final int UUID_STRING_LENGTH = 36;
+
private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool( newDaemonThreadFactory() );
private final AsynchronousServerSocketChannel server;
private final String localHost;
private final int localPort;
+ private final UUID sessionId;
private volatile AsynchronousSocketChannel worker;
private volatile LineConsumerThread out;
@@ -89,6 +95,7 @@ final class SurefireForkChannel extends ForkChannel
InetSocketAddress localAddress = (InetSocketAddress) server.getLocalAddress();
localHost = localAddress.getHostString();
localPort = localAddress.getPort();
+ sessionId = arguments.getSessionId();
}
@Override
@@ -102,6 +109,10 @@ final class SurefireForkChannel extends ForkChannel
try
{
worker = server.accept().get();
+ if ( sessionId != null )
+ {
+ authenticate();
+ }
}
catch ( InterruptedException e )
{
@@ -113,6 +124,25 @@ final class SurefireForkChannel extends ForkChannel
}
}
+ private void authenticate() throws InterruptedException, ExecutionException, IOException
+ {
+ ByteBuffer hash = ByteBuffer.allocate( UUID_STRING_LENGTH );
+ int read;
+ do
+ {
+ read = worker.read( hash ).get();
+ } while ( read != -1 && hash.hasRemaining() );
+ if ( read == -1 )
+ {
+ throw new IOException( "Channel closed while authenticating the client." );
+ }
+ hash.flip();
+ if ( !UUID.fromString( new String( hash.array(), US_ASCII ) ).equals( sessionId ) )
+ {
+ throw new IOException( "authentication failed" );
+ }
+ }
+
@SafeVarargs
private final void setTrueOptions( SocketOption<Boolean>... options )
throws IOException
@@ -129,7 +159,7 @@ final class SurefireForkChannel extends ForkChannel
@Override
public String getForkNodeConnectionString()
{
- return "tcp://" + localHost + ":" + localPort;
+ return "tcp://" + localHost + ":" + localPort + ( sessionId == null ? "" : "?sessionId=" + sessionId );
}
@Override
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/BooterDeserializerStartupConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/BooterDeserializerStartupConfigurationTest.java
index 6fe2ddc..669e73c 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/BooterDeserializerStartupConfigurationTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/BooterDeserializerStartupConfigurationTest.java
@@ -179,10 +179,10 @@ public class BooterDeserializerStartupConfigurationTest
BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration );
String aTest = "aTest";
File propsTest = booterSerializer.serialize( props, getProviderConfiguration(), startupConfiguration, aTest,
- false, null, 1, "tcp://127.0.0.1:63003" );
+ false, null, 1, "tcp://localhost:63003" );
BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
assertNull( booterDeserializer.getPluginPid() );
- assertEquals( "tcp://127.0.0.1:63003", booterDeserializer.getConnectionString() );
+ assertEquals( "tcp://localhost:63003", booterDeserializer.getConnectionString() );
return booterDeserializer.getStartupConfiguration();
}
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/E2ETest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/E2ETest.java
index 30517f3..a6c744d 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/E2ETest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/E2ETest.java
@@ -21,18 +21,23 @@ package org.apache.maven.plugin.surefire.extensions;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.booter.MasterProcessChannelEncoder;
-import org.apache.maven.surefire.booter.spi.SurefireMasterProcessChannelProcessorFactory;
import org.apache.maven.surefire.api.event.Event;
+import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
+import org.apache.maven.surefire.booter.spi.SurefireMasterProcessChannelProcessorFactory;
import org.apache.maven.surefire.extensions.EventHandler;
import org.apache.maven.surefire.extensions.ForkNodeArguments;
import org.apache.maven.surefire.extensions.util.CountdownCloseable;
-import org.apache.maven.surefire.api.report.ConsoleOutputReceiver;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import javax.annotation.Nonnull;
import java.io.Closeable;
+import java.io.IOException;
+import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
+import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -52,8 +57,11 @@ public class E2ETest
private static final String LONG_STRING =
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
+ @Rule
+ public final ExpectedException e = ExpectedException.none();
+
@Test
- public void test() throws Exception
+ public void endToEndTest() throws Exception
{
ConsoleLogger logger = mock( ConsoleLogger.class );
ForkNodeArguments arguments = mock( ForkNodeArguments.class );
@@ -184,4 +192,74 @@ public class E2ETest
.isPositive()
.isLessThanOrEqualTo( 6_000L );
}
+
+ @Test( timeout = 10_000L )
+ public void shouldAuthenticateClient() throws Exception
+ {
+ ForkNodeArguments forkNodeArguments = mock( ForkNodeArguments.class );
+ when( forkNodeArguments.getSessionId() ).thenReturn( UUID.randomUUID() );
+
+ try ( SurefireForkChannel server = new SurefireForkChannel( forkNodeArguments ) )
+ {
+ Thread t = new Thread()
+ {
+ @Override
+ public void run()
+ {
+ SurefireMasterProcessChannelProcessorFactory client
+ = new SurefireMasterProcessChannelProcessorFactory();
+ try
+ {
+ client.connect( server.getForkNodeConnectionString() );
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ throw new RuntimeException( e );
+ }
+ }
+ };
+ t.setDaemon( true );
+ t.start();
+ server.connectToClient();
+ }
+ }
+
+ @Test( timeout = 10_000L )
+ public void shouldNotAuthenticateClient() throws Exception
+ {
+ ForkNodeArguments forkNodeArguments = mock( ForkNodeArguments.class );
+ when( forkNodeArguments.getSessionId() ).thenReturn( UUID.randomUUID() );
+
+ try ( SurefireForkChannel server = new SurefireForkChannel( forkNodeArguments ) )
+ {
+ Thread t = new Thread()
+ {
+ @Override
+ public void run()
+ {
+ SurefireMasterProcessChannelProcessorFactory client
+ = new SurefireMasterProcessChannelProcessorFactory();
+ try
+ {
+ URI connectionString = new URI( server.getForkNodeConnectionString() );
+ client.connect( "tcp://" + connectionString.getHost() + ":" + connectionString.getPort()
+ + "?sessionId=6ba7b812-9dad-11d1-80b4-00c04fd430c8" );
+ }
+ catch ( Exception e )
+ {
+ e.printStackTrace();
+ throw new RuntimeException( e );
+ }
+ }
+ };
+ t.setDaemon( true );
+ t.start();
+
+ e.expect( IOException.class );
+ e.expectMessage( "authentication failed" );
+
+ server.connectToClient();
+ }
+ }
}
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 01bb8f3..e39480a 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
@@ -39,6 +39,7 @@ import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.Queue;
+import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -62,8 +63,16 @@ public class ForkChannelTest
@Test( timeout = TESTCASE_TIMEOUT )
public void shouldRequestReplyMessagesViaTCP() throws Exception
{
+ final UUID sessionId = UUID.randomUUID();
ForkNodeArguments forkNodeArguments = new ForkNodeArguments()
{
+ @Nonnull
+ @Override
+ public UUID getSessionId()
+ {
+ return sessionId;
+ }
+
@Override
public int getForkChannelId()
{
@@ -102,7 +111,8 @@ public class ForkChannelTest
String localHost = InetAddress.getLocalHost().getHostAddress();
assertThat( channel.getForkNodeConnectionString() )
.startsWith( "tcp://" + localHost + ":" )
- .isNotEqualTo( "tcp://" + localHost + ":" );
+ .isNotEqualTo( "tcp://" + localHost + ":" )
+ .endsWith( "?sessionId=" + sessionId );
URI uri = new URI( channel.getForkNodeConnectionString() );
@@ -123,7 +133,7 @@ public class ForkChannelTest
CountdownCloseable cc = new CountdownCloseable( closeable, 2 );
Consumer consumer = new Consumer();
- Client client = new Client( uri.getPort() );
+ Client client = new Client( uri.getPort(), sessionId.toString() );
client.start();
channel.connectToClient();
@@ -164,10 +174,12 @@ public class ForkChannelTest
private final class Client extends Thread
{
private final int port;
+ private final String sessionId;
- private Client( int port )
+ private Client( int port, String sessionId )
{
this.port = port;
+ this.sessionId = sessionId;
}
@Override
@@ -175,6 +187,7 @@ public class ForkChannelTest
{
try ( Socket socket = new Socket( InetAddress.getLocalHost().getHostAddress(), port ) )
{
+ socket.getOutputStream().write( sessionId.getBytes( US_ASCII ) );
byte[] data = new byte[128];
int readLength = socket.getInputStream().read( data );
String token = new String( data, 0, readLength, US_ASCII );
diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
index 004d743..162f38f 100644
--- a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
+++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
@@ -26,6 +26,7 @@ import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketOption;
@@ -80,7 +81,8 @@ public class AsyncSocketTest
AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool( executorService );
AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open( group );
setTrueOptions( server, SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
- server.bind( null, 1 );
+ InetAddress ip = Inet4Address.getLocalHost();
+ server.bind( new InetSocketAddress( ip, 0 ), 1 );
address = (InetSocketAddress) server.getLocalAddress();
System.gc();
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/SurefireMasterProcessChannelProcessorFactory.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/SurefireMasterProcessChannelProcessorFactory.java
index f27aa0c..6ce3dfb 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/SurefireMasterProcessChannelProcessorFactory.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/SurefireMasterProcessChannelProcessorFactory.java
@@ -29,7 +29,10 @@ import java.net.MalformedURLException;
import java.net.SocketOption;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
+import java.util.StringTokenizer;
+import java.util.UUID;
import java.util.concurrent.ExecutionException;
import static java.net.StandardSocketOptions.SO_KEEPALIVE;
@@ -37,6 +40,7 @@ import static java.net.StandardSocketOptions.SO_REUSEADDR;
import static java.net.StandardSocketOptions.TCP_NODELAY;
import static java.nio.channels.AsynchronousChannelGroup.withFixedThreadPool;
import static java.nio.channels.AsynchronousSocketChannel.open;
+import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
import static org.apache.maven.surefire.api.util.internal.Channels.newInputStream;
import static org.apache.maven.surefire.api.util.internal.Channels.newOutputStream;
@@ -75,6 +79,12 @@ public class SurefireMasterProcessChannelProcessorFactory
clientSocketChannel = open( withFixedThreadPool( 2, newDaemonThreadFactory() ) );
setTrueOptions( SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
clientSocketChannel.connect( hostAddress ).get();
+ UUID sessionId = parseAuth( uri );
+ if ( sessionId != null )
+ {
+ ByteBuffer buff = ByteBuffer.wrap( sessionId.toString().getBytes( US_ASCII ) );
+ clientSocketChannel.write( buff );
+ }
}
catch ( URISyntaxException | InterruptedException e )
{
@@ -119,4 +129,23 @@ public class SurefireMasterProcessChannelProcessorFactory
}
}
}
+
+ private UUID parseAuth( URI uri )
+ {
+ String query = uri.getQuery();
+ if ( query == null )
+ {
+ return null;
+ }
+ for ( StringTokenizer tokenizer = new StringTokenizer( query, "&" ); tokenizer.hasMoreTokens(); )
+ {
+ String token = tokenizer.nextToken();
+ int delimiter = token.indexOf( '=' );
+ if ( delimiter != -1 && "sessionId".equals( token.substring( 0, delimiter ) ) )
+ {
+ return UUID.fromString( token.substring( delimiter + 1 ) );
+ }
+ }
+ return null;
+ }
}
diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ForkedBooterMockTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ForkedBooterMockTest.java
index 11a3df2..c9786ef 100644
--- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ForkedBooterMockTest.java
+++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ForkedBooterMockTest.java
@@ -21,11 +21,11 @@ package org.apache.maven.surefire.booter;
import org.apache.maven.surefire.api.booter.MasterProcessChannelDecoder;
import org.apache.maven.surefire.api.booter.MasterProcessChannelEncoder;
+import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.booter.spi.LegacyMasterProcessChannelDecoder;
import org.apache.maven.surefire.booter.spi.LegacyMasterProcessChannelEncoder;
import org.apache.maven.surefire.booter.spi.LegacyMasterProcessChannelProcessorFactory;
import org.apache.maven.surefire.booter.spi.SurefireMasterProcessChannelProcessorFactory;
-import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.shared.utils.cli.ShutdownHookUtils;
import org.apache.maven.surefire.spi.MasterProcessChannelProcessorFactory;
import org.junit.Rule;
@@ -45,11 +45,19 @@ import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
+import java.net.URI;
+import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.FutureTask;
import static java.net.StandardSocketOptions.SO_KEEPALIVE;
import static java.net.StandardSocketOptions.SO_REUSEADDR;
import static java.net.StandardSocketOptions.TCP_NODELAY;
+import static java.nio.charset.StandardCharsets.US_ASCII;
+import static java.util.concurrent.TimeUnit.SECONDS;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.ArgumentMatchers.any;
@@ -83,6 +91,8 @@ import static org.powermock.reflect.Whitebox.setInternalState;
@PowerMockIgnore( { "org.jacoco.agent.rt.*", "com.vladium.emma.rt.*" } )
public class ForkedBooterMockTest
{
+ private static final int UUID_STRING_LENGTH = 36;
+
@Rule
public final ErrorCollector errorCollector = new ErrorCollector();
@@ -319,12 +329,12 @@ public class ForkedBooterMockTest
int serverPort = ( (InetSocketAddress) server.getLocalAddress() ).getPort();
try ( MasterProcessChannelProcessorFactory factory =
- invokeMethod( ForkedBooter.class, "lookupDecoderFactory", "tcp://127.0.0.1:" + serverPort ) )
+ invokeMethod( ForkedBooter.class, "lookupDecoderFactory", "tcp://localhost:" + serverPort ) )
{
assertThat( factory )
.isInstanceOf( SurefireMasterProcessChannelProcessorFactory.class );
- assertThat( factory.canUse( "tcp://127.0.0.1:" + serverPort ) )
+ assertThat( factory.canUse( "tcp://localhost:" + serverPort ) )
.isTrue();
assertThat( factory.canUse( "-- whatever --" ) )
@@ -350,7 +360,7 @@ public class ForkedBooterMockTest
}
} );
- factory.connect( "tcp://127.0.0.1:" + serverPort );
+ factory.connect( "tcp://localhost:" + serverPort );
MasterProcessChannelDecoder decoder = factory.createDecoder();
assertThat( decoder )
@@ -363,6 +373,83 @@ public class ForkedBooterMockTest
}
@Test
+ public void shouldAuthenticate() throws Exception
+ {
+ mockStatic( ForkedBooter.class );
+
+ doCallRealMethod()
+ .when( ForkedBooter.class, "lookupDecoderFactory", anyString() );
+
+ try ( final ServerSocketChannel server = ServerSocketChannel.open() )
+ {
+ if ( server.supportedOptions().contains( SO_REUSEADDR ) )
+ {
+ server.setOption( SO_REUSEADDR, true );
+ }
+
+ if ( server.supportedOptions().contains( TCP_NODELAY ) )
+ {
+ server.setOption( TCP_NODELAY, true );
+ }
+
+ if ( server.supportedOptions().contains( SO_KEEPALIVE ) )
+ {
+ server.setOption( SO_KEEPALIVE, true );
+ }
+
+ server.bind( new InetSocketAddress( 0 ) );
+ int serverPort = ( (InetSocketAddress) server.getLocalAddress() ).getPort();
+ final UUID uuid = UUID.randomUUID();
+ String url = "tcp://localhost:" + serverPort + "?sessionId=" + uuid;
+ try ( final MasterProcessChannelProcessorFactory factory =
+ invokeMethod( ForkedBooter.class, "lookupDecoderFactory", url ) )
+ {
+ assertThat( factory )
+ .isInstanceOf( SurefireMasterProcessChannelProcessorFactory.class );
+
+ FutureTask<Boolean> task = new FutureTask<>( new Callable<Boolean>()
+ {
+ @Override
+ public Boolean call()
+ {
+ try
+ {
+ SocketChannel channel = server.accept();
+ ByteBuffer bb = ByteBuffer.allocate( UUID_STRING_LENGTH );
+ int read = channel.read( bb );
+ assertThat( read )
+ .isEqualTo( UUID_STRING_LENGTH );
+ bb.flip();
+ assertThat( new String( bb.array(), US_ASCII ) )
+ .isEqualTo( uuid.toString() );
+ return true;
+ }
+ catch ( IOException e )
+ {
+ return false;
+ }
+ }
+ } );
+
+ Thread t = new Thread( task );
+ t.setDaemon( true );
+ t.start();
+
+ factory.connect( url );
+
+ try
+ {
+ task.get( 10, SECONDS );
+ }
+ finally
+ {
+ factory.close();
+ }
+ }
+ }
+ }
+
+ @Test
public void testFlushEventChannelOnExit() throws Exception
{
mockStatic( ShutdownHookUtils.class );
@@ -385,4 +472,26 @@ public class ForkedBooterMockTest
} ).when( ShutdownHookUtils.class, "addShutDownHook", any( Thread.class ) );
invokeMethod( booter, "flushEventChannelOnExit" );
}
+
+ @Test
+ public void shouldParseUUID() throws Exception
+ {
+ SurefireMasterProcessChannelProcessorFactory factory = new SurefireMasterProcessChannelProcessorFactory();
+ UUID uuid = UUID.randomUUID();
+ URI uri = new URI( "tcp://localhost:12345?sessionId=" + uuid );
+ UUID parsed = invokeMethod( factory, "parseAuth", uri );
+ assertThat( parsed )
+ .isEqualTo( uuid );
+ }
+
+ @Test
+ public void shouldNotParseUUID() throws Exception
+ {
+ SurefireMasterProcessChannelProcessorFactory factory = new SurefireMasterProcessChannelProcessorFactory();
+ UUID uuid = UUID.randomUUID();
+ URI uri = new URI( "tcp://localhost:12345?xxx=" + uuid );
+ UUID parsed = invokeMethod( factory, "parseAuth", uri );
+ assertThat( parsed )
+ .isNull();
+ }
}
diff --git a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/ForkNodeArguments.java b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/ForkNodeArguments.java
index bdd0b2e..9397c38 100644
--- a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/ForkNodeArguments.java
+++ b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/ForkNodeArguments.java
@@ -24,6 +24,7 @@ import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.io.File;
+import java.util.UUID;
/**
* The properties related to the current JVM.
@@ -33,6 +34,9 @@ import java.io.File;
*/
public interface ForkNodeArguments
{
+ @Nonnull
+ UUID getSessionId();
+
/**
* The index of the forked JVM, from 1 to N.
*
diff --git a/surefire-extensions-spi/src/main/java/org/apache/maven/surefire/spi/MasterProcessChannelProcessorFactory.java b/surefire-extensions-spi/src/main/java/org/apache/maven/surefire/spi/MasterProcessChannelProcessorFactory.java
index 93b61e5..8da6c11 100644
--- a/surefire-extensions-spi/src/main/java/org/apache/maven/surefire/spi/MasterProcessChannelProcessorFactory.java
+++ b/surefire-extensions-spi/src/main/java/org/apache/maven/surefire/spi/MasterProcessChannelProcessorFactory.java
@@ -41,7 +41,7 @@ public interface MasterProcessChannelProcessorFactory extends Closeable
/**
* Open a new connection.
*
- * @param channelConfig e.g. "pipe://3" or "tcp://127.0.0.1:65035"
+ * @param channelConfig e.g. "pipe://3" or "tcp://localhost:65035"
* @throws IOException if cannot connect
*/
void connect( String channelConfig ) throws IOException;