@Test // Server specifies cert that the client trusts (not trust all) public void testTLSClientTrustServerCertWithSNI() throws Exception { testTLS(Cert.NONE, Trust.SNI_JKS_HOST2, Cert.SNI_JKS, Trust.NONE, false, false, false, false, true, true, true, true, new String[0], client -> client.websocketStream(4043, "host2.com", "/")); }
@Test public void testReportProtocolViolationOnClient() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { NetSocket sock = getUpgradedNetSocket(req, "/some/path"); // Let's write an invalid frame Buffer buff = Buffer.buffer(); buff.appendByte((byte)(0x8)).appendByte((byte)0); // Violates protocol with V13 (final control frame) sock.write(buff); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", null, WebsocketVersion.V13). handler(ws -> { AtomicReference<Throwable> failure = new AtomicReference<>(); ws.closeHandler(v -> { assertNotNull(failure.get()); testComplete(); }); ws.exceptionHandler(failure::set); }); }); await(); }
private void testInvalidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String subProtocol = "myprotocol"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols("invalid")).websocketHandler(ws -> { }); server.listen(onSuccess(ar -> { client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol). exceptionHandler(t -> { // Should fail testComplete(); }). handler(ws -> { }); })); await(); }
private void testCloseCallHandlers(boolean local) { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { if (!local) { ws.close(); } }); AtomicInteger doneCount = new AtomicInteger(); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null). endHandler(done -> doneCount.incrementAndGet()). handler(ws -> { assertEquals(0, doneCount.get()); boolean[] closed = new boolean[1]; ws.closeHandler(v -> { closed[0] = true; assertEquals(1, doneCount.get()); testComplete(); }); if (local) { vertx.runOnContext(v -> { ws.close(); }); } }); }); await(); }
server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null). handler(ws -> { Buffer buff = Buffer.buffer();
@Test // Let's manually handle the websocket handshake and write a frame to the client public void testHandleWSManually() throws Exception { String path = "/some/path"; String message = "here is some text data"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { NetSocket sock = getUpgradedNetSocket(req, path); // Let's write a Text frame raw Buffer buff = Buffer.buffer(); buff.appendByte((byte)129); // Text frame buff.appendByte((byte)message.length()); buff.appendString(message); sock.write(buff); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path). handler(ws -> { ws.handler(buff -> { assertEquals(message, buff.toString("UTF-8")); testComplete(); }); }); }); await(); }
@Test // Client trusts all server certs public void testClearClientRequestOptionsSetSSL() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, true, false, new String[0], client -> client.websocketStream(options)); }
@Test // Client trusts all server certs public void testSSLClientRequestOptionsSetSSL() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, true, false, new String[0], client -> client.websocketStream(options)); }
@Test // Client trusts all server certs public void testClearClientRequestOptionsSetClear() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, false, false, new String[0], client -> client.websocketStream(options)); }
@Test // Client trusts all server certs public void testSSLClientRequestOptionsSetClear() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, false, false, new String[0], client -> client.websocketStream(options)); }
@Test public void testRaceConditionWithWebsocketClientWorker2() throws Exception { int size = getOptions().getWorkerPoolSize() - 4; List<Context> workers = createWorkers(size + 1); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)); server.websocketHandler(ws -> { ws.write(Buffer.buffer("hello")); }); server.listen(ar -> { assertTrue(ar.succeeded()); workers.get(0).runOnContext(v -> { ReadStream<WebSocket> webSocketStream = client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/"); webSocketStream.handler(ws -> { ws.handler(buf -> { assertEquals("hello", buf.toString()); testComplete(); }); }); }); }); await(); }
@Test public void testClearClientHandlersOnEnd() { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(WebSocketBase::close); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null). handler(ws -> { ws.endHandler(v -> { try { ws.endHandler(null); ws.exceptionHandler(null); ws.handler(null); } catch (Exception e) { fail("Was expecting to set to null the handlers when the socket is closed"); return; } testComplete(); }); }); }); await(); }
private void testReject(WebsocketVersion version, Integer rejectionStatus, int expectedRejectionStatus) throws Exception { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { assertEquals(path, ws.path()); if (rejectionStatus != null) { ws.reject(rejectionStatus); } else { ws.reject(); } }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version). exceptionHandler(t -> { assertTrue(t instanceof WebsocketRejectedException); assertEquals(expectedRejectionStatus, ((WebsocketRejectedException)t).getStatus()); testComplete(); }). handler(ws -> fail("Should not be called")); }); await(); }
private void testTLS(Cert<?> clientCert, Trust<?> clientTrust, Cert<?> serverCert, Trust<?> serverTrust, boolean requireClientAuth, boolean serverUsesCrl, boolean clientTrustAll, boolean clientUsesCrl, boolean shouldPass, boolean clientSsl, boolean serverSsl, boolean sni, String[] enabledCipherSuites, Function<HttpClient, ReadStream<WebSocket>> wsProvider) throws Exception { HttpClientOptions options = new HttpClientOptions(); options.setSsl(clientSsl); options.setTrustAll(clientTrustAll); if (clientUsesCrl) { options.addCrlPath("tls/root-ca/crl.pem"); } options.setTrustOptions(clientTrust.get()); options.setKeyCertOptions(clientCert.get()); for (String suite: enabledCipherSuites) { options.addEnabledCipherSuite(suite); } client = vertx.createHttpClient(options); HttpServerOptions serverOptions = new HttpServerOptions(); serverOptions.setSsl(serverSsl); serverOptions.setSni(sni); serverOptions.setTrustOptions(serverTrust.get()); serverOptions.setKeyCertOptions(serverCert.get()); if (requireClientAuth) { serverOptions.setClientAuth(ClientAuth.REQUIRED); } if (serverUsesCrl) {
@Test // Server specifies cert that the client trusts (not trust all) public void testTLSClientTrustServerCertWithSNI() throws Exception { testTLS(Cert.NONE, Trust.SNI_JKS_HOST2, Cert.SNI_JKS, Trust.NONE, false, false, false, false, true, true, true, true, new String[0], client -> client.websocketStream(4043, "host2.com", "/")); }
private void testInvalidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String subProtocol = "myprotocol"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols("invalid")).websocketHandler(ws -> { }); server.listen(onSuccess(ar -> { client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol). exceptionHandler(t -> { // Should fail testComplete(); }). handler(ws -> { }); })); await(); }
@Test // Client trusts all server certs public void testSSLClientRequestOptionsSetClear() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, false, false, new String[0], client -> client.websocketStream(options)); }
@Test // Client trusts all server certs public void testClearClientRequestOptionsSetSSL() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, true, false, new String[0], client -> client.websocketStream(options)); }
@Test // Client trusts all server certs public void testSSLClientRequestOptionsSetSSL() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, true, false, new String[0], client -> client.websocketStream(options)); }
@Test // Client trusts all server certs public void testClearClientRequestOptionsSetClear() throws Exception { RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false); testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, false, false, new String[0], client -> client.websocketStream(options)); }