File: BaseInteropTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (372 lines) | stat: -rw-r--r-- 12,393 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
 * The base interop test on SSL/TLS communication.
 */
public abstract class BaseInteropTest<U extends UseCase> {

    protected final Product serverProduct;
    protected final Product clientProduct;
    private static final int MAX_SERVER_RETRIES = 3;

    public BaseInteropTest(Product serverProduct, Product clientProduct) {
        this.serverProduct = serverProduct;
        this.clientProduct = clientProduct;
    }

    public boolean isJdkClient() {
        return Jdk.DEFAULT.equals(clientProduct);
    }

    /*
     * This main entrance of the test execution.
     */
    protected void execute() throws Exception {
        System.out.printf("Server: %s%nClient: %s%n",
                serverProduct, clientProduct);

        if (skipExecute()) {
            System.out.println("This execution was skipped.");
            return;
        }

        List<TestCase<U>> testCases = null;

        Path logPath = getLogPath();
        if (logPath != null) {
            System.out.println("Log: " + logPath);

            PrintStream origStdOut = System.out;
            PrintStream origStdErr = System.err;
            try (PrintStream printStream = new PrintStream(
                    new FileOutputStream(logPath.toFile()))) {
                System.setOut(printStream);
                System.setErr(printStream);

                testCases = runTest();
            } finally {
                System.setOut(origStdOut);
                System.setErr(origStdErr);
            }
        } else {
            testCases = runTest();
        }

        boolean fail = false;
        System.out.println("########## Failed Cases Start ##########");
        for (TestCase<U> testCase : testCases) {
            if (testCase.getStatus() == Status.FAIL) {
                System.out.println("--------------------");
                System.out.println(testCase);
                System.out.println("--------------------");
                fail = true;
            }
        }
        System.out.println("########## Failed Cases End ##########");

        if (fail) {
            throw new RuntimeException(
                    "At least one case failed! Please check log for details.");
        } else {
            System.out.println("This test passed!");
        }
    }

    /*
     * If either of server and client products is unavailable,
     * just skip this test execution.
     */
    protected boolean skipExecute() {
        return serverProduct.getPath() == null || clientProduct.getPath() == null;
    }

    /*
     * Returns the log path.
     * If null, no output will be redirected to local file.
     */
    protected Path getLogPath() {
        return Utilities.LOG_PATH == null
                ? null : Paths.get(Utilities.LOG_PATH);
    }

    /*
     * Provides a default set of test cases for testing.
     */
    protected abstract List<TestCase<U>> getTestCases();

    /*
     * Checks if test case should be ignored.
     */
    protected boolean ignoreTestCase(TestCase<U> testCase) {
        return false;
    }

    /*
     * Runs all test cases with the specified products as server and client
     * respectively.
     */
    protected List<TestCase<U>> runTest() throws Exception {
        List<TestCase<U>> executedTestCases = new ArrayList<>();

        List<TestCase<U>> testCases = getTestCases();
        for (TestCase<U> testCase : testCases) {
            System.out.println("========== Case Start ==========");
            System.out.println(testCase);

            if (!ignoreTestCase(testCase)) {
                Status status = runTestCase(testCase);
                testCase.setStatus(status);

                executedTestCases.add(testCase);
            } else {
                System.out.println("Ignored");
            }

            System.out.println("========== Case End ==========");
        }

        return executedTestCases;
    }

    /*
     * Runs a specific test case.
     */
    protected Status runTestCase(TestCase<U> testCase) throws Exception {
        Status serverStatus = Status.UNSTARTED;
        Status clientStatus = Status.UNSTARTED;

        ExecutorService executor = Executors.newFixedThreadPool(1);
        AbstractServer server = null;
        try {
            server = startAndGetServer(testCase.serverCase, executor);
            int port = server.getPort();
            System.out.println("Server is listening " + port);
            serverStatus = Status.PASS;

            try (AbstractClient client = createClient(testCase.clientCase)) {
                client.connect("localhost", port);
                clientStatus = Status.PASS;

                if (testCase.clientCase instanceof ExtUseCase) {
                    ExtUseCase serverCase = (ExtUseCase) testCase.serverCase;
                    ExtUseCase clientCase = (ExtUseCase) testCase.clientCase;

                    String[] clientAppProtocols = clientCase.getAppProtocols();
                        if (clientAppProtocols != null && clientAppProtocols.length > 0) {
                        String expectedNegoAppProtocol = Utilities.expectedNegoAppProtocol(
                                serverCase.getAppProtocols(),
                                clientAppProtocols);
                        System.out.println("Expected negotiated app protocol: "
                                + expectedNegoAppProtocol);
                        String negoAppProtocol = getNegoAppProtocol(server, client);
                        System.out.println(
                                "Actual negotiated app protocol: " + negoAppProtocol);
                        if (!Utilities.trimStr(negoAppProtocol).equals(
                                Utilities.trimStr(expectedNegoAppProtocol))) {
                            System.out.println(
                                    "Negotiated app protocol is unexpected");
                            clientStatus = Status.FAIL;
                        }
                    }
                }
            } catch (Exception exception) {
                clientStatus = handleClientException(exception);
            }
        } catch (Exception exception) {
            serverStatus = handleServerException(exception);
        } finally {
            if (server != null) {
                server.signalStop();
                server.close();
            }

            executor.shutdown();
        }

        Status caseStatus
                = serverStatus == Status.PASS && clientStatus == Status.PASS
                  ? Status.PASS
                  : Status.FAIL;
        System.out.printf(
                "ServerStatus=%s, ClientStatus=%s, CaseStatus=%s%n",
                serverStatus, clientStatus, caseStatus);
        return caseStatus;
    }

