1/*
2 * Copyright (c) 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 4673940 4930794
27 * @summary Unit tests for inetd feature
28 * @requires (os.family == "linux" | os.family == "solaris")
29 * @library /test/lib
30 * @build jdk.test.lib.Utils
31 *        jdk.test.lib.Asserts
32 *        jdk.test.lib.JDKToolFinder
33 *        jdk.test.lib.JDKToolLauncher
34 *        jdk.test.lib.Platform
35 *        jdk.test.lib.process.*
36 *        StateTest StateTestService EchoTest EchoService CloseTest Launcher Util
37 * @run testng/othervm InheritedChannelTest
38 * @key intermittent
39 */
40
41import java.nio.file.Files;
42import java.nio.file.Path;
43import java.nio.file.Paths;
44import java.util.ArrayList;
45import java.util.List;
46import java.util.Map;
47
48import jdk.test.lib.JDKToolFinder;
49import jdk.test.lib.Utils;
50import jdk.test.lib.process.ProcessTools;
51
52import org.testng.annotations.DataProvider;
53import org.testng.annotations.Test;
54
55import static java.util.Arrays.asList;
56
57public class InheritedChannelTest {
58
59    private static final String TEST_SRC = System.getProperty("test.src");
60    private static final String TEST_CLASSES = System.getProperty("test.classes");
61    private static final Path POLICY_PASS = Paths.get(TEST_SRC, "java.policy.pass");
62    private static final Path POLICY_FAIL = Paths.get(TEST_SRC, "java.policy.fail");
63
64    private static final String OS = System.getProperty("os.name").toLowerCase();
65    private static final String OS_NAME = OS.startsWith("sunos") ? "solaris" : OS;
66
67    private static final String ARCH = System.getProperty("os.arch");
68    private static final String OS_ARCH = ARCH.equals("i386") ? "i586" : ARCH;
69
70    private static final Path LD_LIBRARY_PATH
71            = Paths.get(TEST_SRC, "lib", OS_NAME + "-" + OS_ARCH);
72
73    private static final Path LAUNCHERLIB = LD_LIBRARY_PATH.resolve("libLauncher.so");
74
75    @DataProvider
76    public Object[][] testCases() {
77        return new Object[][]{
78            { "StateTest", List.of(StateTest.class.getName()) },
79            { "EchoTest",  List.of(EchoTest.class.getName())  },
80            { "CloseTest", List.of(CloseTest.class.getName()) },
81
82            // run StateTest with a SecurityManager set
83            // Note that the system properties are arguments to StateTest and not options.
84            // These system properties are passed to the launched service as options:
85            // java [-options] class [args...]
86            { "StateTest run with " + POLICY_PASS, List.of(StateTest.class.getName(),
87                                                           "-Djava.security.manager",
88                                                           "-Djava.security.policy="
89                                                           + POLICY_PASS)
90            },
91            { "StateTest run with " + POLICY_FAIL, List.of(StateTest.class.getName(),
92                                                           "-expectFail",
93                                                           "-Djava.security.manager",
94                                                           "-Djava.security.policy="
95                                                           + POLICY_FAIL)
96            }
97        };
98    }
99
100    @Test(dataProvider = "testCases")
101    public void test(String desc, List<String> opts) throws Throwable {
102        if (!Files.exists(LAUNCHERLIB)) {
103            System.out.println("Cannot find " + LAUNCHERLIB
104                    + " - library not available for this system");
105            return;
106        }
107        System.out.println("LD_LIBRARY_PATH=" + LD_LIBRARY_PATH);
108
109        List<String> args = new ArrayList<>();
110        args.add(JDKToolFinder.getJDKTool("java"));
111        args.addAll(asList(Utils.getTestJavaOpts()));
112        args.addAll(List.of("--add-opens", "java.base/java.io=ALL-UNNAMED",
113                            "--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED"));
114        args.addAll(opts);
115
116        ProcessBuilder pb = new ProcessBuilder(args);
117
118        Map<String, String> env = pb.environment();
119        env.put("CLASSPATH", TEST_CLASSES);
120        env.put("LD_LIBRARY_PATH", LD_LIBRARY_PATH.toString());
121
122        ProcessTools.executeCommand(pb)
123                    .shouldHaveExitValue(0);
124    }
125}
126