Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/SSLSession/SessionCacheSizeTests.java
41152 views
1
/*
2
* Copyright (c) 2001, 2016, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 4366807
32
* @summary Need new APIs to get/set session timeout and session cache size.
33
* @run main/othervm SessionCacheSizeTests
34
*/
35
36
import java.io.*;
37
import java.net.*;
38
import javax.net.ssl.*;
39
import java.util.*;
40
import java.security.*;
41
42
/**
43
* Session cache size tests cover the following cases:
44
* 1. Effect of system property javax.net.ssl.SessionCacheSize (this
45
* property is not documented for public).
46
* 2. Reducing the cache size, results in uncaching of sessions if #of
47
* sessions present exceeds the new size.
48
* 3. Increasing the cache size, results in accomodating new sessions if the
49
* number of cached sessions is the current size limit.
50
*
51
* Invairant for passing this test is, at any given time,
52
* #cached_sessions <= current_cache_size , for current_cache_size > 0
53
*/
54
55
public class SessionCacheSizeTests {
56
57
/*
58
* =============================================================
59
* Set the various variables needed for the tests, then
60
* specify what tests to run on each side.
61
*/
62
63
/*
64
* Should we run the client or server in a separate thread?
65
* Both sides can throw exceptions, but do you have a preference
66
* as to which side should be the main thread.
67
*/
68
static boolean separateServerThread = true;
69
70
/*
71
* Where do we find the keystores?
72
*/
73
static String pathToStores = "../etc";
74
static String keyStoreFile = "keystore";
75
static String trustStoreFile = "truststore";
76
static String passwd = "passphrase";
77
78
/*
79
* Is the server ready to serve?
80
*/
81
volatile static boolean serverReady = false;
82
83
/*
84
* Turn on SSL debugging?
85
*/
86
static boolean debug = false;
87
88
/*
89
* If the client or server is doing some kind of object creation
90
* that the other side depends on, and that thread prematurely
91
* exits, you may experience a hang. The test harness will
92
* terminate all hung threads after its timeout has expired,
93
* currently 3 minutes by default, but you might try to be
94
* smart about it....
95
*/
96
97
/*
98
* Define the server side of the test.
99
*
100
* If the server prematurely exits, serverReady will be set to true
101
* to avoid infinite hangs.
102
*/
103
104
/*
105
* A limit on the number of connections at any given time
106
*/
107
static int MAX_ACTIVE_CONNECTIONS = 4;
108
109
static final int FREE_PORT = 0;
110
111
void doServerSide(int serverConns) throws Exception {
112
try (SSLServerSocket sslServerSocket =
113
(SSLServerSocket) sslssf.createServerSocket(FREE_PORT)) {
114
115
// timeout to accept a connection
116
sslServerSocket.setSoTimeout(45000);
117
118
// make sure createdPorts++ is atomic
119
synchronized(serverPorts) {
120
int serverPort = sslServerSocket.getLocalPort();
121
System.out.printf("server #%d started on port %d%n",
122
createdPorts, serverPort);
123
serverPorts[createdPorts++] = serverPort;
124
125
/*
126
* Signal Client, we're ready for his connect.
127
*/
128
if (createdPorts == serverPorts.length) {
129
serverReady = true;
130
}
131
}
132
int read = 0;
133
int nConnections = 0;
134
135
/*
136
* Divide the max connections among the available server ports.
137
* The use of more than one server port ensures creation of more
138
* than one session.
139
*/
140
SSLSession sessions [] = new SSLSession [serverConns];
141
SSLSessionContext sessCtx = sslctx.getServerSessionContext();
142
143
while (nConnections < serverConns) {
144
try (SSLSocket sslSocket =
145
(SSLSocket)sslServerSocket.accept()) {
146
sslSocket.setSoTimeout(90000); // timeout to read
147
InputStream sslIS = sslSocket.getInputStream();
148
OutputStream sslOS = sslSocket.getOutputStream();
149
read = sslIS.read();
150
sessions[nConnections] = sslSocket.getSession();
151
sslOS.write(85);
152
sslOS.flush();
153
nConnections++;
154
}
155
}
156
}
157
}
158
159
/*
160
* Define the client side of the test.
161
*
162
* If the server prematurely exits, serverReady will be set to true
163
* to avoid infinite hangs.
164
*/
165
void doClientSide() throws Exception {
166
167
/*
168
* Wait for server to get started.
169
*/
170
while (!serverReady) {
171
Thread.sleep(50);
172
}
173
174
int nConnections = 0;
175
SSLSocket sslSockets[] = new SSLSocket [MAX_ACTIVE_CONNECTIONS];
176
Vector sessions = new Vector();
177
SSLSessionContext sessCtx = sslctx.getClientSessionContext();
178
sessCtx.setSessionTimeout(0); // no limit
179
180
try {
181
while (nConnections < (MAX_ACTIVE_CONNECTIONS - 1)) {
182
// divide the connections among the available server ports
183
int serverPort = serverPorts [nConnections % (serverPorts.length)];
184
System.out.printf("client #%d connects to port %d%n",
185
nConnections, serverPort);
186
sslSockets[nConnections] = (SSLSocket) sslsf.
187
createSocket("localhost",
188
serverPort);
189
InputStream sslIS = sslSockets[nConnections].getInputStream();
190
OutputStream sslOS = sslSockets[nConnections].getOutputStream();
191
sslOS.write(237);
192
sslOS.flush();
193
int read = sslIS.read();
194
SSLSession sess = sslSockets[nConnections].getSession();
195
if (!sessions.contains(sess))
196
sessions.add(sess);
197
nConnections++;
198
}
199
System.out.println("Current cacheSize is set to: " +
200
sessCtx.getSessionCacheSize());
201
System.out.println();
202
System.out.println("Currently cached Sessions......");
203
System.out.println("============================================"
204
+ "============================");
205
System.out.println("Session "
206
+ " Session-last-accessTime");
207
System.out.println("============================================"
208
+ "============================");
209
checkCachedSessions(sessCtx, nConnections);
210
// Change session cache size
211
sessCtx.setSessionCacheSize(2);
212
System.out.println("Session cache size changed to: "
213
+ sessCtx.getSessionCacheSize());
214
System.out.println();
215
checkCachedSessions(sessCtx, nConnections);
216
217
// Test the effect of increasing the cache size
218
sessCtx.setSessionCacheSize(3);
219
System.out.println("Session cache size changed to: "
220
+ sessCtx.getSessionCacheSize());
221
// create a new session
222
int serverPort = serverPorts [nConnections % (serverPorts.length)];
223
System.out.printf("client #%d connects to port %d%n",
224
nConnections, serverPort);
225
sslSockets[nConnections] = (SSLSocket) sslsf.
226
createSocket("localhost",
227
serverPort);
228
InputStream sslIS = sslSockets[nConnections].getInputStream();
229
OutputStream sslOS = sslSockets[nConnections].getOutputStream();
230
sslOS.write(237);
231
sslOS.flush();
232
int read = sslIS.read();
233
SSLSession sess = sslSockets[nConnections].getSession();
234
if (!sessions.contains(sess))
235
sessions.add(sess);
236
nConnections++;
237
238
// test the number of sessions cached against the cache size
239
checkCachedSessions(sessCtx, nConnections);
240
} finally {
241
for (int i = 0; i < nConnections; i++) {
242
if (sslSockets[i] != null) {
243
sslSockets[i].close();
244
}
245
}
246
}
247
System.out.println("Session cache size tests passed");
248
}
249
250
void checkCachedSessions(SSLSessionContext sessCtx,
251
int nConn) throws Exception {
252
int nSessions = 0;
253
Enumeration e = sessCtx.getIds();
254
int cacheSize = sessCtx.getSessionCacheSize();
255
SSLSession sess;
256
257
while (e.hasMoreElements()) {
258
sess = sessCtx.getSession((byte[]) e.nextElement());
259
long lastAccessedTime = sess.getLastAccessedTime();
260
System.out.println(sess + " "
261
+ new Date(lastAccessedTime));
262
263
nSessions++;
264
}
265
System.out.println("--------------------------------------------"
266
+ "----------------------------");
267
if ((cacheSize > 0) && (nSessions > cacheSize)) {
268
269
// close all active connections before exiting
270
for (int conn = nConn; conn < MAX_ACTIVE_CONNECTIONS; conn++) {
271
SSLSocket s = (SSLSocket) sslsf.createSocket("localhost",
272
serverPorts [conn % (serverPorts.length)]);
273
s.close();
274
}
275
throw new Exception("Session cache size test failed,"
276
+ " current cache size: " + cacheSize + " #sessions cached: "
277
+ nSessions);
278
}
279
}
280
281
/*
282
* =============================================================
283
* The remainder is just support stuff
284
*/
285
286
/*
287
* #of ports > 1, guarantees creation of more than one session.
288
* Using four ports (one per each connection), we are able to create
289
* alteast four sessions.
290
*/
291
int serverPorts[] = new int[]{0, 0, 0, 0}; // MAX_ACTIVE_CONNECTIONS: 4
292
int createdPorts = 0;
293
static SSLServerSocketFactory sslssf;
294
static SSLSocketFactory sslsf;
295
static SSLContext sslctx;
296
297
volatile Exception serverException = null;
298
volatile Exception clientException = null;
299
300
public static void main(String[] args) throws Exception {
301
String keyFilename =
302
System.getProperty("test.src", "./") + "/" + pathToStores +
303
"/" + keyStoreFile;
304
String trustFilename =
305
System.getProperty("test.src", "./") + "/" + pathToStores +
306
"/" + trustStoreFile;
307
308
System.setProperty("javax.net.ssl.keyStore", keyFilename);
309
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
310
System.setProperty("javax.net.ssl.trustStore", trustFilename);
311
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
312
313
// test the effect of javax.net.ssl.sessionCacheSize
314
System.setProperty("javax.net.ssl.sessionCacheSize", String.valueOf(0));
315
316
sslctx = SSLContext.getInstance("TLS");
317
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
318
KeyStore ks = KeyStore.getInstance("JKS");
319
try (FileInputStream fis = new FileInputStream(keyFilename)) {
320
ks.load(fis, passwd.toCharArray());
321
}
322
kmf.init(ks, passwd.toCharArray());
323
sslctx.init(kmf.getKeyManagers(), null, null);
324
sslssf = (SSLServerSocketFactory) sslctx.getServerSocketFactory();
325
sslsf = (SSLSocketFactory) sslctx.getSocketFactory();
326
if (debug)
327
System.setProperty("javax.net.debug", "all");
328
329
/*
330
* Start the tests.
331
*/
332
new SessionCacheSizeTests();
333
}
334
335
Thread clientThread = null;
336
Thread serverThread = null;
337
338
/*
339
* Primary constructor, used to drive remainder of the test.
340
*
341
* Fork off the other side, then do your work.
342
*/
343
SessionCacheSizeTests() throws Exception {
344
/*
345
* create the SSLServerSocket and SSLSocket factories
346
*/
347
348
/*
349
* Divide the max connections among the available server ports.
350
* The use of more than one server port ensures creation of more
351
* than one session.
352
*/
353
int serverConns = MAX_ACTIVE_CONNECTIONS / (serverPorts.length);
354
int remainingConns = MAX_ACTIVE_CONNECTIONS % (serverPorts.length);
355
356
Exception startException = null;
357
try {
358
if (separateServerThread) {
359
for (int i = 0; i < serverPorts.length; i++) {
360
// distribute remaining connections among the
361
// available ports
362
if (i < remainingConns) {
363
startServer(serverConns + 1, true);
364
} else {
365
startServer(serverConns, true);
366
}
367
}
368
startClient(false);
369
} else {
370
startClient(true);
371
for (int i = 0; i < serverPorts.length; i++) {
372
if (i < remainingConns) {
373
startServer(serverConns + 1, false);
374
} else {
375
startServer(serverConns, false);
376
}
377
}
378
}
379
} catch (Exception e) {
380
startException = e;
381
}
382
383
/*
384
* Wait for other side to close down.
385
*/
386
if (separateServerThread) {
387
if (serverThread != null) {
388
serverThread.join();
389
}
390
} else {
391
if (clientThread != null) {
392
clientThread.join();
393
}
394
}
395
396
/*
397
* When we get here, the test is pretty much over.
398
*/
399
Exception local;
400
Exception remote;
401
402
if (separateServerThread) {
403
remote = serverException;
404
local = clientException;
405
} else {
406
remote = clientException;
407
local = serverException;
408
}
409
410
Exception exception = null;
411
412
/*
413
* Check various exception conditions.
414
*/
415
if ((local != null) && (remote != null)) {
416
// If both failed, return the curthread's exception.
417
local.initCause(remote);
418
exception = local;
419
} else if (local != null) {
420
exception = local;
421
} else if (remote != null) {
422
exception = remote;
423
} else if (startException != null) {
424
exception = startException;
425
}
426
427
/*
428
* If there was an exception *AND* a startException,
429
* output it.
430
*/
431
if (exception != null) {
432
if (exception != startException && startException != null) {
433
exception.addSuppressed(startException);
434
}
435
throw exception;
436
}
437
438
// Fall-through: no exception to throw!
439
}
440
441
void startServer(final int nConns, boolean newThread) throws Exception {
442
if (newThread) {
443
serverThread = new Thread() {
444
public void run() {
445
try {
446
doServerSide(nConns);
447
} catch (Exception e) {
448
/*
449
* Our server thread just died.
450
*
451
* Release the client, if not active already...
452
*/
453
System.err.println("Server died...");
454
e.printStackTrace();
455
serverReady = true;
456
serverException = e;
457
}
458
}
459
};
460
serverThread.start();
461
} else {
462
try {
463
doServerSide(nConns);
464
} catch (Exception e) {
465
serverException = e;
466
} finally {
467
serverReady = true;
468
}
469
}
470
}
471
472
void startClient(boolean newThread)
473
throws Exception {
474
if (newThread) {
475
clientThread = new Thread() {
476
public void run() {
477
try {
478
doClientSide();
479
} catch (Exception e) {
480
/*
481
* Our client thread just died.
482
*/
483
System.err.println("Client died...");
484
clientException = e;
485
}
486
}
487
};
488
clientThread.start();
489
} else {
490
try {
491
doClientSide();
492
} catch (Exception e) {
493
clientException = e;
494
}
495
}
496
}
497
}
498
499