test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/FlowTest.java
author dfuchs
Wed, 06 Dec 2017 16:08:15 +0000
branchhttp-client-branch
changeset 55970 261d4d2f77e2
parent 55968 11a97b370db0
child 55973 4d9b002587db
permissions -rw-r--r--
http-client-branch: workaround issue with SubmissionPublisher::close in several tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     1
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     4
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     8
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    13
 * accompanied this code).
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    14
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    18
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    21
 * questions.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    22
 */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    23
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    24
package jdk.incubator.http;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    25
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    26
import java.io.BufferedOutputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    27
import java.io.File;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    28
import java.io.FileInputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    29
import java.io.IOException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    30
import java.io.InputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    31
import java.io.OutputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    32
import java.net.Socket;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    33
import java.nio.ByteBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    34
import java.security.KeyManagementException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    35
import java.security.KeyStore;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    36
import java.security.KeyStoreException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    37
import java.security.NoSuchAlgorithmException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    38
import java.security.UnrecoverableKeyException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    39
import java.security.cert.CertificateException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    40
import java.util.List;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    41
import java.util.Random;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    42
import java.util.StringTokenizer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    43
import java.util.concurrent.BlockingQueue;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    44
import java.util.concurrent.CompletableFuture;
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    45
import java.util.concurrent.CountDownLatch;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    46
import java.util.concurrent.ExecutorService;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    47
import java.util.concurrent.Executors;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    48
import java.util.concurrent.Flow;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    49
import java.util.concurrent.Flow.Subscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    50
import java.util.concurrent.LinkedBlockingQueue;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    51
import java.util.concurrent.SubmissionPublisher;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    52
import java.util.concurrent.atomic.AtomicInteger;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    53
import java.util.concurrent.atomic.AtomicLong;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    54
import javax.net.ssl.KeyManagerFactory;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    55
import javax.net.ssl.*;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    56
import javax.net.ssl.TrustManagerFactory;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    57
import jdk.incubator.http.internal.common.Utils;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    58
import org.testng.annotations.Test;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    59
import jdk.incubator.http.internal.common.SSLFlowDelegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    60
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    61
@Test
55968
11a97b370db0 http-client-branch: common random seed handling for whitebox tests
chegar
parents: 55840
diff changeset
    62
