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 / AccessController2Test.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 package org.apache.harmony.security.tests.java.security;
19
20 import java.security.AccessControlContext;
21 import java.security.AccessControlException;
22 import java.security.AccessController;
23 import java.security.AllPermission;
24 import java.security.Permission;
25 import java.security.PrivilegedAction;
26 import java.security.PrivilegedActionException;
27 import java.security.PrivilegedExceptionAction;
28
29 import dalvik.annotation.KnownFailure;
30 import dalvik.annotation.TestLevel;
31 import dalvik.annotation.TestTargetClass;
32 import dalvik.annotation.TestTargetNew;
33
34 @TestTargetClass(AccessController.class)
35 public class AccessController2Test extends junit.framework.TestCase {
36
37     PrivilegedAction<Boolean> privAction = new PrivilegedAction<Boolean>() {
38         public Boolean run() {
39             try {
40                 AccessController.checkPermission(new AllPermission());
41                 return new Boolean(false);
42             } catch (SecurityException ex) {
43                 return new Boolean(true);
44             }
45         }
46     };
47
48     PrivilegedExceptionAction<Boolean> privExceptAction =
49         new PrivilegedExceptionAction<Boolean>() {
50         public Boolean run() {
51             try {
52                 AccessController.checkPermission(new AllPermission());
53                 return new Boolean(false);
54             } catch (SecurityException ex) {
55                 return new Boolean(true);
56             }
57         }
58     };
59
60     /**
61      * @tests java.security.AccessController#doPrivileged(java.security.PrivilegedAction,
62      *        java.security.AccessControlContext))
63      */
64     @TestTargetNew(
65         level = TestLevel.COMPLETE,
66         method = "doPrivileged",
67         args = {java.security.PrivilegedAction.class, java.security.AccessControlContext.class}
68     )
69     @KnownFailure("Fails (probably) because no protection domain is set.")
70     public void test_doPrivilegedLjava_security_PrivilegedActionLjava_security_AccessControlContext() {
71         Boolean pass;
72
73         try {
74             AccessController.doPrivileged((PrivilegedAction<?>) null, null);
75             fail("Test 1: NullPointerException expected.");
76         } catch (NullPointerException e) {
77             // Expected.
78         }
79
80         pass = AccessController.doPrivileged(privAction, null);
81         assertTrue("Test 2: Got AllPermission when providing a null " +
82                 "AccessControlContext.", pass.booleanValue());
83
84         AccessControlContext acc = AccessController.getContext();
85         assertNotNull("Test 3: AccessControlContext must not be null", acc);
86
87         pass = AccessController.doPrivileged(privAction, acc);
88         assertTrue("Test 4: Got AllPermission when providing a non-null " +
89                 "AccessControlContext.", pass.booleanValue());
90     }
91
92     /**
93      * @tests java.security.AccessController#doPrivileged(java.security.PrivilegedAction))
94      */
95     @TestTargetNew(
96         level = TestLevel.PARTIAL_COMPLETE,
97         method = "doPrivileged",
98         args = {java.security.PrivilegedAction.class}
99     )
100     @KnownFailure("Fails (probably) because no protection domain is set.")
101     public void test_doPrivilegedLjava_security_PrivilegedAction() {
102         Boolean pass;
103
104         try {
105             AccessController.doPrivileged((PrivilegedAction<?>) null);
106             fail("Test 1: NullPointerException expected.");
107         } catch (NullPointerException e) {
108             // Expected.
109         }
110
111         pass = AccessController.doPrivileged(privAction);
112         assertTrue("Test 2: Got AllPermission when providing no " +
113                 "AccessControlContext.", pass.booleanValue());
114     }
115
116     /**
117      * @tests java.security.AccessController#doPrivileged(java.security.PrivilegedExceptionAction,
118      *        java.security.AccessControlContext))
119      */
120     @TestTargetNew(
121         level = TestLevel.COMPLETE,
122         method = "doPrivileged",
123         args = {java.security.PrivilegedExceptionAction.class, java.security.AccessControlContext.class}
124     )
125     @KnownFailure("Fails (probably) because no protection domain is set.")
126     public void test_doPrivilegedLjava_security_PrivilegedExceptionActionLjava_security_AccessControlContext() {
127         Boolean pass;
128
129         try {
130             AccessController.doPrivileged((PrivilegedExceptionAction<?>) null);
131             fail("Test 1: NullPointerException expected.");
132         } catch (NullPointerException e) {
133             // Expected.
134         } catch (PrivilegedActionException e) {
135             fail("Test 2: Unexpected PrivilegedActionException " +
136                     e.getMessage());
137         }
138
139         try {
140             pass = AccessController.doPrivileged(privExceptAction, null);
141             assertTrue("Test 3: Got AllPermission when providing a null " +
142                     "AccessControlContext.", pass.booleanValue());
143         } catch (PrivilegedActionException e) {
144             fail("Test 4: Unexpected PrivilegedActionException " +
145                     e.getMessage());
146         }
147
148         AccessControlContext acc = AccessController.getContext();
149         assertNotNull("Test 5: AccessControlContext must not be null", acc);
150
151         try {
152             pass = AccessController.doPrivileged(privExceptAction, acc);
153             assertTrue("Test 6: Got AllPermission when providing non-null " +
154                     "AccessControlContext.", pass.booleanValue());
155         } catch (PrivilegedActionException e) {
156             fail("Test 7: Unexpected PrivilegedActionException " +
157                     e.getMessage());
158         }
159     }
160
161     /**
162      * @tests java.security.AccessController#doPrivileged(java.security.PrivilegedExceptionAction))
163      */
164     @TestTargetNew(
165         level = TestLevel.COMPLETE,
166         method = "doPrivileged",
167         args = {java.security.PrivilegedExceptionAction.class}
168     )
169     @KnownFailure("Fails (probably) because no protection domain is set.")
170     public void test_doPrivilegedLjava_security_PrivilegedExceptionAction() {
171         Boolean pass;
172
173         try {
174             AccessController.doPrivileged((PrivilegedExceptionAction<?>) null);
175             fail("Test 1: NullPointerException expected.");
176         } catch (NullPointerException e) {
177             // Expected.
178         } catch (PrivilegedActionException e) {
179             fail("Test 2: Unexpected PrivilegedActionException " +
180                     e.getMessage());
181         }
182
183         try {
184             pass = AccessController.doPrivileged(privExceptAction);
185             assertTrue("Test 3: Got AllPermission when providing no " +
186                     "AccessControlContext.", pass.booleanValue());
187         } catch (PrivilegedActionException e) {
188             fail("Test 4: Unexpected exception " + e.getMessage());
189         }
190     }
191
192     /**
193      * @tests java.security.AccessController#checkPermission(Permission perm)
194      */
195     @TestTargetNew(
196         level = TestLevel.PARTIAL_COMPLETE,
197         notes = "",
198         method = "checkPermission",
199         args = {java.security.Permission.class}
200     )
201     public void test_checkPermission_NullParameter() {
202         //Null parameter
203         try {
204             AccessController.checkPermission(null);
205             fail("Test 1: NullPointerException expected.");
206         } catch (NullPointerException npe) {
207             //expected
208         }
209     }
210
211     /**
212      * @tests java.security.AccessController#checkPermission(Permission perm)
213      */
214     @TestTargetNew(
215         level = TestLevel.PARTIAL_COMPLETE,
216         notes = "",
217         method = "checkPermission",
218         args = {java.security.Permission.class}
219     )
220     @KnownFailure("Fails (probably) because no protection domain is set.")
221     public void test_checkPermission_InvalidPermission() {
222         String[] perm_invalid = {null, "1", "", "invalid", "bla-bla", "testCtor123^%$#&^ &^$"};
223         Permission perm;
224
225         //Null parameter
226         try {
227             AccessController.checkPermission(null);
228             fail("NullPointerException should be thrown for NULL parameter");
229         } catch (NullPointerException npe) {
230             //expected
231         }
232
233         //Invalid parameter
234         for (int i = 0; i < perm_invalid.length; i++) {
235             try {
236                 perm = new RealPermission(perm_invalid[i]);
237                 AccessController.checkPermission(perm);
238                 fail("AccessControlException should be thrown for INVALID parameter " + perm_invalid[i]);
239             } catch (AccessControlException ace) {
240                 //expected
241             } catch (Exception e) {
242                 fail("Unexpected exception caught: " + e.toString());
243             }
244
245         }
246     }
247
248     /**
249      * @tests java.security.AccessController#getContext()
250      */
251     @TestTargetNew(
252         level = TestLevel.COMPLETE,
253         notes = "",
254         method = "getContext",
255         args = {}
256     )
257     public void test_getContext() {
258         try {
259             AccessControlContext acc = AccessController.getContext();
260             assertNotNull(acc);
261             assertTrue(acc instanceof AccessControlContext);
262         } catch (Exception e) {
263             fail("Unexpected exception");
264         }
265     }
266
267     // Bare extension to instantiate abstract Permission class
268     static final class RealPermission extends Permission {
269
270         private static final long serialVersionUID = 1L;
271
272         public RealPermission(String name) {
273             super(name);
274         }
275
276         public boolean equals(Object obj) {
277             return false;
278         }
279
280         public String getActions() {
281             return null;
282         }
283
284         public int hashCode() {
285             return 0;
286         }
287
288         public boolean implies(Permission permission) {
289             return false;
290         }
291     }
292 }