1 /* 2 * Copyright (c) 2007, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import jdk.test.lib.process.OutputAnalyzer; 27 import jdk.test.lib.process.ProcessTools; 28 import jdk.test.lib.util.JarUtils; 29 30 import java.io.FileWriter; 31 import java.io.IOException; 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 35 /* 36 * @test 37 * @bug 8048362 38 * @summary Tests the doPrivileged with accomplice Generate two jars 39 * (DoPrivTest.jar and DoPrivAccomplice.jar) and grant permission to 40 * DoPrivAccmplice.jar for reading user.name property from a PrivilagedAction. 41 * Run DoPrivTest.jar and try to access user.name property using 42 * DoPrivAccmplice.jar. 43 * 44 * @library /test/lib 45 * @build jdk.test.lib.util.JarUtils 46 * jdk.test.lib.Utils 47 * jdk.test.lib.Asserts 48 * jdk.test.lib.JDKToolFinder 49 * jdk.test.lib.JDKToolLauncher 50 * jdk.test.lib.Platform 51 * jdk.test.lib.process.* 52 * @run main/othervm DoPrivAccompliceTest 53 */ 54 55 public class DoPrivAccompliceTest { 56 private static final String ACTION_SOURCE = DoPrivAccomplice.class.getName(); 57 private static final String TEST_SOURCE = DoPrivTest.class.getName(); 58 59 private static void createPolicyFile(Path jarFile, Path policy) { 60 String codebase = jarFile.toFile().toURI().toString(); 61 String quotes = "\""; 62 StringBuilder policyFile = new StringBuilder(); 63 policyFile.append("grant codeBase ") 64 .append(quotes).append(codebase).append(quotes) 65 .append("{\n") 66 .append("permission java.util.PropertyPermission ") 67 .append(quotes).append("user.name").append(quotes) 68 .append(",") 69 .append(quotes).append("read").append(quotes) 70 .append(";\n};"); 71 try (FileWriter writer = new FileWriter(policy.toFile())) { 72 writer.write(policyFile.toString()); 73 } catch (IOException e) { 74 throw new Error("Error while creating policy file " + policy, e); 75 } 76 } 77 78 public static void main(String[] args) throws Exception { 79 // copy class files to pwd 80 ClassFileInstaller.main(ACTION_SOURCE, TEST_SOURCE); 81 Path pwd = Paths.get("."); 82 Path jarFile1 = pwd.resolve(ACTION_SOURCE + ".jar").toAbsolutePath(); 83 Path jarFile2 = pwd.resolve(TEST_SOURCE + ".jar").toAbsolutePath(); 84 Path policy = pwd.resolve("java.policy").toAbsolutePath(); 85 86 JarUtils.createJar(jarFile1.toString(), ACTION_SOURCE + ".class"); 87 System.out.println("Created jar file " + jarFile1); 88 JarUtils.createJar(jarFile2.toString(), TEST_SOURCE + ".class"); 89 System.out.println("Created jar file " + jarFile2); 90 91 92 String pathSepartor = System.getProperty("path.separator"); 93 String[] commands = { 94 "-Djava.security.manager", 95 "-Djava.security.policy=" + policy, 96 "-classpath", jarFile1 + pathSepartor + jarFile2, 97 TEST_SOURCE 98 }; 99 100 String userName = System.getProperty("user.name"); 101 102 createPolicyFile(jarFile1, policy); 103 System.out.println("Created policy for " + jarFile1); 104 ProcessTools.executeTestJava(commands) 105 .shouldHaveExitValue(0) 106 .shouldContain(userName) 107 .stderrShouldBeEmpty(); 108 109 createPolicyFile(jarFile2, policy); 110 System.out.println("Created policy for " + jarFile2); 111 ProcessTools.executeTestJava(commands) 112 .shouldNotHaveExitValue(0) 113 .shouldNotContain(userName) 114 .stderrShouldContain("java.security.AccessControlException"); 115 116 System.out.println("Test PASSES"); 117 } 118 }