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