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 Alexey V. Varlamov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.tests.java.security;
24
25import java.security.Permission;
26import java.security.SecurityPermission;
27
28import junit.framework.TestCase;
29import dalvik.annotation.KnownFailure;
30import dalvik.annotation.TestLevel;
31import dalvik.annotation.TestTargetClass;
32import dalvik.annotation.TestTargetNew;
33@TestTargetClass(Permission.class)
34/**
35 * Tests for <code>Permission</code>
36 */
37
38public class PermissionTest extends TestCase {
39
40    public static void main(String[] args) {
41        junit.textui.TestRunner.run(PermissionTest.class);
42    }
43
44    // Bare extension to instantiate abstract Permission class
45    static final class RealPermission extends Permission {
46
47        public RealPermission(String name) {
48            super(name);
49        }
50
51        public boolean equals(Object obj) {
52            return false;
53        }
54
55        public String getActions() {
56            return null;
57        }
58
59        public int hashCode() {
60            return 0;
61        }
62
63        public boolean implies(Permission permission) {
64            return false;
65        }
66    }
67
68    /**
69     * Test that a permission object is created with the specified name and is
70     * properly converted to String
71     */
72    @TestTargetNew(
73        level = TestLevel.PARTIAL,
74        notes = "Non null string parameter verified",
75        method = "Permission",
76        args = {java.lang.String.class}
77    )
78    public void testCtor() {
79        String name = "testCtor123^%$#&^ &^$";
80        Permission test = new RealPermission(name);
81        assertEquals(name, test.getName());
82        assertEquals("(" + test.getClass().getName() + " " + name + ")", test
83            .toString());
84    }
85
86    /**
87     * Test checkGuard() realization: if SecurityManager is installed, it's
88     * checkPermission() should be called with this permission, otherwise
89     * nothing happens
90     */
91    @TestTargetNew(
92        level = TestLevel.PARTIAL_COMPLETE,
93        notes = "",
94        method = "checkGuard",
95        args = {java.lang.Object.class}
96    )
97    public void testCheckGuard() {
98        final Permission test = new RealPermission("234234");
99        SecurityManager old = System.getSecurityManager();
100        try {
101            System.setSecurityManager(null);
102            test.checkGuard(this);
103            final boolean[] callFlag = new boolean[] { false };
104            System.setSecurityManager(new SecurityManager() {
105
106                public void checkPermission(Permission p) {
107                    if (p == test) {
108                        callFlag[0] = true;
109                    }
110                }
111            });
112            test.checkGuard(null);
113            assertTrue(callFlag[0]);
114        } finally {
115            System.setSecurityManager(old);
116        }
117
118        class TestSecurityManager extends SecurityManager {
119            boolean called = false;
120            private final String permissionName;
121
122            public TestSecurityManager(String permissionName) {
123                this.permissionName = permissionName;
124            }
125
126            @Override
127            public void checkPermission(Permission permission) {
128                if (permission instanceof SecurityPermission
129                        && permissionName.equals(permission.getName())) {
130                    called = true;
131                    super.checkPermission(permission);
132                }
133            }
134        }
135
136        TestSecurityManager sm = new TestSecurityManager("testGuardPermission");
137        try {
138            System.setSecurityManager(sm);
139            Permission p = new SecurityPermission("testGuardPermission");
140            p.checkGuard(this);
141            assertTrue("SecurityManager must be invoked", sm.called);
142        } finally {
143            System.setSecurityManager(old);
144        }
145
146    }
147
148    /** newPermissionCollection() should return null */
149    @TestTargetNew(
150        level = TestLevel.PARTIAL,
151        notes = "Returned parameter was tested.",
152        method = "newPermissionCollection",
153        args = {}
154    )
155    public void testCollection() {
156        assertNull(new RealPermission("123").newPermissionCollection());
157    }
158}
159