    /*
     * Return a server once it is properly started to avoid client connection issues.
     * Retry operation if needed, server may fail to bind a port
     */
    protected AbstractServer startAndGetServer(U useCase, ExecutorService executor)
            throws Exception {
        int maxRetries = getServerMaxRetries();
        boolean serverAlive;
        AbstractServer server;

        do {
            server = createServer(useCase, executor);
            serverAlive = Utilities.waitFor(Server::isAlive, server);
            if (!serverAlive) {
                server.signalStop();
            }

            maxRetries--;
        } while (!serverAlive && maxRetries > 0);

        if (!serverAlive) {
            throw new RuntimeException("Server failed to start");
        }

        return server;
    }

    /*
     * Handles server side exception, and determines the status.
     */
    protected Status handleServerException(Exception exception) {
        return handleException(exception);
    }

    /*
     * Handles client side exception, and determines the status.
     */
    protected Status handleClientException(Exception exception) {
        return handleException(exception);
    }

    private Status handleException(Exception exception) {
        if (exception == null) {
            return Status.PASS;
        }

        exception.printStackTrace(System.out);
        return Status.FAIL;
    }

    /*
     * Creates server.
     */
    protected AbstractServer createServer(U useCase, ExecutorService executor) throws Exception {
        AbstractServer server = createServerBuilder(useCase).build();
        executor.submit(new ServerTask(server));
        return server;
    }

    protected AbstractServer.Builder createServerBuilder(U useCase)
            throws Exception {
        return (JdkServer.Builder) ((JdkServer.Builder) new JdkServer.Builder()
                .setProtocols(useCase.getProtocols())
                .setCipherSuites(useCase.getCipherSuites())
                .setCertTuple(useCase.getCertTuple()))
                .setClientAuth(useCase.isClientAuth());
    }

    /*
     * Creates client.
     */
    protected AbstractClient createClient(U useCase) throws Exception {
        return createClientBuilder(useCase).build();
    }

    protected AbstractClient.Builder createClientBuilder(U useCase)
            throws Exception {
        return (JdkClient.Builder) new JdkClient.Builder()
                .setProtocols(useCase.getProtocols())
                .setCipherSuites(useCase.getCipherSuites())
                .setCertTuple(useCase.getCertTuple());
    }

    /*
     * Returns the maximum number of attempts to start a server.
     */
    protected int getServerMaxRetries() {
        return MAX_SERVER_RETRIES;
    }

    /*
     * Determines the negotiated application protocol.
     * Generally, using JDK client to get this value.
     */
    protected String getNegoAppProtocol(AbstractServer server,
            AbstractClient client) throws SSLTestException {
        return isJdkClient() ? client.getNegoAppProtocol()
                             : server.getNegoAppProtocol();
    }

    protected static class ServerTask implements Callable<Void> {

        private final AbstractServer server;

        protected ServerTask(AbstractServer server) {
            this.server = server;
        }

        @Override
        public Void call() throws IOException {
            server.accept();
            return null;
        }
    }

    protected static class ClientTask implements Callable<Exception> {

        private final AbstractClient client;

        private final String host;
        private final int port;

        protected ClientTask(AbstractClient client, String host, int port) {
            this.client = client;

            this.host = host;
            this.port = port;
        }

        protected ClientTask(AbstractClient client, int port) {
            this(client, "localhost", port);
        }

        @Override
        public Exception call() {
            try (AbstractClient c = client) {
                c.connect(host, port);
            } catch (Exception exception) {
                return exception;
            }

            return null;
        }
    }
}