test/jdk/java/net/httpclient/InvalidSSLContextTest.java
author chegar
Wed, 07 Feb 2018 14:17:24 +0000
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56082 1da51fab3032
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: move to standard package and module name
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;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    35
import java.net.URI;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    36
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
    37
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
    38
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
    39
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
    40
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
    41
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
    42
import javax.net.ssl.SSLSocket;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    43
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    44
import java.net.http.HttpClient.Version;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    45
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    46
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    47
import java.net.http.HttpResponse.BodyHandler;
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    48
import jdk.testlibrary.SimpleSSLContext;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    49
import org.testng.Assert;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    50
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
    51
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
    52
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
    53
import org.testng.annotations.Test;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    54
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
    55
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
    56
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    57
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    58
public class InvalidSSLContextTest {
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
    SSLContext sslContext;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    61
    volatile SSLServerSocket sslServerSocket;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    62
    volatile String uri;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    63
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    64
    @DataProvider(name = "versions")
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    65
    public Object[][] versions() {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    66
        return new Object[][]{
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    67
                { HTTP_1_1 },
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    68
                { HTTP_2   }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    69
        };
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    70
    }
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
    @Test(dataProvider = "versions")
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    73
    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
    74
        // 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
    75
        HttpClient client = HttpClient.newBuilder()
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    76
                .sslContext(SSLContext.getDefault())
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    77
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    78
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    79
        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
    80
                .version(version)
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    81
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    82
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    83
        try {
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56044
diff changeset
    84
            HttpResponse<?> response = client.send(request, BodyHandler.discard());
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    85
            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
    86
        } catch (SSLException sslex) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    87
            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
    88
        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    89
    }
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
    @Test(dataProvider = "versions")
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    92
    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
    93
        // 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
    94
        HttpClient client = HttpClient.newBuilder()
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    95
                .sslContext(SSLContext.getDefault())
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    96
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    97
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
    98
        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
    99
                .version(version)
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   100
                .build();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   101
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   102
        assertExceptionally(SSLException.class,
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56044
diff changeset
   103
                            client.sendAsync(request, BodyHandler.discard()));
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   104
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   105
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   106
    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
   107
                                    CompletableFuture<?> stage) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   108
        stage.handle((result, error) -> {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   109
            if (result != null) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   110
                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
   111
                return null;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   112
            }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   113
            if (error instanceof CompletionException) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   114
                Throwable cause = error.getCause();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   115
                if (cause == null) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   116
                    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
   117
                }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   118
                assertException(clazz, cause);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   119
            } else {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   120
                assertException(clazz, error);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   121
            }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   122
            return null;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   123
        }).join();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   124
    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   125
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   126
    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
   127
        if (t == null) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   128
            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
   129
        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   130
        if (!clazz.isInstance(t)) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   131
            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
   132
        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   133
    }
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
    @BeforeTest
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   136
    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
   137
        sslContext = new SimpleSSLContext().get();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   138
        if (sslContext == null)
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   139
            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
   140
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   141
        // 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
   142
        sslServerSocket = (SSLServerSocket)sslContext
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   143
                .getServerSocketFactory()
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   144
                .createServerSocket(0);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   145
        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
   146
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   147
        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
   148
            @Override
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   149
            public void run() {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   150
                while (true) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   151
                    try {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   152
                        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
   153
                        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
   154
                        // 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
   155
                        // 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
   156
                        // 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
   157
                        Thread.sleep(500);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   158
                        s.startHandshake();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   159
                        s.close();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   160
                        Assert.fail("SERVER: UNEXPECTED ");
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   161
                    } catch (SSLHandshakeException he) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   162
                        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
   163
                    } catch (IOException e) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   164
                        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
   165
                        if (!sslServerSocket.isClosed()) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   166
                            throw new UncheckedIOException(e);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   167
                        }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   168
                        break;
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   169
                    } catch (InterruptedException ie) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   170
                        throw new RuntimeException(ie);
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   171
                    }
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
            }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   174
        };
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   175
        t.start();
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
    @AfterTest
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
diff changeset
   179
    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
   180
        sslServerSocket.close();
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
}