Regression.java revision 16935:81c76df23278
1/*
2 * Copyright (c) 2000, 2017, 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
24/*
25 * @test
26 * @bug 4390546
27 * @modules jdk.security.auth
28 * @summary     performance regression and other bugs in
29 *              SubjectDomainCombiner.combine
30 *
31 * @run main/othervm/policy=Regression.policy -Djava.security.auth.debug=combiner Regression
32 */
33
34import java.security.ProtectionDomain;
35import java.security.CodeSource;
36import java.net.URL;
37import java.util.Set;
38import java.util.HashSet;
39import javax.security.auth.Subject;
40import javax.security.auth.SubjectDomainCombiner;
41
42public class Regression {
43
44    public static void main(String[] args) {
45
46        Set principals = new HashSet();
47        principals.add(new com.sun.security.auth.NTUserPrincipal("test1"));
48        principals.add(new com.sun.security.auth.NTUserPrincipal("test2"));
49
50        Subject subject = new Subject
51                (false, principals, new HashSet(), new HashSet());
52
53        SubjectDomainCombiner sdc = new SubjectDomainCombiner(subject);
54
55        URL url1;
56        URL url2;
57        URL url3;
58        URL url4;
59        try {
60            url1 = new URL("http://one");
61            url2 = new URL("http://two");
62            url3 = new URL("http://three");
63            url4 = new URL("http://four");
64        } catch (java.net.MalformedURLException mue) {
65            mue.printStackTrace();
66            throw new SecurityException("Test failed: " + mue.toString());
67        }
68
69        ProtectionDomain d1 = new ProtectionDomain
70                                (new CodeSource(url1,
71                                    (java.security.cert.Certificate[]) null),
72                                null,                   // permissions
73                                null,                   // class loader
74                                null);                  // principals
75        ProtectionDomain d2 = new ProtectionDomain
76                                (new CodeSource(url2,
77                                    (java.security.cert.Certificate[]) null),
78                                null,                   // permissions
79                                null,                   // class loader
80                                null);                  // principals
81        ProtectionDomain d3 = new ProtectionDomain
82                                (new CodeSource(url3,
83                                    (java.security.cert.Certificate[]) null),
84                                null,                   // permissions
85                                null,                   // class loader
86                                null);                  // principals
87        ProtectionDomain d4 = new ProtectionDomain
88                                (new CodeSource(url4,
89                                    (java.security.cert.Certificate[]) null),
90                                null,                   // permissions
91                                null,                   // class loader
92                                null);                  // principals
93
94        // test 1
95        // -- regular combine, make sure we get a proper combination back
96
97        ProtectionDomain currentDomains[] = { d1, d2, d3 };
98        ProtectionDomain assignedDomains[] = { d4 };
99        ProtectionDomain domains1[] = sdc.combine
100                        (currentDomains, assignedDomains);
101
102        if (domains1.length != 4 ||
103            domains1[0] == d1 || domains1[1] == d2 || domains1[2] == d3 ||
104            domains1[3] != d4 ||
105            !domains1[0].implies(new RuntimePermission("queuePrintJob"))) {
106            throw new SecurityException("Test failed: combine test 1 failed");
107        }
108
109        System.out.println("-------- TEST ONE PASSED --------");
110
111        // test 2
112        // -- repeat combine, make sure combiner cachine returned the
113        //    same PD's back
114
115        ProtectionDomain domains2[] = sdc.combine
116                        (currentDomains, assignedDomains);
117        if (domains2.length != 4 ||
118            domains2[0] != domains1[0] || domains2[1] != domains1[1] ||
119            domains2[2] != domains1[2] ||
120            domains2[3] != domains1[3] ||
121            !domains2[0].implies(new RuntimePermission("queuePrintJob"))) {
122            throw new SecurityException("Test failed: combine test 2 failed");
123        }
124
125        System.out.println("-------- TEST TWO PASSED --------");
126
127        // test 3
128        // -- mutate the Subject and make sure the combiner cache
129        //    got cleared out
130
131        subject.getPrincipals().remove
132                (new com.sun.security.auth.NTUserPrincipal("test2"));
133        ProtectionDomain domains3[] = sdc.combine
134                        (currentDomains, assignedDomains);
135        if (domains3.length != 4 ||
136            domains3[0] == domains1[0] || domains3[1] == domains1[1] ||
137            domains3[2] == domains1[2] ||
138            domains3[3] != domains1[3] ||
139            !domains3[0].implies(new RuntimePermission("createClassLoader")) ||
140            domains3[0].implies(new RuntimePermission("queuePrintJob"))) {
141            throw new SecurityException("Test failed: combine test 3 failed");
142        }
143
144        System.out.println("-------- TEST THREE PASSED --------");
145
146        System.out.println("Test Passed");
147    }
148}
149