001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Aleksei Y. Semenov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.*;
024: import org.apache.harmony.security.tests.support.IdentityScopeStub;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Tests for <code>IdentityScope</code>
030: *
031: */
032:
033: public class IdentityScopeTest extends TestCase {
034:
035: public static class MySecurityManager extends SecurityManager {
036: public Permissions denied = new Permissions();
037:
038: public void checkPermission(Permission permission) {
039: if (denied != null && denied.implies(permission))
040: throw new SecurityException();
041: }
042: }
043:
044: public static void main(String[] args) {
045: junit.textui.TestRunner.run(IdentityScopeTest.class);
046: }
047:
048: IdentityScope is;
049:
050: /**
051: * Constructor for IdentityScopeTest.
052: * @param arg0
053: */
054: public IdentityScopeTest(String arg0) {
055: super (arg0);
056: }
057:
058: /**
059: * Class under test for String toString()
060: */
061: public final void testToString() {
062: assertNotNull(new IdentityScopeStub("Aleksei Semenov")
063: .toString());
064: }
065:
066: /**
067: * test default constructor void IdentityScope()
068: */
069: public final void testIdentityScope() {
070: assertNotNull(new IdentityScopeStub());
071: }
072:
073: /**
074: * check that void IdentityScope(String) creates instance with given name
075: */
076: public final void testIdentityScopeString() {
077: is = new IdentityScopeStub("Aleksei Semenov");
078: assertNotNull(is);
079: assertEquals("Aleksei Semenov", is.getName());
080: }
081:
082: /**
083: * check that void IdentityScope(String, IdentityScope) creates instance with given name and within given scope
084: */
085: public final void testIdentityScopeStringIdentityScope()
086: throws Exception {
087: IdentityScope scope = new IdentityScopeStub("my scope");
088: is = new IdentityScopeStub("Aleksei Semenov", scope);
089: assertNotNull(is);
090: assertEquals("Aleksei Semenov", is.getName());
091: assertEquals(scope.getName(), is.getScope().getName());
092: }
093:
094: /**
095: * just call IdentityScope.getSystemScope()
096: */
097: public final void testGetSystemScope() {
098: String name = Security.getProperty("system.scope");
099: assertNotNull(name);
100: IdentityScope scope = IdentityScope.getSystemScope();
101: assertNotNull(scope);
102: assertEquals(name, scope.getClass().getName());
103: }
104:
105: /**
106: * check that if permission given - set/get works
107: * if permission is denied than SecurityException is thrown
108: *
109: */
110:
111: public final void testSetSystemScope() {
112: // default implementation is specified by security property system.scope
113: IdentityScope systemScope = IdentityScope.getSystemScope();
114:
115: try {
116: // all permissions are granted - sm is not installed
117: is = new IdentityScopeStub("Aleksei Semenov");
118: IdentityScopeStub.mySetSystemScope(is);
119: assertSame(is, IdentityScope.getSystemScope());
120: // all permissions are granted - sm is installed
121: MySecurityManager sm = new MySecurityManager();
122: System.setSecurityManager(sm);
123: try {
124: is = new IdentityScopeStub("aaa");
125: IdentityScopeStub.mySetSystemScope(is);
126: assertSame(is, IdentityScope.getSystemScope());
127: // permission is denied
128: sm.denied.add(new SecurityPermission("setSystemScope"));
129: IdentityScope is2 = new IdentityScopeStub("bbb");
130: try {
131: IdentityScopeStub.mySetSystemScope(is2);
132: fail("SecurityException should be thrown");
133: } catch (SecurityException e) {
134: assertSame(is, IdentityScope.getSystemScope());
135: }
136: } finally {
137: System.setSecurityManager(null);
138: assertNull("Error, security manager is not removed!",
139: System.getSecurityManager());
140: }
141: } finally {
142: IdentityScopeStub.mySetSystemScope(systemScope);
143: }
144: }
145:
146: /**
147: * Class under test for Identity getIdentity(Principal)
148: */
149: public final void testGetIdentityPrincipal() {
150: is = new IdentityScopeStub("Aleksei Semenov");
151: IdentityScope sc2 = new IdentityScopeStub("aaa");
152: assertSame(is, is.getIdentity(sc2));
153: }
154:
155: }
|