ProtectionDomainTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 Alexander V. Astapchuk 20*/ 21 22package org.apache.harmony.security.tests.java.security; 23import java.security.*; 24import java.net.URL; 25import java.net.MalformedURLException; 26import java.net.URLClassLoader; 27 28import junit.framework.TestCase; 29 30 31/** 32 * Unit tests for java.security.ProtectionDomain. 33 * 34 */ 35 36public class ProtectionDomainTest extends TestCase { 37 38 /** 39 * Entry point for standalone runs. 40 * @param args command line arguments 41 */ 42 public static void main(String[] args) { 43 junit.textui.TestRunner.run(ProtectionDomainTest.class); 44 } 45 46 private final AllPermission allperm = new AllPermission(); 47 48 private URL url = null; 49 50 private CodeSource cs = null; 51 52 private PermissionCollection perms = null; 53 54 private ClassLoader classldr = null; 55 56 private Principal[] principals = null; // changed in setUp() 57 58 /* 59 * @see TestCase#setUp() 60 */ 61 protected void setUp() throws Exception { 62 super.setUp(); 63 try { 64 url = new URL("http://localhost"); 65 } catch (MalformedURLException ex) { 66 throw new Error(ex); 67 } 68 cs = new CodeSource(url, (java.security.cert.Certificate[]) null); 69 perms = allperm.newPermissionCollection(); 70 perms.add(allperm); 71 classldr = URLClassLoader.newInstance(new URL[] { url }); 72 principals = new Principal[] { new TestPrincipal("0"), 73 new TestPrincipal("1"), new TestPrincipal("2"), 74 new TestPrincipal("3"), new TestPrincipal("4"), }; 75 } 76 77 /** 78 * Class under test for void ProtectionDomain(CodeSource, 79 * PermissionCollection) 80 */ 81 public void testProtectionDomainCodeSourcePermissionCollection_00() { 82 new ProtectionDomain(null, null); 83 new ProtectionDomain(cs, null); 84 85 new ProtectionDomain(cs, perms); 86 } 87 88 /** 89 * the ctor must set the PermissionCollection read-only 90 */ 91 public void testProtectionDomainCodeSourcePermissionCollection_01() { 92 assertFalse(perms.isReadOnly()); 93 new ProtectionDomain(null, perms); 94 assertTrue(perms.isReadOnly()); 95 } 96 97 /** 98 * Test for ProtectionDomain(CodeSource, PermissionCollection, ClassLoader, Principal[]) 99 */ 100 public void testProtectionDomainCodeSourcePermissionCollectionClassLoaderPrincipalArray() { 101 new ProtectionDomain(null, null, null, null); 102 103 new ProtectionDomain(cs, null, null, null); 104 new ProtectionDomain(null, perms, null, null); 105 new ProtectionDomain(null, null, classldr, null); 106 new ProtectionDomain(null, null, null, principals); 107 108 new ProtectionDomain(cs, perms, classldr, principals); 109 } 110 111 /** 112 * Tests for ProtectionDomain.getClassLoader() 113 */ 114 public void testGetClassLoader() { 115 assertNull(new ProtectionDomain(null, null).getClassLoader()); 116 assertSame(new ProtectionDomain(null, null, classldr, null) 117 .getClassLoader(), classldr); 118 } 119 120 /** 121 * Tests for ProtectionDomain.getCodeSource() 122 */ 123 public void testGetCodeSource() { 124 assertNull(new ProtectionDomain(null, null).getCodeSource()); 125 assertSame(new ProtectionDomain(cs, null).getCodeSource(), cs); 126 } 127 128 /** 129 * Tests for ProtectionDomain.getPermissions() 130 */ 131 public void testGetPermissions() { 132 assertNull(new ProtectionDomain(null, null).getPermissions()); 133 assertSame(new ProtectionDomain(null, perms).getPermissions(), perms); 134 } 135 136 /** 137 * getPrincipals() always returns non null array 138 */ 139 public void testGetPrincipals_00() { 140 assertNotNull(new ProtectionDomain(null, null).getPrincipals()); 141 } 142 143 /** 144 * getPrincipals() returns new array each time it's called 145 */ 146 public void testGetPrincipals_01() { 147 ProtectionDomain pd = new ProtectionDomain(null, null, null, principals); 148 Principal[] got = pd.getPrincipals(); 149 assertNotNull(got); 150 assertNotSame(got, principals); 151 assertNotSame(got, pd.getPrincipals()); 152 assertTrue(got.length == principals.length); 153 } 154 155 /** 156 * ProtectionDomain with null Permissions must not imply() permissions. 157 */ 158 public void testImplies_00() { 159 assertFalse(new ProtectionDomain(null, null).implies(allperm)); 160 } 161 162 /** 163 * ProtectionDomain with PermissionCollection which contains AllPermission 164 * must imply() AllPermission. 165 */ 166 public void testImplies_01() { 167 assertTrue(new ProtectionDomain(null, perms).implies(allperm)); 168 } 169 170 /** 171 * ProtectionDomain created with a static set of permissions must not query 172 * policy. 173 */ 174 public void testImplies_02() { 175 TestPolicy policy = new TestPolicy(); 176 // null set of permissions [must] force the PD to use Policy - for 177 // dynamic permissions 178 ProtectionDomain pd = new ProtectionDomain(cs, null); 179 policy.setTrackPD(pd); 180 try { 181 Policy.setPolicy(policy); 182 pd.implies(allperm); 183 } finally { 184 Policy.setPolicy(null); 185 } 186 assertFalse(policy.getPdTracked()); 187 } 188 189 /** 190 * ProtectionDomain created with dynamic set of permissions must query 191 * policy. 192 */ 193 public void testImplies_03() { 194 TestPolicy policy = new TestPolicy(); 195 ProtectionDomain pd = new ProtectionDomain(cs, null, ClassLoader 196 .getSystemClassLoader(), principals); 197 policy.setTrackPD(pd); 198 try { 199 Policy.setPolicy(policy); 200 pd.implies(allperm); 201 } finally { 202 Policy.setPolicy(null); 203 } 204 assertTrue(policy.getPdTracked()); 205 } 206 207 /** 208 * Simply checks that it's working somehow 209 */ 210 public void testToString() { 211 new ProtectionDomain(null, null).toString(); 212 new ProtectionDomain(cs, perms).toString(); 213 new ProtectionDomain(null, null, null, null).toString(); 214 new ProtectionDomain(cs, perms, classldr, principals).toString(); 215 } 216 217 /** 218 * Test principal used during the testing. Does nothing. 219 */ 220 221 private static class TestPrincipal implements Principal { 222 private String name; 223 224 TestPrincipal(String name) { 225 this.name = name; 226 } 227 228 public String getName() { 229 return "TestPrincipal: " + name; 230 } 231 } 232 233 private static class TestPolicy extends Policy { 234 ProtectionDomain trackPD = null; 235 236 boolean pdTracked = false; 237 238 ProtectionDomain setTrackPD(ProtectionDomain pd) { 239 ProtectionDomain tmp = trackPD; 240 trackPD = pd; 241 pdTracked = false; 242 return tmp; 243 } 244 245 boolean getPdTracked() { 246 return pdTracked; 247 } 248 249 public PermissionCollection getPermissions(CodeSource cs) { 250 return new Permissions(); 251 } 252 253 // public PermissionCollection getPermissions(ProtectionDomain domain) { 254 // return super.getPermissions(domain); 255 // } 256 public boolean implies(ProtectionDomain domain, Permission permission) { 257 if (trackPD != null && trackPD == domain) { 258 pdTracked = true; 259 } 260 return super.implies(domain, permission); 261 } 262 263 public void refresh() { 264 // do nothing 265 } 266 } 267 268} 269