2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
|
2
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @author Danny Hendler
|
|
27 |
* @author Gary Ellison
|
|
28 |
* @bug 4244271
|
|
29 |
* @summary New policy sometimes has no effect with no indication given
|
|
30 |
* @run main/othervm/policy=setpolicy.jp TestDynamicPolicy
|
|
31 |
*/
|
|
32 |
|
|
33 |
/*
|
|
34 |
The test should be given the following permissions:
|
|
35 |
|
|
36 |
grant codeBase "file:testPath" {
|
|
37 |
permission java.security.SecurityPermission "setPolicy";
|
|
38 |
permission java.security.SecurityPermission "getPolicy";
|
|
39 |
};
|
|
40 |
|
|
41 |
*/
|
|
42 |
|
|
43 |
|
|
44 |
import java.io.PrintStream;
|
|
45 |
import java.io.IOException;
|
|
46 |
|
|
47 |
import java.lang.System;
|
|
48 |
import java.security.Policy;
|
|
49 |
|
|
50 |
|
|
51 |
public class TestDynamicPolicy {
|
|
52 |
|
|
53 |
public static void main(String args[]) throws Exception {
|
|
54 |
|
|
55 |
try {
|
|
56 |
//
|
|
57 |
TestDynamicPolicy jstest = new TestDynamicPolicy();
|
|
58 |
jstest.doit();
|
|
59 |
} catch(Exception e) {
|
|
60 |
System.out.println("Failed. Unexpected exception:" + e);
|
|
61 |
throw e;
|
|
62 |
}
|
|
63 |
System.out.println("Passed. OKAY");
|
|
64 |
}
|
|
65 |
|
|
66 |
private void doit() throws Exception {
|
|
67 |
// A security manager must be installed
|
|
68 |
SecurityManager sm=System.getSecurityManager();
|
|
69 |
if (sm==null)
|
|
70 |
throw new
|
|
71 |
Exception("Test must be run with a security manager installed");
|
|
72 |
|
|
73 |
// Instantiate and set the new policy
|
|
74 |
DynamicPolicy dp = new DynamicPolicy();
|
|
75 |
Policy.setPolicy(dp);
|
|
76 |
|
|
77 |
// Verify that policy has been set
|
|
78 |
if (dp != Policy.getPolicy())
|
|
79 |
throw new Exception("Policy was not set!!");
|
|
80 |
|
|
81 |
// now see this class can access user.name
|
|
82 |
String usr = getUserName();
|
|
83 |
|
|
84 |
if (usr != null) {
|
|
85 |
System.out.println("Test was able to read user.name prior to refresh!");
|
|
86 |
throw new
|
|
87 |
Exception("Test was able to read user.name prior to refresh!");
|
|
88 |
}
|
|
89 |
|
|
90 |
// Now, make policy allow reading user.name
|
|
91 |
dp.refresh();
|
|
92 |
|
|
93 |
// now I should be able to read it
|
|
94 |
usr = getUserName();
|
|
95 |
|
|
96 |
if (usr == null) {
|
|
97 |
System.out.println("Test was unable to read user.name after refresh!");
|
|
98 |
throw new
|
|
99 |
Exception("Test was unable to read user.name after refresh!");
|
|
100 |
}
|
|
101 |
// Now, take away permission to read user.name
|
|
102 |
dp.refresh();
|
|
103 |
|
|
104 |
// now I should not be able to read it
|
|
105 |
usr = getUserName();
|
|
106 |
|
|
107 |
if (usr != null) {
|
|
108 |
System.out.println("Test was able to read user.name following 2nd refresh!");
|
|
109 |
throw new
|
|
110 |
Exception("Test was able to read user.name following 2nd refresh!");
|
|
111 |
}
|
|
112 |
|
|
113 |
}
|
|
114 |
|
|
115 |
private String getUserName() {
|
|
116 |
String usr = null;
|
|
117 |
|
|
118 |
try {
|
|
119 |
usr = System.getProperty("user.name");
|
|
120 |
} catch (Exception e) {
|
|
121 |
}
|
|
122 |
return usr;
|
|
123 |
}
|
|
124 |
}
|