Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/AccessController/DoPrivAccompliceTest.java
38813 views
/*1* Copyright (c) 2007, 2014, 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 java.io.File;26import java.io.FileWriter;27import java.io.IOException;28import java.net.URI;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.List;3233/**34* @test35* @bug 804836236* @compile ../../../lib/testlibrary/JavaToolUtils.java37* DoPrivAccomplice.java DoPrivTest.java38* @summary Tests the doPrivileged with accomplice Generate two jars39* (DoPrivTest.jar and DoPrivAccomplice.jar) and grant permission to40* DoPrivAccmplice.jar for reading user.home property from a PrivilagedAction.41* Run DoPrivTest.jar and try to access user.home property using42* DoPrivAccmplice.jar.43* @run main/othervm DoPrivAccompliceTest44*/4546public class DoPrivAccompliceTest {4748private static final String PWD = System.getProperty("test.classes", "./");49private static final String ACTION_SOURCE = "DoPrivAccomplice";50private static final String TEST_SOURCE = "DoPrivTest";5152public static void createPolicyFile(URI codebaseURL) throws IOException {53String codebase = codebaseURL.toString();54String quotes = "\"";55StringBuilder policyFile = new StringBuilder();56policyFile.append("grant codeBase ").append(quotes).57append(codebase).append(quotes).append("{\n").58append("permission java.util.PropertyPermission ").59append(quotes).append("user.name").append(quotes).60append(",").append(quotes).append("read").append(quotes).61append(";\n};");62try (FileWriter writer = new FileWriter(new File(PWD, "java.policy"))) {63writer.write(policyFile.toString());64writer.close();65} catch (IOException e) {66System.err.println("Error while creating policy file");67throw e;68}69}7071public static void main(String[] args) throws Exception {72final File class1 = new File(PWD, ACTION_SOURCE + ".class");73final File class2 = new File(PWD, TEST_SOURCE + ".class");74final File jarFile1 = new File(PWD, ACTION_SOURCE + ".jar");75final File jarFile2 = new File(PWD, TEST_SOURCE + ".jar");76System.out.println("Compilation successfull");77JavaToolUtils.createJar(jarFile1, Arrays.asList(new File[]{class1}));78System.out.println("Created jar file " + jarFile1);79JavaToolUtils.createJar(jarFile2, Arrays.asList(new File[]{class2}));80System.out.println("Created jar file " + jarFile2);81createPolicyFile(jarFile1.toURI());8283List<String> commands = new ArrayList<>();84final String pathSepartor = System.getProperty("path.separator");85commands.add("-Djava.security.manager");86commands.add("-Djava.security.policy=" + PWD + "/java.policy");87commands.add("-classpath");88commands.add(PWD + "/" + TEST_SOURCE + ".jar" + pathSepartor89+ PWD + "/" + ACTION_SOURCE + ".jar");90commands.add(TEST_SOURCE);91if (JavaToolUtils.runJava(commands) == 0) {92System.out.println("Test PASSES");93}94}9596}979899