test/jdk/java/net/httpclient/InvalidSSLContextTest.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56233 1753108d07b9
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     1
/*
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     4
 *
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     8
 *
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    13
 * accompanied this code).
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    14
 *
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    18
 *
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    21
 * questions.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    22
 */
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    23
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    24
/*
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    25
 * @test
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    26
 * @summary Test to ensure the HTTP client throws an appropriate SSL exception
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    27
 *          when SSL context is not valid.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    28
 * @library /lib/testlibrary
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    29
 * @build jdk.testlibrary.SimpleSSLContext
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    30
 * @run testng/othervm -Djdk.internal.httpclient.debug=true InvalidSSLContextTest
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    31
 */
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    32
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    33
import java.io.IOException;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    34
import java.io.UncheckedIOException;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
    35
import java.net.InetAddress;
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    36
import java.net.InetSocketAddress;
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    37
import java.net.URI;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    38
import java.util.concurrent.CompletableFuture;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    39
import java.util.concurrent.CompletionException;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    40
import javax.net.ssl.SSLContext;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    41
import javax.net.ssl.SSLException;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    42
import javax.net.ssl.SSLHandshakeException;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    43
import javax.net.ssl.SSLServerSocket;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    44
import javax.net.ssl.SSLSocket;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    45
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    46
import java.net.http.HttpClient.Version;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    47
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    48
import java.net.http.HttpResponse;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    49
import java.net.http.HttpResponse.BodyHandlers;
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    50
import jdk.testlibrary.SimpleSSLContext;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    51
import org.testng.Assert;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    52
import org.testng.annotations.AfterTest;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    53
import org.testng.annotations.BeforeTest;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    54
import org.testng.annotations.DataProvider;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    55
import org.testng.annotations.Test;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    56
import static java.net.http.HttpClient.Version.HTTP_1_1;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    57
import static java.net.http.HttpClient.Version.HTTP_2;
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    58
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    59
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    60
public class InvalidSSLContextTest {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    61
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    62
    SSLContext sslContext;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    63
    volatile SSLServerSocket sslServerSocket;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    64
    volatile String uri;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    65
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    66
    @DataProvider(name = "versions")
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    67
    public Object[][] versions() {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    68
        return new Object[][]{
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    69
                { HTTP_1_1 },
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    70
                { HTTP_2   }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    71
        };
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    72
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    73
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    74
    @Test(dataProvider = "versions")
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    75
    public void testSync(Version version) throws Exception {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    76
        // client-side uses a different context to that of the server-side
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    77
        HttpClient client = HttpClient.newBuilder()
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    78
                .sslContext(SSLContext.getDefault())
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    79
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    80
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    81
        HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    82
                .version(version)
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    83
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    84
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    85
        try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    86
            HttpResponse<?> response = client.send(request, BodyHandlers.discarding());
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    87
            Assert.fail("UNEXPECTED response" + response);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    88
        } catch (SSLException sslex) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    89
            System.out.println("Caught expected: " + sslex);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    90
        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    91
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    92
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    93
    @Test(dataProvider = "versions")
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    94
    public void testAsync(Version version) throws Exception {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    95
        // client-side uses a different context to that of the server-side
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    96
        HttpClient client = HttpClient.newBuilder()
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    97
                .sslContext(SSLContext.getDefault())
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    98
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    99
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   100
        HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   101
                .version(version)
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   102
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   103
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   104
        assertExceptionally(SSLException.class,
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   105
                            client.sendAsync(request, BodyHandlers.discarding()));
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   106
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   107
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   108
    static void assertExceptionally(Class<? extends Throwable> clazz,
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   109
                                    CompletableFuture<?> stage) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   110
        stage.handle((result, error) -> {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   111
            if (result != null) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   112
                Assert.fail("UNEXPECTED result: " + result);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   113
                return null;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   114
            }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   115
            if (error instanceof CompletionException) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   116
                Throwable cause = error.getCause();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   117
                if (cause == null) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   118
                    Assert.fail("Unexpected null cause: " + error);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   119
                }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   120
                assertException(clazz, cause);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   121
            } else {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   122
                assertException(clazz, error);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   123
            }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   124
            return null;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   125
        }).join();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   126
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   127
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   128
    static void assertException(Class<? extends Throwable> clazz, Throwable t) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   129
        if (t == null) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   130
            Assert.fail("Expected " + clazz + ", caught nothing");
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   131
        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   132
        if (!clazz.isInstance(t)) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   133
            Assert.fail("Expected " + clazz + ", caught " + t);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   134
        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   135
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   136
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   137
    @BeforeTest
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   138
    public void setup() throws Exception {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   139
        sslContext = new SimpleSSLContext().get();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   140
        if (sslContext == null)
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   141
            throw new AssertionError("Unexpected null sslContext");
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   142
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   143
        // server-side uses a different context to that of the client-side
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   144
        sslServerSocket = (SSLServerSocket)sslContext
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   145
                .getServerSocketFactory()
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
   146
                .createServerSocket();
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
   147
        sslServerSocket.setReuseAddress(false);
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
   148
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
   149
        sslServerSocket.bind(addr);
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   150
        uri = "https://localhost:" + sslServerSocket.getLocalPort() + "/";
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   151
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   152
        Thread t = new Thread("SSL-Server-Side") {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   153
            @Override
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   154
            public void run() {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   155
                while (true) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   156
                    try {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   157
                        SSLSocket s = (SSLSocket) sslServerSocket.accept();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   158
                        System.out.println("SERVER: accepted: " + s);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   159
                        // artificially slow down the handshake reply to mimic
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   160
                        // a slow(ish) network, and hopefully delay the
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   161
                        // SequentialScheduler on in the client.
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   162
                        Thread.sleep(500);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   163
                        s.startHandshake();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   164
                        s.close();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   165
                        Assert.fail("SERVER: UNEXPECTED ");
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   166
                    } catch (SSLHandshakeException he) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   167
                        System.out.println("SERVER: caught expected " + he);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   168
                    } catch (IOException e) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   169
                        System.out.println("SERVER: caught: " + e);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   170
                        if (!sslServerSocket.isClosed()) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   171
                            throw new UncheckedIOException(e);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   172
                        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   173
                        break;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   174
                    } catch (InterruptedException ie) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   175
                        throw new RuntimeException(ie);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   176
                    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   177
                }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   178
            }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   179
        };
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   180
        t.start();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   181
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   182
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   183
    @AfterTest
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   184
    public void teardown() throws Exception {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   185
        sslServerSocket.close();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   186
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   187
}