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