Path: blob/master/test/jdk/java/security/AccessController/DoPrivAccompliceTest.java
41149 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import jdk.test.lib.process.ProcessTools;26import jdk.test.lib.util.JarUtils;27import jdk.test.lib.helpers.ClassFileInstaller;2829import java.io.FileWriter;30import java.io.IOException;31import java.nio.file.Path;32import java.nio.file.Paths;3334/*35* @test36* @bug 804836237* @summary Tests the doPrivileged with accomplice Generate two jars38* (DoPrivTest.jar and DoPrivAccomplice.jar) and grant permission to39* DoPrivAccmplice.jar for reading user.name property from a PrivilagedAction.40* Run DoPrivTest.jar and try to access user.name property using41* DoPrivAccmplice.jar.42*43* @library /test/lib44* @build jdk.test.lib.util.JarUtils45* jdk.test.lib.Utils46* jdk.test.lib.Asserts47* jdk.test.lib.JDKToolFinder48* jdk.test.lib.JDKToolLauncher49* jdk.test.lib.Platform50* jdk.test.lib.process.*51* @run main/othervm DoPrivAccompliceTest52*/5354public class DoPrivAccompliceTest {55private static final String ACTION_SOURCE = DoPrivAccomplice.class.getName();56private static final String TEST_SOURCE = DoPrivTest.class.getName();5758private static void createPolicyFile(Path jarFile, Path policy) {59String codebase = jarFile.toFile().toURI().toString();60String quotes = "\"";61StringBuilder policyFile = new StringBuilder();62policyFile.append("grant codeBase ")63.append(quotes).append(codebase).append(quotes)64.append("{\n")65.append("permission java.util.PropertyPermission ")66.append(quotes).append("user.name").append(quotes)67.append(",")68.append(quotes).append("read").append(quotes)69.append(";\n};");70try (FileWriter writer = new FileWriter(policy.toFile())) {71writer.write(policyFile.toString());72} catch (IOException e) {73throw new Error("Error while creating policy file " + policy, e);74}75}7677public static void main(String[] args) throws Exception {78// copy class files to pwd79ClassFileInstaller.main(ACTION_SOURCE, TEST_SOURCE);80Path pwd = Paths.get(".");81Path jarFile1 = pwd.resolve(ACTION_SOURCE + ".jar").toAbsolutePath();82Path jarFile2 = pwd.resolve(TEST_SOURCE + ".jar").toAbsolutePath();83Path policy = pwd.resolve("java.policy").toAbsolutePath();8485JarUtils.createJar(jarFile1.toString(), ACTION_SOURCE + ".class");86System.out.println("Created jar file " + jarFile1);87JarUtils.createJar(jarFile2.toString(), TEST_SOURCE + ".class");88System.out.println("Created jar file " + jarFile2);899091String pathSepartor = System.getProperty("path.separator");92String[] commands = {93"-Djava.security.manager",94"-Djava.security.policy=" + policy,95"-classpath", jarFile1 + pathSepartor + jarFile2,96TEST_SOURCE97};9899String userName = System.getProperty("user.name");100101createPolicyFile(jarFile1, policy);102System.out.println("Created policy for " + jarFile1);103ProcessTools.executeTestJava(commands)104.shouldHaveExitValue(0)105.shouldContain(userName)106.stderrShouldBeEmptyIgnoreWarnings();107108createPolicyFile(jarFile2, policy);109System.out.println("Created policy for " + jarFile2);110ProcessTools.executeTestJava(commands)111.shouldNotHaveExitValue(0)112.shouldNotContain(userName)113.stderrShouldContain("java.security.AccessControlException");114115System.out.println("Test PASSES");116}117}118119120