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 2013/07/25 14:01:32 UTC

svn commit: r1506918 - in /httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio: integration/ testserver/

Author: olegk
Date: Thu Jul 25 12:01:32 2013
New Revision: 1506918

URL: http://svn.apache.org/r1506918
Log:
HTTPCORE-346: added custom SSL context test case

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLClientConnectionFactory.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLServerConnectionFactory.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/SimpleIOReactorExceptionHandler.java

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java?rev=1506918&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java Thu Jul 25 12:01:32 2013
@@ -0,0 +1,148 @@
+/*
+ * ====================================================================
+ * 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.http.nio.integration;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.impl.nio.pool.BasicNIOConnFactory;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
+import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
+import org.apache.http.nio.reactor.IOReactorStatus;
+import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.nio.reactor.ListenerEndpoint;
+import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
+import org.apache.http.nio.testserver.HttpClientNio;
+import org.apache.http.nio.testserver.HttpServerNio;
+import org.apache.http.nio.testserver.LoggingSSLClientConnectionFactory;
+import org.apache.http.nio.testserver.LoggingSSLServerConnectionFactory;
+import org.apache.http.nio.testserver.SSLTestContexts;
+import org.apache.http.nio.testserver.SimpleIOReactorExceptionHandler;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSession;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.net.InetSocketAddress;
+import java.util.concurrent.Future;
+
+public class TestCustomSSL {
+
+    protected HttpServerNio server;
+    protected HttpClientNio client;
+
+    @After
+    public void shutDownClient() throws Exception {
+        if (this.client != null) {
+            this.client.shutdown();
+            this.client = null;
+        }
+    }
+
+    @After
+    public void shutDownServer() throws Exception {
+        if (this.server != null) {
+            this.server.shutdown();
+            this.server = null;
+        }
+    }
+
+    @Test
+    public void testCustomSSLContext() throws Exception {
+        final SSLSetupHandler sslSetupHandler = new SSLSetupHandler() {
+
+            public void initalize(
+                    final SSLEngine sslengine) throws SSLException {
+            }
+
+            public void verify(
+                    final IOSession iosession, final SSLSession sslsession) throws SSLException {
+                final BigInteger sslid = new BigInteger(sslsession.getId());
+                iosession.setAttribute("ssl-id", sslid);
+            }
+
+        };
+
+        final HttpRequestHandler requestHandler = new HttpRequestHandler() {
+
+            public void handle(
+                    final HttpRequest request,
+                    final HttpResponse response,
+                    final HttpContext context) throws HttpException, IOException {
+                final NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        HttpCoreContext.HTTP_CONNECTION);
+                final BigInteger sslid = (BigInteger) conn.getContext().getAttribute(
+                        "ssl-id");
+                Assert.assertNotNull(sslid);
+            }
+
+        };
+
+        this.server = new HttpServerNio(
+                new LoggingSSLServerConnectionFactory(
+                        SSLTestContexts.createServerSSLContext(), sslSetupHandler));
+        this.server.setExceptionHandler(new SimpleIOReactorExceptionHandler());
+        this.server.setTimeout(5000);
+        this.client = new HttpClientNio(
+                new BasicNIOConnFactory(
+                        new LoggingSSLClientConnectionFactory(
+                                SSLTestContexts.createClientSSLContext()), null));
+        this.client.setExceptionHandler(new SimpleIOReactorExceptionHandler());
+        this.client.setTimeout(5000);
+
+        final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
+        registry.register("*", new BasicAsyncRequestHandler(requestHandler));
+
+        this.server.start(HttpServerNio.DEFAULT_HTTP_PROC, registry, null);
+        this.client.start(HttpClientNio.DEFAULT_HTTP_PROC);
+
+        final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+
+        Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
+        final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
+
+        final HttpHost target = new HttpHost("localhost", address.getPort());
+        final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        final Future<HttpResponse> future = this.client.execute(target, request);
+        final HttpResponse response = future.get();
+        Assert.assertNotNull(response);
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestCustomSSL.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLClientConnectionFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLClientConnectionFactory.java?rev=1506918&r1=1506917&r2=1506918&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLClientConnectionFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLClientConnectionFactory.java Thu Jul 25 12:01:32 2013
@@ -33,19 +33,27 @@ import org.apache.http.nio.NHttpConnecti
 import org.apache.http.nio.reactor.IOSession;
 import org.apache.http.nio.reactor.ssl.SSLIOSession;
 import org.apache.http.nio.reactor.ssl.SSLMode;
+import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
 
 public class LoggingSSLClientConnectionFactory implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
 
     private final SSLContext sslcontext;
+    private final SSLSetupHandler setupHandler;
 
-    public LoggingSSLClientConnectionFactory(final SSLContext sslcontext) {
+    public LoggingSSLClientConnectionFactory(
+            final SSLContext sslcontext, final SSLSetupHandler setupHandler) {
         super();
         this.sslcontext = sslcontext;
+        this.setupHandler = setupHandler;
+    }
+
+    public LoggingSSLClientConnectionFactory(final SSLContext sslcontext) {
+        this(sslcontext, null);
     }
 
     public DefaultNHttpClientConnection createConnection(final IOSession iosession) {
         final SSLIOSession ssliosession = new SSLIOSession(
-                iosession, SSLMode.CLIENT, this.sslcontext, null);
+                iosession, SSLMode.CLIENT, this.sslcontext, this.setupHandler);
         iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
         return new LoggingNHttpClientConnection(ssliosession);
     }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLServerConnectionFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLServerConnectionFactory.java?rev=1506918&r1=1506917&r2=1506918&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLServerConnectionFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/LoggingSSLServerConnectionFactory.java Thu Jul 25 12:01:32 2013
@@ -33,19 +33,27 @@ import org.apache.http.nio.NHttpConnecti
 import org.apache.http.nio.reactor.IOSession;
 import org.apache.http.nio.reactor.ssl.SSLIOSession;
 import org.apache.http.nio.reactor.ssl.SSLMode;
+import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
 
 public class LoggingSSLServerConnectionFactory implements NHttpConnectionFactory<DefaultNHttpServerConnection> {
 
     private final SSLContext sslcontext;
+    private final SSLSetupHandler setupHandler;
 
-    public LoggingSSLServerConnectionFactory(final SSLContext sslcontext) {
+    public LoggingSSLServerConnectionFactory(
+            final SSLContext sslcontext, final SSLSetupHandler setupHandler) {
         super();
         this.sslcontext = sslcontext;
+        this.setupHandler = setupHandler;
+    }
+
+    public LoggingSSLServerConnectionFactory(final SSLContext sslcontext) {
+        this(sslcontext, null);
     }
 
     public DefaultNHttpServerConnection createConnection(final IOSession iosession) {
         final SSLIOSession ssliosession = new SSLIOSession(
-                iosession, SSLMode.SERVER, this.sslcontext, null);
+                iosession, SSLMode.SERVER, this.sslcontext, this.setupHandler);
         iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
         return new LoggingNHttpServerConnection(ssliosession);
     }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/SimpleIOReactorExceptionHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/SimpleIOReactorExceptionHandler.java?rev=1506918&r1=1506917&r2=1506918&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/SimpleIOReactorExceptionHandler.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/testserver/SimpleIOReactorExceptionHandler.java Thu Jul 25 12:01:32 2013
@@ -31,7 +31,7 @@ import java.io.IOException;
 import org.apache.http.OoopsieRuntimeException;
 import org.apache.http.nio.reactor.IOReactorExceptionHandler;
 
-class SimpleIOReactorExceptionHandler implements IOReactorExceptionHandler {
+public class SimpleIOReactorExceptionHandler implements IOReactorExceptionHandler {
 
     public boolean handle(final RuntimeException ex) {
         if (!(ex instanceof OoopsieRuntimeException)) {