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 Aleksei Y. Semenov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.tests.java.security;
24
25import dalvik.annotation.TestTargetClass;
26import dalvik.annotation.TestTargets;
27import dalvik.annotation.TestLevel;
28import dalvik.annotation.TestTargetNew;
29
30import java.security.IdentityScope;
31import java.security.Permission;
32import java.security.Permissions;
33import java.security.Security;
34import java.security.SecurityPermission;
35
36import org.apache.harmony.security.tests.support.IdentityScopeStub;
37
38import junit.framework.TestCase;
39@TestTargetClass(IdentityScope.class)
40/**
41 * Tests for <code>IdentityScope</code>
42 *
43 */
44@SuppressWarnings("deprecation")
45public class IdentityScopeTest extends TestCase {
46
47    public static class MySecurityManager extends SecurityManager {
48        public Permissions denied = new Permissions();
49        public void checkPermission(Permission permission){
50            if (denied!=null && denied.implies(permission)) throw new SecurityException();
51        }
52    }
53
54    public static void main(String[] args) {
55        junit.textui.TestRunner.run(IdentityScopeTest.class);
56    }
57
58    IdentityScope is;
59
60    /**
61     * Class under test for String toString()
62     */
63    @TestTargetNew(
64        level = TestLevel.COMPLETE,
65        notes = "",
66        method = "toString",
67        args = {}
68    )
69    public final void testToString() {
70        assertNotNull(new IdentityScopeStub("Aleksei Semenov").toString());
71    }
72
73    /**
74     * test default constructor void IdentityScope()
75     */
76    @TestTargetNew(
77        level = TestLevel.COMPLETE,
78        notes = "",
79        method = "IdentityScope",
80        args = {}
81    )
82    public final void testIdentityScope() {
83        assertNotNull(new IdentityScopeStub());
84    }
85
86    /**
87     * check that void IdentityScope(String) creates instance with given name
88     */
89    @TestTargetNew(
90        level = TestLevel.PARTIAL,
91        notes = "Verifies just positive case with non null parameter",
92        method = "IdentityScope",
93        args = {java.lang.String.class}
94    )
95    public final void testIdentityScopeString() {
96        is = new IdentityScopeStub("Aleksei Semenov");
97        assertNotNull(is);
98        assertEquals("Aleksei Semenov", is.getName());
99    }
100
101    /**
102     * check that void IdentityScope(String, IdentityScope) creates instance with given name and within given scope
103     */
104    @TestTargetNew(
105        level = TestLevel.PARTIAL,
106        notes = "Verifies just positive test with both non null parameters",
107        method = "IdentityScope",
108        args = {java.lang.String.class, java.security.IdentityScope.class}
109    )
110    public final void testIdentityScopeStringIdentityScope() throws Exception {
111        IdentityScope scope = new IdentityScopeStub("my scope");
112        is = new IdentityScopeStub("Aleksei Semenov", scope);
113        assertNotNull(is);
114        assertEquals("Aleksei Semenov", is.getName());
115        assertEquals(scope.getName(), is.getScope().getName());
116    }
117
118    /**
119     * just call IdentityScope.getSystemScope()
120     */
121    @TestTargetNew(
122        level = TestLevel.COMPLETE,
123        notes = "",
124        method = "getSystemScope",
125        args = {}
126    )
127    public final void testGetSystemScope() {
128        String name = Security.getProperty("system.scope");
129        assertNotNull(name);
130        IdentityScope scope = IdentityScope.getSystemScope();
131        assertNotNull(scope);
132        assertEquals(name, scope.getClass().getName());
133    }
134
135    /**
136     * check that if permission given - set/get works
137     * if permission is denied than SecurityException is thrown
138     *
139     */
140    @TestTargets({
141        @TestTargetNew(
142            level = TestLevel.COMPLETE,
143            notes = "",
144            method = "setSystemScope",
145            args = {java.security.IdentityScope.class}
146        ),
147        @TestTargetNew(
148            level = TestLevel.COMPLETE,
149            notes = "",
150            method = "getSystemScope",
151            args = {}
152        )
153    })
154    public final void testSetSystemScope() {
155//      default implementation is specified by security property system.scope
156        IdentityScope systemScope = IdentityScope.getSystemScope();
157
158        try {
159            // all permissions are granted - sm is not installed
160            is = new IdentityScopeStub("Aleksei Semenov");
161            IdentityScopeStub.mySetSystemScope(is);
162            assertSame(is, IdentityScope.getSystemScope());
163            // all permissions are granted - sm is installed
164            MySecurityManager sm = new MySecurityManager();
165            System.setSecurityManager(sm);
166            try {
167                is = new IdentityScopeStub("aaa");
168                IdentityScopeStub.mySetSystemScope(is);
169                assertSame(is, IdentityScope.getSystemScope());
170                // permission is denied
171                sm.denied.add(new SecurityPermission("setSystemScope"));
172                IdentityScope is2 = new IdentityScopeStub("bbb");
173                try{
174                    IdentityScopeStub.mySetSystemScope(is2);
175                    fail("SecurityException should be thrown");
176                } catch (SecurityException e){
177                    assertSame(is, IdentityScope.getSystemScope());
178                }
179            } finally {
180                System.setSecurityManager(null);
181                assertNull("Error, security manager is not removed!", System.getSecurityManager());
182            }
183        } finally {
184            IdentityScopeStub.mySetSystemScope(systemScope);
185        }
186    }
187}
188