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