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