author | chegar |
Wed, 06 Dec 2017 14:29:06 +0000 | |
branch | http-client-branch |
changeset 55968 | 11a97b370db0 |
parent 55947 | c4f314605d28 |
child 55970 | 261d4d2f77e2 |
permissions | -rw-r--r-- |
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; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
28 |
import jdk.incubator.http.internal.common.SSLFlowDelegate; |
55763 | 29 |
import jdk.incubator.http.internal.common.SSLTube; |
30 |
import jdk.incubator.http.internal.common.SequentialScheduler; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
31 |
import jdk.incubator.http.internal.common.Utils; |
55763 | 32 |
import org.testng.annotations.Test; |
33 |
||
34 |
import javax.net.ssl.KeyManagerFactory; |
|
35 |
import javax.net.ssl.SSLContext; |
|
36 |
import javax.net.ssl.SSLEngine; |
|
37 |
import javax.net.ssl.SSLParameters; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
38 |
import javax.net.ssl.SSLServerSocket; |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
39 |
import javax.net.ssl.SSLServerSocketFactory; |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
40 |
import javax.net.ssl.SSLSocket; |
55763 | 41 |
import javax.net.ssl.TrustManagerFactory; |
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
42 |
import java.io.BufferedOutputStream; |
55763 | 43 |
import java.io.File; |
44 |
import java.io.FileInputStream; |
|
45 |
import java.io.IOException; |
|
46 |
import java.io.InputStream; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
47 |
import java.io.OutputStream; |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
48 |
import java.net.Socket; |
55763 | 49 |
import java.nio.ByteBuffer; |
50 |
import java.security.KeyManagementException; |
|
51 |
import java.security.KeyStore; |
|
52 |
import java.security.KeyStoreException; |
|
53 |
import java.security.NoSuchAlgorithmException; |
|
54 |
import java.security.UnrecoverableKeyException; |
|
55 |
import java.security.cert.CertificateException; |
|
56 |
import java.util.List; |
|
57 |
import java.util.Queue; |
|
55942
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
58 |
import java.util.Random; |
55763 | 59 |
import java.util.StringTokenizer; |
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
60 |
import java.util.concurrent.BlockingQueue; |
55763 | 61 |
import java.util.concurrent.CompletableFuture; |
62 |
import java.util.concurrent.ConcurrentLinkedQueue; |
|
63 |
import java.util.concurrent.Executor; |
|
64 |
import java.util.concurrent.ExecutorService; |
|
65 |
import java.util.concurrent.Executors; |
|
66 |
import java.util.concurrent.Flow; |
|
67 |
import java.util.concurrent.ForkJoinPool; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
68 |
import java.util.concurrent.LinkedBlockingQueue; |
55763 | 69 |
import java.util.concurrent.SubmissionPublisher; |
70 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
71 |
import java.util.concurrent.atomic.AtomicInteger; |
55763 | 72 |
import java.util.concurrent.atomic.AtomicLong; |
55942
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
73 |
import java.util.concurrent.atomic.AtomicReference; |
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
74 |
import java.util.function.Consumer; |
55763 | 75 |
|
55968
11a97b370db0
http-client-branch: common random seed handling for whitebox tests
chegar
parents:
55947
diff
changeset
|
76 |
public class AbstractSSLTubeTest extends AbstractRandomTest { |
55763 | 77 |
|
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
78 |
public static final long COUNTER = 600; |
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
79 |
public static final int LONGS_PER_BUF = 800; |
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
80 |
public static final long TOTAL_LONGS = COUNTER * LONGS_PER_BUF; |
55942
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
81 |
public static final ByteBuffer SENTINEL = ByteBuffer.allocate(0); |
55763 | 82 |
|
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
83 |
protected static ByteBuffer getBuffer(long startingAt) { |
55763 | 84 |
ByteBuffer buf = ByteBuffer.allocate(LONGS_PER_BUF * 8); |
85 |
for (int j = 0; j < LONGS_PER_BUF; j++) { |
|
86 |
buf.putLong(startingAt++); |
|
87 |
} |
|
88 |
buf.flip(); |
|
89 |
return buf; |
|
90 |
} |
|
91 |
||
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
92 |
protected void run(FlowTube server, ExecutorService sslExecutor) throws IOException { |
55763 | 93 |
FlowTube client = new SSLTube(createSSLEngine(true), |
94 |
sslExecutor, |
|
95 |
server); |
|
96 |
SubmissionPublisher<List<ByteBuffer>> p = |
|
97 |
new SubmissionPublisher<>(ForkJoinPool.commonPool(), |
|
98 |
Integer.MAX_VALUE); |
|
99 |
FlowTube.TubePublisher begin = p::subscribe; |
|
100 |
CompletableFuture<Void> completion = new CompletableFuture<>(); |
|
101 |
EndSubscriber end = new EndSubscriber(TOTAL_LONGS, completion); |
|
102 |
client.connectFlows(begin, end); |
|
103 |
/* End of wiring */ |
|
104 |
||
105 |
long count = 0; |
|
106 |
System.out.printf("Submitting %d buffer arrays\n", COUNTER); |
|
107 |
System.out.printf("LoopCount should be %d\n", TOTAL_LONGS); |
|
108 |
for (long i = 0; i < COUNTER; i++) { |
|
109 |
ByteBuffer b = getBuffer(count); |
|
110 |
count += LONGS_PER_BUF; |
|
111 |
p.submit(List.of(b)); |
|
112 |
} |
|
113 |
System.out.println("Finished submission. Waiting for loopback"); |
|
114 |
p.close(); |
|
115 |
try { |
|
116 |
completion.join(); |
|
117 |
System.out.println("OK"); |
|
118 |
} finally { |
|
119 |
sslExecutor.shutdownNow(); |
|
120 |
} |
|
121 |
} |
|
122 |
||
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
123 |
protected static void sleep(long millis) { |
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
124 |
try { |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
125 |
Thread.sleep(millis); |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
126 |
} catch (InterruptedException e) { |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
127 |
|
55763 | 128 |
} |
129 |
} |
|
55942
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
130 |
|
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
131 |
/** |
55763 | 132 |
* The final subscriber which receives the decrypted looped-back data. Just |
133 |
* needs to compare the data with what was sent. The given CF is either |
|
134 |
* completed exceptionally with an error or normally on success. |
|
135 |
*/ |
|
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
136 |
protected static class EndSubscriber implements FlowTube.TubeSubscriber { |
55763 | 137 |
|
138 |
private static final int REQUEST_WINDOW = 13; |
|
139 |
||
140 |
private final long nbytes; |
|
141 |
private final AtomicLong counter = new AtomicLong(); |
|
142 |
private final CompletableFuture<?> completion; |
|
143 |
private volatile Flow.Subscription subscription; |
|
144 |
private long unfulfilled; |
|
145 |
||
146 |
EndSubscriber(long nbytes, CompletableFuture<?> completion) { |
|
147 |
this.nbytes = nbytes; |
|
148 |
this.completion = completion; |
|
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public void onSubscribe(Flow.Subscription subscription) { |
|
153 |
this.subscription = subscription; |
|
154 |
unfulfilled = REQUEST_WINDOW; |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
155 |
System.out.println("EndSubscriber request " + REQUEST_WINDOW); |
55763 | 156 |
subscription.request(REQUEST_WINDOW); |
157 |
} |
|
158 |
||
159 |
public static String info(List<ByteBuffer> i) { |
|
160 |
StringBuilder sb = new StringBuilder(); |
|
161 |
sb.append("size: ").append(Integer.toString(i.size())); |
|
162 |
int x = 0; |
|
163 |
for (ByteBuffer b : i) |
|
164 |
x += b.remaining(); |
|
165 |
sb.append(" bytes: ").append(x); |
|
166 |
return sb.toString(); |
|
167 |
} |
|
168 |
||
169 |
@Override |
|
170 |
public void onNext(List<ByteBuffer> buffers) { |
|
171 |
if (--unfulfilled == (REQUEST_WINDOW / 2)) { |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
172 |
long req = REQUEST_WINDOW - unfulfilled; |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
173 |
System.out.println("EndSubscriber request " + req); |
55942
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
174 |
unfulfilled = REQUEST_WINDOW; |
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
175 |
subscription.request(req); |
55763 | 176 |
} |
177 |
||
178 |
long currval = counter.get(); |
|
179 |
if (currval % 500 == 0) { |
|
55942
8d4770c22b63
http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents:
55909
diff
changeset
|
180 |
System.out.println("EndSubscriber: " + currval); |
55763 | 181 |
} |
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
182 |
System.out.println("EndSubscriber onNext " + Utils.remaining(buffers)); |
55763 | 183 |
|
184 |
for (ByteBuffer buf : buffers) { |
|
185 |
while (buf.hasRemaining()) { |
|
186 |
long n = buf.getLong(); |
|
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
187 |
if (currval > (AbstractSSLTubeTest.TOTAL_LONGS - 50)) { |
55763 | 188 |
System.out.println("End: " + currval); |
189 |
} |
|
190 |
if (n != currval++) { |
|
191 |
System.out.println("ERROR at " + n + " != " + (currval - 1)); |
|
192 |
completion.completeExceptionally(new RuntimeException("ERROR")); |
|
193 |
subscription.cancel(); |
|
194 |
return; |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
counter.set(currval); |
|
200 |
} |
|
201 |
||
202 |
@Override |
|
203 |
public void onError(Throwable throwable) { |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
204 |
System.out.println("EndSubscriber onError " + throwable); |
55763 | 205 |
completion.completeExceptionally(throwable); |
206 |
} |
|
207 |
||
208 |
@Override |
|
209 |
public void onComplete() { |
|
210 |
long n = counter.get(); |
|
211 |
if (n != nbytes) { |
|
212 |
System.out.printf("nbytes=%d n=%d\n", nbytes, n); |
|
213 |
completion.completeExceptionally(new RuntimeException("ERROR AT END")); |
|
214 |
} else { |
|
215 |
System.out.println("DONE OK"); |
|
216 |
completion.complete(null); |
|
217 |
} |
|
218 |
} |
|
55909
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
219 |
@Override |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
220 |
public String toString() { |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
221 |
return "EndSubscriber"; |
583695a0ed6a
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents:
55763
diff
changeset
|
222 |
} |
55763 | 223 |
} |
224 |
||
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
225 |
protected static SSLEngine createSSLEngine(boolean client) throws IOException { |
55763 | 226 |
SSLContext context = (new SimpleSSLContext()).get(); |
227 |
SSLEngine engine = context.createSSLEngine(); |
|
228 |
SSLParameters params = context.getSupportedSSLParameters(); |
|
229 |
params.setProtocols(new String[]{"TLSv1.2"}); // TODO: This is essential. Needs to be protocol impl |
|
230 |
if (client) { |
|
231 |
params.setApplicationProtocols(new String[]{"proto1", "proto2"}); // server will choose proto2 |
|
232 |
} else { |
|
233 |
params.setApplicationProtocols(new String[]{"proto2"}); // server will choose proto2 |
|
234 |
} |
|
235 |
engine.setSSLParameters(params); |
|
236 |
engine.setUseClientMode(client); |
|
237 |
return engine; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Creates a simple usable SSLContext for SSLSocketFactory or a HttpsServer |
|
242 |
* using either a given keystore or a default one in the test tree. |
|
243 |
* |
|
244 |
* Using this class with a security manager requires the following |
|
245 |
* permissions to be granted: |
|
246 |
* |
|
247 |
* permission "java.util.PropertyPermission" "test.src.path", "read"; |
|
248 |
* permission java.io.FilePermission "${test.src}/../../../../lib/testlibrary/jdk/testlibrary/testkeys", |
|
249 |
* "read"; The exact path above depends on the location of the test. |
|
250 |
*/ |
|
55947
c4f314605d28
http-client-branch: split SSLTubeTest for better diagnosis
dfuchs
parents:
55942
diff
changeset
|
251 |
protected static class SimpleSSLContext { |
55763 | 252 |
|
253 |
private final SSLContext ssl; |
|
254 |
||
255 |
/** |
|
256 |
* Loads default keystore from SimpleSSLContext source directory |
|
257 |
*/ |
|
258 |
public SimpleSSLContext() throws IOException { |
|
259 |
String paths = System.getProperty("test.src.path"); |
|
260 |
StringTokenizer st = new StringTokenizer(paths, File.pathSeparator); |
|
261 |
boolean securityExceptions = false; |
|
262 |
SSLContext sslContext = null; |
|
263 |
while (st.hasMoreTokens()) { |
|
264 |
String path = st.nextToken(); |
|
265 |
try { |
|
266 |
File f = new File(path, "../../../../lib/testlibrary/jdk/testlibrary/testkeys"); |
|
267 |
if (f.exists()) { |
|
268 |
try (FileInputStream fis = new FileInputStream(f)) { |
|
269 |
sslContext = init(fis); |
|
270 |
break; |
|
271 |
} |
|
272 |
} |
|
273 |
} catch (SecurityException e) { |
|
274 |
// catch and ignore because permission only required |
|
275 |
// for one entry on path (at most) |
|
276 |
securityExceptions = true; |
|
277 |
} |
|
278 |
} |
|
279 |
if (securityExceptions) { |
|
280 |
System.err.println("SecurityExceptions thrown on loading testkeys"); |
|
281 |
} |
|
282 |
ssl = sslContext; |
|
283 |
} |
|
284 |
||
285 |
private SSLContext init(InputStream i) throws IOException { |
|
286 |
try { |
|
287 |
char[] passphrase = "passphrase".toCharArray(); |
|
288 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
289 |
ks.load(i, passphrase); |
|
290 |
||
291 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
292 |
kmf.init(ks, passphrase); |
|
293 |
||
294 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
295 |
tmf.init(ks); |
|
296 |
||
297 |
SSLContext ssl = SSLContext.getInstance("TLS"); |
|
298 |
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
299 |
return ssl; |
|
300 |
} catch (KeyManagementException | KeyStoreException | |
|
301 |
UnrecoverableKeyException | CertificateException | |
|
302 |
NoSuchAlgorithmException e) { |
|
303 |
throw new RuntimeException(e.getMessage()); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
public SSLContext get() { |
|
308 |
return ssl; |
|
309 |
} |
|
310 |
} |
|
311 |
} |