blob: 4bf7ca8f60d33f2e45b12fe803d6af42f4be4f46 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 5100561
27 * @bug 6273812
28 * @summary Can not explicitly create a java.security.Policy object from a file
29 * @build GetInstancePolicySpi GetInstanceProvider
30 * @run main/othervm/policy=GetInstance.policy GetInstance
31 */
32
33import java.security.*;
34
35import java.io.File;
36import java.net.URI;
37import sun.net.www.ParseUtil;
38
39public class GetInstance {
40
41 private static final String JAVA_POLICY = "JavaPolicy";
42
43 private static class BadParam implements Policy.Parameters { }
44
45 public static void main(String[] args) throws Exception {
46
47 int testnum = 1;
48 GetInstance gi = new GetInstance();
49
50 testnum = gi.testDefault(testnum);
51 testnum = gi.testStringProvider(testnum);
52 testnum = gi.testProvider(testnum);
53 testnum = gi.testCustomImpl(testnum);
54 testnum = gi.testBadParam(testnum);
55
56 // make this go last because we don't want to leave its policy set
57 // for other tests
58 testnum = gi.testURIParam(testnum);
59 }
60
61 private int testDefault(int testnum) throws Exception {
62 // get an instance of the default PolicySpiFile
63 Policy p = Policy.getInstance(JAVA_POLICY, null);
64 doTest(p, testnum++);
65 Policy.setPolicy(p);
66 doTestSM(testnum++);
67
68 // get an instance of FooPolicy
69 try {
70 p = Policy.getInstance("FooPolicy", null);
71 throw new SecurityException("test " + testnum++ + " failed");
72 } catch (NoSuchAlgorithmException nsae) {
73 // good
74 System.out.println("test " + testnum++ + " passed");
75 }
76
77 return testnum;
78 }
79
80 private int testStringProvider(int testnum) throws Exception {
81 // get an instance of JavaPolicy from SUN
82 Policy p = Policy.getInstance(JAVA_POLICY, null, "SUN");
83 doTest(p, testnum++);
84 Policy.setPolicy(p);
85 doTestSM(testnum++);
86
87 // get an instance of JavaPolicy from SunRsaSign
88 try {
89 p = Policy.getInstance(JAVA_POLICY, null, "SunRsaSign");
90 throw new SecurityException("test " + testnum++ + " failed");
91 } catch (NoSuchAlgorithmException nsae) {
92 // good
93 System.out.println("test " + testnum++ + " passed");
94 }
95
96 // get an instance of JavaPolicy from FOO
97 try {
98 p = Policy.getInstance(JAVA_POLICY, null, "FOO");
99 throw new SecurityException("test " + testnum++ + " failed");
100 } catch (NoSuchProviderException nspe) {
101 // good
102 System.out.println("test " + testnum++ + " passed");
103 }
104
105 return testnum;
106 }
107
108 private int testProvider(int testnum) throws Exception {
109 // get an instance of JavaPolicy from SUN
110 Policy p = Policy.getInstance(JAVA_POLICY,
111 null,
112 Security.getProvider("SUN"));
113 doTest(p, testnum++);
114 Policy.setPolicy(p);
115 doTestSM(testnum++);
116
117 // get an instance of JavaPolicy from SunRsaSign
118 try {
119 p = Policy.getInstance(JAVA_POLICY,
120 null,
121 Security.getProvider("SunRsaSign"));
122 throw new SecurityException("test " + testnum++ + " failed");
123 } catch (NoSuchAlgorithmException nsae) {
124 // good
125 System.out.println("test " + testnum++ + " passed");
126 }
127
128 return testnum;
129 }
130
131 private int testBadParam(int testnum) throws Exception {
132
133 // pass bad param
134
135 try {
136 Policy p = Policy.getInstance(JAVA_POLICY,
137 new BadParam());
138 throw new SecurityException("test " + testnum++ + " failed");
139 } catch (IllegalArgumentException iae) {
140 // good
141 System.out.println("test " + testnum++ + " passed");
142 }
143
144 try {
145 Policy p = Policy.getInstance(JAVA_POLICY,
146 new BadParam(),
147 "SUN");
148 throw new SecurityException("test " + testnum++ + " failed");
149 } catch (IllegalArgumentException iae) {
150 // good
151 System.out.println("test " + testnum++ + " passed");
152 }
153
154 try {
155 Policy p = Policy.getInstance(JAVA_POLICY,
156 new BadParam(),
157 Security.getProvider("SUN"));
158 throw new SecurityException("test " + testnum++ + " failed");
159 } catch (IllegalArgumentException iae) {
160 // good
161 System.out.println("test " + testnum++ + " passed");
162 }
163
164 return testnum;
165 }
166
167 private int testURIParam(int testnum) throws Exception {
168 // get an instance of JavaPolicy from SUN and have it read from the URL
169
170 File file = new File(System.getProperty("test.src", "."),
171 "GetInstance.policyURL");
172 URI uri = file.toURI();
173 Policy p = Policy.getInstance(JAVA_POLICY, new URIParameter(uri));
174
175 doTest(p, testnum++);
176 Policy.setPolicy(p);
177 doTestSM(testnum++);
178
179 return testnum;
180 }
181
182 private int testCustomImpl(int testnum) throws Exception {
183 Provider customProvider = new GetInstanceProvider();
184 Policy p = Policy.getInstance("GetInstancePolicySpi",
185 null,
186 customProvider);
187
188 // doTest has a case that will not work with custom policies,
189 // so do not call it
190 //
191 // doTest(p, testnum++);
192
193 Policy.setPolicy(p);
194 doTestSM(testnum++);
195
196 return testnum;
197 }
198
199 private void doTest(Policy p, int testnum) throws Exception {
200
201 // check granted perm
202 if (p.implies(this.getClass().getProtectionDomain(),
203 new SecurityPermission("GetInstanceTest"))) {
204 System.out.println("test " + testnum + ".1 passed");
205 } else {
206 throw new SecurityException("test " + testnum + ".1 failed");
207 }
208
209 // check perm not granted
210 if (p.implies(this.getClass().getProtectionDomain(),
211 new SecurityPermission("NotGranted"))) {
212 throw new SecurityException("test " + testnum + ".2 failed");
213 } else {
214 System.out.println("test " + testnum + ".2 passed");
215 }
216
217 // test getProvider
218 if ("SUN".equals(p.getProvider().getName())) {
219 System.out.println("test " + testnum + ".3 passed");
220 } else {
221 throw new SecurityException("test " + testnum + ".3 failed");
222 }
223
224 // test getType
225 if (JAVA_POLICY.equals(p.getType())) {
226 System.out.println("test " + testnum + ".4 passed");
227 } else {
228 throw new SecurityException("test " + testnum + ".4 failed");
229 }
230 }
231
232 private void doTestSM(int testnum) throws Exception {
233
234 // check granted perm
235 System.getSecurityManager().checkPermission
236 (new SecurityPermission("GetInstanceTest"));
237 System.out.println("test " + testnum + ".1 passed");
238
239 // check perm not granted
240 try {
241 System.getSecurityManager().checkPermission
242 (new SecurityPermission("NotGranted"));
243 throw new RuntimeException("test " + testnum + ".2 failed");
244 } catch (SecurityException se) {
245 System.out.println("test " + testnum + ".2 passed");
246 }
247 }
248}