1 /*
   2  * Copyright (c) 2015, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8087112
  29  * @library /lib/testlibrary/
  30  * @modules java.httpclient
  31  *          jdk.httpserver
  32  * @build jdk.testlibrary.SimpleSSLContext jdk.testlibrary.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 driver/timeout=60 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.util.ArrayList;
  54 import java.util.List;
  55 import java.util.stream.Collectors;
  56 
  57 import jdk.testlibrary.OutputAnalyzer;
  58 import jdk.testlibrary.Utils;
  59 
  60 /**
  61  * Driver for tests
  62  */
  63 public class Driver {
  64 
  65     public static void main(String[] args) throws Throwable {
  66         System.out.println("Starting Driver");
  67         runtest("1.policy", "1");
  68         runtest("10.policy", "10");
  69         runtest("11.policy", "11");
  70         runtest("12.policy", "12");
  71         System.out.println("DONE");
  72     }
  73 
  74     static class Logger extends Thread {
  75         private final OutputStream ps;
  76         private final InputStream stdout;
  77 
  78         Logger(String cmdLine, Process p, String dir) throws IOException {
  79             super();
  80             setDaemon(true);
  81             cmdLine = "Command line = [" + cmdLine + "]";
  82             stdout = p.getInputStream();
  83             File f = File.createTempFile("debug", ".txt", new File(dir));
  84             ps = new FileOutputStream(f);
  85             ps.write(cmdLine.getBytes());
  86             ps.flush();
  87         }
  88 
  89         public void run() {
  90             try {
  91                 byte[] buf = new byte[128];
  92                 int c;
  93                 while ((c = stdout.read(buf)) != -1) {
  94                     ps.write(buf, 0, c);
  95                     ps.flush();
  96                 }
  97                 ps.close();
  98             } catch (Throwable e) {
  99                 e.printStackTrace();
 100             }
 101         }
 102     }
 103 
 104     public static void runtest(String policy, String testnum) throws Throwable {
 105 
 106         String testJdk = System.getProperty("test.jdk", "?");
 107         String testSrc = System.getProperty("test.src", "?");
 108         String testClassPath = System.getProperty("test.class.path", "?");
 109         String testClasses = System.getProperty("test.classes", "?");
 110         String sep = System.getProperty("file.separator", "?");
 111         String javaCmd = testJdk + sep + "bin" + sep + "java";
 112         int retval = 10; // 10 is special exit code denoting a bind error
 113                          // in which case, we retry
 114         while (retval == 10) {
 115             List<String> cmd = new ArrayList<>();
 116             cmd.add(javaCmd);
 117             cmd.add("-Dtest.jdk=" + testJdk);
 118             cmd.add("-Dtest.src=" + testSrc);
 119             cmd.add("-Dtest.classes=" + testClasses);
 120             cmd.add("-Djava.security.manager");
 121             cmd.add("-Djava.security.policy=" + testSrc + sep + policy);
 122             cmd.add("-Dport.number=" + Integer.toString(Utils.getFreePort()));
 123             cmd.add("-Dport.number1=" + Integer.toString(Utils.getFreePort()));
 124             cmd.add("-cp");
 125             cmd.add(testClassPath);
 126             cmd.add("Security");
 127             cmd.add(testnum);
 128 
 129             ProcessBuilder processBuilder = new ProcessBuilder(cmd)
 130                 .redirectOutput(ProcessBuilder.Redirect.PIPE)
 131                 .redirectErrorStream(true);
 132 
 133             String cmdLine = cmd.stream().collect(Collectors.joining(" "));
 134             Process child = processBuilder.start();
 135             Logger log = new Logger(cmdLine, child, testClasses);
 136             log.start();
 137             retval = child.waitFor();
 138             System.out.println("retval = " + retval);
 139         }
 140         if (retval != 0) {
 141             Thread.sleep(2000);
 142             throw new RuntimeException("Non zero return value");
 143         }
 144     }
 145 }