1 /*
   2  * Copyright (c) 2017, 2018, 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  * @summary This test is used to check the interop compatibility on JSSE among
  27  *     different JDK releases.
  28  *     Note that, this is a manual test. For more details about the test and
  29  *     its usages, please look through README.
  30  *
  31  * @library /test/lib
  32  * @compile -source 1.7 -target 1.7 JdkUtils.java Parameter.java Server.java Client.java
  33  * @run main/manual Compatibility
  34  */
  35 
  36 import java.io.File;
  37 import java.io.FileOutputStream;
  38 import java.io.FileWriter;
  39 import java.io.IOException;
  40 import java.io.PrintStream;
  41 import java.nio.file.Files;
  42 import java.nio.file.Paths;
  43 import java.util.ArrayList;
  44 import java.util.LinkedHashMap;
  45 import java.util.LinkedHashSet;
  46 import java.util.List;
  47 import java.util.Map;
  48 import java.util.Set;
  49 import java.util.concurrent.ExecutorService;
  50 import java.util.concurrent.Executors;
  51 import java.util.concurrent.Future;
  52 import java.util.concurrent.TimeUnit;
  53 import java.util.stream.Collectors;
  54 import java.util.stream.Stream;
  55 
  56 import jdk.test.lib.process.OutputAnalyzer;
  57 
  58 public class Compatibility {
  59 
  60     public static void main(String[] args) throws Throwable {
  61         String javaSecurityFile
  62                 = System.getProperty("test.src") + "/java.security";
  63         boolean debug = Utils.getBoolProperty("debug");
  64 
  65         Set<JdkInfo> jdkInfos = jdkInfoList();
  66 
  67         System.out.println("Test start");
  68 
  69         List<TestCase> testCases = new ArrayList<>();
  70         ExecutorService executor = Executors.newCachedThreadPool();
  71         PrintStream origStdOut = System.out;
  72         PrintStream origStdErr = System.err;
  73 
  74         try (PrintStream printStream = new PrintStream(
  75                 new FileOutputStream(Utils.TEST_LOG, true))) {
  76             System.setOut(printStream);
  77             System.setErr(printStream);
  78 
  79             System.out.println(Utils.startHtml());
  80             System.out.println(Utils.startPre());
  81 
  82             for (UseCase useCase : UseCase.getAllUseCases()) {
  83                 for (JdkInfo serverJdk : jdkInfos) {
  84                     if (useCase.ignoredByJdk(serverJdk)) {
  85                         continue;
  86                     }
  87 
  88                     Map<String, String> props = new LinkedHashMap<>();
  89                     if (debug) {
  90                         props.put("javax.net.debug", "ssl");
  91                     }
  92                     props.put("java.security.properties", javaSecurityFile);
  93 
  94                     props.put(Utils.PROP_PROTOCOL, useCase.protocol.version);
  95                     props.put(Utils.PROP_CIPHER_SUITE, useCase.cipherSuite.name());
  96                     props.put(Utils.PROP_CLIENT_AUTH, useCase.clientAuth.name());
  97                     if (useCase.appProtocol != AppProtocol.NONE) {
  98                         props.put(Utils.PROP_APP_PROTOCOLS,
  99                                 Utils.join(Utils.VALUE_DELIMITER,
 100                                         useCase.appProtocol.appProtocols));
 101                         props.put(Utils.PROP_NEGO_APP_PROTOCOL,
 102                                 useCase.appProtocol.negoAppProtocol);
 103                     }
 104                     props.put(Utils.PROP_SERVER_JDK, serverJdk.version);
 105 
 106                     props.put(Utils.PROP_SUPPORTS_SNI_ON_SERVER,
 107                             serverJdk.supportsSNI + "");
 108                     props.put(Utils.PROP_SUPPORTS_ALPN_ON_SERVER,
 109                             serverJdk.supportsALPN + "");
 110 
 111                     for (JdkInfo clientJdk : jdkInfos) {
 112                         if (useCase.ignoredByJdk(clientJdk)) {
 113                             continue;
 114                         }
 115 
 116                         TestCase testCase = new TestCase(serverJdk, clientJdk,
 117                                 useCase);
 118                         System.out.println(Utils.anchorName(testCase.toString(),
 119                                 "----- Case start -----"));
 120                         System.out.println(testCase.toString());
 121 
 122                         props.put(Utils.PROP_NEGATIVE_CASE_ON_SERVER,
 123                                 testCase.negativeCaseOnServer + "");
 124                         props.put(Utils.PROP_NEGATIVE_CASE_ON_CLIENT,
 125                                 testCase.negativeCaseOnClient + "");
 126 
 127                         Future<OutputAnalyzer> serverFuture = executor.submit(() -> {
 128                             return runServer(serverJdk.jdkPath, props);
 129                         });
 130                         int port = waitForServerStarted();
 131                         System.out.println("port=" + port);
 132 
 133                         props.put(Utils.PROP_PORT, port + "");
 134 
 135                         props.put(Utils.PROP_CLIENT_JDK, clientJdk.version);
 136 
 137                         props.put(Utils.PROP_SUPPORTS_SNI_ON_CLIENT,
 138                                 clientJdk.supportsSNI + "");
 139                         props.put(Utils.PROP_SUPPORTS_ALPN_ON_CLIENT,
 140                                 clientJdk.supportsALPN + "");
 141                         if (useCase.serverName != ServerName.NONE) {
 142                             props.put(Utils.PROP_SERVER_NAME,
 143                                     useCase.serverName.name);
 144                         }
 145 
 146                         Status clientStatus = null;
 147                         if (port != -1) {
 148                             String clientOutput = runClient(clientJdk.jdkPath,
 149                                     props).getOutput();
 150                             clientStatus = getStatus(clientOutput);
 151                         }
 152 
 153                         String serverOutput = serverFuture.get().getOutput();
 154                         Status serverStatus = getStatus(serverOutput);
 155                         testCase.setStatus(caseStatus(serverStatus, clientStatus));
 156                         testCases.add(testCase);
 157                         System.out.printf(
 158                                 "ServerStatus=%s, ClientStatus=%s, CaseStatus=%s%n",
 159                                 serverStatus, clientStatus, testCase.getStatus());
 160 
 161                         System.out.println("----- Case end -----");
 162                     }
 163                 }
 164             }
 165 
 166             System.out.println(Utils.endPre());
 167             System.out.println(Utils.endHtml());
 168         }
 169         System.setOut(origStdOut);
 170         System.setErr(origStdErr);
 171         executor.shutdown();
 172 
 173         System.out.println("Test end");
 174         System.out.println("Report is being generated...");
 175         boolean failed = generateReport(testCases);
 176         System.out.println("Report is generated.");
 177         if (failed) {
 178             throw new RuntimeException("At least one case failed. "
 179                     + "Please check logs for more details.");
 180         }
 181     }
 182 
 183     private static Status getStatus(String log) {
 184         if (log.contains(Status.UNEXPECTED_SUCCESS.name())) {
 185             return Status.UNEXPECTED_SUCCESS;
 186         } else if (log.contains(Status.SUCCESS.name())) {
 187             return Status.SUCCESS;
 188         } else if (log.contains(Status.EXPECTED_FAIL.name())) {
 189             return Status.EXPECTED_FAIL;
 190         } else if (log.contains(Status.TIMEOUT.name())) {
 191             return Status.TIMEOUT;
 192         } else {
 193             return Status.FAIL;
 194         }
 195     }
 196 
 197     private static Status caseStatus(Status serverStatus, Status clientStatus) {
 198         if (clientStatus == null || clientStatus == Status.TIMEOUT) {
 199             return serverStatus == Status.EXPECTED_FAIL
 200                    ? Status.EXPECTED_FAIL
 201                    : Status.FAIL;
 202         } else if (serverStatus == Status.TIMEOUT) {
 203             return clientStatus == Status.EXPECTED_FAIL
 204                    ? Status.EXPECTED_FAIL
 205                    : Status.FAIL;
 206         } else {
 207             return serverStatus == clientStatus
 208                    ? serverStatus
 209                    : Status.FAIL;
 210         }
 211     }
 212 
 213     // Retrieves JDK info from the file which is specified by jdkListFile.
 214     // If no such file or no JDK is specified by the file, the current testing
 215     // JDK will be used.
 216     private static Set<JdkInfo> jdkInfoList() throws Throwable {
 217         List<String> jdkList = jdkList("jdkListFile");
 218         if (jdkList.size() == 0) {
 219             jdkList.add(System.getProperty("test.jdk"));
 220         }
 221 
 222         Set<JdkInfo> jdkInfoList = new LinkedHashSet<>();
 223         for (String jdkPath : jdkList) {
 224             JdkInfo jdkInfo = new JdkInfo(jdkPath);
 225             // JDK version must be unique.
 226             if (!jdkInfoList.add(jdkInfo)) {
 227                 System.out.println("The JDK version is duplicate: " + jdkPath);
 228             }
 229         }
 230         return jdkInfoList;
 231     }
 232 
 233     private static List<String> jdkList(String listFileProp) throws IOException {
 234         String listFile = System.getProperty(listFileProp);
 235         System.out.println(listFileProp + "=" + listFile);
 236         if (listFile != null && Files.exists(Paths.get(listFile))) {
 237             try (Stream<String> lines = Files.lines(Paths.get(listFile))) {
 238                 return lines.filter(line -> {
 239                     return !line.trim().isEmpty();
 240                 }).collect(Collectors.toList());
 241             }
 242         } else {
 243             return new ArrayList<>();
 244         }
 245     }
 246 
 247     // Checks if server is already launched, and returns server port.
 248     private static int waitForServerStarted()
 249             throws IOException, InterruptedException {
 250         System.out.print("Waiting for server");
 251         long deadline = System.currentTimeMillis() + Utils.TIMEOUT;
 252         int port;
 253         while ((port = getServerPort()) == -1
 254                 && System.currentTimeMillis() < deadline) {
 255             System.out.print(".");
 256             TimeUnit.SECONDS.sleep(1);
 257         }
 258         System.out.println();
 259 
 260         return port;
 261     }
 262 
 263     // Retrieves the latest server port from port.log.
 264     private static int getServerPort() throws IOException {
 265         if (!Files.exists(Paths.get(Utils.PORT_LOG))) {
 266             return -1;
 267         }
 268 
 269         try (Stream<String> lines = Files.lines(Paths.get(Utils.PORT_LOG))) {
 270             return Integer.valueOf(lines.findFirst().get());
 271         }
 272     }
 273 
 274     private static OutputAnalyzer runServer(String jdkPath,
 275             Map<String, String> props) {
 276         return ProcessUtils.java(jdkPath, props, Server.class);
 277     }
 278 
 279     private static OutputAnalyzer runClient(String jdkPath,
 280             Map<String, String> props) {
 281         return ProcessUtils.java(jdkPath, props, Client.class);
 282     }
 283 
 284     // Generates the test result report.
 285     private static boolean generateReport(List<TestCase> testCases)
 286             throws IOException {
 287         boolean failed = false;
 288         StringBuilder report = new StringBuilder();
 289         report.append(Utils.startHtml());
 290         report.append(Utils.tableStyle());
 291         report.append(Utils.startTable());
 292         report.append(Utils.row(
 293                 "No.",
 294                 "ServerJDK",
 295                 "ClientJDK",
 296                 "Protocol",
 297                 "CipherSuite",
 298                 "ClientAuth",
 299                 "SNI",
 300                 "ALPN",
 301                 "Status"));
 302         for (int i = 0, size = testCases.size(); i < size; i++) {
 303             TestCase testCase = testCases.get(i);
 304 
 305             report.append(Utils.row(
 306                     Utils.anchorLink(
 307                             Utils.TEST_LOG,
 308                             testCase.toString(),
 309                             i + ""),
 310                     testCase.serverJdk.version,
 311                     testCase.clientJdk.version,
 312                     testCase.useCase.protocol.version,
 313                     testCase.useCase.cipherSuite,
 314                     Utils.boolToStr(
 315                             testCase.useCase.clientAuth == ClientAuth.TRUE),
 316                     Utils.boolToStr(
 317                             testCase.useCase.serverName == ServerName.EXAMPLE),
 318                     Utils.boolToStr(
 319                             testCase.useCase.appProtocol == AppProtocol.EXAMPLE),
 320                     testCase.getStatus()));
 321             failed = failed
 322                     || testCase.getStatus() == Status.FAIL
 323                     || testCase.getStatus() == Status.UNEXPECTED_SUCCESS;
 324         }
 325         report.append(Utils.endTable());
 326         report.append(Utils.endHtml());
 327 
 328         generateFile("report.html", report.toString());
 329         return failed;
 330     }
 331 
 332     private static void generateFile(String path, String content)
 333             throws IOException {
 334         try(FileWriter writer = new FileWriter(new File(path))) {
 335             writer.write(content);
 336         }
 337     }
 338 }