2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @author Alexander V. Astapchuk
23 package org.apache.harmony.security.tests.java.security;
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
31 import java.net.MalformedURLException;
32 import java.net.URLClassLoader;
33 import java.security.AllPermission;
34 import java.security.CodeSource;
35 import java.security.Permission;
36 import java.security.PermissionCollection;
37 import java.security.Permissions;
38 import java.security.Policy;
39 import java.security.Principal;
40 import java.security.ProtectionDomain;
42 import junit.framework.TestCase;
43 @TestTargetClass(ProtectionDomain.class)
45 * Unit tests for java.security.ProtectionDomain.
49 public class ProtectionDomainTest extends TestCase {
51 private final AllPermission allperm = new AllPermission();
53 private URL url = null;
55 private CodeSource cs = null;
57 private PermissionCollection perms = null;
59 private ClassLoader classldr = null;
61 private Principal[] principals = null; // changed in setUp()
64 * @see TestCase#setUp()
66 protected void setUp() throws Exception {
69 url = new URL("http://localhost");
70 } catch (MalformedURLException ex) {
73 cs = new CodeSource(url, (java.security.cert.Certificate[]) null);
74 perms = allperm.newPermissionCollection();
76 classldr = URLClassLoader.newInstance(new URL[] { url });
77 principals = new Principal[] { new TestPrincipal("0"),
78 new TestPrincipal("1"), new TestPrincipal("2"),
79 new TestPrincipal("3"), new TestPrincipal("4"), };
83 * Class under test for void ProtectionDomain(CodeSource,
84 * PermissionCollection)
87 level = TestLevel.PARTIAL_COMPLETE,
89 method = "ProtectionDomain",
90 args = {java.security.CodeSource.class, java.security.PermissionCollection.class}
92 public void testProtectionDomainCodeSourcePermissionCollection_00() {
93 new ProtectionDomain(null, null);
94 new ProtectionDomain(cs, null);
96 new ProtectionDomain(cs, perms);
100 * the ctor must set the PermissionCollection read-only
103 level = TestLevel.PARTIAL_COMPLETE,
105 method = "ProtectionDomain",
106 args = {java.security.CodeSource.class, java.security.PermissionCollection.class}
108 public void testProtectionDomainCodeSourcePermissionCollection_01() {
109 assertFalse(perms.isReadOnly());
110 new ProtectionDomain(null, perms);
111 assertTrue(perms.isReadOnly());
115 * Test for ProtectionDomain(CodeSource, PermissionCollection, ClassLoader, Principal[])
118 level = TestLevel.COMPLETE,
120 method = "ProtectionDomain",
121 args = {java.security.CodeSource.class, java.security.PermissionCollection.class, java.lang.ClassLoader.class, java.security.Principal[].class}
123 public void testProtectionDomainCodeSourcePermissionCollectionClassLoaderPrincipalArray() {
124 new ProtectionDomain(null, null, null, null);
126 new ProtectionDomain(cs, null, null, null);
127 new ProtectionDomain(null, perms, null, null);
128 new ProtectionDomain(null, null, classldr, null);
129 new ProtectionDomain(null, null, null, principals);
131 new ProtectionDomain(cs, perms, classldr, principals);
135 * Tests for ProtectionDomain.getClassLoader()
138 level = TestLevel.COMPLETE,
140 method = "getClassLoader",
143 public void testGetClassLoader() {
144 assertNull(new ProtectionDomain(null, null).getClassLoader());
145 assertSame(new ProtectionDomain(null, null, classldr, null)
146 .getClassLoader(), classldr);
150 * Tests for ProtectionDomain.getCodeSource()
153 level = TestLevel.COMPLETE,
155 method = "getCodeSource",
158 public void testGetCodeSource() {
159 assertNull(new ProtectionDomain(null, null).getCodeSource());
160 assertSame(new ProtectionDomain(cs, null).getCodeSource(), cs);
164 * Tests for ProtectionDomain.getPermissions()
167 level = TestLevel.COMPLETE,
169 method = "getPermissions",
172 public void testGetPermissions() {
173 assertNull(new ProtectionDomain(null, null).getPermissions());
174 assertSame(new ProtectionDomain(null, perms).getPermissions(), perms);
178 * getPrincipals() always returns non null array
181 level = TestLevel.PARTIAL_COMPLETE,
183 method = "getPrincipals",
186 public void testGetPrincipals_00() {
187 assertNotNull(new ProtectionDomain(null, null).getPrincipals());
191 * getPrincipals() returns new array each time it's called
194 level = TestLevel.PARTIAL_COMPLETE,
196 method = "getPrincipals",
199 public void testGetPrincipals_01() {
200 ProtectionDomain pd = new ProtectionDomain(null, null, null, principals);
201 Principal[] got = pd.getPrincipals();
203 assertNotSame(got, principals);
204 assertNotSame(got, pd.getPrincipals());
205 assertTrue(got.length == principals.length);
209 * ProtectionDomain with null Permissions must not imply() permissions.
212 level = TestLevel.PARTIAL_COMPLETE,
215 args = {java.security.Permission.class}
217 public void testImplies_00() {
218 assertFalse(new ProtectionDomain(null, null).implies(allperm));
222 * ProtectionDomain with PermissionCollection which contains AllPermission
223 * must imply() AllPermission.
226 level = TestLevel.PARTIAL_COMPLETE,
229 args = {java.security.Permission.class}
231 public void testImplies_01() {
232 assertTrue(new ProtectionDomain(null, perms).implies(allperm));
236 * ProtectionDomain created with a static set of permissions must not query
240 level = TestLevel.PARTIAL_COMPLETE,
243 args = {java.security.Permission.class}
245 public void testImplies_02() {
246 TestPolicy policy = new TestPolicy();
247 // null set of permissions [must] force the PD to use Policy - for
248 // dynamic permissions
249 ProtectionDomain pd = new ProtectionDomain(cs, null);
250 policy.setTrackPD(pd);
252 Policy.setPolicy(policy);
255 Policy.setPolicy(null);
257 assertFalse(policy.getPdTracked());
261 * ProtectionDomain created with dynamic set of permissions must query
265 level = TestLevel.PARTIAL_COMPLETE,
268 args = {java.security.Permission.class}
270 public void testImplies_03() {
271 TestPolicy policy = new TestPolicy();
272 ProtectionDomain pd = new ProtectionDomain(cs, null, ClassLoader
273 .getSystemClassLoader(), principals);
274 policy.setTrackPD(pd);
276 Policy.setPolicy(policy);
279 Policy.setPolicy(null);
281 assertTrue(policy.getPdTracked());
285 * Simply checks that it's working somehow
288 level = TestLevel.COMPLETE,
293 public void testToString() {
295 res = new ProtectionDomain(null, null).toString();
296 assertTrue(res.contains("ProtectionDomain"));
297 res = new ProtectionDomain(cs, perms).toString();
298 assertTrue(res.contains("ProtectionDomain"));
299 res = new ProtectionDomain(null, null, null, null).toString();
300 assertTrue(res.contains("ProtectionDomain"));
301 res = new ProtectionDomain(cs, perms, classldr, principals).toString();
302 assertTrue(res.contains("ProtectionDomain"));
306 * Test principal used during the testing. Does nothing.
309 private static class TestPrincipal implements Principal {
312 TestPrincipal(String name) {
316 public String getName() {
317 return "TestPrincipal: " + name;
321 private static class TestPolicy extends Policy {
322 ProtectionDomain trackPD = null;
324 boolean pdTracked = false;
326 ProtectionDomain setTrackPD(ProtectionDomain pd) {
327 ProtectionDomain tmp = trackPD;
333 boolean getPdTracked() {
337 public PermissionCollection getPermissions(CodeSource cs) {
338 return new Permissions();
341 // public PermissionCollection getPermissions(ProtectionDomain domain) {
342 // return super.getPermissions(domain);
344 public boolean implies(ProtectionDomain domain, Permission permission) {
345 if (trackPD != null && trackPD == domain) {
348 return super.implies(domain, permission);
351 public void refresh() {