Path: blob/master/test/jdk/java/security/AccessController/PreserveCombiner.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 481919426* @summary doPrivileged should preserve DomainCombiner27*/2829import java.security.*;30import javax.security.auth.Subject;31import javax.security.auth.x500.X500Principal;3233public class PreserveCombiner {3435public static void main(String[] args) throws Exception {3637Subject s = new Subject();38s.getPrincipals().add(new X500Principal("cn=duke"));3940String result = (String)Subject.doAs(s, new PrivilegedAction() {41public Object run() {4243// get subject from current ACC - this always worked44Subject doAsSubject =45Subject.getSubject(AccessController.getContext());46if (doAsSubject == null) {47return "test 1 failed";48} else {49System.out.println(doAsSubject);50System.out.println("test 1 passed");51}5253// try doPriv (PrivilegedAction) test54String result = AccessController.doPrivilegedWithCombiner55(new PrivilegedAction<String>() {56public String run() {57// get subject after doPriv58Subject doPrivSubject =59Subject.getSubject(AccessController.getContext());60if (doPrivSubject == null) {61return "test 2 failed";62} else {63System.out.println(doPrivSubject);64return "test 2 passed";65}66}67});6869if ("test 2 failed".equals(result)) {70return result;71} else {72System.out.println(result);73}7475// try doPriv (PrivilegedExceptionAction) test76try {77result = AccessController.doPrivilegedWithCombiner78(new PrivilegedExceptionAction<String>() {79public String run() throws PrivilegedActionException {80// get subject after doPriv81Subject doPrivSubject = Subject.getSubject82(AccessController.getContext());83if (doPrivSubject == null) {84return "test 3 failed";85} else {86System.out.println(doPrivSubject);87return "test 3 passed";88}89}90});91} catch (PrivilegedActionException pae) {92result = "test 3 failed";93}9495if ("test 3 failed".equals(result)) {96return result;97} else {98System.out.println(result);99}100101// tests passed102return result;103}104});105106if (result.indexOf("passed") <= 0) {107throw new SecurityException("overall test failed");108}109}110}111112113