Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/compatibility/Compatibility.java
38854 views
/*1* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary This test is used to check the interop compatibility on JSSE among26* different JDK releases.27* Note that, this is a manual test. For more details about the test and28* its usages, please look through README.29*30* @library /test/lib ../TLSCommon31* @compile -source 1.6 -target 1.6 JdkUtils.java Server.java Client.java32* @run main/manual Compatibility33*/3435import java.io.File;36import java.io.FileOutputStream;37import java.io.FileWriter;38import java.io.IOException;39import java.io.PrintStream;40import java.nio.file.Files;41import java.nio.file.Paths;42import java.util.ArrayList;43import java.util.LinkedHashMap;44import java.util.LinkedHashSet;45import java.util.List;46import java.util.Map;47import java.util.Set;48import java.util.concurrent.ExecutorService;49import java.util.concurrent.Executors;50import java.util.concurrent.Future;51import java.util.concurrent.TimeUnit;52import java.util.stream.Collectors;53import java.util.stream.Stream;5455import jdk.test.lib.process.OutputAnalyzer;5657public class Compatibility {5859protected List<UseCase> getUseCases() {60return UseCase.getAllUseCases();61}6263protected Set<JdkInfo> getJdkInfos() {64return jdkInfoList();65}6667protected List<TestCase> runTest() throws Exception {68Set<JdkInfo> jdkInfos = getJdkInfos();6970List<TestCase> testCases = new ArrayList<>();71ExecutorService executor = Executors.newCachedThreadPool();72PrintStream origStdOut = System.out;73PrintStream origStdErr = System.err;7475boolean debug = Boolean.getBoolean("debug");7677String securityPropertiesFile = System.getProperty(78"test.security.properties",79System.getProperty("test.src") + "/java.security");80System.out.println("security properties: " + securityPropertiesFile);8182// If true, server and client CANNOT be a same JDK83boolean disallowSameEndpoint = Boolean.getBoolean("disallowSameEndpoint");84System.out.println("disallowSameEndpoint: " + disallowSameEndpoint);8586try (PrintStream printStream = new PrintStream(87new FileOutputStream(Utils.TEST_LOG, true))) {88System.setOut(printStream);89System.setErr(printStream);9091System.out.println(Utils.startHtml());92System.out.println(Utils.startPre());9394for (UseCase useCase : getUseCases()) {95for (JdkInfo serverJdk : jdkInfos) {96Map<String, String> props = new LinkedHashMap<>();97if (debug) {98props.put("javax.net.debug", "all");99}100props.put("java.security.properties", securityPropertiesFile);101102props.put(Utils.PROP_PROTOCOL, useCase.protocol.name);103props.put(Utils.PROP_CIPHER_SUITE, useCase.cipherSuite.name());104props.put(Utils.PROP_CLIENT_AUTH, String.valueOf(useCase.clientAuth));105if (useCase.appProtocol != UseCase.AppProtocol.NONE) {106props.put(Utils.PROP_APP_PROTOCOLS,107Utils.join(Utils.VALUE_DELIMITER,108useCase.appProtocol.appProtocols));109props.put(Utils.PROP_NEGO_APP_PROTOCOL,110useCase.appProtocol.negoAppProtocol);111}112props.put(Utils.PROP_SERVER_JDK, serverJdk.version);113114props.put(Utils.PROP_SUPPORTS_SNI_ON_SERVER,115serverJdk.supportsSNI + "");116props.put(Utils.PROP_SUPPORTS_ALPN_ON_SERVER,117serverJdk.supportsALPN + "");118119for (JdkInfo clientJdk : jdkInfos) {120if (disallowSameEndpoint && clientJdk == serverJdk) {121continue;122}123124TestCase testCase = new TestCase(serverJdk, clientJdk,125useCase);126System.out.println(Utils.anchorName(testCase.toString(),127"===== Case start ====="));128System.out.println(testCase.toString());129130props.put(Utils.PROP_NEGATIVE_CASE_ON_SERVER,131testCase.negativeCaseOnServer + "");132props.put(Utils.PROP_NEGATIVE_CASE_ON_CLIENT,133testCase.negativeCaseOnClient + "");134135Future<OutputAnalyzer> serverFuture = executor.submit(() -> {136return runServer(serverJdk.jdkPath, props);137});138int port = waitForServerStarted();139System.out.println("port=" + port);140141props.put(Utils.PROP_PORT, port + "");142143props.put(Utils.PROP_CLIENT_JDK, clientJdk.version);144145props.put(Utils.PROP_SUPPORTS_SNI_ON_CLIENT,146clientJdk.supportsSNI + "");147props.put(Utils.PROP_SUPPORTS_ALPN_ON_CLIENT,148clientJdk.supportsALPN + "");149if (useCase.serverName != UseCase.ServerName.NONE) {150props.put(Utils.PROP_SERVER_NAME,151useCase.serverName.name);152}153154Status clientStatus = null;155if (port != -1) {156String clientOutput = runClient(clientJdk.jdkPath,157props).getOutput();158clientStatus = getStatus(clientOutput);159}160161String serverOutput = serverFuture.get().getOutput();162Status serverStatus = getStatus(serverOutput);163testCase.setStatus(caseStatus(serverStatus, clientStatus));164testCases.add(testCase);165System.out.printf(166"ServerStatus=%s, ClientStatus=%s, CaseStatus=%s%n",167serverStatus, clientStatus, testCase.getStatus());168169System.out.println("===== Case end =====");170}171}172}173174System.out.println(Utils.endPre());175System.out.println(Utils.endHtml());176}177System.setOut(origStdOut);178System.setErr(origStdErr);179executor.shutdown();180181return testCases;182}183184// Generates the test result report.185protected boolean generateReport(List<TestCase> testCases)186throws IOException {187boolean failed = false;188StringBuilder report = new StringBuilder();189report.append(Utils.startHtml());190report.append(Utils.tableStyle());191report.append(Utils.startTable());192report.append(Utils.row(193"No.",194"ServerJDK",195"ClientJDK",196"Protocol",197"CipherSuite",198"ClientAuth",199"SNI",200"ALPN",201"Status"));202for (int i = 0, size = testCases.size(); i < size; i++) {203TestCase testCase = testCases.get(i);204205report.append(Utils.row(206Utils.anchorLink(207Utils.TEST_LOG,208testCase.toString(),209i + ""),210testCase.serverJdk.version,211testCase.clientJdk.version,212testCase.useCase.protocol.name,213testCase.useCase.cipherSuite,214Utils.boolToStr(215testCase.useCase.clientAuth),216Utils.boolToStr(217testCase.useCase.serverName == UseCase.ServerName.EXAMPLE),218Utils.boolToStr(219testCase.useCase.appProtocol == UseCase.AppProtocol.EXAMPLE),220testCase.getStatus()));221failed = failed222|| testCase.getStatus() == Status.FAIL223|| testCase.getStatus() == Status.UNEXPECTED_SUCCESS;224}225report.append(Utils.endTable());226report.append(Utils.endHtml());227228generateFile("report.html", report.toString());229return failed;230}231232protected void run() throws Exception {233System.out.println("Test start");234List<TestCase> testCases= runTest();235System.out.println("Test end");236237boolean failed = generateReport(testCases);238System.out.println("Report was generated.");239240if (failed) {241throw new RuntimeException("At least one case failed. "242+ "Please check logs for more details.");243}244}245246public static void main(String[] args) throws Throwable {247new Compatibility().run();;248}249250private static Status getStatus(String log) {251if (log.contains(Status.UNEXPECTED_SUCCESS.name())) {252return Status.UNEXPECTED_SUCCESS;253} else if (log.contains(Status.SUCCESS.name())) {254return Status.SUCCESS;255} else if (log.contains(Status.EXPECTED_FAIL.name())) {256return Status.EXPECTED_FAIL;257} else if (log.contains(Status.TIMEOUT.name())) {258return Status.TIMEOUT;259} else {260return Status.FAIL;261}262}263264private static Status caseStatus(Status serverStatus, Status clientStatus) {265if (clientStatus == null || clientStatus == Status.TIMEOUT) {266return serverStatus == Status.EXPECTED_FAIL267? Status.EXPECTED_FAIL268: Status.FAIL;269} else if (serverStatus == Status.TIMEOUT) {270return clientStatus == Status.EXPECTED_FAIL271? Status.EXPECTED_FAIL272: Status.FAIL;273} else {274return serverStatus == clientStatus275? serverStatus276: Status.FAIL;277}278}279280// Retrieves JDK info from the file which is specified by jdkListFile.281// And the current testing JDK, which is specified by test.jdk, always be used.282private static Set<JdkInfo> jdkInfoList() {283List<String> jdkList = jdkList();284jdkList.add(System.getProperty("test.jdk"));285286Set<JdkInfo> jdkInfoList = new LinkedHashSet<>();287for (String jdkPath : jdkList) {288JdkInfo jdkInfo = new JdkInfo(jdkPath);289// JDK version must be unique.290if (!jdkInfoList.add(jdkInfo)) {291System.out.println("The JDK version is duplicate: " + jdkPath);292}293}294return jdkInfoList;295}296297private static List<String> jdkList() {298String listFile = System.getProperty("jdkListFile");299System.out.println("jdk list file: " + listFile);300if (listFile != null && Files.exists(Paths.get(listFile))) {301try (Stream<String> lines = Files.lines(Paths.get(listFile))) {302return lines.filter(line -> {303return !line.trim().isEmpty();304}).collect(Collectors.toList());305} catch (IOException e) {306throw new RuntimeException("Cannot get jdk list", e);307}308} else {309return new ArrayList<>();310}311}312313// Checks if server is already launched, and returns server port.314private static int waitForServerStarted()315throws IOException, InterruptedException {316System.out.print("Waiting for server");317long deadline = System.currentTimeMillis() + Utils.TIMEOUT;318int port;319while ((port = getServerPort()) == -1320&& System.currentTimeMillis() < deadline) {321System.out.print(".");322TimeUnit.SECONDS.sleep(1);323}324System.out.println();325326return port;327}328329// Retrieves the latest server port from port.log.330private static int getServerPort() throws IOException {331if (!Files.exists(Paths.get(Utils.PORT_LOG))) {332return -1;333}334335try (Stream<String> lines = Files.lines(Paths.get(Utils.PORT_LOG))) {336return Integer.valueOf(lines.findFirst().get());337}338}339340private static OutputAnalyzer runServer(String jdkPath,341Map<String, String> props) {342return ProcessUtils.java(jdkPath, props, Server.class);343}344345private static OutputAnalyzer runClient(String jdkPath,346Map<String, String> props) {347return ProcessUtils.java(jdkPath, props, Client.class);348}349350private static void generateFile(String path, String content)351throws IOException {352try(FileWriter writer = new FileWriter(new File(path))) {353writer.write(content);354}355}356}357358359