author | iignatyev |
Fri, 26 May 2017 22:30:19 -0700 | |
changeset 45282 | e1cef38d150d |
parent 41226 | da753c438a07 |
child 45287 | e0bb5f83e17a |
permissions | -rw-r--r-- |
26362 | 1 |
/* |
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
2 |
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. |
26362 | 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 |
||
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
26 |
import jdk.test.lib.process.OutputAnalyzer; |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
27 |
import jdk.test.lib.process.ProcessTools; |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
28 |
import jdk.testlibrary.JarUtils; |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
29 |
|
26362 | 30 |
import java.io.FileWriter; |
31 |
import java.io.IOException; |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
32 |
import java.nio.file.Path; |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
33 |
import java.nio.file.Paths; |
26362 | 34 |
|
41226
da753c438a07
8166378: Missing dependencies in several java/security tests
skovalev
parents:
26362
diff
changeset
|
35 |
/* |
26362 | 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 |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
40 |
* DoPrivAccmplice.jar for reading user.name property from a PrivilagedAction. |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
41 |
* Run DoPrivTest.jar and try to access user.name property using |
26362 | 42 |
* DoPrivAccmplice.jar. |
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
43 |
* |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
44 |
* @library /test/lib /lib/testlibrary |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
45 |
* |
26362 | 46 |
* @run main/othervm DoPrivAccompliceTest |
47 |
*/ |
|
48 |
||
49 |
public class DoPrivAccompliceTest { |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
50 |
private static final String ACTION_SOURCE = DoPrivAccomplice.class.getName(); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
51 |
private static final String TEST_SOURCE = DoPrivTest.class.getName(); |
26362 | 52 |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
53 |
private static void createPolicyFile(Path jarFile, Path policy) { |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
54 |
String codebase = jarFile.toFile().toURI().toString(); |
26362 | 55 |
String quotes = "\""; |
56 |
StringBuilder policyFile = new StringBuilder(); |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
57 |
policyFile.append("grant codeBase ") |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
58 |
.append(quotes).append(codebase).append(quotes) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
59 |
.append("{\n") |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
60 |
.append("permission java.util.PropertyPermission ") |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
61 |
.append(quotes).append("user.name").append(quotes) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
62 |
.append(",") |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
63 |
.append(quotes).append("read").append(quotes) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
64 |
.append(";\n};"); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
65 |
try (FileWriter writer = new FileWriter(policy.toFile())) { |
26362 | 66 |
writer.write(policyFile.toString()); |
67 |
} catch (IOException e) { |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
68 |
throw new Error("Error while creating policy file " + policy, e); |
26362 | 69 |
} |
70 |
} |
|
71 |
||
72 |
public static void main(String[] args) throws Exception { |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
73 |
// copy class files to pwd |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
74 |
ClassFileInstaller.main(ACTION_SOURCE, TEST_SOURCE); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
75 |
Path pwd = Paths.get("."); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
76 |
Path jarFile1 = pwd.resolve(ACTION_SOURCE + ".jar").toAbsolutePath(); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
77 |
Path jarFile2 = pwd.resolve(TEST_SOURCE + ".jar").toAbsolutePath(); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
78 |
Path policy = pwd.resolve("java.policy").toAbsolutePath(); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
79 |
|
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
80 |
JarUtils.createJar(jarFile1.toString(), ACTION_SOURCE + ".class"); |
26362 | 81 |
System.out.println("Created jar file " + jarFile1); |
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
82 |
JarUtils.createJar(jarFile2.toString(), TEST_SOURCE + ".class"); |
26362 | 83 |
System.out.println("Created jar file " + jarFile2); |
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
84 |
|
26362 | 85 |
|
45282
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
86 |
String pathSepartor = System.getProperty("path.separator"); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
87 |
String[] commands = { |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
88 |
"-Djava.security.manager", |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
89 |
"-Djava.security.policy=" + policy, |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
90 |
"-classpath", jarFile1 + pathSepartor + jarFile2, |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
91 |
TEST_SOURCE |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
92 |
}; |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
93 |
|
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
94 |
String userName = System.getProperty("user.name"); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
95 |
|
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
96 |
createPolicyFile(jarFile1, policy); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
97 |
System.out.println("Created policy for " + jarFile1); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
98 |
ProcessTools.executeTestJava(commands) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
99 |
.shouldHaveExitValue(0) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
100 |
.shouldContain(userName) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
101 |
.stderrShouldBeEmpty(); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
102 |
|
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
103 |
createPolicyFile(jarFile2, policy); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
104 |
System.out.println("Created policy for " + jarFile2); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
105 |
ProcessTools.executeTestJava(commands) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
106 |
.shouldNotHaveExitValue(0) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
107 |
.shouldNotContain(userName) |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
108 |
.stderrShouldContain("java.security.AccessControlException"); |
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
109 |
|
e1cef38d150d
8180895: java/security/AccessController/DoPrivAccompliceTest.java has to be improved
iignatyev
parents:
41226
diff
changeset
|
110 |
System.out.println("Test PASSES"); |
26362 | 111 |
} |
112 |
} |