test/jdk/java/net/httpclient/whitebox/jdk.incubator.httpclient/jdk/incubator/http/SSLTubeTest.java
author chegar
Sun, 05 Nov 2017 17:32:13 +0000
branchhttp-client-branch
changeset 55763 634d8e14c172
child 55909 583695a0ed6a
permissions -rw-r--r--
http-client-branch: intial load from jdk10/sandbox
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 jdk.incubator.http.internal.common.Demand;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    27
import jdk.incubator.http.internal.common.FlowTube;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    28
import jdk.incubator.http.internal.common.SSLTube;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    29
import jdk.incubator.http.internal.common.SequentialScheduler;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    30
import org.testng.annotations.Test;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    31
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    32
import javax.net.ssl.KeyManagerFactory;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    33
import javax.net.ssl.SSLContext;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    34
import javax.net.ssl.SSLEngine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    35
import javax.net.ssl.SSLParameters;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    36
import javax.net.ssl.TrustManagerFactory;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    37
import java.io.File;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    38
import java.io.FileInputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    39
import java.io.IOException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    40
import java.io.InputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    41
import java.nio.ByteBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    42
import java.security.KeyManagementException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    43
import java.security.KeyStore;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    44
import java.security.KeyStoreException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    45
import java.security.NoSuchAlgorithmException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    46
import java.security.UnrecoverableKeyException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    47
import java.security.cert.CertificateException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    48
import java.util.List;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    49
import java.util.Queue;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    50
import java.util.StringTokenizer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    51
import java.util.concurrent.CompletableFuture;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    52
import java.util.concurrent.ConcurrentLinkedQueue;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    53
import java.util.concurrent.Executor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    54
import java.util.concurrent.ExecutorService;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    55
import java.util.concurrent.Executors;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    56
import java.util.concurrent.Flow;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    57
import java.util.concurrent.ForkJoinPool;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    58
import java.util.concurrent.SubmissionPublisher;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    59
import java.util.concurrent.atomic.AtomicBoolean;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    60
import java.util.concurrent.atomic.AtomicLong;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    61
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    62
@Test
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    63
public class SSLTubeTest {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    64
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    65
    private static final long COUNTER = 600;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    66
    private static final int LONGS_PER_BUF = 800;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    67
    private static final long TOTAL_LONGS = COUNTER * LONGS_PER_BUF;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    68
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    69
    private static ByteBuffer getBuffer(long startingAt) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    70
        ByteBuffer buf = ByteBuffer.allocate(LONGS_PER_BUF * 8);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    71
        for (int j = 0; j < LONGS_PER_BUF; j++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    72
            buf.putLong(startingAt++);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    73
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    74
        buf.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    75
        return buf;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    76
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    77
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    78
    @Test(timeOut = 30000)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    79
    public void run() throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    80
        /* Start of wiring */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    81
        ExecutorService sslExecutor = Executors.newCachedThreadPool();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    82
        /* Emulates an echo server */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    83
        FlowTube server = new SSLTube(createSSLEngine(false),
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    84
                                      sslExecutor,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    85
                                      new EchoTube(16));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    86
        FlowTube client = new SSLTube(createSSLEngine(true),
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
                                      sslExecutor,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    88
                                      server);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    89
        SubmissionPublisher<List<ByteBuffer>> p =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    90
                new SubmissionPublisher<>(ForkJoinPool.commonPool(),
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    91
                                          Integer.MAX_VALUE);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    92
        FlowTube.TubePublisher begin = p::subscribe;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    93
        CompletableFuture<Void> completion = new CompletableFuture<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    94
        EndSubscriber end = new EndSubscriber(TOTAL_LONGS, completion);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    95
        client.connectFlows(begin, end);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    96
        /* End of wiring */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    97
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    98
        long count = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    99
        System.out.printf("Submitting %d buffer arrays\n", COUNTER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   100
        System.out.printf("LoopCount should be %d\n", TOTAL_LONGS);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   101
        for (long i = 0; i < COUNTER; i++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   102
            ByteBuffer b = getBuffer(count);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   103
            count += LONGS_PER_BUF;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   104
            p.submit(List.of(b));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   105
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   106
        System.out.println("Finished submission. Waiting for loopback");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   107
        p.close();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   108
        try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   109
            completion.join();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   110
            System.out.println("OK");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   111
        } finally {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   112
            sslExecutor.shutdownNow();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   113
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   114
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   115
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   116
    private static final class EchoTube implements FlowTube {
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
        private final static Object EOF = new Object();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   119
        private final Executor executor = Executors.newSingleThreadExecutor();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   120
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   121
        private final Queue<Object> queue = new ConcurrentLinkedQueue<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   122
        private final int maxQueueSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   123
        private final SequentialScheduler processingScheduler =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   124
                new SequentialScheduler(createProcessingTask());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   125
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   126
        /* Writing into this tube */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   127
        private long unfulfilled;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   128
        private Flow.Subscription subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   129
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   130
        /* Reading from this tube */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   131
        private final Demand demand = new Demand();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   132
        private final AtomicBoolean cancelled = new AtomicBoolean();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   133
        private Flow.Subscriber<? super List<ByteBuffer>> subscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   134
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   135
        private EchoTube(int maxBufferSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   136
            if (maxBufferSize < 1)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   137
                throw new IllegalArgumentException();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   138
            this.maxQueueSize = maxBufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   139
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   140
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   141
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   142
        public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   143
            this.subscriber = subscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   144
            this.subscriber.onSubscribe(new InternalSubscription());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   145
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   146
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   147
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   148
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   149
            unfulfilled = maxQueueSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   150
            (this.subscription = subscription).request(maxQueueSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   151
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   152
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   153
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   154
        public void onNext(List<ByteBuffer> item) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   155
            if (--unfulfilled == (maxQueueSize / 2)) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   156
                subscription.request(maxQueueSize - unfulfilled);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   157
                unfulfilled = maxQueueSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   158
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   159
            queue.add(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   160
            processingScheduler.deferOrSchedule(executor);
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
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
            queue.add(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   166
            processingScheduler.deferOrSchedule(executor);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   167
        }
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
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   170
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   171
            queue.add(EOF);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   172
            processingScheduler.deferOrSchedule(executor);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   173
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   174
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   175
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   176
        public boolean isFinished() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   177
            return false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   178
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   179
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   180
        private class InternalSubscription implements Flow.Subscription {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   181
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   182
            @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   183
            public void request(long n) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   184
                if (n <= 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   185
                    throw new InternalError();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   186
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   187
                demand.increase(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   188
                processingScheduler.runOrSchedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   189
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   190
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   191
            @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   192
            public void cancel() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   193
                cancelled.set(true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   195
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   196
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   197
        private SequentialScheduler.RestartableTask createProcessingTask() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
            return new SequentialScheduler.CompleteRestartableTask() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
                @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
                protected void run() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
                    while (!cancelled.get()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
                        Object item = queue.peek();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
                        if (item == null)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
                            return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
                        try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   207
                            if (item instanceof List) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   208
                                if (!demand.tryDecrement())
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   209
                                    return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   210
                                @SuppressWarnings("unchecked")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
                                List<ByteBuffer> bytes = (List<ByteBuffer>) item;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
                                subscriber.onNext(bytes);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
                            } else if (item instanceof Throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   214
                                cancelled.set(true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   215
                                subscriber.onError((Throwable) item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   216
                            } else if (item == EOF) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   217
                                cancelled.set(true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
                                subscriber.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
                            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   220
                                throw new InternalError(String.valueOf(item));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   221
                            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   222
                        } finally {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   223
                            Object removed = queue.remove();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
                            assert removed == item;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   226
                    }
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
        }
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
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   232
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   233
     * The final subscriber which receives the decrypted looped-back data. Just
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   234
     * needs to compare the data with what was sent. The given CF is either
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   235
     * completed exceptionally with an error or normally on success.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   236
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   237
    private static class EndSubscriber implements FlowTube.TubeSubscriber {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
        private static final int REQUEST_WINDOW = 13;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   241
        private final long nbytes;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   242
        private final AtomicLong counter = new AtomicLong();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   243
        private final CompletableFuture<?> completion;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   244
        private volatile Flow.Subscription subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   245
        private long unfulfilled;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   246
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   247
        EndSubscriber(long nbytes, CompletableFuture<?> completion) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   248
            this.nbytes = nbytes;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   249
            this.completion = completion;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   250
        }
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
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
            this.subscription = subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
            unfulfilled = REQUEST_WINDOW;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
            subscription.request(REQUEST_WINDOW);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   258
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   259
        public static String info(List<ByteBuffer> i) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   260
            StringBuilder sb = new StringBuilder();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   261
            sb.append("size: ").append(Integer.toString(i.size()));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   262
            int x = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   263
            for (ByteBuffer b : i)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   264
                x += b.remaining();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   265
            sb.append(" bytes: ").append(x);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   266
            return sb.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   267
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   268
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   269
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   270
        public void onNext(List<ByteBuffer> buffers) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   271
            if (--unfulfilled == (REQUEST_WINDOW / 2)) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   272
                subscription.request(REQUEST_WINDOW - unfulfilled);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   273
                unfulfilled = REQUEST_WINDOW;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   274
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   275
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   276
            long currval = counter.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   277
            if (currval % 500 == 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   278
                System.out.println("End: " + currval);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   279
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   280
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   281
            for (ByteBuffer buf : buffers) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   282
                while (buf.hasRemaining()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   283
                    long n = buf.getLong();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   284
                    if (currval > (SSLTubeTest.TOTAL_LONGS - 50)) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   285
                        System.out.println("End: " + currval);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   286
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   287
                    if (n != currval++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   288
                        System.out.println("ERROR at " + n + " != " + (currval - 1));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   289
                        completion.completeExceptionally(new RuntimeException("ERROR"));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   290
                        subscription.cancel();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   291
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   292
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   293
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   294
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   295
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   296
            counter.set(currval);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   297
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   298
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   299
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   300
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   301
            completion.completeExceptionally(throwable);
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
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   304
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   305
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   306
            long n = counter.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   307
            if (n != nbytes) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   308
                System.out.printf("nbytes=%d n=%d\n", nbytes, n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   309
                completion.completeExceptionally(new RuntimeException("ERROR AT END"));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   310
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   311
                System.out.println("DONE OK");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   312
                completion.complete(null);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   313
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   314
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   315
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   316
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   317
    private static SSLEngine createSSLEngine(boolean client) throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   318
        SSLContext context = (new SimpleSSLContext()).get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   319
        SSLEngine engine = context.createSSLEngine();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   320
        SSLParameters params = context.getSupportedSSLParameters();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   321
        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
   322
        if (client) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   323
            params.setApplicationProtocols(new String[]{"proto1", "proto2"}); // server will choose proto2
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   324
        } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   325
            params.setApplicationProtocols(new String[]{"proto2"}); // server will choose proto2
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   326
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   327
        engine.setSSLParameters(params);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   328
        engine.setUseClientMode(client);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   329
        return engine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   330
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   331
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
     * Creates a simple usable SSLContext for SSLSocketFactory or a HttpsServer
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   334
     * using either a given keystore or a default one in the test tree.
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
     * Using this class with a security manager requires the following
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   337
     * permissions to be granted:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   338
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   339
     * permission "java.util.PropertyPermission" "test.src.path", "read";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   340
     * permission java.io.FilePermission "${test.src}/../../../../lib/testlibrary/jdk/testlibrary/testkeys",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   341
     * "read"; The exact path above depends on the location of the test.
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
    private static class SimpleSSLContext {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   344
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   345
        private final SSLContext ssl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   346
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   347
        /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   348
         * Loads default keystore from SimpleSSLContext source directory
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
        public SimpleSSLContext() throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   351
            String paths = System.getProperty("test.src.path");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   352
            StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   353
            boolean securityExceptions = false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   354
            SSLContext sslContext = null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   355
            while (st.hasMoreTokens()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   356
                String path = st.nextToken();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
                try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
                    File f = new File(path, "../../../../lib/testlibrary/jdk/testlibrary/testkeys");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   359
                    if (f.exists()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   360
                        try (FileInputStream fis = new FileInputStream(f)) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   361
                            sslContext = init(fis);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   362
                            break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   363
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   365
                } catch (SecurityException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
                    // catch and ignore because permission only required
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
                    // for one entry on path (at most)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   368
                    securityExceptions = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   369
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   371
            if (securityExceptions) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   372
                System.err.println("SecurityExceptions thrown on loading testkeys");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   373
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   374
            ssl = sslContext;
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
        private SSLContext init(InputStream i) throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   378
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   379
                char[] passphrase = "passphrase".toCharArray();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   380
                KeyStore ks = KeyStore.getInstance("JKS");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   381
                ks.load(i, passphrase);
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
                KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   384
                kmf.init(ks, passphrase);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   385
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   386
                TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   387
                tmf.init(ks);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   388
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   389
                SSLContext ssl = SSLContext.getInstance("TLS");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   390
                ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   391
                return ssl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   392
            } catch (KeyManagementException | KeyStoreException |
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   393
                    UnrecoverableKeyException | CertificateException |
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   394
                    NoSuchAlgorithmException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   395
                throw new RuntimeException(e.getMessage());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   396
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   397
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   398
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   399
        public SSLContext get() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   400
            return ssl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   401
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   402
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   403
}