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
18package tests.api.java.security;
19
20import java.security.AccessControlContext;
21import java.security.AccessController;
22import java.security.AllPermission;
23import java.security.BasicPermission;
24import java.security.CodeSource;
25import java.security.DomainCombiner;
26import java.security.Permission;
27import java.security.PermissionCollection;
28import java.security.Permissions;
29import java.security.PrivilegedAction;
30import java.security.ProtectionDomain;
31import java.security.SecurityPermission;
32import java.security.cert.Certificate;
33
34public class DomainCombinerTest extends junit.framework.TestCase {
35
36    /**
37     * @tests java.security.DomainCombiner#combine(java.security.ProtectionDomain[],
38     *java.security.ProtectionDomain[])
39     */
40    public void test_combine$Ljava_security_ProtectionDomain$Ljava_security_ProtectionDomain() {
41        final boolean[] calledDomainCombiner = new boolean[] { false, false };
42
43        class MyCombiner implements DomainCombiner {
44            int i;
45
46            MyCombiner(int i) {
47                this.i = i;
48            }
49
50            public ProtectionDomain[] combine(
51                    ProtectionDomain[] executionDomains,
52                    ProtectionDomain[] parentDomains) {
53                calledDomainCombiner[i] = true;
54                PermissionCollection pc = new Permissions();
55                pc.add(new AllPermission());
56                ProtectionDomain pd;
57                // if run with the system classloader then there will be no
58                // execution domains
59                if (executionDomains.length > 0) {
60                    pd = new ProtectionDomain(executionDomains[0]
61                            .getCodeSource(), pc);
62                } else {
63                    pd = new ProtectionDomain(parentDomains[0].getCodeSource(),
64                            pc);
65                }
66                return new ProtectionDomain[] { pd };
67            }
68        }
69
70        ProtectionDomain[] domains = new ProtectionDomain[] { new ProtectionDomain(
71                new CodeSource(null, (Certificate[]) null), new Permissions()) };
72
73        AccessControlContext parent = new AccessControlContext(domains);
74        AccessControlContext c0 = new AccessControlContext(parent,
75                new MyCombiner(0));
76        final AccessControlContext c1 = new AccessControlContext(parent,
77                new MyCombiner(1));
78
79        class TestPermission extends BasicPermission {
80            TestPermission(String s) {
81                super(s);
82            }
83        }
84
85    }
86}
87