1 /*
   2  * Copyright (c) 2015, 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  * @bug 8087112
  27  * @library /lib/testlibrary/
  28  * @library /test/lib
  29  * @modules java.net.http
  30  *          java.logging
  31  *          jdk.httpserver
  32  * @build jdk.testlibrary.SimpleSSLContext jdk.test.lib.Utils
  33  * @compile ../../../../com/sun/net/httpserver/LogFilter.java
  34  * @compile ../../../../com/sun/net/httpserver/FileServerHandler.java
  35  * @compile ../ProxyServer.java
  36  * @build Security
  37  *
  38  * @run main/othervm Driver
  39  */
  40 
  41 /**
  42  * driver required for allocating free portnumbers and putting this number
  43  * into security policy file used in some tests.
  44  *
  45  * The tests are in Security.java and port number supplied in -Dport.number
  46  * and -Dport.number1 for tests that require a second free port
  47  */
  48 import java.io.File;
  49 import java.io.FileOutputStream;
  50 import java.io.IOException;
  51 import java.io.InputStream;
  52 import java.io.OutputStream;
  53 import java.nio.file.Files;
  54 import java.nio.file.Path;
  55 import java.nio.file.Paths;
  56 import java.util.ArrayList;
  57 import java.util.List;
  58 import java.util.stream.Collectors;
  59 import jdk.test.lib.Utils;
  60 
  61 /**
  62  * Driver for tests
  63  */
  64 public class Driver {
  65     // change the default value to "true" to get the subprocess traces.
  66     final static boolean DEBUG = Boolean.parseBoolean(System.getProperty("test.debug", "true"));
  67 
  68     public static void main(String[] args) throws Throwable {
  69         System.out.println("Starting Driver");
  70         runtest("1.policy", "1");
  71         runtest("10.policy", "10");
  72         runtest("11.policy", "11");
  73         runtest("12.policy", "12");
  74         System.out.println("DONE");
  75     }
  76 
  77     static final Path CWD = Paths.get(".");
  78 
  79     static class Logger extends Thread {
  80         private final OutputStream ps;
  81         private final InputStream stdout;
  82 
  83         Logger(String cmdLine, Process p) throws IOException {
  84             super();
  85             setDaemon(true);
  86             cmdLine = "Command line = [" + cmdLine + "]\n";
  87             stdout = p.getInputStream();
  88             File f = Files.createTempFile(CWD, "debug", ".txt").toFile();
  89             ps = new FileOutputStream(f);
  90             ps.write(cmdLine.getBytes());
  91             ps.flush();
  92             if (DEBUG) {
  93                 System.out.print(cmdLine);
  94                 System.out.flush();
  95             }
  96         }
  97 
  98         public void run() {
  99             try {
 100                 byte[] buf = new byte[128];
 101                 int c;
 102                 while ((c = stdout.read(buf)) != -1) {
 103                     if (DEBUG) {
 104                         System.out.write(buf, 0, c);
 105                         System.out.flush();
 106                     }
 107                     ps.write(buf, 0, c);
 108                     ps.flush();
 109                 }
 110                 ps.close();
 111             } catch (Throwable e) {
 112                 e.printStackTrace();
 113             }
 114         }
 115     }
 116 
 117     public static void runtest(String policy, String testnum) throws Throwable {
 118 
 119         String testJdk = System.getProperty("test.jdk", "?");
 120         String testSrc = System.getProperty("test.src", "?");
 121         String testClassPath = System.getProperty("test.class.path", "?");
 122         String testClasses = System.getProperty("test.classes", "?");
 123         String sep = System.getProperty("file.separator", "?");
 124         String javaCmd = testJdk + sep + "bin" + sep + "java";
 125         int retval = 10; // 10 is special exit code denoting a bind error
 126                          // in which case, we retry
 127         while (retval == 10) {
 128             List<String> cmd = new ArrayList<>();
 129             cmd.add(javaCmd);
 130             cmd.add("-ea");
 131             cmd.add("-esa");
 132             cmd.add("-Dtest.jdk=" + testJdk);
 133             cmd.add("-Dtest.src=" + testSrc);
 134             cmd.add("-Dtest.classes=" + testClasses);
 135             cmd.add("-Djava.security.manager");
 136             cmd.add("-Djava.security.policy=" + testSrc + sep + policy);
 137             cmd.add("-Dport.number=" + Integer.toString(Utils.getFreePort()));
 138             cmd.add("-Dport.number1=" + Integer.toString(Utils.getFreePort()));
 139             cmd.add("-Djdk.httpclient.HttpClient.log=all,frames:all");
 140             cmd.add("-cp");
 141             cmd.add(testClassPath);
 142             cmd.add("Security");
 143             cmd.add(testnum);
 144 
 145             ProcessBuilder processBuilder = new ProcessBuilder(cmd)
 146                 .redirectOutput(ProcessBuilder.Redirect.PIPE)
 147                 .redirectErrorStream(true);
 148 
 149             String cmdLine = cmd.stream().collect(Collectors.joining(" "));
 150             long start = System.currentTimeMillis();
 151             Process child = processBuilder.start();
 152             Logger log = new Logger(cmdLine, child);
 153             log.start();
 154             retval = child.waitFor();
 155             long elapsed = System.currentTimeMillis() - start;
 156             System.out.println("Security " + testnum
 157                                + ": retval = " + retval
 158                                + ", duration=" + elapsed+" ms");
 159         }
 160         if (retval != 0) {
 161             Thread.sleep(2000);
 162             throw new RuntimeException("Non zero return value");
 163         }
 164     }
 165 }