OSDN Git Service

Use GCC visibility to reduce the size of libdvm by 10%
[android-x86/dalvik.git] / libcore / security / src / test / java / tests / security / acl / IAclTest.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 tests.security.acl;
19
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
24
25 import junit.framework.TestCase;
26
27 import java.security.acl.Acl;
28 import java.security.acl.AclEntry;
29 import java.security.acl.NotOwnerException;
30 import java.security.acl.Permission;
31 import java.security.Principal;
32 import java.util.Enumeration;
33 import java.util.Vector;
34
35 import org.apache.harmony.security.tests.support.acl.*;
36
37 @TestTargetClass(Acl.class)
38 public class IAclTest extends TestCase {
39     
40     class MyAcl extends AclImpl {
41         public MyAcl(Principal principal, String str) {
42             super(principal, str);
43         }
44     }
45
46     
47     /**
48      * @tests java.security.acl.Acl#addEntry(Principal caller, AclEntry entry)
49      * @tests java.security.acl.Acl#removeEntry(Principal caller, AclEntry entry)
50      */
51     @TestTargets({
52         @TestTargetNew(
53             level = TestLevel.COMPLETE,
54             notes = "",
55             method = "addEntry",
56             args = {java.security.Principal.class, java.security.acl.AclEntry.class}
57         ),
58         @TestTargetNew(
59             level = TestLevel.COMPLETE,
60             notes = "",
61             method = "removeEntry",
62             args = {java.security.Principal.class, java.security.acl.AclEntry.class}
63         )
64     })
65     public void test_Acl01() {
66         Principal pr = new PrincipalImpl("TestPrincipal");
67         String str = "TestName";
68         MyAcl acl = new MyAcl(pr, str);
69         AclEntry ae = new AclEntryImpl(pr);
70         try {
71             assertTrue(acl.addEntry(pr, ae));
72             assertFalse(acl.addEntry(pr, ae));
73             assertTrue(acl.removeEntry(pr, ae));
74             assertFalse(acl.removeEntry(pr, ae));
75         } catch (Exception ex) {
76             fail("Unexpected exception " + ex);
77         }
78         
79         try {
80             acl.addEntry(new PrincipalImpl("NewPrincipal"), ae);
81             fail("NotOwnerException was not thrown");
82         } catch (NotOwnerException noe) {
83             //expected
84         }
85         
86         try {
87             acl.removeEntry(new PrincipalImpl("NewPrincipal"), ae);
88             fail("NotOwnerException was not thrown");
89         } catch (NotOwnerException noe) {
90             //expected
91         }
92     }
93     
94     /**
95      * @tests java.security.acl.Acl#setName(Principal caller, String name)
96      * @tests java.security.acl.Acl#getName()
97      */
98     @TestTargets({
99         @TestTargetNew(
100             level = TestLevel.COMPLETE,
101             notes = "",
102             method = "setName",
103             args = {java.security.Principal.class, java.lang.String.class}
104         ),
105         @TestTargetNew(
106             level = TestLevel.COMPLETE,
107             notes = "",
108             method = "getName",
109             args = {}
110         )
111     })
112     public void test_Acl02() {
113         Principal pr = new PrincipalImpl("TestPrincipal");
114         String str = "TestName";
115         String newStr = "NewName";
116         MyAcl acl = new MyAcl(pr, str);
117         try {
118             assertEquals("Names are not equal", str, acl.getName());
119             acl.setName(pr, newStr);
120             assertEquals("Names are not equal", newStr, acl.getName());
121         } catch (Exception ex) {
122             fail("Unexpected exception " + ex);
123         }
124         
125         try {
126             acl.setName(new PrincipalImpl("NewPrincipal"), str);
127             fail("NotOwnerException was not thrown");
128         } catch (NotOwnerException noe) {
129             //expected
130         }
131     }
132     
133     /**
134      * @tests java.security.acl.Acl#toString()
135      */
136     @TestTargetNew(
137         level = TestLevel.COMPLETE,
138         notes = "",
139         method = "toString",
140         args = {}
141     )
142     public void test_Acl03() {
143         Principal pr = new PrincipalImpl("TestPrincipal");
144         String str = "TestName";
145         MyAcl acl = new MyAcl(pr, str);
146         AclEntry ae = new AclEntryImpl(pr);
147         Permission perm = new PermissionImpl("Permission_1");
148         try {
149             ae.addPermission(perm);
150             acl.addEntry(pr, ae);
151             String res = acl.toString();
152             assertTrue(res.contains(perm.toString()));
153         } catch (Exception ex) {
154             fail("Unexpected exception " + ex);
155         }
156     }
157     
158     /**
159      * @tests java.security.acl.Acl#entries()
160      */
161     @TestTargetNew(
162         level = TestLevel.COMPLETE,
163         notes = "",
164         method = "entries",
165         args = {}
166     )
167     public void test_Acl04() {
168         Principal pr = new PrincipalImpl("TestPrincipal");
169         String str = "TestName";
170         MyAcl acl = new MyAcl(pr, str);
171         AclEntry ae1 = new AclEntryImpl(pr);
172         try {
173             ae1.addPermission(new PermissionImpl("Permission_1"));
174             acl.addEntry(pr, ae1);
175             Enumeration en = acl.entries();
176             Vector v = new Vector();
177             while (en.hasMoreElements()) {
178                 v.addElement(en.nextElement());
179             }
180             assertEquals(v.size(), 1);
181         } catch (Exception ex) {
182             fail("Unexpected exception " + ex);
183         }
184     }
185     
186     /**
187      * @tests java.security.acl.Acl#checkPermission(Principal principal, Permission permission)
188      * @tests java.security.acl.Acl#getPermissions(Principal principal)
189      */
190     @TestTargets({
191         @TestTargetNew(
192             level = TestLevel.COMPLETE,
193             notes = "",
194             method = "checkPermission",
195             args = {java.security.Principal.class, java.security.acl.Permission.class}
196         ),
197         @TestTargetNew(
198             level = TestLevel.COMPLETE,
199             notes = "",
200             method = "getPermissions",
201             args = {java.security.Principal.class}
202         )
203     })
204     public void test_Acl05() {
205         Principal pr = new PrincipalImpl("TestPrincipal");
206         String str = "TestName";
207         MyAcl acl = new MyAcl(pr, str);
208         AclEntry ae = new AclEntryImpl(pr);
209         Permission perm = new PermissionImpl("Permission_1");
210         try {
211             ae.addPermission(perm);
212             acl.addEntry(pr, ae);
213             
214             //checkPermission verification
215             assertTrue("Incorrect permission", acl.checkPermission(pr, perm));
216             assertFalse(acl.checkPermission(pr, new PermissionImpl("Permission_2")));
217             
218             //getPermissions
219             Enumeration en = acl.getPermissions(pr);
220             Vector v = new Vector();
221             while (en.hasMoreElements()) {
222                 v.addElement(en.nextElement());
223             }
224             assertEquals(v.size(), 1);
225             assertEquals(v.elementAt(0).toString(), perm.toString());
226         } catch (Exception ex) {
227             fail("Exception " + ex + " was thrown");
228         }
229     }
230 }