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 java.security.IdentityScope; 26import java.security.Permission; 27import java.security.Permissions; 28import java.security.Security; 29import java.security.SecurityPermission; 30 31import org.apache.harmony.security.tests.support.IdentityScopeStub; 32 33import junit.framework.TestCase; 34 35/** 36 * Tests for <code>IdentityScope</code> 37 * 38 */ 39@SuppressWarnings("deprecation") 40public class IdentityScopeTest extends TestCase { 41 42 public static class MySecurityManager extends SecurityManager { 43 public Permissions denied = new Permissions(); 44 public void checkPermission(Permission permission){ 45 if (denied!=null && denied.implies(permission)) throw new SecurityException(); 46 } 47 } 48 49 IdentityScope is; 50 51 /** 52 * Class under test for String toString() 53 */ 54 public final void testToString() { 55 assertNotNull(new IdentityScopeStub("Aleksei Semenov").toString()); 56 } 57 58 /** 59 * test default constructor void IdentityScope() 60 */ 61 public final void testIdentityScope() { 62 assertNotNull(new IdentityScopeStub()); 63 } 64 65 /** 66 * check that void IdentityScope(String) creates instance with given name 67 */ 68 public final void testIdentityScopeString() { 69 is = new IdentityScopeStub("Aleksei Semenov"); 70 assertNotNull(is); 71 assertEquals("Aleksei Semenov", is.getName()); 72 } 73 74 /** 75 * check that void IdentityScope(String, IdentityScope) creates instance with given name and within given scope 76 */ 77 public final void testIdentityScopeStringIdentityScope() throws Exception { 78 IdentityScope scope = new IdentityScopeStub("my scope"); 79 is = new IdentityScopeStub("Aleksei Semenov", scope); 80 assertNotNull(is); 81 assertEquals("Aleksei Semenov", is.getName()); 82 assertEquals(scope.getName(), is.getScope().getName()); 83 } 84 85 /** 86 * just call IdentityScope.getSystemScope() 87 */ 88 public final void testGetSystemScope() { 89 String name = Security.getProperty("system.scope"); 90 assertNotNull(name); 91 IdentityScope scope = IdentityScope.getSystemScope(); 92 assertNotNull(scope); 93 assertEquals(name, scope.getClass().getName()); 94 } 95 96 /** 97 * check that if permission given - set/get works 98 * if permission is denied than SecurityException is thrown 99 * 100 */ 101 public final void testSetSystemScope() { 102// default implementation is specified by security property system.scope 103 IdentityScope systemScope = IdentityScope.getSystemScope(); 104 105 try { 106 // all permissions are granted - sm is not installed 107 is = new IdentityScopeStub("Aleksei Semenov"); 108 IdentityScopeStub.mySetSystemScope(is); 109 assertSame(is, IdentityScope.getSystemScope()); 110 } finally { 111 IdentityScopeStub.mySetSystemScope(systemScope); 112 } 113 } 114} 115