public class FlowTest extends AbstractRandomTest {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    63
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    64
    private final SubmissionPublisher<List<ByteBuffer>> srcPublisher;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    65
    private final ExecutorService executor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    66
    private static final long COUNTER = 3000;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    67
    private static final int LONGS_PER_BUF = 800;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    68
    static final long TOTAL_LONGS = COUNTER * LONGS_PER_BUF;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    69
    public static final ByteBuffer SENTINEL = ByteBuffer.allocate(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    70
    static volatile String alpn;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    71
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    72
    // This is a hack to work around an issue with SubmissionPublisher.
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    73
    // SubmissionPublisher will call onComplete immediately without forwarding
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    74
    // remaining pending data if SubmissionPublisher.close() is called when
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    75
    // there is no demand. In other words, it doesn't wait for the subscriber
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    76
    // to pull all the data before calling onComplete.
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    77
    // We use a CountDownLatch to figure out when it is safe to call close().
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    78
    // This may cause the test to hang if data are buffered.
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    79
    final CountDownLatch allBytesReceived = new CountDownLatch(1);
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    80
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    81
    private final CompletableFuture<Void> completion;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    82
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    83
    public FlowTest() throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    84
        executor = Executors.newCachedThreadPool();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    85
        srcPublisher = new SubmissionPublisher<>(executor, 20,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    86
                                                 this::handlePublisherException);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
        SSLContext ctx = (new SimpleSSLContext()).get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    88
        SSLEngine engineClient = ctx.createSSLEngine();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    89
        SSLParameters params = ctx.getSupportedSSLParameters();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    90
        params.setApplicationProtocols(new String[]{"proto1", "proto2"}); // server will choose proto2
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    91
        params.setProtocols(new String[]{"TLSv1.2"}); // TODO: This is essential. Needs to be protocol impl
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    92
        engineClient.setSSLParameters(params);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    93
        engineClient.setUseClientMode(true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    94
        completion = new CompletableFuture<>();
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    95
        SSLLoopbackSubscriber looper = new SSLLoopbackSubscriber(ctx, executor, allBytesReceived);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    96
        looper.start();
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
    97
        EndSubscriber end = new EndSubscriber(TOTAL_LONGS, completion, allBytesReceived);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    98
        SSLFlowDelegate sslClient = new SSLFlowDelegate(engineClient, executor, end, looper);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    99
        // going to measure how long handshake takes
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   100
        final long start = System.currentTimeMillis();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   101
        sslClient.alpn().whenComplete((String s, Throwable t) -> {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   102
            if (t != null)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   103
                t.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   104
            long endTime = System.currentTimeMillis();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   105
            alpn = s;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   106
            System.out.println("ALPN: " + alpn);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   107
            long period = (endTime - start);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   108
            System.out.printf("Handshake took %d ms\n", period);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   109
        });
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   110
        Subscriber<List<ByteBuffer>> reader = sslClient.upstreamReader();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   111
        Subscriber<List<ByteBuffer>> writer = sslClient.upstreamWriter();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   112
        looper.setReturnSubscriber(reader);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   113
        // now connect all the pieces
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   114
        srcPublisher.subscribe(writer);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   115
        String aa = sslClient.alpn().join();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   116
        System.out.println("AAALPN = " + aa);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   117
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   118
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   119
    private void handlePublisherException(Object o, Throwable t) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   120
        System.out.println("Src Publisher exception");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   121
        t.printStackTrace(System.out);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   122
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   123
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   124
    private static ByteBuffer getBuffer(long startingAt) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   125
        ByteBuffer buf = ByteBuffer.allocate(LONGS_PER_BUF * 8);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   126
        for (int j = 0; j < LONGS_PER_BUF; j++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   127
            buf.putLong(startingAt++);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   128
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   129
        buf.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   130
        return buf;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   131
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   132
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   133
    @Test
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   134
    public void run() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   135
        long count = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   136
        System.out.printf("Submitting %d buffer arrays\n", COUNTER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   137
        System.out.printf("LoopCount should be %d\n", TOTAL_LONGS);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   138
        for (long i = 0; i < COUNTER; i++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   139
            ByteBuffer b = getBuffer(count);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   140
            count += LONGS_PER_BUF;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   141
            srcPublisher.submit(List.of(b));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   142
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   143
        System.out.println("Finished submission. Waiting for loopback");
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   144
        // make sure we don't wait for allBytesReceived in case of error.
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   145
        completion.whenComplete((r,t) -> allBytesReceived.countDown());
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   146
        try {
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   147
            allBytesReceived.await();
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   148
        } catch (InterruptedException e) {
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   149
            throw new RuntimeException(e);
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   150
        }
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   151
        System.out.println("All bytes received: ");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   152
        srcPublisher.close();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   153
        try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   154
            completion.join();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   155
            if (!alpn.equals("proto2")) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   156
                throw new RuntimeException("wrong alpn received");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   157
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   158
            System.out.println("OK");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   159
        } finally {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   160
            executor.shutdownNow();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   161
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   162
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   163
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
    public static void main(String[]args) throws Exception {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   166
        FlowTest test = new FlowTest();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   167
        test.run();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   168
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   169
*/
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   170
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   171
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   172
     * This Subscriber simulates an SSL loopback network. The object itself
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   173
     * accepts outgoing SSL encrypted data which is looped back via two sockets
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   174
     * (one of which is an SSLSocket emulating a server). The method
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   175
     * {@link #setReturnSubscriber(java.util.concurrent.Flow.Subscriber) }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   176
     * is used to provide the Subscriber which feeds the incoming side
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   177
     * of SSLFlowDelegate. Three threads are used to implement this behavior
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   178
     * and a SubmissionPublisher drives the incoming read side.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   179
     * <p>
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   180
     * A thread reads from the buffer, writes
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   181
     * to the client j.n.Socket which is connected to a SSLSocket operating
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   182
     * in server mode. A second thread loops back data read from the SSLSocket back to the
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   183
     * client again. A third thread reads the client socket and pushes the data to
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   184
     * a SubmissionPublisher that drives the reader side of the SSLFlowDelegate
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   185
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   186
    static class SSLLoopbackSubscriber implements Subscriber<List<ByteBuffer>> {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   187
        private final BlockingQueue<ByteBuffer> buffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   188
        private final Socket clientSock;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   189
        private final SSLSocket serverSock;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   190
        private final Thread thread1, thread2, thread3;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   191
        private volatile Flow.Subscription clientSubscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   192
        private final SubmissionPublisher<List<ByteBuffer>> publisher;
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   193
        private final CountDownLatch allBytesReceived;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   195
        SSLLoopbackSubscriber(SSLContext ctx,
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   196
                              ExecutorService exec,
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   197
                              CountDownLatch allBytesReceived) throws IOException {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
            SSLServerSocketFactory fac = ctx.getServerSocketFactory();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
            SSLServerSocket serv = (SSLServerSocket) fac.createServerSocket(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
            SSLParameters params = serv.getSSLParameters();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
            params.setApplicationProtocols(new String[]{"proto2"});
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
            serv.setSSLParameters(params);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
            int serverPort = serv.getLocalPort();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
            clientSock = new Socket("127.0.0.1", serverPort);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   207
            serverSock = (SSLSocket) serv.accept();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   208
            this.buffer = new LinkedBlockingQueue<>();
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   209
            this.allBytesReceived = allBytesReceived;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   210
            thread1 = new Thread(this::clientWriter, "clientWriter");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
            thread2 = new Thread(this::serverLoopback, "serverLoopback");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
            thread3 = new Thread(this::clientReader, "clientReader");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
            publisher = new SubmissionPublisher<>(exec, Flow.defaultBufferSize(),
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   214
                    this::handlePublisherException);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   215
            SSLFlowDelegate.Monitor.add(this::monitor);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   216
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   217
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
        public void start() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
            thread1.start();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   220
            thread2.start();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   221
            thread3.start();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   222
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   223
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
        private void handlePublisherException(Object o, Throwable t) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
            System.out.println("Loopback Publisher exception");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   226
            t.printStackTrace(System.out);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   227
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   228
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   229
        private final AtomicInteger readCount = new AtomicInteger();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   230
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   231
        // reads off the SSLSocket the data from the "server"
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   232
        private void clientReader() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   233
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   234
                InputStream is = clientSock.getInputStream();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   235
                final int bufsize = FlowTest.randomRange(512, 16 * 1024);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   236
                System.out.println("clientReader: bufsize = " + bufsize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   237
                while (true) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
                    byte[] buf = new byte[bufsize];
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
                    int n = is.read(buf);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
                    if (n == -1) {
55840
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   241
                        System.out.println("clientReader close: read "
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   242
                                + readCount.get() + " bytes");
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   243
                        System.out.println("clientReader: got EOF. "
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   244
                                            + "Waiting signal to close publisher.");
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   245
                        allBytesReceived.await();
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   246
                        System.out.println("clientReader: closing publisher");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   247
                        publisher.close();
55796
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   248
                        sleep(2000);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   249
                        Utils.close(is, clientSock);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   250
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   251
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   252
                    ByteBuffer bb = ByteBuffer.wrap(buf, 0, n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
                    readCount.addAndGet(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
                    publisher.submit(List.of(bb));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
            } catch (Throwable e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
                e.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   258
                Utils.close(clientSock);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   259
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   260
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   261
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   262
        // writes the encrypted data from SSLFLowDelegate to the j.n.Socket
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   263
        // which is connected to the SSLSocket emulating a server.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   264
        private void clientWriter() {
55840
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   265
            long nbytes = 0;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   266
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   267
                OutputStream os =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   268
                        new BufferedOutputStream(clientSock.getOutputStream());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   269
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   270
                while (true) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   271
                    ByteBuffer buf = buffer.take();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   272
                    if (buf == FlowTest.SENTINEL) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   273
                        // finished
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   274
                        //Utils.sleep(2000);
55840
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   275
                        System.out.println("clientWriter close: " + nbytes + " written");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   276
                        clientSock.shutdownOutput();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   277
                        System.out.println("clientWriter close return");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   278
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   279
                    }
55840
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   280
                    int len = buf.remaining();
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   281
                    int written = writeToStream(os, buf);
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   282
                    assert len == written;
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   283
                    nbytes += len;
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   284
                    assert !buf.hasRemaining()
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   285
                            : "buffer has " + buf.remaining() + " bytes left";
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   286
                    clientSubscription.request(1);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   287
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   288
            } catch (Throwable e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   289
                e.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   290
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   291
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   292
55840
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   293
        private int writeToStream(OutputStream os, ByteBuffer buf) throws IOException {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   294
            byte[] b = buf.array();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   295
            int offset = buf.arrayOffset() + buf.position();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   296
            int n = buf.limit() - buf.position();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   297
            os.write(b, offset, n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   298
            buf.position(buf.limit());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   299
            os.flush();
55840
2c38de4b50bd http-client-branch: more debugging, and hopefully a fix, for a FlowTest intermittent failure (ERROR AT END)
dfuchs
parents: 55796
diff changeset
   300
            return n;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   301
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   302
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   303
        private final AtomicInteger loopCount = new AtomicInteger();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   304
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   305
        public String monitor() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   306
            return "serverLoopback: loopcount = " + loopCount.toString()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   307
                    + " clientRead: count = " + readCount.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   308
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   309
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   310
        // thread2
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   311
        private void serverLoopback() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   312
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   313
                InputStream is = serverSock.getInputStream();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   314
                OutputStream os = serverSock.getOutputStream();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   315
                final int bufsize = FlowTest.randomRange(512, 16 * 1024);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   316
                System.out.println("serverLoopback: bufsize = " + bufsize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   317
                byte[] bb = new byte[bufsize];
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   318
                while (true) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   319
                    int n = is.read(bb);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   320
                    if (n == -1) {
55796
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   321
                        sleep(2000);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   322
                        is.close();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   323
                        serverSock.close();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   324
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   325
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   326
                    os.write(bb, 0, n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   327
                    os.flush();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   328
                    loopCount.addAndGet(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   329
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   330
            } catch (Throwable e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   331
                e.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   332
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   333
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   334
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   335
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   336
        /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   337
         * This needs to be called before the chain is subscribed. It can't be
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   338
         * supplied in the constructor.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   339
         */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   340
        public void setReturnSubscriber(Subscriber<List<ByteBuffer>> returnSubscriber) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   341
            publisher.subscribe(returnSubscriber);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   342
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   343
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   344
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   345
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   346
            clientSubscription = subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   347
            clientSubscription.request(5);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   348
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   349
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   350
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   351
        public void onNext(List<ByteBuffer> item) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   352
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   353
                for (ByteBuffer b : item)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   354
                    buffer.put(b);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   355
            } catch (InterruptedException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   356
                e.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
                Utils.close(clientSock);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   359
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   360
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   361
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   362
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   363
            throwable.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
            Utils.close(clientSock);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   365
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   368
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   369
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
                buffer.put(FlowTest.SENTINEL);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   371
            } catch (InterruptedException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   372
                e.printStackTrace();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   373
                Utils.close(clientSock);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   374
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   375
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   376
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   377
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   378
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   379
     * The final subscriber which receives the decrypted looped-back data.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   380
     * Just needs to compare the data with what was sent. The given CF is
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   381
     * either completed exceptionally with an error or normally on success.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   382
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   383
    static class EndSubscriber implements Subscriber<List<ByteBuffer>> {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   384
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   385
        private final long nbytes;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   386
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   387
        private final AtomicLong counter;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   388
        private volatile Flow.Subscription subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   389
        private final CompletableFuture<Void> completion;
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   390
        private final CountDownLatch allBytesReceived;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   391
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   392
        EndSubscriber(long nbytes,
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   393
                      CompletableFuture<Void> completion,
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   394
                      CountDownLatch allBytesReceived) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   395
            counter = new AtomicLong(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   396
            this.nbytes = nbytes;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   397
            this.completion = completion;
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   398
            this.allBytesReceived = allBytesReceived;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   399
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   400
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   401
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   402
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   403
            this.subscription = subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   404
            subscription.request(5);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   405
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   406
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   407
        public static String info(List<ByteBuffer> i) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   408
            StringBuilder sb = new StringBuilder();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   409
            sb.append("size: ").append(Integer.toString(i.size()));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   410
            int x = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   411
            for (ByteBuffer b : i)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   412
                x += b.remaining();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   413
            sb.append(" bytes: " + Integer.toString(x));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   414
            return sb.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   415
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   416
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   417
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   418
        public void onNext(List<ByteBuffer> buffers) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   419
            long currval = counter.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   420
            //if (currval % 500 == 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   421
            //System.out.println("End: " + currval);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   422
            //}
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   423
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   424
            for (ByteBuffer buf : buffers) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   425
                while (buf.hasRemaining()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   426
                    long n = buf.getLong();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   427
                    //if (currval > (FlowTest.TOTAL_LONGS - 50)) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   428
                    //System.out.println("End: " + currval);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   429
                    //}
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   430
                    if (n != currval++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   431
                        System.out.println("ERROR at " + n + " != " + (currval - 1));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   432
                        completion.completeExceptionally(new RuntimeException("ERROR"));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   433
                        subscription.cancel();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   434
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   435
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   436
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   437
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   438
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   439
            counter.set(currval);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   440
            subscription.request(1);
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   441
            if (currval >= TOTAL_LONGS) {
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   442
                allBytesReceived.countDown();
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   443
            }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   444
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   445
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   446
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   447
        public void onError(Throwable throwable) {
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   448
            allBytesReceived.countDown();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   449
            completion.completeExceptionally(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   450
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   451
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   452
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   453
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   454
            long n = counter.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   455
            if (n != nbytes) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   456
                System.out.printf("nbytes=%d n=%d\n", nbytes, n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   457
                completion.completeExceptionally(new RuntimeException("ERROR AT END"));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   458
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   459
                System.out.println("DONE OK: counter = " + n);
55970
261d4d2f77e2 http-client-branch: workaround issue with SubmissionPublisher::close in several tests
dfuchs
parents: 55968
diff changeset
   460
                allBytesReceived.countDown();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   461
                completion.complete(null);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   462
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   463
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   464
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   465
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   466
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   467
     * Creates a simple usable SSLContext for SSLSocketFactory
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   468
     * or a HttpsServer using either a given keystore or a default
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   469
     * one in the test tree.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   470
     * <p>
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   471
     * Using this class with a security manager requires the following
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   472
     * permissions to be granted:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   473
     * <p>
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   474
     * permission "java.util.PropertyPermission" "test.src.path", "read";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   475
     * permission java.io.FilePermission
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   476
     * "${test.src}/../../../../lib/testlibrary/jdk/testlibrary/testkeys", "read";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   477
     * The exact path above depends on the location of the test.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   478
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   479
    static class SimpleSSLContext {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   480
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   481
        private final SSLContext ssl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   482
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   483
        /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   484
         * Loads default keystore from SimpleSSLContext source directory
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   485
         */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   486
        public SimpleSSLContext() throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   487
            String paths = System.getProperty("test.src.path");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   488
            StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   489
            boolean securityExceptions = false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   490
            SSLContext sslContext = null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   491
            while (st.hasMoreTokens()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   492
                String path = st.nextToken();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   493
                try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   494
                    File f = new File(path, "../../../../lib/testlibrary/jdk/testlibrary/testkeys");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   495
                    if (f.exists()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   496
                        try (FileInputStream fis = new FileInputStream(f)) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   497
                            sslContext = init(fis);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   498
                            break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   499
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   500
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   501
                } catch (SecurityException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   502
                    // catch and ignore because permission only required
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   503
                    // for one entry on path (at most)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   504
                    securityExceptions = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   505
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   506
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   507
            if (securityExceptions) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   508
                System.out.println("SecurityExceptions thrown on loading testkeys");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   509
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   510
            ssl = sslContext;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   511
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   512
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   513
        private SSLContext init(InputStream i) throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   514
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   515
                char[] passphrase = "passphrase".toCharArray();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   516
                KeyStore ks = KeyStore.getInstance("JKS");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   517
                ks.load(i, passphrase);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   518
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   519
                KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   520
                kmf.init(ks, passphrase);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   521
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   522
                TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   523
                tmf.init(ks);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   524
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   525
                SSLContext ssl = SSLContext.getInstance("TLS");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   526
                ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   527
                return ssl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   528
            } catch (KeyManagementException | KeyStoreException |
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   529
                    UnrecoverableKeyException | CertificateException |
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   530
                    NoSuchAlgorithmException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   531
                throw new RuntimeException(e.getMessage());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   532
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   533
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   534
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   535
        public SSLContext get() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   536
            return ssl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   537
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   538
    }
55796
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   539
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   540
    private static void sleep(int millis) {
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   541
        try {
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   542
            Thread.sleep(millis);
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   543
        } catch (Exception e) {
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   544
            e.printStackTrace();
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   545
        }
dfc99ea2b65a http-client-branch: (cleanup)
prappo
parents: 55763
diff changeset
   546
    }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   547
}