OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / security / tests / java / security / ProtectionDomainTest.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19 * @author Alexander V. Astapchuk
20 * @version $Revision$
21 */
22
23 package org.apache.harmony.security.tests.java.security;
24
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
29
30 import java.net.URL;
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;
41
42 import junit.framework.TestCase;
43 @TestTargetClass(ProtectionDomain.class)
44 /**
45  * Unit tests for java.security.ProtectionDomain.
46  *
47  */
48
49 public class ProtectionDomainTest extends TestCase {
50
51     private final AllPermission allperm = new AllPermission();
52
53     private URL url = null;
54
55     private CodeSource cs = null;
56
57     private PermissionCollection perms = null;
58
59     private ClassLoader classldr = null;
60
61     private Principal[] principals = null; // changed in setUp()
62
63     /*
64      * @see TestCase#setUp()
65      */
66     protected void setUp() throws Exception {
67         super.setUp();
68         try {
69             url = new URL("http://localhost");
70         } catch (MalformedURLException ex) {
71             throw new Error(ex);
72         }
73         cs = new CodeSource(url, (java.security.cert.Certificate[]) null);
74         perms = allperm.newPermissionCollection();
75         perms.add(allperm);
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"), };
80     }
81
82     /**
83      * Class under test for void ProtectionDomain(CodeSource,
84      * PermissionCollection)
85      */
86     @TestTargetNew(
87         level = TestLevel.PARTIAL_COMPLETE,
88         notes = "",
89         method = "ProtectionDomain",
90         args = {java.security.CodeSource.class, java.security.PermissionCollection.class}
91     )
92     public void testProtectionDomainCodeSourcePermissionCollection_00() {
93         new ProtectionDomain(null, null);
94         new ProtectionDomain(cs, null);
95
96         new ProtectionDomain(cs, perms);
97     }
98
99     /**
100      * the ctor must set the PermissionCollection read-only
101      */
102     @TestTargetNew(
103         level = TestLevel.PARTIAL_COMPLETE,
104         notes = "",
105         method = "ProtectionDomain",
106         args = {java.security.CodeSource.class, java.security.PermissionCollection.class}
107     )
108     public void testProtectionDomainCodeSourcePermissionCollection_01() {
109         assertFalse(perms.isReadOnly());
110         new ProtectionDomain(null, perms);
111         assertTrue(perms.isReadOnly());
112     }
113
114     /**
115      * Test for ProtectionDomain(CodeSource, PermissionCollection, ClassLoader, Principal[])
116      */
117     @TestTargetNew(
118         level = TestLevel.COMPLETE,
119         notes = "",
120         method = "ProtectionDomain",
121         args = {java.security.CodeSource.class, java.security.PermissionCollection.class, java.lang.ClassLoader.class, java.security.Principal[].class}
122     )
123     public void testProtectionDomainCodeSourcePermissionCollectionClassLoaderPrincipalArray() {
124         new ProtectionDomain(null, null, null, null);
125
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);
130
131         new ProtectionDomain(cs, perms, classldr, principals);
132     }
133
134     /**
135      * Tests for ProtectionDomain.getClassLoader()
136      */
137     @TestTargetNew(
138         level = TestLevel.COMPLETE,
139         notes = "",
140         method = "getClassLoader",
141         args = {}
142     )
143     public void testGetClassLoader() {
144         assertNull(new ProtectionDomain(null, null).getClassLoader());
145         assertSame(new ProtectionDomain(null, null, classldr, null)
146                 .getClassLoader(), classldr);
147     }
148
149     /**
150      * Tests for ProtectionDomain.getCodeSource()
151      */
152     @TestTargetNew(
153         level = TestLevel.COMPLETE,
154         notes = "",
155         method = "getCodeSource",
156         args = {}
157     )
158     public void testGetCodeSource() {
159         assertNull(new ProtectionDomain(null, null).getCodeSource());
160         assertSame(new ProtectionDomain(cs, null).getCodeSource(), cs);
161     }
162
163     /**
164      * Tests for ProtectionDomain.getPermissions()
165      */
166     @TestTargetNew(
167         level = TestLevel.COMPLETE,
168         notes = "",
169         method = "getPermissions",
170         args = {}
171     )
172     public void testGetPermissions() {
173         assertNull(new ProtectionDomain(null, null).getPermissions());
174         assertSame(new ProtectionDomain(null, perms).getPermissions(), perms);
175     }
176
177     /**
178      * getPrincipals() always returns non null array
179      */
180     @TestTargetNew(
181         level = TestLevel.PARTIAL_COMPLETE,
182         notes = "",
183         method = "getPrincipals",
184         args = {}
185     )
186     public void testGetPrincipals_00() {
187         assertNotNull(new ProtectionDomain(null, null).getPrincipals());
188     }
189
190     /**
191      * getPrincipals() returns new array each time it's called
192      */
193     @TestTargetNew(
194         level = TestLevel.PARTIAL_COMPLETE,
195         notes = "",
196         method = "getPrincipals",
197         args = {}
198     )
199     public void testGetPrincipals_01() {
200         ProtectionDomain pd = new ProtectionDomain(null, null, null, principals);
201         Principal[] got = pd.getPrincipals();
202         assertNotNull(got);
203         assertNotSame(got, principals);
204         assertNotSame(got, pd.getPrincipals());
205         assertTrue(got.length == principals.length);
206     }
207
208     /**
209      * ProtectionDomain with null Permissions must not imply() permissions.
210      */
211     @TestTargetNew(
212         level = TestLevel.PARTIAL_COMPLETE,
213         notes = "",
214         method = "implies",
215         args = {java.security.Permission.class}
216     )
217     public void testImplies_00() {
218         assertFalse(new ProtectionDomain(null, null).implies(allperm));
219     }
220
221     /**
222      * ProtectionDomain with PermissionCollection which contains AllPermission
223      * must imply() AllPermission.
224      */
225     @TestTargetNew(
226         level = TestLevel.PARTIAL_COMPLETE,
227         notes = "",
228         method = "implies",
229         args = {java.security.Permission.class}
230     )
231     public void testImplies_01() {
232         assertTrue(new ProtectionDomain(null, perms).implies(allperm));
233     }
234
235     /**
236      * ProtectionDomain created with a static set of permissions must not query
237      * policy.
238      */
239     @TestTargetNew(
240         level = TestLevel.PARTIAL_COMPLETE,
241         notes = "",
242         method = "implies",
243         args = {java.security.Permission.class}
244     )
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);
251         try {
252             Policy.setPolicy(policy);
253             pd.implies(allperm);
254         } finally {
255             Policy.setPolicy(null);
256         }
257         assertFalse(policy.getPdTracked());
258     }
259
260     /**
261      * ProtectionDomain created with dynamic set of permissions must query
262      * policy.
263      */
264     @TestTargetNew(
265         level = TestLevel.PARTIAL_COMPLETE,
266         notes = "",
267         method = "implies",
268         args = {java.security.Permission.class}
269     )
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);
275         try {
276             Policy.setPolicy(policy);
277             pd.implies(allperm);
278         } finally {
279             Policy.setPolicy(null);
280         }
281         assertTrue(policy.getPdTracked());
282     }
283
284     /**
285      * Simply checks that it's working somehow
286      */
287     @TestTargetNew(
288         level = TestLevel.COMPLETE,
289         notes = "",
290         method = "toString",
291         args = {}
292     )
293     public void testToString() {
294         String res;
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"));
303     }
304
305     /**
306      * Test principal used during the testing. Does nothing.
307      */
308
309     private static class TestPrincipal implements Principal {
310         private String name;
311
312         TestPrincipal(String name) {
313             this.name = name;
314         }
315
316         public String getName() {
317             return "TestPrincipal: " + name;
318         }
319     }
320
321     private static class TestPolicy extends Policy {
322         ProtectionDomain trackPD = null;
323
324         boolean pdTracked = false;
325
326         ProtectionDomain setTrackPD(ProtectionDomain pd) {
327             ProtectionDomain tmp = trackPD;
328             trackPD = pd;
329             pdTracked = false;
330             return tmp;
331         }
332
333         boolean getPdTracked() {
334             return pdTracked;
335         }
336
337         public PermissionCollection getPermissions(CodeSource cs) {
338             return new Permissions();
339         }
340
341         //        public PermissionCollection getPermissions(ProtectionDomain domain) {
342         //            return super.getPermissions(domain);
343         //        }
344         public boolean implies(ProtectionDomain domain, Permission permission) {
345             if (trackPD != null && trackPD == domain) {
346                 pdTracked = true;
347             }
348             return super.implies(domain, permission);
349         }
350
351         public void refresh() {
352             // do nothing
353         }
354     }
355
356 }