55763
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package jdk.incubator.http;
|
|
25 |
|
|
26 |
import jdk.incubator.http.internal.common.Demand;
|
|
27 |
import jdk.incubator.http.internal.common.FlowTube;
|
|
28 |
import jdk.incubator.http.internal.common.SSLTube;
|
|
29 |
import jdk.incubator.http.internal.common.SequentialScheduler;
|
|
30 |
import org.testng.annotations.Test;
|
|
31 |
|
|
32 |
import javax.net.ssl.KeyManagerFactory;
|
|
33 |
import javax.net.ssl.SSLContext;
|
|
34 |
import javax.net.ssl.SSLEngine;
|
|
35 |
import javax.net.ssl.SSLParameters;
|
|
36 |
import javax.net.ssl.TrustManagerFactory;
|
|
37 |
import java.io.File;
|
|
38 |
import java.io.FileInputStream;
|
|
39 |
import java.io.IOException;
|
|
40 |
import java.io.InputStream;
|
|
41 |
import java.nio.ByteBuffer;
|
|
42 |
import java.security.KeyManagementException;
|
|
43 |
import java.security.KeyStore;
|
|
44 |
import java.security.KeyStoreException;
|
|
45 |
import java.security.NoSuchAlgorithmException;
|
|
46 |
import java.security.UnrecoverableKeyException;
|
|
47 |
import java.security.cert.CertificateException;
|
|
48 |
import java.util.List;
|
|
49 |
import java.util.Queue;
|
|
50 |
import java.util.StringTokenizer;
|
|
51 |
import java.util.concurrent.CompletableFuture;
|
|
52 |
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
53 |
import java.util.concurrent.Executor;
|
|
54 |
import java.util.concurrent.ExecutorService;
|
|
55 |
import java.util.concurrent.Executors;
|
|
56 |
import java.util.concurrent.Flow;
|
|
57 |
import java.util.concurrent.ForkJoinPool;
|
|
58 |
import java.util.concurrent.SubmissionPublisher;
|
|
59 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
60 |
import java.util.concurrent.atomic.AtomicLong;
|
|
61 |
|
|
62 |
@Test
|
|
63 |
public class SSLTubeTest {
|
|
64 |
|
|
65 |
private static final long COUNTER = 600;
|
|
66 |
private static final int LONGS_PER_BUF = 800;
|
|
67 |
private static final long TOTAL_LONGS = COUNTER * LONGS_PER_BUF;
|
|
68 |
|
|
69 |
private static ByteBuffer getBuffer(long startingAt) {
|
|
70 |
ByteBuffer buf = ByteBuffer.allocate(LONGS_PER_BUF * 8);
|
|
71 |
for (int j = 0; j < LONGS_PER_BUF; j++) {
|
|
72 |
buf.putLong(startingAt++);
|
|
73 |
}
|
|
74 |
buf.flip();
|
|
75 |
return buf;
|
|
76 |
}
|
|
77 |
|
|
78 |
@Test(timeOut = 30000)
|
|
79 |
public void run() throws IOException {
|
|
80 |
/* Start of wiring */
|
|
81 |
ExecutorService sslExecutor = Executors.newCachedThreadPool();
|
|
82 |
/* Emulates an echo server */
|
|
83 |
FlowTube server = new SSLTube(createSSLEngine(false),
|
|
84 |
sslExecutor,
|
|
85 |
new EchoTube(16));
|
|
86 |
FlowTube client = new SSLTube(createSSLEngine(true),
|
|
87 |
sslExecutor,
|
|
88 |
server);
|
|
89 |
SubmissionPublisher<List<ByteBuffer>> p =
|
|
90 |
new SubmissionPublisher<>(ForkJoinPool.commonPool(),
|
|
91 |
Integer.MAX_VALUE);
|
|
92 |
FlowTube.TubePublisher begin = p::subscribe;
|
|
93 |
CompletableFuture<Void> completion = new CompletableFuture<>();
|
|
94 |
EndSubscriber end = new EndSubscriber(TOTAL_LONGS, completion);
|
|
95 |
client.connectFlows(begin, end);
|
|
96 |
/* End of wiring */
|
|
97 |
|
|
98 |
long count = 0;
|
|
99 |
System.out.printf("Submitting %d buffer arrays\n", COUNTER);
|
|
100 |
System.out.printf("LoopCount should be %d\n", TOTAL_LONGS);
|
|
101 |
for (long i = 0; i < COUNTER; i++) {
|
|
102 |
ByteBuffer b = getBuffer(count);
|
|
103 |
count += LONGS_PER_BUF;
|
|
104 |
p.submit(List.of(b));
|
|
105 |
}
|
|
106 |
System.out.println("Finished submission. Waiting for loopback");
|
|
107 |
p.close();
|
|
108 |
try {
|
|
109 |
completion.join();
|
|
110 |
System.out.println("OK");
|
|
111 |
} finally {
|
|
112 |
sslExecutor.shutdownNow();
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
private static final class EchoTube implements FlowTube {
|
|
117 |
|
|
118 |
private final static Object EOF = new Object();
|
|
119 |
private final Executor executor = Executors.newSingleThreadExecutor();
|
|
120 |
|
|
121 |
private final Queue<Object> queue = new ConcurrentLinkedQueue<>();
|
|
122 |
private final int maxQueueSize;
|
|
123 |
private final SequentialScheduler processingScheduler =
|
|
124 |
new SequentialScheduler(createProcessingTask());
|
|
125 |
|
|
126 |
/* Writing into this tube */
|
|
127 |
private long unfulfilled;
|
|
128 |
private Flow.Subscription subscription;
|
|
129 |
|
|
130 |
/* Reading from this tube */
|
|
131 |
private final Demand demand = new Demand();
|
|
132 |
private final AtomicBoolean cancelled = new AtomicBoolean();
|
|
133 |
private Flow.Subscriber<? super List<ByteBuffer>> subscriber;
|
|
134 |
|
|
135 |
private EchoTube(int maxBufferSize) {
|
|
136 |
if (maxBufferSize < 1)
|
|
137 |
throw new IllegalArgumentException();
|
|
138 |
this.maxQueueSize = maxBufferSize;
|
|
139 |
}
|
|
140 |
|
|
141 |
@Override
|
|
142 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) {
|
|
143 |
this.subscriber = subscriber;
|
|
144 |
this.subscriber.onSubscribe(new InternalSubscription());
|
|
145 |
}
|
|
146 |
|
|
147 |
@Override
|
|
148 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
149 |
unfulfilled = maxQueueSize;
|
|
150 |
(this.subscription = subscription).request(maxQueueSize);
|
|
151 |
}
|
|
152 |
|
|
153 |
@Override
|
|
154 |
public void onNext(List<ByteBuffer> item) {
|
|
155 |
if (--unfulfilled == (maxQueueSize / 2)) {
|
|
156 |
subscription.request(maxQueueSize - unfulfilled);
|
|
157 |
unfulfilled = maxQueueSize;
|
|
158 |
}
|
|
159 |
queue.add(item);
|
|
160 |
processingScheduler.deferOrSchedule(executor);
|
|
161 |
}
|
|
162 |
|
|
163 |
@Override
|
|
164 |
public void onError(Throwable throwable) {
|
|
165 |
queue.add(throwable);
|
|
166 |
processingScheduler.deferOrSchedule(executor);
|
|
167 |
}
|
|
168 |
|
|
169 |
@Override
|
|
170 |
public void onComplete() {
|
|
171 |
queue.add(EOF);
|
|
172 |
processingScheduler.deferOrSchedule(executor);
|
|
173 |
}
|
|
174 |
|
|
175 |
@Override
|
|
176 |
public boolean isFinished() {
|
|
177 |
return false;
|
|
178 |
}
|
|
179 |
|
|
180 |
private class InternalSubscription implements Flow.Subscription {
|
|
181 |
|
|
182 |
@Override
|
|
183 |
public void request(long n) {
|
|
184 |
if (n <= 0) {
|
|
185 |
throw new InternalError();
|
|
186 |
}
|
|
187 |
demand.increase(n);
|
|
188 |
processingScheduler.runOrSchedule();
|
|
189 |
}
|
|
190 |
|
|
191 |
@Override
|
|
192 |
public void cancel() {
|
|
193 |
cancelled.set(true);
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
private SequentialScheduler.RestartableTask createProcessingTask() {
|
|
198 |
return new SequentialScheduler.CompleteRestartableTask() {
|
|
199 |
|
|
200 |
@Override
|
|
201 |
protected void run() {
|
|
202 |
while (!cancelled.get()) {
|
|
203 |
Object item = queue.peek();
|
|
204 |
if (item == null)
|
|
205 |
return;
|
|
206 |
try {
|
|
207 |
if (item instanceof List) {
|
|
208 |
if (!demand.tryDecrement())
|
|
209 |
return;
|
|
210 |
@SuppressWarnings("unchecked")
|
|
211 |
List<ByteBuffer> bytes = (List<ByteBuffer>) item;
|
|
212 |
subscriber.onNext(bytes);
|
|
213 |
} else if (item instanceof Throwable) {
|
|
214 |
cancelled.set(true);
|
|
215 |
subscriber.onError((Throwable) item);
|
|
216 |
} else if (item == EOF) {
|
|
217 |
cancelled.set(true);
|
|
218 |
subscriber.onComplete();
|
|
219 |
} else {
|
|
220 |
throw new InternalError(String.valueOf(item));
|
|
221 |
}
|
|
222 |
} finally {
|
|
223 |
Object removed = queue.remove();
|
|
224 |
assert removed == item;
|
|
225 |
}
|
|
226 |
}
|
|
227 |
}
|
|
228 |
};
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
* The final subscriber which receives the decrypted looped-back data. Just
|
|
234 |
* needs to compare the data with what was sent. The given CF is either
|
|
235 |
* completed exceptionally with an error or normally on success.
|
|
236 |
*/
|
|
237 |
private static class EndSubscriber implements FlowTube.TubeSubscriber {
|
|
238 |
|
|
239 |
private static final int REQUEST_WINDOW = 13;
|
|
240 |
|
|
241 |
private final long nbytes;
|
|
242 |
private final AtomicLong counter = new AtomicLong();
|
|
243 |
private final CompletableFuture<?> completion;
|
|
244 |
private volatile Flow.Subscription subscription;
|
|
245 |
private long unfulfilled;
|
|
246 |
|
|
247 |
EndSubscriber(long nbytes, CompletableFuture<?> completion) {
|
|
248 |
this.nbytes = nbytes;
|
|
249 |
this.completion = completion;
|
|
250 |
}
|
|
251 |
|
|
252 |
@Override
|
|
253 |
public void onSubscribe(Flow.Subscription subscription) {
|
|
254 |
this.subscription = subscription;
|
|
255 |
unfulfilled = REQUEST_WINDOW;
|
|
256 |
subscription.request(REQUEST_WINDOW);
|
|
257 |
}
|
|
258 |
|
|
259 |
public static String info(List<ByteBuffer> i) {
|
|
260 |
StringBuilder sb = new StringBuilder();
|
|
261 |
sb.append("size: ").append(Integer.toString(i.size()));
|
|
262 |
int x = 0;
|
|
263 |
for (ByteBuffer b : i)
|
|
264 |
x += b.remaining();
|
|
265 |
sb.append(" bytes: ").append(x);
|
|
266 |
return sb.toString();
|
|
267 |
}
|
|
268 |
|
|
269 |
@Override
|
|
270 |
public void onNext(List<ByteBuffer> buffers) {
|
|
271 |
if (--unfulfilled == (REQUEST_WINDOW / 2)) {
|
|
272 |
subscription.request(REQUEST_WINDOW - unfulfilled);
|
|
273 |
unfulfilled = REQUEST_WINDOW;
|
|
274 |
}
|
|
275 |
|
|
276 |
long currval = counter.get();
|
|
277 |
if (currval % 500 == 0) {
|
|
278 |
System.out.println("End: " + currval);
|
|
279 |
}
|
|
280 |
|
|
281 |
for (ByteBuffer buf : buffers) {
|
|
282 |
while (buf.hasRemaining()) {
|
|
283 |
long n = buf.getLong();
|
|
284 |
if (currval > (SSLTubeTest.TOTAL_LONGS - 50)) {
|
|
285 |
System.out.println("End: " + currval);
|
|
286 |
}
|
|
287 |
if (n != currval++) {
|
|
288 |
System.out.println("ERROR at " + n + " != " + (currval - 1));
|
|
289 |
completion.completeExceptionally(new RuntimeException("ERROR"));
|
|
290 |
subscription.cancel();
|
|
291 |
return;
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
counter.set(currval);
|
|
297 |
}
|
|
298 |
|
|
299 |
@Override
|
|
300 |
public void onError(Throwable throwable) {
|
|
301 |
completion.completeExceptionally(throwable);
|
|
302 |
}
|
|
303 |
|
|
304 |
@Override
|
|
305 |
public void onComplete() {
|
|
306 |
long n = counter.get();
|
|
307 |
if (n != nbytes) {
|
|
308 |
System.out.printf("nbytes=%d n=%d\n", nbytes, n);
|
|
309 |
completion.completeExceptionally(new RuntimeException("ERROR AT END"));
|
|
310 |
} else {
|
|
311 |
System.out.println("DONE OK");
|
|
312 |
completion.complete(null);
|
|
313 |
}
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
private static SSLEngine createSSLEngine(boolean client) throws IOException {
|
|
318 |
SSLContext context = (new SimpleSSLContext()).get();
|
|
319 |
SSLEngine engine = context.createSSLEngine();
|
|
320 |
SSLParameters params = context.getSupportedSSLParameters();
|
|
321 |
params.setProtocols(new String[]{"TLSv1.2"}); // TODO: This is essential. Needs to be protocol impl
|
|
322 |
if (client) {
|
|
323 |
params.setApplicationProtocols(new String[]{"proto1", "proto2"}); // server will choose proto2
|
|
324 |
} else {
|
|
325 |
params.setApplicationProtocols(new String[]{"proto2"}); // server will choose proto2
|
|
326 |
}
|
|
327 |
engine.setSSLParameters(params);
|
|
328 |
engine.setUseClientMode(client);
|
|
329 |
return engine;
|
|
330 |
}
|
|
331 |
|
|
332 |
/**
|
|
333 |
* Creates a simple usable SSLContext for SSLSocketFactory or a HttpsServer
|
|
334 |
* using either a given keystore or a default one in the test tree.
|
|
335 |
*
|
|
336 |
* Using this class with a security manager requires the following
|
|
337 |
* permissions to be granted:
|
|
338 |
*
|
|
339 |
* permission "java.util.PropertyPermission" "test.src.path", "read";
|
|
340 |
* permission java.io.FilePermission "${test.src}/../../../../lib/testlibrary/jdk/testlibrary/testkeys",
|
|
341 |
* "read"; The exact path above depends on the location of the test.
|
|
342 |
*/
|
|
343 |
private static class SimpleSSLContext {
|
|
344 |
|
|
345 |
private final SSLContext ssl;
|
|
346 |
|
|
347 |
/**
|
|
348 |
* Loads default keystore from SimpleSSLContext source directory
|
|
349 |
*/
|
|
350 |
public SimpleSSLContext() throws IOException {
|
|
351 |
String paths = System.getProperty("test.src.path");
|
|
352 |
StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
|
|
353 |
boolean securityExceptions = false;
|
|
354 |
SSLContext sslContext = null;
|
|
355 |
while (st.hasMoreTokens()) {
|
|
356 |
String path = st.nextToken();
|
|
357 |
try {
|
|
358 |
File f = new File(path, "../../../../lib/testlibrary/jdk/testlibrary/testkeys");
|
|
359 |
if (f.exists()) {
|
|
360 |
try (FileInputStream fis = new FileInputStream(f)) {
|
|
361 |
sslContext = init(fis);
|
|
362 |
break;
|
|
363 |
}
|
|
364 |
}
|
|
365 |
} catch (SecurityException e) {
|
|
366 |
// catch and ignore because permission only required
|
|
367 |
// for one entry on path (at most)
|
|
368 |
securityExceptions = true;
|
|
369 |
}
|
|
370 |
}
|
|
371 |
if (securityExceptions) {
|
|
372 |
System.err.println("SecurityExceptions thrown on loading testkeys");
|
|
373 |
}
|
|
374 |
ssl = sslContext;
|
|
375 |
}
|
|
376 |
|
|
377 |
private SSLContext init(InputStream i) throws IOException {
|
|
378 |
try {
|
|
379 |
char[] passphrase = "passphrase".toCharArray();
|
|
380 |
KeyStore ks = KeyStore.getInstance("JKS");
|
|
381 |
ks.load(i, passphrase);
|
|
382 |
|
|
383 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
|
384 |
kmf.init(ks, passphrase);
|
|
385 |
|
|
386 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
|
|
387 |
tmf.init(ks);
|
|
388 |
|
|
389 |
SSLContext ssl = SSLContext.getInstance("TLS");
|
|
390 |
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
|
391 |
return ssl;
|
|
392 |
} catch (KeyManagementException | KeyStoreException |
|
|
393 |
UnrecoverableKeyException | CertificateException |
|
|
394 |
NoSuchAlgorithmException e) {
|
|
395 |
throw new RuntimeException(e.getMessage());
|
|
396 |
}
|
|
397 |
}
|
|
398 |
|
|
399 |
public SSLContext get() {
|
|
400 |
return ssl;
|
|
401 |
}
|
|
402 |
}
|
|
403 |
}
|