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