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