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.
18 package tests.security.acl;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
25 import junit.framework.TestCase;
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;
35 import org.apache.harmony.security.tests.support.acl.*;
37 @TestTargetClass(Acl.class)
38 public class IAclTest extends TestCase {
40 class MyAcl extends AclImpl {
41 public MyAcl(Principal principal, String str) {
42 super(principal, str);
48 * @tests java.security.acl.Acl#addEntry(Principal caller, AclEntry entry)
49 * @tests java.security.acl.Acl#removeEntry(Principal caller, AclEntry entry)
53 level = TestLevel.COMPLETE,
56 args = {java.security.Principal.class, java.security.acl.AclEntry.class}
59 level = TestLevel.COMPLETE,
61 method = "removeEntry",
62 args = {java.security.Principal.class, java.security.acl.AclEntry.class}
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);
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);
80 acl.addEntry(new PrincipalImpl("NewPrincipal"), ae);
81 fail("NotOwnerException was not thrown");
82 } catch (NotOwnerException noe) {
87 acl.removeEntry(new PrincipalImpl("NewPrincipal"), ae);
88 fail("NotOwnerException was not thrown");
89 } catch (NotOwnerException noe) {
95 * @tests java.security.acl.Acl#setName(Principal caller, String name)
96 * @tests java.security.acl.Acl#getName()
100 level = TestLevel.COMPLETE,
103 args = {java.security.Principal.class, java.lang.String.class}
106 level = TestLevel.COMPLETE,
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);
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);
126 acl.setName(new PrincipalImpl("NewPrincipal"), str);
127 fail("NotOwnerException was not thrown");
128 } catch (NotOwnerException noe) {
134 * @tests java.security.acl.Acl#toString()
137 level = TestLevel.COMPLETE,
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");
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);
159 * @tests java.security.acl.Acl#entries()
162 level = TestLevel.COMPLETE,
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);
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());
180 assertEquals(v.size(), 1);
181 } catch (Exception ex) {
182 fail("Unexpected exception " + ex);
187 * @tests java.security.acl.Acl#checkPermission(Principal principal, Permission permission)
188 * @tests java.security.acl.Acl#getPermissions(Principal principal)
192 level = TestLevel.COMPLETE,
194 method = "checkPermission",
195 args = {java.security.Principal.class, java.security.acl.Permission.class}
198 level = TestLevel.COMPLETE,
200 method = "getPermissions",
201 args = {java.security.Principal.class}
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");
211 ae.addPermission(perm);
212 acl.addEntry(pr, ae);
214 //checkPermission verification
215 assertTrue("Incorrect permission", acl.checkPermission(pr, perm));
216 assertFalse(acl.checkPermission(pr, new PermissionImpl("Permission_2")));
219 Enumeration en = acl.getPermissions(pr);
220 Vector v = new Vector();
221 while (en.hasMoreElements()) {
222 v.addElement(en.nextElement());
224 assertEquals(v.size(), 1);
225 assertEquals(v.elementAt(0).toString(), perm.toString());
226 } catch (Exception ex) {
227 fail("Exception " + ex + " was thrown");