Search in sources :

Example 1 with Cert

use of io.vertx.test.tls.Cert in project vert.x by eclipse.

the class WebSocketTest method testTLS.

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, BiConsumer<HttpClient, Handler<AsyncResult<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) {
        serverOptions.addCrlPath("tls/root-ca/crl.pem");
    }
    for (String suite : enabledCipherSuites) {
        serverOptions.addEnabledCipherSuite(suite);
    }
    server = vertx.createHttpServer(serverOptions.setPort(4043));
    server.webSocketHandler(ws -> {
        ws.handler(ws::write);
    });
    try {
        server.listen(ar -> {
            assertTrue(ar.succeeded());
            Handler<AsyncResult<WebSocket>> handler = ar2 -> {
                if (ar2.succeeded()) {
                    WebSocket ws = ar2.result();
                    if (clientSsl && sni) {
                        try {
                            Certificate clientPeerCert = ws.peerCertificates().get(0);
                            assertEquals("host2.com", TestUtils.cnOf(clientPeerCert));
                        } catch (Exception err) {
                            fail(err);
                        }
                    }
                    int size = 100;
                    Buffer received = Buffer.buffer();
                    ws.handler(data -> {
                        received.appendBuffer(data);
                        if (received.length() == size) {
                            ws.close();
                            testComplete();
                        }
                    });
                    Buffer buff = Buffer.buffer(TestUtils.randomByteArray(size));
                    ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
                } else {
                    if (shouldPass) {
                        ar2.cause().printStackTrace();
                        fail("Should not throw exception");
                    } else {
                        testComplete();
                    }
                }
            };
            wsProvider.accept(client, handler);
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    await();
}
Also used : WebSocketInternal(io.vertx.core.http.impl.WebSocketInternal) MultiMap(io.vertx.core.MultiMap) Context(io.vertx.core.Context) Matcher(java.util.regex.Matcher) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DEFAULT_HTTP_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST) ReadStream(io.vertx.core.streams.ReadStream) HAProxy(io.vertx.test.proxy.HAProxy) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) CheckingSender(io.vertx.test.core.CheckingSender) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxOptions(io.vertx.core.VertxOptions) Trust(io.vertx.test.tls.Trust) BlockingQueue(java.util.concurrent.BlockingQueue) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) AbstractVerticle(io.vertx.core.AbstractVerticle) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cert(io.vertx.test.tls.Cert) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) NetSocket(io.vertx.core.net.NetSocket) java.util(java.util) MessageDigest(java.security.MessageDigest) DEFAULT_HTTP_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DEFAULT_HTTPS_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_HOST) BiConsumer(java.util.function.BiConsumer) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocket13FrameDecoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder) AsyncResult(io.vertx.core.AsyncResult) SocketAddress(io.vertx.core.net.SocketAddress) DEFAULT_TEST_URI(io.vertx.core.http.HttpTestBase.DEFAULT_TEST_URI) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) Promise(io.vertx.core.Promise) TestUtils.randomAlphaString(io.vertx.test.core.TestUtils.randomAlphaString) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) IOException(java.io.IOException) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WebSocket13FrameEncoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameEncoder) DeploymentOptions(io.vertx.core.DeploymentOptions) NetServer(io.vertx.core.net.NetServer) DEFAULT_HTTPS_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_PORT) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) Http1xClientConnection(io.vertx.core.http.impl.Http1xClientConnection) Buffer(io.vertx.core.buffer.Buffer) TestUtils.randomAlphaString(io.vertx.test.core.TestUtils.randomAlphaString) AsyncResult(io.vertx.core.AsyncResult) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) IOException(java.io.IOException) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) Certificate(java.security.cert.Certificate)

Aggregations

HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1 WebSocket13FrameDecoder (io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder)1 WebSocket13FrameEncoder (io.netty.handler.codec.http.websocketx.WebSocket13FrameEncoder)1 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)1 PlatformDependent (io.netty.util.internal.PlatformDependent)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 MultiMap (io.vertx.core.MultiMap)1 Promise (io.vertx.core.Promise)1 Vertx (io.vertx.core.Vertx)1 VertxOptions (io.vertx.core.VertxOptions)1 Buffer (io.vertx.core.buffer.Buffer)1 DEFAULT_HTTPS_HOST (io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_HOST)1 DEFAULT_HTTPS_PORT (io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_PORT)1 DEFAULT_HTTP_HOST (io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST)1