IAclEntryTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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
18package tests.security.acl;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import junit.framework.TestCase;
26
27import java.security.acl.AclEntry;
28import java.security.acl.Permission;
29import java.security.Principal;
30import java.util.Enumeration;
31import java.util.Vector;
32
33import org.apache.harmony.security.tests.support.acl.*;
34
35@TestTargetClass(AclEntry.class)
36public class IAclEntryTest extends TestCase {
37
38    class MyAclEntry extends AclEntryImpl {
39        public MyAclEntry() {
40            super();
41        }
42        public MyAclEntry(Principal pr) {
43            super(pr);
44        }
45    }
46
47
48    /**
49     * @tests java.security.acl.AclEntry#addPermission(Permission permission)
50     * @tests java.security.acl.AclEntry#checkPermission(Permission permission)
51     * @tests java.security.acl.AclEntry#removePermission(Permission permission)
52     */
53    @TestTargets({
54        @TestTargetNew(
55            level = TestLevel.COMPLETE,
56            notes = "",
57            method = "addPermission",
58            args = {java.security.acl.Permission.class}
59        ),
60        @TestTargetNew(
61            level = TestLevel.COMPLETE,
62            notes = "",
63            method = "checkPermission",
64            args = {java.security.acl.Permission.class}
65        ),
66        @TestTargetNew(
67            level = TestLevel.COMPLETE,
68            notes = "",
69            method = "removePermission",
70            args = {java.security.acl.Permission.class}
71        )
72    })
73    public void test_AclEntry01() {
74        Permission perm = new PermissionImpl("Permission_1");
75        MyAclEntry ae = new MyAclEntry(new PrincipalImpl("TestPrincipal"));
76        try {
77            assertTrue(ae.addPermission(perm));
78            assertFalse(ae.addPermission(perm));
79            assertTrue(ae.checkPermission(perm));
80            assertTrue(ae.removePermission(perm));
81            assertFalse(ae.removePermission(perm));
82            assertFalse(ae.checkPermission(perm));
83        } catch (Exception ex) {
84            fail("Unexpected exception " + ex);
85        }
86    }
87
88    /**
89     * @tests java.security.acl.AclEntry#getPrincipal()
90     * @tests java.security.acl.AclEntry#setPrincipal(Principal user)
91     */
92    @TestTargets({
93        @TestTargetNew(
94            level = TestLevel.COMPLETE,
95            notes = "",
96            method = "getPrincipal",
97            args = {}
98        ),
99        @TestTargetNew(
100            level = TestLevel.COMPLETE,
101            notes = "",
102            method = "setPrincipal",
103            args = {java.security.Principal.class}
104        )
105    })
106    public void test_AclEntry02() {
107        MyAclEntry ae = new MyAclEntry();
108        Principal mp = new PrincipalImpl("TestPrincipal");
109        try {
110            assertTrue(ae.setPrincipal(mp));
111            Principal p = ae.getPrincipal();
112            assertEquals("Names are not equal", p.getName(), mp.getName());
113            assertFalse(ae.setPrincipal(mp));
114        } catch (Exception ex) {
115            fail("Unexpected exception " + ex);
116        }
117    }
118
119    /**
120     * @tests java.security.acl.AclEntry#setNegativePermissions()
121     * @tests java.security.acl.AclEntry#isNegative()
122     */
123    @TestTargets({
124        @TestTargetNew(
125            level = TestLevel.COMPLETE,
126            notes = "",
127            method = "setNegativePermissions",
128            args = {}
129        ),
130        @TestTargetNew(
131            level = TestLevel.COMPLETE,
132            notes = "",
133            method = "isNegative",
134            args = {}
135        )
136    })
137    public void test_AclEntry03() {
138        MyAclEntry ae = new MyAclEntry(new PrincipalImpl("TestPrincipal"));
139        try {
140            assertFalse("isNegative() returns TRUE",ae.isNegative());
141            ae.setNegativePermissions();
142            assertTrue("isNegative() returns FALSE", ae.isNegative());
143        } catch (Exception ex) {
144            fail("Unexpected exception " + ex);
145        }
146    }
147
148    /**
149     * @tests java.security.acl.AclEntry#permissions()
150     */
151    @TestTargetNew(
152        level = TestLevel.COMPLETE,
153        notes = "",
154        method = "permissions",
155        args = {}
156    )
157    public void test_AclEntry04() {
158        MyAclEntry ae = new MyAclEntry(new PrincipalImpl("TestPrincipal"));
159        Permission perm = new PermissionImpl("Permission_1");
160        try {
161            Enumeration en = ae.permissions();
162            assertFalse("Not empty enumeration", en.hasMoreElements());
163            ae.addPermission(perm);
164            en = ae.permissions();
165            assertTrue("Eempty enumeration", en.hasMoreElements());
166            Vector v = new Vector();
167            while (en.hasMoreElements()) {
168                v.addElement(en.nextElement());
169            }
170            assertEquals(v.size(), 1);
171            assertEquals(v.elementAt(0).toString(), perm.toString());
172        } catch (Exception ex) {
173            fail("Unexpected exception " + ex);
174        }
175    }
176
177    /**
178     * @tests java.security.acl.AclEntry#toString()
179     */
180    @TestTargetNew(
181        level = TestLevel.COMPLETE,
182        notes = "",
183        method = "toString",
184        args = {}
185    )
186    public void test_AclEntry05() {
187        MyAclEntry ae = new MyAclEntry(new PrincipalImpl("TestPrincipal"));
188        try {
189            String res = ae.toString();
190            assertTrue(res.contains("TestPrincipal"));
191        } catch (Exception ex) {
192            fail("Unexpected exception " + ex);
193        }
194    }
195
196    /**
197     * @tests java.security.acl.AclEntry#clone()
198     */
199    @TestTargetNew(
200        level = TestLevel.COMPLETE,
201        notes = "",
202        method = "clone",
203        args = {}
204    )
205    public void test_AclEntry06() {
206        MyAclEntry ae = new MyAclEntry(new PrincipalImpl("TestPrincipal"));
207        try {
208            assertEquals("Objects are not equal", ae.toString(), ae.clone().toString());
209        } catch (Exception ex) {
210            fail("Unexpected exception " + ex);
211        }
212    }
213}