1 /*
  2  * Copyright (c) 2015, 2024, 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 import jdk.test.lib.process.ProcessTools;
 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         runtest("16.policy", "16", "-Djdk.httpclient.allowRestrictedHeaders=Host");
 75         runtest("17.policy", "17", "-Djdk.httpclient.allowRestrictedHeaders=Host");
 76         System.out.println("DONE");
 77     }
 78 
 79     static final Path CWD = Paths.get(".");
 80 
 81     static class Logger extends Thread {
 82         private final OutputStream ps;
 83         private final InputStream stdout;
 84 
 85         Logger(String cmdLine, Process p) throws IOException {
 86             super();
 87             setDaemon(true);
 88             cmdLine = "Command line = [" + cmdLine + "]\n";
 89             stdout = p.getInputStream();
 90             File f = Files.createTempFile(CWD, "debug", ".txt").toFile();
 91             ps = new FileOutputStream(f);
 92             ps.write(cmdLine.getBytes());
 93             ps.flush();
 94             if (DEBUG) {
 95                 System.out.print(cmdLine);
 96                 System.out.flush();
 97             }
 98         }
 99 
100         public void run() {
101             try {
102                 byte[] buf = new byte[128];
103                 int c;
104                 while ((c = stdout.read(buf)) != -1) {
105                     if (DEBUG) {
106                         System.out.write(buf, 0, c);
107                         System.out.flush();
108                     }
109                     ps.write(buf, 0, c);
110                     ps.flush();
111                 }
112                 ps.close();
113             } catch (Throwable e) {
114                 e.printStackTrace();
115             }
116         }
117     }
118 
119     public static void runtest(String policy, String testnum) throws Throwable {
120         runtest(policy, testnum, null);
121     }
122 
123 
124     public static void runtest(String policy, String testnum, String addProp) throws Throwable {
125         String testJdk = System.getProperty("test.jdk", "?");
126         String testSrc = System.getProperty("test.src", "?");
127         String testClassPath = System.getProperty("test.class.path", "?");
128         String testClasses = System.getProperty("test.classes", "?");
129         String sep = System.getProperty("file.separator", "?");
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("-ea");
135             cmd.add("-esa");
136             cmd.add("-Dtest.jdk=" + testJdk);
137             cmd.add("-Dtest.src=" + testSrc);
138             cmd.add("-Dtest.classes=" + testClasses);
139             cmd.add("-Djava.security.manager");
140             cmd.add("-Djava.security.policy=" + testSrc + sep + policy);
141             cmd.add("-Dport.number=" + Integer.toString(Utils.getFreePort()));
142             cmd.add("-Dport.number1=" + Integer.toString(Utils.getFreePort()));
143             cmd.add("-Djdk.httpclient.HttpClient.log=all,frames:all");
144             if (addProp != null) {
145                 cmd.add(addProp);
146             }
147             cmd.add("-cp");
148             cmd.add(testClassPath);
149             cmd.add("Security");
150             cmd.add(testnum);
151 
152             ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(cmd)
153                 .redirectOutput(ProcessBuilder.Redirect.PIPE)
154                 .redirectErrorStream(true);
155 
156             String cmdLine = cmd.stream().collect(Collectors.joining(" "));
157             long start = System.currentTimeMillis();
158             Process child = processBuilder.start();
159             Logger log = new Logger(cmdLine, child);
160             log.start();
161             retval = child.waitFor();
162             long elapsed = System.currentTimeMillis() - start;
163             System.out.println("Security " + testnum
164                                + ": retval = " + retval
165                                + ", duration=" + elapsed+" ms");
166         }
167         if (retval != 0) {
168             Thread.sleep(2000);
169             throw new RuntimeException("Non zero return value");
170         }
171     }
172 }