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 Alexander V. Astapchuk
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.*;
024: import java.net.URL;
025: import java.net.MalformedURLException;
026: import java.net.URLClassLoader;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * Unit tests for java.security.ProtectionDomain.
032: *
033: */
034:
035: public class ProtectionDomainTest extends TestCase {
036:
037: /**
038: * Entry point for standalone runs.
039: * @param args command line arguments
040: */
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(ProtectionDomainTest.class);
043: }
044:
045: private final AllPermission allperm = new AllPermission();
046:
047: private URL url = null;
048:
049: private CodeSource cs = null;
050:
051: private PermissionCollection perms = null;
052:
053: private ClassLoader classldr = null;
054:
055: private Principal[] principals = null; // changed in setUp()
056:
057: /*
058: * @see TestCase#setUp()
059: */
060: protected void setUp() throws Exception {
061: super .setUp();
062: try {
063: url = new URL("http://localhost");
064: } catch (MalformedURLException ex) {
065: throw new Error(ex);
066: }
067: cs = new CodeSource(url,
068: (java.security.cert.Certificate[]) null);
069: perms = allperm.newPermissionCollection();
070: perms.add(allperm);
071: classldr = URLClassLoader.newInstance(new URL[] { url });
072: principals = new Principal[] { new TestPrincipal("0"),
073: new TestPrincipal("1"), new TestPrincipal("2"),
074: new TestPrincipal("3"), new TestPrincipal("4"), };
075: }
076:
077: /**
078: * Class under test for void ProtectionDomain(CodeSource,
079: * PermissionCollection)
080: */
081: public void testProtectionDomainCodeSourcePermissionCollection_00() {
082: new ProtectionDomain(null, null);
083: new ProtectionDomain(cs, null);
084:
085: new ProtectionDomain(cs, perms);
086: }
087:
088: /**
089: * the ctor must set the PermissionCollection read-only
090: */
091: public void testProtectionDomainCodeSourcePermissionCollection_01() {
092: assertFalse(perms.isReadOnly());
093: new ProtectionDomain(null, perms);
094: assertTrue(perms.isReadOnly());
095: }
096:
097: /**
098: * Test for ProtectionDomain(CodeSource, PermissionCollection, ClassLoader, Principal[])
099: */
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(),
134: perms);
135: }
136:
137: /**
138: * getPrincipals() always returns non null array
139: */
140: public void testGetPrincipals_00() {
141: assertNotNull(new ProtectionDomain(null, null).getPrincipals());
142: }
143:
144: /**
145: * getPrincipals() returns new array each time it's called
146: */
147: public void testGetPrincipals_01() {
148: ProtectionDomain pd = new ProtectionDomain(null, null, null,
149: principals);
150: Principal[] got = pd.getPrincipals();
151: assertNotNull(got);
152: assertNotSame(got, principals);
153: assertNotSame(got, pd.getPrincipals());
154: assertTrue(got.length == principals.length);
155: }
156:
157: /**
158: * ProtectionDomain with null Permissions must not imply() permissions.
159: */
160: public void testImplies_00() {
161: assertFalse(new ProtectionDomain(null, null).implies(allperm));
162: }
163:
164: /**
165: * ProtectionDomain with PermissionCollection which contains AllPermission
166: * must imply() AllPermission.
167: */
168: public void testImplies_01() {
169: assertTrue(new ProtectionDomain(null, perms).implies(allperm));
170: }
171:
172: /**
173: * ProtectionDomain created with a static set of permissions must not query
174: * policy.
175: */
176: public void testImplies_02() {
177: TestPolicy policy = new TestPolicy();
178: // null set of permissions [must] force the PD to use Policy - for
179: // dynamic permissions
180: ProtectionDomain pd = new ProtectionDomain(cs, null);
181: policy.setTrackPD(pd);
182: try {
183: Policy.setPolicy(policy);
184: pd.implies(allperm);
185: } finally {
186: Policy.setPolicy(null);
187: }
188: assertFalse(policy.getPdTracked());
189: }
190:
191: /**
192: * ProtectionDomain created with dynamic set of permissions must query
193: * policy.
194: */
195: public void testImplies_03() {
196: TestPolicy policy = new TestPolicy();
197: ProtectionDomain pd = new ProtectionDomain(cs, null,
198: ClassLoader.getSystemClassLoader(), principals);
199: policy.setTrackPD(pd);
200: try {
201: Policy.setPolicy(policy);
202: pd.implies(allperm);
203: } finally {
204: Policy.setPolicy(null);
205: }
206: assertTrue(policy.getPdTracked());
207: }
208:
209: /**
210: * Simply checks that it's working somehow
211: */
212: public void testToString() {
213: new ProtectionDomain(null, null).toString();
214: new ProtectionDomain(cs, perms).toString();
215: new ProtectionDomain(null, null, null, null).toString();
216: new ProtectionDomain(cs, perms, classldr, principals)
217: .toString();
218: }
219:
220: /**
221: * Test principal used during the testing. Does nothing.
222: */
223:
224: private static class TestPrincipal implements Principal {
225: private String name;
226:
227: TestPrincipal(String name) {
228: this .name = name;
229: }
230:
231: public String getName() {
232: return "TestPrincipal: " + name;
233: }
234: }
235:
236: private static class TestPolicy extends Policy {
237: ProtectionDomain trackPD = null;
238:
239: boolean pdTracked = false;
240:
241: ProtectionDomain setTrackPD(ProtectionDomain pd) {
242: ProtectionDomain tmp = trackPD;
243: trackPD = pd;
244: pdTracked = false;
245: return tmp;
246: }
247:
248: boolean getPdTracked() {
249: return pdTracked;
250: }
251:
252: public PermissionCollection getPermissions(CodeSource cs) {
253: return new Permissions();
254: }
255:
256: // public PermissionCollection getPermissions(ProtectionDomain domain) {
257: // return super.getPermissions(domain);
258: // }
259: public boolean implies(ProtectionDomain domain,
260: Permission permission) {
261: if (trackPD != null && trackPD == domain) {
262: pdTracked = true;
263: }
264: return super .implies(domain, permission);
265: }
266:
267: public void refresh() {
268: // do nothing
269: }
270: }
271:
272: }
|