Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket tests with ssl connection #444

Merged
merged 11 commits into from
Sep 25, 2020
Merged
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
7 changes: 6 additions & 1 deletion jdi-dark-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.26.v20200117</version>
<version>9.4.31.v20200723</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
<version>9.4.31.v20200723</version>
</dependency>
<dependency>
<groupId>io.rest-assured.examples</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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));
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -134,9 +125,4 @@ public void jsonClientEchoTest()
);
client.close();
}

@AfterClass
public void tearDown() {
server.stop();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion jdi-dark-tests/src/test/resources/general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<class name="com.epam.jdi.httptests.performance.PerformanceTests"/>
</classes>
</test>
<test name="Websoket tests">
<test name="Websocket tests">
<packages>
<package name="com.epam.jdi.websockettests"/>
</packages>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> properties) {
for(Map.Entry<String, Object> entry: properties.entrySet()) {
client.getProperties().put(entry.getKey(), entry.getValue());
Expand All @@ -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
Expand Down