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