You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/10/14 10:00:49 UTC

[httpcomponents-core] 01/01: Conscrypt JSSE provider integration tests

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch jsse-provider-tests
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit a0329d338b700713d15b2b2863efb0356b21669a
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Oct 12 11:54:25 2019 +0200

    Conscrypt JSSE provider integration tests
---
 httpcore5-testing/pom.xml                          |   5 +
 .../testing/nio/JSSEProviderIntegrationTest.java   | 325 +++++++++++++++++++++
 .../src/test/resources/test-client.p12             | Bin 0 -> 8845 bytes
 .../src/test/resources/test-server.p12             | Bin 0 -> 5071 bytes
 pom.xml                                            |   1 +
 5 files changed, 331 insertions(+)

diff --git a/httpcore5-testing/pom.xml b/httpcore5-testing/pom.xml
index 85854ac..e3686b1 100644
--- a/httpcore5-testing/pom.xml
+++ b/httpcore5-testing/pom.xml
@@ -83,6 +83,11 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.conscrypt</groupId>
+      <artifactId>conscrypt-openjdk-uber</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/JSSEProviderIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/JSSEProviderIntegrationTest.java
new file mode 100644
index 0000000..57642e8
--- /dev/null
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/JSSEProviderIntegrationTest.java
@@ -0,0 +1,325 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.testing.nio;
+
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.Future;
+
+import javax.net.ssl.SSLEngine;
+
+import org.apache.hc.core5.function.Supplier;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.Message;
+import org.apache.hc.core5.http.Methods;
+import org.apache.hc.core5.http.config.Http1Config;
+import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
+import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
+import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
+import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
+import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
+import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
+import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.http.protocol.RequestValidateHost;
+import org.apache.hc.core5.net.NamedEndpoint;
+import org.apache.hc.core5.reactor.IOReactorConfig;
+import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
+import org.apache.hc.core5.ssl.SSLContextBuilder;
+import org.apache.hc.core5.util.ReflectionUtils;
+import org.apache.hc.core5.util.TimeValue;
+import org.apache.hc.core5.util.Timeout;
+import org.conscrypt.Conscrypt;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExternalResource;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@RunWith(Parameterized.class)
+public class JSSEProviderIntegrationTest {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Parameterized.Parameters(name = "{0} {1}")
+    public static Collection<Object[]> protocols() {
+        return Arrays.asList(new Object[][]{
+                {"Oracle", null},
+                {"Conscrypt", "TLSv1.2"},
+                {"Conscrypt", "TLSv1.3"},
+        });
+    }
+
+    private final String securityProviderName;
+    private final String protocolVersion;
+
+    public JSSEProviderIntegrationTest(final String securityProviderName, final String protocolVersion) {
+        super();
+        this.securityProviderName = securityProviderName;
+        this.protocolVersion = protocolVersion;
+    }
+
+    private static final Timeout TIMEOUT = Timeout.ofSeconds(30);
+    private static final int REQ_NUM = 25;
+
+    private Provider securityProvider;
+    private Http1TestServer server;
+    private Http1TestClient client;
+
+    @BeforeClass
+    public static void determineJavaVersion() {
+        Assume.assumeTrue("Java version must be 8 or greater", ReflectionUtils.determineJRELevel() >= 8);
+    }
+
+    @Rule
+    public TestRule resourceRules = RuleChain.outerRule(new ExternalResource() {
+
+        @Override
+        protected void before() throws Throwable {
+            if ("Conscrypt".equalsIgnoreCase(securityProviderName)) {
+                securityProvider = Conscrypt.newProviderBuilder().provideTrustManager(true).build();
+            } else {
+                securityProvider = null;
+            }
+            if (securityProvider != null) {
+                Security.insertProviderAt(securityProvider, 1);
+            }
+        }
+
+        @Override
+        protected void after() {
+            if (securityProvider != null) {
+                Security.removeProvider(securityProvider.getName());
+                securityProvider = null;
+            }
+        }
+
+    }).around(new ExternalResource() {
+
+        @Override
+        protected void before() throws Throwable {
+            log.debug("Starting up test server");
+
+            final URL keyStoreURL = getClass().getResource("/test-server.p12");
+            final String storePassword = "nopassword";
+
+            server = new Http1TestServer(
+                    IOReactorConfig.custom()
+                            .setSoTimeout(TIMEOUT)
+                            .build(),
+                    SSLContextBuilder.create()
+                            .setProvider(securityProvider)
+                            .setKeyStoreType("pkcs12")
+                            .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
+                            .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
+                            .setSecureRandom(new SecureRandom())
+                            .build(),
+                    new SSLSessionInitializer() {
+
+                        @Override
+                        public void initialize(final NamedEndpoint endpoint, final SSLEngine sslEngine) {
+                            if (protocolVersion != null) {
+                                sslEngine.setEnabledProtocols(new String[]{protocolVersion});
+                            }
+                        }
+
+                    },
+                    null);
+        }
+
+        @Override
+        protected void after() {
+            log.debug("Shutting down test server");
+            if (server != null) {
+                server.shutdown(TimeValue.ofSeconds(5));
+            }
+        }
+
+    }).around(new ExternalResource() {
+
+        @Override
+        protected void before() throws Throwable {
+            log.debug("Starting up test client");
+
+            final URL keyStoreURL = getClass().getResource("/test-client.p12");
+            final String storePassword = "nopassword";
+
+            client = new Http1TestClient(
+                    IOReactorConfig.custom()
+                            .setSoTimeout(TIMEOUT)
+                            .build(),
+                    SSLContextBuilder.create()
+                            .setProvider(securityProvider)
+                            .setKeyStoreType("pkcs12")
+                            .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
+                            .setSecureRandom(new SecureRandom())
+                            .build(),
+                    new SSLSessionInitializer() {
+
+                        @Override
+                        public void initialize(final NamedEndpoint endpoint, final SSLEngine sslEngine) {
+                            if (protocolVersion != null) {
+                                sslEngine.setEnabledProtocols(new String[]{protocolVersion});
+                            }
+                        }
+
+                    },
+                    null);
+        }
+
+        @Override
+        protected void after() {
+            log.debug("Shutting down test client");
+            if (client != null) {
+                client.shutdown(TimeValue.ofSeconds(5));
+            }
+        }
+
+    });
+
+    private URI createRequestURI(final InetSocketAddress serverEndpoint, final String path) {
+        try {
+            return new URI("https", null, "localhost", serverEndpoint.getPort(), path, null, null);
+        } catch (final URISyntaxException e) {
+            throw new IllegalStateException();
+        }
+    }
+
+    @Test
+    public void testSimpleGet() throws Exception {
+        server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {
+
+            @Override
+            public AsyncServerExchangeHandler get() {
+                return new SingleLineResponseHandler("Hi there");
+            }
+
+        });
+        final InetSocketAddress serverEndpoint = server.start();
+
+        client.start();
+        final Future<ClientSessionEndpoint> connectFuture = client.connect(
+                "localhost", serverEndpoint.getPort(), TIMEOUT);
+        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
+
+        for (int i = 0; i < REQ_NUM; i++) {
+            final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(
+                    new BasicRequestProducer(Methods.GET, createRequestURI(serverEndpoint, "/hello")),
+                    new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+            final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+            Assert.assertNotNull(result);
+            final HttpResponse response1 = result.getHead();
+            final String entity1 = result.getBody();
+            Assert.assertNotNull(response1);
+            Assert.assertEquals(200, response1.getCode());
+            Assert.assertEquals("Hi there", entity1);
+        }
+    }
+
+    @Test
+    public void testSimpleGetConnectionClose() throws Exception {
+        server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {
+
+            @Override
+            public AsyncServerExchangeHandler get() {
+                return new SingleLineResponseHandler("Hi there");
+            }
+
+        });
+        final InetSocketAddress serverEndpoint = server.start();
+
+        client.start();
+        final URI requestURI = createRequestURI(serverEndpoint, "/hello");
+        for (int i = 0; i < REQ_NUM; i++) {
+            final Future<ClientSessionEndpoint> connectFuture = client.connect(
+                    "localhost", serverEndpoint.getPort(), TIMEOUT);
+            try (final ClientSessionEndpoint streamEndpoint = connectFuture.get()) {
+                final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(
+                        AsyncRequestBuilder.get(requestURI)
+                                .addHeader(HttpHeaders.CONNECTION, "close")
+                                .build(),
+                        new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+                final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+                Assert.assertNotNull(result);
+                final HttpResponse response1 = result.getHead();
+                final String entity1 = result.getBody();
+                Assert.assertNotNull(response1);
+                Assert.assertEquals(200, response1.getCode());
+                Assert.assertEquals("Hi there", entity1);
+            }
+        }
+    }
+
+    @Test
+    public void testSimpleGetIdentityTransfer() throws Exception {
+        server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {
+
+            @Override
+            public AsyncServerExchangeHandler get() {
+                return new SingleLineResponseHandler("Hi there");
+            }
+
+        });
+        final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
+        final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
+
+        client.start();
+
+        for (int i = 0; i < REQ_NUM; i++) {
+            final Future<ClientSessionEndpoint> connectFuture = client.connect(
+                    "localhost", serverEndpoint.getPort(), TIMEOUT);
+            try (final ClientSessionEndpoint streamEndpoint = connectFuture.get()) {
+                final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(
+                        new BasicRequestProducer(Methods.GET, createRequestURI(serverEndpoint, "/hello")),
+                        new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+                final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+                Assert.assertNotNull(result);
+                final HttpResponse response = result.getHead();
+                final String entity = result.getBody();
+                Assert.assertNotNull(response);
+                Assert.assertEquals(200, response.getCode());
+                Assert.assertEquals("Hi there", entity);
+            }
+        }
+    }
+
+}
diff --git a/httpcore5-testing/src/test/resources/test-client.p12 b/httpcore5-testing/src/test/resources/test-client.p12
new file mode 100644
index 0000000..f1170f5
Binary files /dev/null and b/httpcore5-testing/src/test/resources/test-client.p12 differ
diff --git a/httpcore5-testing/src/test/resources/test-server.p12 b/httpcore5-testing/src/test/resources/test-server.p12
new file mode 100644
index 0000000..c4cca0e
Binary files /dev/null and b/httpcore5-testing/src/test/resources/test-server.p12 differ
diff --git a/pom.xml b/pom.xml
index 7e47cea..8072ee7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -77,6 +77,7 @@
     <maven.compiler.target>1.7</maven.compiler.target>
     <maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
     <conscrypt.version>2.2.1</conscrypt.version>
+    <bouncycastle.version>1.63</bouncycastle.version>
     <junit.version>4.12</junit.version>
     <mockito.version>2.25.1</mockito.version>
     <slf4j.version>1.7.25</slf4j.version>