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