1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.security.AccessControlContext;
25import java.security.AccessController;
26import java.security.DomainCombiner;
27import java.security.PrivilegedAction;
28import java.security.ProtectionDomain;
29import jdk.internal.misc.SharedSecrets;
30
31/*
32 * @test
33 * @bug 8064331
34 * @summary Make sure that JavaSecurityAccess.doIntersectionPrivilege()
35 *          is not dropping the information about the domain combiner of
36 *          the stack ACC
37 * @modules java.base/jdk.internal.misc
38 */
39
40public class PreserveCombinerTest {
41    public static void main(String[]args) throws Exception {
42        final DomainCombiner dc = new DomainCombiner() {
43            @Override
44            public ProtectionDomain[] combine(ProtectionDomain[] currentDomains, ProtectionDomain[] assignedDomains) {
45                return currentDomains; // basically a no-op
46            }
47        };
48
49        // Get an instance of the saved ACC
50        AccessControlContext saved = AccessController.getContext();
51        // Simulate the stack ACC with a DomainCombiner attached
52        AccessControlContext stack = new AccessControlContext(AccessController.getContext(), dc);
53
54        // Now try to run JavaSecurityAccess.doIntersectionPrivilege() and assert
55        // whether the DomainCombiner from the stack ACC is preserved
56        boolean ret = SharedSecrets.getJavaSecurityAccess().doIntersectionPrivilege(new PrivilegedAction<Boolean>() {
57            @Override
58            public Boolean run() {
59                return dc == AccessController.getContext().getDomainCombiner();
60            }
61        }, stack, saved);
62
63        if (!ret) {
64            System.exit(1);
65        }
66    }
67}
68
69