57485
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
/*
|
|
25 |
* @test
|
|
26 |
* @library /test/lib
|
|
27 |
* @summary Test that a New Session Ticket will be generated when a
|
|
28 |
* SSLSessionBindingListener is set (boundValues)
|
|
29 |
* @run main/othervm ResumptionUpdateBoundValues
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.OutputStream;
|
|
34 |
import java.lang.ref.Reference;
|
|
35 |
import java.lang.ref.WeakReference;
|
|
36 |
import java.util.concurrent.ArrayBlockingQueue;
|
|
37 |
|
|
38 |
import javax.net.ssl.SSLServerSocket;
|
|
39 |
import javax.net.ssl.SSLServerSocketFactory;
|
|
40 |
import javax.net.ssl.SSLSession;
|
|
41 |
import javax.net.ssl.SSLSessionBindingEvent;
|
|
42 |
import javax.net.ssl.SSLSessionBindingListener;
|
|
43 |
import javax.net.ssl.SSLSocket;
|
|
44 |
import javax.net.ssl.SSLSocketFactory;
|
|
45 |
|
|
46 |
import jdk.test.lib.process.OutputAnalyzer;
|
|
47 |
import jdk.test.lib.process.ProcessTools;
|
|
48 |
import jdk.test.lib.Utils;
|
|
49 |
|
|
50 |
public class ResumptionUpdateBoundValues {
|
|
51 |
|
|
52 |
static boolean separateServerThread = true;
|
|
53 |
|
|
54 |
/*
|
|
55 |
* Where do we find the keystores?
|
|
56 |
*/
|
|
57 |
static String pathToStores = "../../../../javax/net/ssl/etc/";
|
|
58 |
static String keyStoreFile = "keystore";
|
|
59 |
static String trustStoreFile = "truststore";
|
|
60 |
static String passwd = "passphrase";
|
|
61 |
|
|
62 |
/*
|
|
63 |
* Is the server ready to serve?
|
|
64 |
*/
|
|
65 |
volatile static boolean serverReady = false;
|
|
66 |
|
|
67 |
/*
|
|
68 |
* Turn on SSL debugging?
|
|
69 |
*/
|
|
70 |
static boolean debug = false;
|
|
71 |
|
|
72 |
/*
|
|
73 |
* Define the server side of the test.
|
|
74 |
*
|
|
75 |
* If the server prematurely exits, serverReady will be set to true
|
|
76 |
* to avoid infinite hangs.
|
|
77 |
*/
|
|
78 |
void doServerSide() throws Exception {
|
|
79 |
SSLServerSocketFactory sslssf =
|
|
80 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
|
81 |
SSLServerSocket sslServerSocket =
|
|
82 |
(SSLServerSocket) sslssf.createServerSocket(serverPort);
|
|
83 |
serverPort = sslServerSocket.getLocalPort();
|
|
84 |
|
|
85 |
/*
|
|
86 |
* Signal Client, we're ready for his connect.
|
|
87 |
*/
|
|
88 |
serverReady = true;
|
|
89 |
|
|
90 |
while (serverReady) {
|
|
91 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
|
|
92 |
InputStream sslIS = sslSocket.getInputStream();
|
|
93 |
OutputStream sslOS = sslSocket.getOutputStream();
|
|
94 |
|
|
95 |
sslIS.read();
|
|
96 |
sslOS.write(85);
|
|
97 |
sslOS.flush();
|
|
98 |
SSLSession sslSession = sslSocket.getSession();
|
|
99 |
SBListener sbListener = new SBListener(sslSession);
|
|
100 |
sslSession.putValue("x", sbListener);
|
|
101 |
|
|
102 |
sslIS.read();
|
|
103 |
sslOS.write(85);
|
|
104 |
sslOS.flush();
|
|
105 |
|
|
106 |
sslSocket.close();
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
/*
|
|
111 |
* Define the client side of the test.
|
|
112 |
*
|
|
113 |
* If the server prematurely exits, serverReady will be set to true
|
|
114 |
* to avoid infinite hangs.
|
|
115 |
*/
|
|
116 |
SBListener doClientSide() throws Exception {
|
|
117 |
|
|
118 |
/*
|
|
119 |
* Wait for server to get started.
|
|
120 |
*/
|
|
121 |
while (!serverReady) {
|
|
122 |
Thread.sleep(50);
|
|
123 |
}
|
|
124 |
|
|
125 |
SSLSocketFactory sslsf =
|
|
126 |
(SSLSocketFactory) SSLSocketFactory.getDefault();
|
|
127 |
|
|
128 |
try {
|
|
129 |
SSLSocket sslSocket = (SSLSocket)
|
|
130 |
sslsf.createSocket("localhost", serverPort);
|
|
131 |
InputStream sslIS = sslSocket.getInputStream();
|
|
132 |
OutputStream sslOS = sslSocket.getOutputStream();
|
|
133 |
|
|
134 |
sslOS.write(280);
|
|
135 |
sslOS.flush();
|
|
136 |
sslIS.read();
|
|
137 |
|
|
138 |
SSLSession sslSession = sslSocket.getSession();
|
|
139 |
System.out.printf(" sslSession: %s %n %s%n", sslSession, sslSession.getClass());
|
|
140 |
SBListener sbListener = new SBListener(sslSession);
|
|
141 |
|
|
142 |
sslOS.write(280);
|
|
143 |
sslOS.flush();
|
|
144 |
sslIS.read();
|
|
145 |
|
|
146 |
sslOS.write(280);
|
|
147 |
sslOS.flush();
|
|
148 |
sslIS.read();
|
|
149 |
|
|
150 |
sslOS.close();
|
|
151 |
sslIS.close();
|
|
152 |
sslSocket.close();
|
|
153 |
|
|
154 |
sslOS = null;
|
|
155 |
sslIS = null;
|
|
156 |
sslSession = null;
|
|
157 |
sslSocket = null;
|
|
158 |
Reference.reachabilityFence(sslOS);
|
|
159 |
Reference.reachabilityFence(sslIS);
|
|
160 |
Reference.reachabilityFence(sslSession);
|
|
161 |
Reference.reachabilityFence(sslSocket);
|
|
162 |
|
|
163 |
return sbListener;
|
|
164 |
} catch (Exception ex) {
|
|
165 |
ex.printStackTrace();
|
|
166 |
throw ex;
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
/*
|
|
171 |
* =============================================================
|
|
172 |
* The remainder is just support stuff
|
|
173 |
*/
|
|
174 |
|
|
175 |
// use any free port by default
|
|
176 |
volatile int serverPort = 0;
|
|
177 |
|
|
178 |
volatile Exception serverException = null;
|
|
179 |
volatile Exception clientException = null;
|
|
180 |
|
|
181 |
public static void main(String[] args) throws Exception {
|
|
182 |
|
|
183 |
if (args.length == 0) {
|
|
184 |
System.setProperty("test.java.opts",
|
|
185 |
"-Dtest.src=" + System.getProperty("test.src") +
|
|
186 |
" -Dtest.jdk=" + System.getProperty("test.jdk") +
|
|
187 |
" -Djavax.net.debug=ssl,handshake");
|
|
188 |
|
|
189 |
System.out.println("test.java.opts: " +
|
|
190 |
System.getProperty("test.java.opts"));
|
|
191 |
|
|
192 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
|
|
193 |
Utils.addTestJavaOpts("ResumptionUpdateBoundValues", "p"));
|
|
194 |
|
|
195 |
OutputAnalyzer output = ProcessTools.executeProcess(pb);
|
|
196 |
try {
|
|
197 |
output.shouldContain("trigger new session ticket");
|
|
198 |
System.out.println("Found NST in debugging");
|
|
199 |
} catch (Exception e) {
|
|
200 |
throw e;
|
|
201 |
} finally {
|
|
202 |
System.out.println("-- BEGIN Stdout:");
|
|
203 |
System.out.println(output.getStdout());
|
|
204 |
System.out.println("-- END Stdout");
|
|
205 |
System.out.println("-- BEGIN Stderr:");
|
|
206 |
System.out.println(output.getStderr());
|
|
207 |
System.out.println("-- END Stderr");
|
|
208 |
}
|
|
209 |
return;
|
|
210 |
}
|
|
211 |
|
|
212 |
String keyFilename =
|
|
213 |
System.getProperty("test.src", "./") + "/" + pathToStores +
|
|
214 |
"/" + keyStoreFile;
|
|
215 |
String trustFilename =
|
|
216 |
System.getProperty("test.src", "./") + "/" + pathToStores +
|
|
217 |
"/" + trustStoreFile;
|
|
218 |
System.setProperty("javax.net.ssl.keyStore", keyFilename);
|
|
219 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
|
|
220 |
System.setProperty("javax.net.ssl.trustStore", trustFilename);
|
|
221 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
|
|
222 |
|
|
223 |
if (debug)
|
|
224 |
System.setProperty("javax.net.debug", "all");
|
|
225 |
|
|
226 |
/*
|
|
227 |
* Start the tests.
|
|
228 |
*/
|
|
229 |
|
|
230 |
new ResumptionUpdateBoundValues();
|
|
231 |
}
|
|
232 |
|
|
233 |
ArrayBlockingQueue<Thread> threads = new ArrayBlockingQueue<Thread>(100);
|
|
234 |
|
|
235 |
ArrayBlockingQueue<SBListener> sbListeners = new ArrayBlockingQueue<>(100);
|
|
236 |
|
|
237 |
/*
|
|
238 |
* Primary constructor, used to drive remainder of the test.
|
|
239 |
*
|
|
240 |
* Fork off the other side, then do your work.
|
|
241 |
*/
|
|
242 |
ResumptionUpdateBoundValues() throws Exception {
|
|
243 |
final int count = 1;
|
|
244 |
if (separateServerThread) {
|
|
245 |
startServer(true);
|
|
246 |
startClients(true, count);
|
|
247 |
} else {
|
|
248 |
startClients(true, count);
|
|
249 |
startServer(true);
|
|
250 |
}
|
|
251 |
|
|
252 |
/*
|
|
253 |
* Wait for other side to close down.
|
|
254 |
*/
|
|
255 |
Thread t;
|
|
256 |
while ((t = threads.take()) != Thread.currentThread()) {
|
|
257 |
System.out.printf(" joining: %s%n", t);
|
|
258 |
t.join(1000L);
|
|
259 |
}
|
|
260 |
serverReady = false;
|
|
261 |
System.gc();
|
|
262 |
System.gc();
|
|
263 |
|
|
264 |
|
|
265 |
SBListener listener = null;
|
|
266 |
while ((listener = sbListeners.poll()) != null) {
|
|
267 |
if (!listener.check()) {
|
|
268 |
System.out.printf(" sbListener not called on finalize: %s%n",
|
|
269 |
listener);
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
/*
|
|
274 |
* When we get here, the test is pretty much over.
|
|
275 |
*
|
|
276 |
* If the main thread excepted, that propagates back
|
|
277 |
* immediately. If the other thread threw an exception, we
|
|
278 |
* should report back.
|
|
279 |
*/
|
|
280 |
if (serverException != null) {
|
|
281 |
System.out.print("Server Exception:");
|
|
282 |
throw serverException;
|
|
283 |
}
|
|
284 |
if (clientException != null) {
|
|
285 |
System.out.print("Client Exception:");
|
|
286 |
throw clientException;
|
|
287 |
}
|
|
288 |
}
|
|
289 |
|
|
290 |
void startServer(boolean newThread) throws Exception {
|
|
291 |
if (newThread) {
|
|
292 |
Thread t = new Thread("Server") {
|
|
293 |
public void run() {
|
|
294 |
try {
|
|
295 |
doServerSide();
|
|
296 |
} catch (Exception e) {
|
|
297 |
/*
|
|
298 |
* Our server thread just died.
|
|
299 |
*
|
|
300 |
* Release the client, if not active already...
|
|
301 |
*/
|
|
302 |
System.err.println("Server died..." + e);
|
|
303 |
serverReady = true;
|
|
304 |
serverException = e;
|
|
305 |
}
|
|
306 |
}
|
|
307 |
};
|
|
308 |
threads.add(t);
|
|
309 |
t.setDaemon(true);
|
|
310 |
t.start();
|
|
311 |
} else {
|
|
312 |
doServerSide();
|
|
313 |
}
|
|
314 |
}
|
|
315 |
|
|
316 |
void startClients(boolean newThread, int count) throws Exception {
|
|
317 |
for (int i = 0; i < count; i++) {
|
|
318 |
System.out.printf(" newClient: %d%n", i);
|
|
319 |
startClient(newThread);
|
|
320 |
}
|
|
321 |
serverReady = false;
|
|
322 |
|
|
323 |
threads.add(Thread.currentThread()); // add ourselves at the 'end'
|
|
324 |
}
|
|
325 |
void startClient(boolean newThread) throws Exception {
|
|
326 |
if (newThread) {
|
|
327 |
Thread t = new Thread("Client") {
|
|
328 |
public void run() {
|
|
329 |
try {
|
|
330 |
sbListeners.add(doClientSide());
|
|
331 |
} catch (Exception e) {
|
|
332 |
/*
|
|
333 |
* Our client thread just died.
|
|
334 |
*/
|
|
335 |
System.err.println("Client died..." + e);
|
|
336 |
clientException = e;
|
|
337 |
}
|
|
338 |
}
|
|
339 |
};
|
|
340 |
System.out.printf(" starting: %s%n", t);
|
|
341 |
threads.add(t);
|
|
342 |
t.start();
|
|
343 |
} else {
|
|
344 |
sbListeners.add(doClientSide());
|
|
345 |
}
|
|
346 |
}
|
|
347 |
|
|
348 |
|
|
349 |
static class SBListener implements SSLSessionBindingListener {
|
|
350 |
private volatile int unboundNotified;
|
|
351 |
private final WeakReference<SSLSession> session;
|
|
352 |
|
|
353 |
SBListener(SSLSession session) {
|
|
354 |
this.unboundNotified = 0;
|
|
355 |
this.session = new WeakReference<SSLSession>(session);
|
|
356 |
}
|
|
357 |
|
|
358 |
boolean check() {
|
|
359 |
System.out.printf(" check: %s%n", this);
|
|
360 |
return unboundNotified > 0 && session.get() == null;
|
|
361 |
}
|
|
362 |
|
|
363 |
@Override
|
|
364 |
public void valueBound(SSLSessionBindingEvent event) {
|
|
365 |
System.out.printf(" valueBound: %s%n", event.getName());
|
|
366 |
}
|
|
367 |
|
|
368 |
@Override
|
|
369 |
public void valueUnbound(SSLSessionBindingEvent event) {
|
|
370 |
System.out.printf(" valueUnbound: %s%n", event.getName());
|
|
371 |
unboundNotified++;
|
|
372 |
}
|
|
373 |
|
|
374 |
public String toString() {
|
|
375 |
return "count: " + unboundNotified +
|
|
376 |
", ref: " + session.get();
|
|
377 |
}
|
|
378 |
}
|
|
379 |
}
|
|
380 |
|