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 Alexey V. Varlamov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.AllPermission;
024: import java.security.CodeSource;
025: import java.security.PermissionCollection;
026: import java.security.Policy;
027: import java.security.ProtectionDomain;
028: import java.security.Security;
029: import java.security.SecurityPermission;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * Tests for <code>Policy</code>
035: *
036: */
037:
038: public class Policy_ImplTest extends TestCase {
039:
040: public static class TestProvider extends Policy {
041:
042: PermissionCollection pc;
043:
044: public PermissionCollection getPermissions(CodeSource cs) {
045: return pc;
046: }
047:
048: public void refresh() {
049: }
050: }
051:
052: /**
053: * Tests loading of a default provider, both valid and invalid class
054: * references.
055: */
056: public void testGetPolicy_LoadDefaultProvider() {
057: Policy oldPolicy = Policy.getPolicy();
058: String POLICY_PROVIDER = "policy.provider";
059: String oldProvider = Security.getProperty(POLICY_PROVIDER);
060: try {
061: Security.setProperty(POLICY_PROVIDER, TestProvider.class
062: .getName());
063: Policy.setPolicy(null);
064: Policy p = Policy.getPolicy();
065: assertNotNull(p);
066: assertEquals(TestProvider.class.getName(), p.getClass()
067: .getName());
068:
069: Security.setProperty(POLICY_PROVIDER, "a.b.c.D");
070: Policy.setPolicy(null);
071: p = Policy.getPolicy();
072: assertNotNull(p);
073: //exact type of default provider does not matter
074: //assertEquals(DefaultPolicy.class.getName(), p.getClass().getName());
075: } finally {
076: Security.setProperty(POLICY_PROVIDER,
077: (oldProvider == null) ? "" : oldProvider);
078: Policy.setPolicy(oldPolicy);
079: }
080: }
081:
082: /**
083: * Tests that implies() does proper permission evaluation.
084: */
085: public void testImplies() {
086: TestProvider policy = new TestProvider();
087: SecurityPermission sp = new SecurityPermission("abc");
088: policy.pc = sp.newPermissionCollection();
089:
090: policy.pc.add(sp);
091: assertTrue(policy.implies(new ProtectionDomain(null, null), sp));
092: assertFalse(policy.implies(null, sp));
093: assertFalse(policy.implies(new ProtectionDomain(null, null),
094: null));
095: assertFalse(policy.implies(null, null));
096:
097: ProtectionDomain pd = new ProtectionDomain(null, policy.pc);
098: policy.pc = null;
099: assertTrue(policy.implies(pd, sp));
100: assertFalse(policy.implies(pd, new AllPermission()));
101: }
102:
103: }
|