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
24import java.io.IOException;
25import java.io.UncheckedIOException;
26import java.nio.file.Files;
27import java.nio.file.Path;
28import java.nio.file.Paths;
29import java.util.Arrays;
30import java.util.stream.Stream;
31
32import jdk.test.lib.util.FileUtils;
33import jdk.test.lib.compiler.CompilerUtils;
34import static jdk.testlibrary.ProcessTools.*;
35
36import org.testng.annotations.BeforeClass;
37import org.testng.annotations.Test;
38import static org.testng.Assert.assertTrue;
39
40/**
41 * @test
42 * @bug 8087335
43 * @summary Tests for Class.forName(Module,String)
44 * @library /lib/testlibrary /test/lib
45 * @modules jdk.compiler
46 * @build jdk.test.lib.Platform
47 *        jdk.test.lib.util.FileUtils
48 *        jdk.test.lib.compiler.CompilerUtils
49 *        jdk.testlibrary.ProcessTools
50 *        TestDriver TestMain TestLayer
51 * @run testng TestDriver
52 */
53
54public class TestDriver {
55
56    private static final String TEST_SRC =
57            Paths.get(System.getProperty("test.src")).toString();
58    private static final String TEST_CLASSES =
59            Paths.get(System.getProperty("test.classes")).toString();
60
61    private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "src");
62    private static final Path MOD_DEST_DIR = Paths.get("mods");
63
64    private static final String[] modules = new String[] {"m1", "m2", "m3"};
65
66    /**
67     * Compiles all modules used by the test.
68     */
69    @BeforeClass
70    public void setup() throws Exception {
71        assertTrue(CompilerUtils.compile(
72                        MOD_SRC_DIR, MOD_DEST_DIR,
73                        "--module-source-path",
74                        MOD_SRC_DIR.toString()));
75
76        copyDirectories(MOD_DEST_DIR.resolve("m1"), Paths.get("mods1"));
77        copyDirectories(MOD_DEST_DIR.resolve("m2"), Paths.get("mods2"));
78    }
79
80    @Test
81    public void test() throws Exception {
82        String[] options = new String[] {
83                "-cp", TEST_CLASSES,
84                "--module-path", MOD_DEST_DIR.toString(),
85                "--add-modules", String.join(",", modules),
86                "-m", "m2/p2.test.Main"
87        };
88        runTest(options);
89    }
90
91    @Test
92    public void testUnnamedModule() throws Exception {
93        String[] options = new String[] {
94                "-cp", TEST_CLASSES,
95                "--module-path", MOD_DEST_DIR.toString(),
96                "--add-modules", String.join(",", modules),
97                "TestMain"
98        };
99        runTest(options);
100    }
101
102    @Test
103    public void testLayer() throws Exception {
104        String[] options = new String[] {
105                "-cp", TEST_CLASSES,
106                "TestLayer"
107        };
108
109        runTest(options);
110    }
111
112    @Test
113    public void testDeniedClassLoaderAccess() throws Exception {
114        String[] options = new String[] {
115                "--module-path", MOD_DEST_DIR.toString(),
116                "--add-modules", String.join(",", modules),
117                "-m", "m3/p3.NoGetClassLoaderAccess"
118        };
119        assertTrue(executeTestJava(options)
120                        .outputTo(System.out)
121                        .errorTo(System.err)
122                        .getExitValue() == 0);
123    }
124
125    @Test
126    public void testDeniedAccess() throws Exception {
127        Path policyFile = Paths.get(TEST_SRC, "policy.denied");
128
129        String[] options = new String[] {
130                "-Djava.security.manager",
131                "-Djava.security.policy=" + policyFile.toString(),
132                "--module-path", MOD_DEST_DIR.toString(),
133                "--add-modules", String.join(",", modules),
134                "-m", "m3/p3.NoAccess"
135        };
136        assertTrue(executeTestJava(options)
137                        .outputTo(System.out)
138                        .errorTo(System.err)
139                        .getExitValue() == 0);
140    }
141
142    private String[] runWithSecurityManager(String[] options) {
143        Path policyFile = Paths.get(TEST_SRC, "policy");
144        Stream<String> opts = Stream.concat(Stream.of("-Djava.security.manager",
145                                                      "-Djava.security.policy=" + policyFile.toString()),
146                                            Arrays.stream(options));
147        return opts.toArray(String[]::new);
148    }
149
150    private void runTest(String[] options) throws Exception {
151        assertTrue(executeTestJava(options)
152                        .outputTo(System.out)
153                        .errorTo(System.err)
154                        .getExitValue() == 0);
155
156        assertTrue(executeTestJava(runWithSecurityManager(options))
157                        .outputTo(System.out)
158                        .errorTo(System.err)
159                        .getExitValue() == 0);
160    }
161
162    private void copyDirectories(Path source, Path dest) throws IOException {
163        if (Files.exists(dest))
164            FileUtils.deleteFileTreeWithRetry(dest);
165        Files.walk(source, Integer.MAX_VALUE)
166                .filter(Files::isRegularFile)
167                .forEach(p -> {
168                    try {
169                        Path to = dest.resolve(source.relativize(p));
170                        Files.createDirectories(to.getParent());
171                        Files.copy(p, to);
172                    } catch (IOException e) {
173                        throw new UncheckedIOException(e);
174                    }
175                });
176    }
177}
178