blob: 86e2a7084d093ddf7bf09b1655a5b67243bc7454 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4819194
27 * @summary doPrivileged should preserve DomainCombiner
28 */
29
30import java.security.*;
31import javax.security.auth.Subject;
32import javax.security.auth.x500.X500Principal;
33
34public class PreserveCombiner {
35
36 public static void main(String[] args) throws Exception {
37
38 Subject s = new Subject();
39 s.getPrincipals().add(new X500Principal("cn=duke"));
40
41 String result = (String)Subject.doAs(s, new PrivilegedAction() {
42 public Object run() {
43
44 // get subject from current ACC - this always worked
45 Subject doAsSubject =
46 Subject.getSubject(AccessController.getContext());
47 if (doAsSubject == null) {
48 return "test 1 failed";
49 } else {
50 System.out.println(doAsSubject);
51 System.out.println("test 1 passed");
52 }
53
54 // try doPriv (PrivilegedAction) test
55 String result = AccessController.doPrivilegedWithCombiner
56 (new PrivilegedAction<String>() {
57 public String run() {
58 // get subject after doPriv
59 Subject doPrivSubject =
60 Subject.getSubject(AccessController.getContext());
61 if (doPrivSubject == null) {
62 return "test 2 failed";
63 } else {
64 System.out.println(doPrivSubject);
65 return "test 2 passed";
66 }
67 }
68 });
69
70 if ("test 2 failed".equals(result)) {
71 return result;
72 } else {
73 System.out.println(result);
74 }
75
76 // try doPriv (PrivilegedExceptionAction) test
77 try {
78 result = AccessController.doPrivilegedWithCombiner
79 (new PrivilegedExceptionAction<String>() {
80 public String run() throws PrivilegedActionException {
81 // get subject after doPriv
82 Subject doPrivSubject = Subject.getSubject
83 (AccessController.getContext());
84 if (doPrivSubject == null) {
85 return "test 3 failed";
86 } else {
87 System.out.println(doPrivSubject);
88 return "test 3 passed";
89 }
90 }
91 });
92 } catch (PrivilegedActionException pae) {
93 result = "test 3 failed";
94 }
95
96 if ("test 3 failed".equals(result)) {
97 return result;
98 } else {
99 System.out.println(result);
100 }
101
102 // tests passed
103 return result;
104 }
105 });
106
107 if (result.indexOf("passed") <= 0) {
108 throw new SecurityException("overall test failed");
109 }
110 }
111}