1/*
2 * Copyright (c) 2007, 2016, 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
26import java.io.File;
27import java.io.FileWriter;
28import java.io.IOException;
29import java.net.URI;
30import java.util.ArrayList;
31import java.util.Arrays;
32import java.util.List;
33
34/*
35 * @test
36 * @bug 8048362
37 * @compile ../../../lib/testlibrary/JavaToolUtils.java
38 *                             DoPrivAccomplice.java DoPrivTest.java
39 * @summary Tests the doPrivileged with accomplice Generate two jars
40 * (DoPrivTest.jar and DoPrivAccomplice.jar) and grant permission to
41 * DoPrivAccmplice.jar for reading user.home property from a PrivilagedAction.
42 * Run DoPrivTest.jar and try to access user.home property using
43 * DoPrivAccmplice.jar.
44 * @modules jdk.compiler
45 * @run main/othervm DoPrivAccompliceTest
46 */
47
48public class DoPrivAccompliceTest {
49
50    private static final String PWD = System.getProperty("test.classes", "./");
51    private static final String ACTION_SOURCE = "DoPrivAccomplice";
52    private static final String TEST_SOURCE = "DoPrivTest";
53
54    public static void createPolicyFile(URI codebaseURL) throws IOException {
55        String codebase = codebaseURL.toString();
56        String quotes = "\"";
57        StringBuilder policyFile = new StringBuilder();
58        policyFile.append("grant codeBase ").append(quotes).
59                append(codebase).append(quotes).append("{\n").
60                append("permission java.util.PropertyPermission ").
61                append(quotes).append("user.name").append(quotes).
62                append(",").append(quotes).append("read").append(quotes).
63                append(";\n};");
64        try (FileWriter writer = new FileWriter(new File(PWD, "java.policy"))) {
65            writer.write(policyFile.toString());
66            writer.close();
67        } catch (IOException e) {
68            System.err.println("Error while creating policy file");
69            throw e;
70        }
71    }
72
73    public static void main(String[] args) throws Exception {
74        final File class1 = new File(PWD, ACTION_SOURCE + ".class");
75        final File class2 = new File(PWD, TEST_SOURCE + ".class");
76        final File jarFile1 = new File(PWD, ACTION_SOURCE + ".jar");
77        final File jarFile2 = new File(PWD, TEST_SOURCE + ".jar");
78        System.out.println("Compilation successfull");
79        JavaToolUtils.createJar(jarFile1, Arrays.asList(new File[]{class1}));
80        System.out.println("Created jar file " + jarFile1);
81        JavaToolUtils.createJar(jarFile2, Arrays.asList(new File[]{class2}));
82        System.out.println("Created jar file " + jarFile2);
83        createPolicyFile(jarFile1.toURI());
84
85        List<String> commands = new ArrayList<>();
86        final String pathSepartor = System.getProperty("path.separator");
87        commands.add("-Djava.security.manager");
88        commands.add("-Djava.security.policy=" + PWD + "/java.policy");
89        commands.add("-classpath");
90        commands.add(PWD + "/" + TEST_SOURCE + ".jar" + pathSepartor
91                + PWD + "/" + ACTION_SOURCE + ".jar");
92        commands.add(TEST_SOURCE);
93        if (JavaToolUtils.runJava(commands) == 0) {
94            System.out.println("Test PASSES");
95        }
96    }
97
98}
99