Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/BaseInteropTest.java
41154 views
1
/*
2
* Copyright (c) 2020, 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
import java.io.FileOutputStream;
25
import java.io.IOException;
26
import java.io.PrintStream;
27
import java.nio.file.Path;
28
import java.nio.file.Paths;
29
import java.util.ArrayList;
30
import java.util.List;
31
import java.util.concurrent.Callable;
32
import java.util.concurrent.ExecutorService;
33
import java.util.concurrent.Executors;
34
35
/*
36
* The base interop test on SSL/TLS communication.
37
*/
38
public abstract class BaseInteropTest<U extends UseCase> {
39
40
protected final Product serverProduct;
41
protected final Product clientProduct;
42
private static final int MAX_SERVER_RETRIES = 3;
43
44
public BaseInteropTest(Product serverProduct, Product clientProduct) {
45
this.serverProduct = serverProduct;
46
this.clientProduct = clientProduct;
47
}
48
49
public boolean isJdkClient() {
50
return Jdk.DEFAULT.equals(clientProduct);
51
}
52
53
/*
54
* This main entrance of the test execution.
55
*/
56
protected void execute() throws Exception {
57
System.out.printf("Server: %s%nClient: %s%n",
58
serverProduct, clientProduct);
59
60
if (skipExecute()) {
61
System.out.println("This execution was skipped.");
62
return;
63
}
64
65
List<TestCase<U>> testCases = null;
66
67
Path logPath = getLogPath();
68
if (logPath != null) {
69
System.out.println("Log: " + logPath);
70
71
PrintStream origStdOut = System.out;
72
PrintStream origStdErr = System.err;
73
try (PrintStream printStream = new PrintStream(
74
new FileOutputStream(logPath.toFile()))) {
75
System.setOut(printStream);
76
System.setErr(printStream);
77
78
testCases = runTest();
79
} finally {
80
System.setOut(origStdOut);
81
System.setErr(origStdErr);
82
}
83
} else {
84
testCases = runTest();
85
}
86
87
boolean fail = false;
88
System.out.println("########## Failed Cases Start ##########");
89
for (TestCase<U> testCase : testCases) {
90
if (testCase.getStatus() == Status.FAIL) {
91
System.out.println("--------------------");
92
System.out.println(testCase);
93
System.out.println("--------------------");
94
fail = true;
95
}
96
}
97
System.out.println("########## Failed Cases End ##########");
98
99
if (fail) {
100
throw new RuntimeException(
101
"At least one case failed! Please check log for details.");
102
} else {
103
System.out.println("This test passed!");
104
}
105
}
106
107
/*
108
* If either of server and client products is unavailable,
109
* just skip this test execution.
110
*/
111
protected boolean skipExecute() {
112
return serverProduct.getPath() == null || clientProduct.getPath() == null;
113
}
114
115
/*
116
* Returns the log path.
117
* If null, no output will be redirected to local file.
118
*/
119
protected Path getLogPath() {
120
return Utilities.LOG_PATH == null
121
? null : Paths.get(Utilities.LOG_PATH);
122
}
123
124
/*
125
* Provides a default set of test cases for testing.
126
*/
127
protected abstract List<TestCase<U>> getTestCases();
128
129
/*
130
* Checks if test case should be ignored.
131
*/
132
protected boolean ignoreTestCase(TestCase<U> testCase) {
133
return false;
134
}
135
136
/*
137
* Runs all test cases with the specified products as server and client
138
* respectively.
139
*/
140
protected List<TestCase<U>> runTest() throws Exception {
141
List<TestCase<U>> executedTestCases = new ArrayList<>();
142
143
List<TestCase<U>> testCases = getTestCases();
144
for (TestCase<U> testCase : testCases) {
145
System.out.println("========== Case Start ==========");
146
System.out.println(testCase);
147
148
if (!ignoreTestCase(testCase)) {
149
Status status = runTestCase(testCase);
150
testCase.setStatus(status);
151
152
executedTestCases.add(testCase);
153
} else {
154
System.out.println("Ignored");
155
}
156
157
System.out.println("========== Case End ==========");
158
}
159
160
return executedTestCases;
161
}
162
163
/*
164
* Runs a specific test case.
165
*/
166
protected Status runTestCase(TestCase<U> testCase) throws Exception {
167
Status serverStatus = Status.UNSTARTED;
168
Status clientStatus = Status.UNSTARTED;
169
170
ExecutorService executor = Executors.newFixedThreadPool(1);
171
AbstractServer server = null;
172
try {
173
server = startAndGetServer(testCase.serverCase, executor);
174
int port = server.getPort();
175
System.out.println("Server is listening " + port);
176
serverStatus = Status.PASS;
177
178
try (AbstractClient client = createClient(testCase.clientCase)) {
179
client.connect("localhost", port);
180
clientStatus = Status.PASS;
181
182
if (testCase.clientCase instanceof ExtUseCase) {
183
ExtUseCase serverCase = (ExtUseCase) testCase.serverCase;
184
ExtUseCase clientCase = (ExtUseCase) testCase.clientCase;
185
186
String[] clientAppProtocols = clientCase.getAppProtocols();
187
if (clientAppProtocols != null && clientAppProtocols.length > 0) {
188
String expectedNegoAppProtocol = Utilities.expectedNegoAppProtocol(
189
serverCase.getAppProtocols(),
190
clientAppProtocols);
191
System.out.println("Expected negotiated app protocol: "
192
+ expectedNegoAppProtocol);
193
String negoAppProtocol = getNegoAppProtocol(server, client);
194
System.out.println(
195
"Actual negotiated app protocol: " + negoAppProtocol);
196
if (!Utilities.trimStr(negoAppProtocol).equals(
197
Utilities.trimStr(expectedNegoAppProtocol))) {
198
System.out.println(
199
"Negotiated app protocol is unexpected");
200
clientStatus = Status.FAIL;
201
}
202
}
203
}
204
} catch (Exception exception) {
205
clientStatus = handleClientException(exception);
206
}
207
} catch (Exception exception) {
208
serverStatus = handleServerException(exception);
209
} finally {
210
if (server != null) {
211
server.signalStop();
212
server.close();
213
}
214
215
executor.shutdown();
216
}
217
218
Status caseStatus
219
= serverStatus == Status.PASS && clientStatus == Status.PASS
220
? Status.PASS
221
: Status.FAIL;
222
System.out.printf(
223
"ServerStatus=%s, ClientStatus=%s, CaseStatus=%s%n",
224
serverStatus, clientStatus, caseStatus);
225
return caseStatus;
226
}
227
228
/*
229
* Return a server once it is properly started to avoid client connection issues.
230
* Retry operation if needed, server may fail to bind a port
231
*/
232
protected AbstractServer startAndGetServer(U useCase, ExecutorService executor)
233
throws Exception {
234
int maxRetries = getServerMaxRetries();
235
boolean serverAlive;
236
AbstractServer server;
237
238
do {
239
server = createServer(useCase, executor);
240
serverAlive = Utilities.waitFor(Server::isAlive, server);
241
if (!serverAlive) {
242
server.signalStop();
243
}
244
245
maxRetries--;
246
} while (!serverAlive && maxRetries > 0);
247
248
if (!serverAlive) {
249
throw new RuntimeException("Server failed to start");
250
}
251
252
return server;
253
}
254
255
/*
256
* Handles server side exception, and determines the status.
257
*/
258
protected Status handleServerException(Exception exception) {
259
return handleException(exception);
260
}
261
262
/*
263
* Handles client side exception, and determines the status.
264
*/
265
protected Status handleClientException(Exception exception) {
266
return handleException(exception);
267
}
268
269
private Status handleException(Exception exception) {
270
if (exception == null) {
271
return Status.PASS;
272
}
273
274
exception.printStackTrace(System.out);
275
return Status.FAIL;
276
}
277
278
/*
279
* Creates server.
280
*/
281
protected AbstractServer createServer(U useCase, ExecutorService executor) throws Exception {
282
AbstractServer server = createServerBuilder(useCase).build();
283
executor.submit(new ServerTask(server));
284
return server;
285
}
286
287
protected AbstractServer.Builder createServerBuilder(U useCase)
288
throws Exception {
289
return (JdkServer.Builder) ((JdkServer.Builder) new JdkServer.Builder()
290
.setProtocols(useCase.getProtocols())
291
.setCipherSuites(useCase.getCipherSuites())
292
.setCertTuple(useCase.getCertTuple()))
293
.setClientAuth(useCase.isClientAuth());
294
}
295
296
/*
297
* Creates client.
298
*/
299
protected AbstractClient createClient(U useCase) throws Exception {
300
return createClientBuilder(useCase).build();
301
}
302
303
protected AbstractClient.Builder createClientBuilder(U useCase)
304
throws Exception {
305
return (JdkClient.Builder) new JdkClient.Builder()
306
.setProtocols(useCase.getProtocols())
307
.setCipherSuites(useCase.getCipherSuites())
308
.setCertTuple(useCase.getCertTuple());
309
}
310
311
/*
312
* Returns the maximum number of attempts to start a server.
313
*/
314
protected int getServerMaxRetries() {
315
return MAX_SERVER_RETRIES;
316
}
317
318
/*
319
* Determines the negotiated application protocol.
320
* Generally, using JDK client to get this value.
321
*/
322
protected String getNegoAppProtocol(AbstractServer server,
323
AbstractClient client) throws SSLTestException {
324
return isJdkClient() ? client.getNegoAppProtocol()
325
: server.getNegoAppProtocol();
326
}
327
328
protected static class ServerTask implements Callable<Void> {
329
330
private final AbstractServer server;
331
332
protected ServerTask(AbstractServer server) {
333
this.server = server;
334
}
335
336
@Override
337
public Void call() throws IOException {
338
server.accept();
339
return null;
340
}
341
}
342
343
protected static class ClientTask implements Callable<Exception> {
344
345
private final AbstractClient client;
346
347
private final String host;
348
private final int port;
349
350
protected ClientTask(AbstractClient client, String host, int port) {
351
this.client = client;
352
353
this.host = host;
354
this.port = port;
355
}
356
357
protected ClientTask(AbstractClient client, int port) {
358
this(client, "localhost", port);
359
}
360
361
@Override
362
public Exception call() {
363
try (AbstractClient c = client) {
364
c.connect(host, port);
365
} catch (Exception exception) {
366
return exception;
367
}
368
369
return null;
370
}
371
}
372
}
373
374