diff --git a/jdi-dark-tests/pom.xml b/jdi-dark-tests/pom.xml index 9e68665e..f520673e 100644 --- a/jdi-dark-tests/pom.xml +++ b/jdi-dark-tests/pom.xml @@ -53,7 +53,12 @@ org.eclipse.jetty jetty-webapp - 9.4.26.v20200117 + 9.4.31.v20200723 + + + org.eclipse.jetty.websocket + javax-websocket-server-impl + 9.4.31.v20200723 io.rest-assured.examples diff --git a/jdi-dark-tests/src/test/java/com/epam/jdi/httptests/support/WithJettyWebSockets.java b/jdi-dark-tests/src/test/java/com/epam/jdi/httptests/support/WithJettyWebSockets.java new file mode 100644 index 00000000..85a513cc --- /dev/null +++ b/jdi-dark-tests/src/test/java/com/epam/jdi/httptests/support/WithJettyWebSockets.java @@ -0,0 +1,71 @@ +package com.epam.jdi.httptests.support; + +import com.epam.jdi.services.websockets.WSEchoServer; +import com.epam.jdi.services.websockets.WSItemServer; +import org.eclipse.jetty.http.HttpVersion; +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.SslConnectionFactory; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; +import org.glassfish.tyrus.client.SslContextConfigurator; +import org.glassfish.tyrus.client.SslEngineConfigurator; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; + +import javax.websocket.server.ServerContainer; + +public abstract class WithJettyWebSockets { + private Server server; + protected final String host = "ws://localhost:8081"; + protected final String sslHost = "wss://localhost:8443"; + + @BeforeClass + public void init() throws Exception { + server = new Server(); + + HttpConfiguration httpsConfig = new HttpConfiguration(); + httpsConfig.setSecureScheme("https"); + httpsConfig.setSecurePort(8443); + + String file = "src/test/resources/jetty_localhost_server.jks"; + SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); + sslContextFactory.setKeyStorePath(file); + sslContextFactory.setKeyStorePassword("test1234"); + + ServerConnector https = new ServerConnector(server, + new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), + new HttpConnectionFactory(httpsConfig)); + https.setPort(8443); + + ServerConnector http = new ServerConnector(server); + http.setPort(8081); + + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/"); + ServerContainer wscontainer = WebSocketServerContainerInitializer.initialize(context); + wscontainer.addEndpoint(WSEchoServer.class); + wscontainer.addEndpoint(WSItemServer.class); + + server.setHandler(context); + server.setConnectors(new Connector[]{https, http}); + server.start(); + } + + @AfterClass + public void tearDown() throws Exception { + server.stop(); + } + + protected SslEngineConfigurator getClientSslConfig() { + SslContextConfigurator config = new SslContextConfigurator(); + config.setTrustStoreFile("src/test/resources/jetty_localhost_client.jks"); + config.setTrustStorePassword("test1234"); + SslEngineConfigurator sslEngineConfigurator = new SslEngineConfigurator(config, true, false, false); + return sslEngineConfigurator.setHostVerificationEnabled(false); + } +} diff --git a/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketClientTests.java b/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketClientTests.java index 72a1f944..764ed896 100644 --- a/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketClientTests.java +++ b/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketClientTests.java @@ -3,11 +3,9 @@ import com.epam.jdi.dto.Item; import com.epam.jdi.http.WebSocketJsonClient; import com.epam.jdi.http.WebSocketTextClient; +import com.epam.jdi.httptests.support.WithJettyWebSockets; import com.epam.jdi.services.websockets.*; import com.google.gson.*; -import org.glassfish.tyrus.server.Server; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import javax.websocket.DeploymentException; @@ -18,14 +16,7 @@ import static org.testng.Assert.*; -public class WebSocketClientTests { - private Server server; - - @BeforeClass - public void init() throws DeploymentException { - server = new Server(WSEchoServer.class, WSItemServer.class); - server.start(); - } +public class WebSocketClientTests extends WithJettyWebSockets { @Test public void textMessageTest() @@ -34,7 +25,7 @@ public void textMessageTest() String message = "Simple text test message"; WebSocketTextClient client = new WebSocketTextClient(); - client.connect("ws://localhost:8025/echo-ws"); + client.connect(host + "/echo-ws"); client.sendPlainText(message); assertTrue(client.waitNewMessage(100)); assertEquals( @@ -58,7 +49,7 @@ public void sendObjectGenericTest() Item message = new Item(2, "sofa"); WSItemClient client = new WSItemClient(); - client.connect("ws://localhost:8025/item-ws"); + client.connect(host + "/item-ws"); client.sendMessage(message); assertTrue(client.waitNewMessage(100)); assertEquals( @@ -75,7 +66,7 @@ public void binaryTextMessageTest() String message = "Simple text test message"; WebSocketTextClient client = new WebSocketTextClient(); - client.connect("ws://localhost:8025/echo-ws"); + client.connect(host + "/echo-ws"); client.sendBinary(ByteBuffer.wrap(message.getBytes())); assertTrue(client.waitNewMessage(100)); @@ -93,7 +84,7 @@ public void jsonOverTextTest() String message = "{\"text\":\"Simple text test message\"}"; WebSocketJsonClient client = new WebSocketJsonClient(); - client.connect("ws://localhost:8025/echo-ws"); + client.connect(host + "/echo-ws"); client.sendPlainText(message); assertEquals( client.waitAndGetNewMessage(100).toString(), message, @@ -116,7 +107,7 @@ public void jsonClientEchoTest() Gson gson = new Gson(); String message = gson.toJson(item); - client.connect("ws://localhost:8025/echo-ws"); + client.connect(host + "/echo-ws"); client.sendPlainText(message); assertEquals( client.waitAndGetNewMessage(100).toString(), message, @@ -134,9 +125,4 @@ public void jsonClientEchoTest() ); client.close(); } - - @AfterClass - public void tearDown() { - server.stop(); - } } diff --git a/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketSslTests.java b/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketSslTests.java new file mode 100644 index 00000000..bba6f97a --- /dev/null +++ b/jdi-dark-tests/src/test/java/com/epam/jdi/websockettests/WebSocketSslTests.java @@ -0,0 +1,56 @@ +package com.epam.jdi.websockettests; + +import com.epam.jdi.http.WebSocketTextClient; +import com.epam.jdi.httptests.support.WithJettyWebSockets; +import org.glassfish.tyrus.client.ClientProperties; +import org.testng.annotations.Test; + +import javax.websocket.DeploymentException; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Collections; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +public class WebSocketSslTests extends WithJettyWebSockets { + + @Test + public void sslOutsideConfigTest() + throws DeploymentException, IOException, URISyntaxException, InterruptedException + { + String message = "Simple text test message"; + WebSocketTextClient client = new WebSocketTextClient(); + + client.setClientProperties(Collections.singletonMap(ClientProperties.SSL_ENGINE_CONFIGURATOR, getClientSslConfig())); + client.connect(sslHost + "/echo-ws"); + client.sendPlainText(message); + assertTrue(client.waitNewMessage(100)); + assertEquals( + client.getLastMessage(), message, + "Unexpected response from server" + ); + client.close(); + } + + @Test + public void sslInnerConfigTest() + throws DeploymentException, IOException, URISyntaxException, InterruptedException + { + String message = "Simple text test message"; + WebSocketTextClient client = new WebSocketTextClient(); + + client.setClientSslConfig( + "src/test/resources/jetty_localhost_client.jks", + "test1234", true, false, false + ); + client.connect(sslHost + "/echo-ws"); + client.sendPlainText(message); + assertTrue(client.waitNewMessage(100)); + assertEquals( + client.getLastMessage(), message, + "Unexpected response from server" + ); + client.close(); + } +} diff --git a/jdi-dark-tests/src/test/resources/general.xml b/jdi-dark-tests/src/test/resources/general.xml index fa8bdaa8..52ddaab9 100644 --- a/jdi-dark-tests/src/test/resources/general.xml +++ b/jdi-dark-tests/src/test/resources/general.xml @@ -21,7 +21,7 @@ - + diff --git a/jdi-dark-ws/src/main/java/com/epam/jdi/http/WebSocketGenericEndpoint.java b/jdi-dark-ws/src/main/java/com/epam/jdi/http/WebSocketGenericEndpoint.java index 49e6b330..9129a4d0 100644 --- a/jdi-dark-ws/src/main/java/com/epam/jdi/http/WebSocketGenericEndpoint.java +++ b/jdi-dark-ws/src/main/java/com/epam/jdi/http/WebSocketGenericEndpoint.java @@ -2,6 +2,9 @@ import com.epam.http.logger.ILogger; import org.glassfish.tyrus.client.ClientManager; +import org.glassfish.tyrus.client.ClientProperties; +import org.glassfish.tyrus.client.SslContextConfigurator; +import org.glassfish.tyrus.client.SslEngineConfigurator; import javax.websocket.*; import java.io.IOException; @@ -42,6 +45,13 @@ public void connect(String path) throws IOException, DeploymentException, URISyn connect(new URI(path)); } + public void close() throws IOException { + logger.info("Close connection"); + session.close(new CloseReason( + CloseReason.CloseCodes.GOING_AWAY, "Going away." + )); + } + public void setClientProperties(Map properties) { for(Map.Entry entry: properties.entrySet()) { client.getProperties().put(entry.getKey(), entry.getValue()); @@ -54,11 +64,21 @@ public void setClientProperties(Properties properties) { } } - public void close() throws IOException { - logger.info("Close connection"); - session.close(new CloseReason( - CloseReason.CloseCodes.GOING_AWAY, "Going away." - )); + public void setClientSslConfig( + String trustStorePath, + String trustStorePassword, + Boolean clientMode, + Boolean needClientAuth, + Boolean wantClientAuth + ) { + SslContextConfigurator config = new SslContextConfigurator(); + config.setTrustStoreFile(trustStorePath); + config.setTrustStorePassword(trustStorePassword); + SslEngineConfigurator sslEngineConfigurator = new SslEngineConfigurator( + config, clientMode, needClientAuth, wantClientAuth + ); + sslEngineConfigurator.setHostVerificationEnabled(false); + client.getProperties().put(ClientProperties.SSL_ENGINE_CONFIGURATOR, sslEngineConfigurator); } @OnOpen