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 * 46 * @run main/othervm DoPrivAccompliceTest 47 */ 48 49 public class DoPrivAccompliceTest { 50 private static final String ACTION_SOURCE = DoPrivAccomplice.class.getName(); 51 private static final String TEST_SOURCE = DoPrivTest.class.getName(); 52 53 private static void createPolicyFile(Path jarFile, Path policy) { 54 String codebase = jarFile.toFile().toURI().toString(); 55 String quotes = "\""; 56 StringBuilder policyFile = new StringBuilder(); 57 policyFile.append("grant codeBase ") 58 .append(quotes).append(codebase).append(quotes) 59 .append("{\n") 60 .append("permission java.util.PropertyPermission ") 61 .append(quotes).append("user.name").append(quotes) 62 .append(",") 63 .append(quotes).append("read").append(quotes) 64 .append(";\n};"); 65 try (FileWriter writer = new FileWriter(policy.toFile())) { 66 writer.write(policyFile.toString()); 67 } catch (IOException e) { 68 throw new Error("Error while creating policy file " + policy, e); 69 } 70 } 71 72 public static void main(String[] args) throws Exception { 73 // copy class files to pwd 74 ClassFileInstaller.main(ACTION_SOURCE, TEST_SOURCE); 75 Path pwd = Paths.get("."); 76 Path jarFile1 = pwd.resolve(ACTION_SOURCE + ".jar").toAbsolutePath(); 77 Path jarFile2 = pwd.resolve(TEST_SOURCE + ".jar").toAbsolutePath(); 78 Path policy = pwd.resolve("java.policy").toAbsolutePath(); 79 80 JarUtils.createJar(jarFile1.toString(), ACTION_SOURCE + ".class"); 81 System.out.println("Created jar file " + jarFile1); 82 JarUtils.createJar(jarFile2.toString(), TEST_SOURCE + ".class"); 83 System.out.println("Created jar file " + jarFile2); 84 85 86 String pathSepartor = System.getProperty("path.separator"); 87 String[] commands = { 88 "-Djava.security.manager", 89 "-Djava.security.policy=" + policy, 90 "-classpath", jarFile1 + pathSepartor + jarFile2, 91 TEST_SOURCE 92 }; 93 94 String userName = System.getProperty("user.name"); 95 96 createPolicyFile(jarFile1, policy); 97 System.out.println("Created policy for " + jarFile1); 98 ProcessTools.executeTestJava(commands) 99 .shouldHaveExitValue(0) 100 .shouldContain(userName) 101 .stderrShouldBeEmpty(); 102 103 createPolicyFile(jarFile2, policy); 104 System.out.println("Created policy for " + jarFile2); 105 ProcessTools.executeTestJava(commands) 106 .shouldNotHaveExitValue(0) 107 .shouldNotContain(userName) 108 .stderrShouldContain("java.security.AccessControlException"); 109 110 System.out.println("Test PASSES"); 111 } 112 }