Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/Subject/doAs/NestedActions.java
38860 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights2* 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324import jdk.testlibrary.ProcessTools;2526import javax.security.auth.Subject;27import javax.security.auth.x500.X500Principal;28import java.io.*;29import java.security.*;30import java.util.ArrayList;31import java.util.Arrays;32import java.util.Collections;33import java.util.List;34import java.util.jar.JarEntry;35import java.util.jar.JarOutputStream;36import java.util.jar.Manifest;37import java.nio.file.Paths;3839/**40* @test41* @bug 804814742* @summary Check if proper AccessControlException is thrown43* in case of nested Subject.doAs() invocations44* when one of protection domains doesn't have permissions45*46* @library /lib/testlibrary47*48* @run main NestedActions jar NestedActionsACE.jar49* NestedActionsACE.class Utils.class50* @run main NestedActions jar NestedActionsPAE.jar51* NestedActionsPAE.class Utils.class52* @run main NestedActions jar NestedActionsOnePrincipal.jar53* NestedActionsOnePrincipal.class Utils.class54* @run main NestedActions jar NestedActionsTwoPrincipals.jar55* NestedActionsTwoPrincipals.class Utils.class56* @run main NestedActions jar WriteToFileAction.jar57* WriteToFileAction.class58* @run main NestedActions jar WriteToFileNegativeAction.jar59* WriteToFileNegativeAction.class60* @run main NestedActions jar WriteToFileExceptionAction.jar61* WriteToFileExceptionAction.class62* @run main NestedActions jar ReadFromFileAction.jar63* ReadFromFileAction.class64* @run main NestedActions jar ReadFromFileNegativeAction.jar65* ReadFromFileNegativeAction.class66* @run main NestedActions jar ReadFromFileExceptionAction.jar67* ReadFromFileExceptionAction.class68* @run main NestedActions jar ReadPropertyAction.jar69* ReadPropertyAction.class70* @run main NestedActions jar ReadPropertyNegativeAction.jar71* ReadPropertyNegativeAction.class72* @run main NestedActions jar ReadPropertyExceptionAction.jar73* ReadPropertyExceptionAction.class ReadPropertyException.class74*75* @run main NestedActions NestedActionsACE policy.expect.ace76* NestedActionsACE.jar WriteToFileNegativeAction.jar77* ReadFromFileNegativeAction.jar ReadPropertyNegativeAction.jar78* @run main NestedActions NestedActionsPAE policy.expect.pae79* NestedActionsPAE.jar WriteToFileExceptionAction.jar80* ReadFromFileExceptionAction.jar ReadPropertyExceptionAction.jar81* @run main NestedActions NestedActionsOnePrincipal policy.one.principal82* NestedActionsOnePrincipal.jar WriteToFileAction.jar83* ReadFromFileAction.jar ReadPropertyAction.jar84* @run main NestedActions NestedActionsTwoPrincipals policy.two.principals85* NestedActionsTwoPrincipals.jar WriteToFileAction.jar86* ReadFromFileAction.jar ReadPropertyAction.jar87*/88public class NestedActions {8990static final String file = "NestedActions.tmp";91static final String PS = System.getProperty("path.separator");92static final String FS = System.getProperty("file.separator");93static final String TEST_CLASSES = System.getProperty("test.classes");94static final String TEST_SOURCES = System.getProperty("test.src");95static final String JAVA_OPTS = System.getProperty("test.java.opts");96static final String JAVA = System.getProperty("java.home")97+ FS + "bin" + FS + "java";9899public static void main(String[] args) throws IOException {100if (args.length > 0) {101if ("jar".equals(args[0]) && args.length > 2) {102createJar(args[1],103Arrays.copyOfRange(args, 2, args.length));104} else {105runJava(args);106}107} else {108throw new RuntimeException("Wrong parameters");109}110}111112static void createJar(String dest, String... files) throws IOException {113System.out.println("Create " + dest + " with the following content:");114try (JarOutputStream jos = new JarOutputStream(115new FileOutputStream(dest), new Manifest())) {116for (String file : files) {117System.out.println(" " + file);118jos.putNextEntry(new JarEntry(file));119try (FileInputStream fis = new FileInputStream(120TEST_CLASSES + FS + file)) {121jdk.testlibrary.Utils.transferTo(fis, jos);122}123}124}125}126127128129static void runJava(String[] args) {130if (args == null || args.length < 3) {131throw new IllegalArgumentException("wrong parameters");132}133134List<String> cmds = new ArrayList<>();135cmds.add(JAVA);136StringBuilder sb = new StringBuilder();137cmds.add("-classpath");138for (int i=2; i<args.length; i++) {139sb.append(args[i]).append(PS);140}141cmds.add(sb.toString());142if (JAVA_OPTS != null && !JAVA_OPTS.isEmpty()) {143Collections.addAll(cmds, JAVA_OPTS.trim().split("\\s+"));144}145cmds.add("-Djava.security.manager");146cmds.add("-Djava.security.policy=" + TEST_SOURCES + FS + args[1]);147cmds.add(args[0]);148try {149ProcessTools.executeCommand(cmds.toArray(new String[cmds.size()]))150.shouldHaveExitValue(0);151} catch (Throwable e) {152throw new RuntimeException(e);153}154}155}156157/**158* Test for nested Subject.doAs() invocation:159*160* WriteToFileAction (CN=Duke principal) ->161* ReadFromFileAction (CN=Duke principal) ->162* ReadPropertyAction (CN=Duke principal)163*164* The test expects AccessControllException.165*/166class NestedActionsACE {167168public static void main(String args[]) {169Subject subject = new Subject();170subject.getPrincipals().add(new X500Principal("CN=Duke"));171WriteToFileNegativeAction writeToFile172= new WriteToFileNegativeAction(NestedActions.file);173Subject.doAs(subject, writeToFile);174}175}176177/**178* Test for nested Subject.doAs() invocation:179*180* WriteToFileAction (CN=Duke principal) ->181* ReadFromFileAction (CN=Duke principal) ->182* ReadPropertyAction (CN=Duke principal)183*184* The test expects PrivilegedActionException185* that caused by AccessControlEception.186*/187class NestedActionsPAE {188189public static void main(String args[]) {190Subject subject = new Subject();191subject.getPrincipals().add(new X500Principal("CN=Duke"));192try {193WriteToFileExceptionAction writeToFile =194new WriteToFileExceptionAction(NestedActions.file);195Subject.doAs(subject, writeToFile);196throw new RuntimeException(197"Test failed: no PrivilegedActionException thrown");198} catch (PrivilegedActionException pae) {199System.out.println(200"PrivilegedActionException thrown as expected: "201+ pae);202203// check if AccessControlException caused PrivilegedActionException204Throwable exception = pae.getException();205do {206if (!(exception instanceof PrivilegedActionException)) {207break;208}209exception = ((PrivilegedActionException) exception).210getException();211} while (true);212213if (!(exception instanceof ReadPropertyException)) {214throw new RuntimeException(215"Test failed: PrivilegedActionException "216+ "was not caused by ReadPropertyException");217}218219exception = exception.getCause();220if (!(exception instanceof AccessControlException)) {221throw new RuntimeException(222"Test failed: PrivilegedActionException "223+ "was not caused by ReadPropertyException");224}225226System.out.println(227"Test passed: PrivilegedActionException "228+ "was caused by AccessControlException");229}230}231}232233/**234* Test for nested Subject.doAs() invocation:235*236* WriteToFileAction (CN=Duke principal) ->237* ReadFromFileAction (CN=Duke principal) ->238* ReadPropertyAction (CN=Duke principal)239*/240class NestedActionsOnePrincipal {241242public static void main(String args[]) {243Subject subject = new Subject();244subject.getPrincipals().add(new X500Principal("CN=Duke"));245WriteToFileAction writeToFile =246new WriteToFileAction(NestedActions.file);247Subject.doAs(subject, writeToFile);248}249}250251/**252* Test for nested Subject.doAs() invocation:253*254* WriteToFileAction (CN=Duke principal) ->255* ReadFromFileAction (CN=Duke principal) ->256* ReadPropertyAction (CN=Java principal)257*/258class NestedActionsTwoPrincipals {259260public static void main(String args[]) {261Subject subject = new Subject();262subject.getPrincipals().add(new X500Principal("CN=Duke"));263Subject anotherSubject = new Subject();264anotherSubject.getPrincipals().add(new X500Principal("CN=Java"));265ReadFromFileAction readFromFile266= new ReadFromFileAction(NestedActions.file, anotherSubject);267WriteToFileAction writeToFile268= new WriteToFileAction(NestedActions.file, readFromFile);269Subject.doAs(subject, writeToFile);270}271}272273/**274* Helper class.275*/276class Utils {277278static void readFile(String filename) {279System.out.println("ReadFromFileAction: try to read " + filename);280AccessControlContext acc = AccessController.getContext();281Subject subject = Subject.getSubject(acc);282System.out.println("principals = " + subject.getPrincipals());283try (FileInputStream fis = new FileInputStream(filename)) {284// do nothing285} catch (IOException e) {286throw new RuntimeException("Unexpected IOException", e);287}288}289290static void writeFile(String filename) {291System.out.println("WriteToFileAction: try to write to " + filename);292AccessControlContext acc = AccessController.getContext();293Subject subject = Subject.getSubject(acc);294System.out.println("principals = " + subject.getPrincipals());295try (BufferedOutputStream bos = new BufferedOutputStream(296new FileOutputStream(filename))) {297bos.write(0);298bos.flush();299} catch (IOException e) {300throw new RuntimeException("Unexpected IOException", e);301}302}303304}305306class WriteToFileAction implements PrivilegedAction {307308private final String filename;309private final PrivilegedAction nextAction;310311WriteToFileAction(String filename, PrivilegedAction nextAction) {312this.filename = filename;313this.nextAction = nextAction;314}315316WriteToFileAction(String filename) {317this(filename, new ReadFromFileAction(filename));318}319320@Override321public Object run() {322Utils.writeFile(filename);323AccessControlContext acc = AccessController.getContext();324Subject subject = Subject.getSubject(acc);325return Subject.doAs(subject, nextAction);326}327328}329330class ReadFromFileAction implements PrivilegedAction {331332private final String filename;333private final Subject anotherSubject;334335ReadFromFileAction(String filename) {336this(filename, null);337}338339ReadFromFileAction(String filename, Subject anotherSubject) {340this.filename = filename;341this.anotherSubject = anotherSubject;342}343344@Override345public Object run() {346Utils.readFile(filename);347348AccessControlContext acc = AccessController.getContext();349Subject subject = Subject.getSubject(acc);350ReadPropertyAction readProperty = new ReadPropertyAction();351if (anotherSubject != null) {352return Subject.doAs(anotherSubject, readProperty);353} else {354return Subject.doAs(subject, readProperty);355}356}357358}359360class ReadPropertyAction implements PrivilegedAction {361362@Override363public java.lang.Object run() {364System.out.println("ReadPropertyAction: "365+ "try to read 'java.class.path' property");366367AccessControlContext acc = AccessController.getContext();368Subject s = Subject.getSubject(acc);369System.out.println("principals = " + s.getPrincipals());370System.out.println("java.class.path = "371+ System.getProperty("java.class.path"));372373return null;374}375376}377378class WriteToFileNegativeAction implements PrivilegedAction {379380private final String filename;381382public WriteToFileNegativeAction(String filename) {383this.filename = filename;384}385386@Override387public Object run() {388AccessControlContext acc = AccessController.getContext();389Subject subject = Subject.getSubject(acc);390System.out.println("principals = " + subject.getPrincipals());391392try {393Utils.writeFile(filename);394new File(filename).delete();395throw new RuntimeException(396"Test failed: no AccessControlException thrown");397} catch (AccessControlException ace) {398System.out.println(399"AccessControlException thrown as expected: "400+ ace.getMessage());401}402403ReadFromFileNegativeAction readFromFile404= new ReadFromFileNegativeAction(filename);405return Subject.doAs(subject, readFromFile);406}407408}409410class ReadFromFileNegativeAction implements PrivilegedAction {411412private final String filename;413414public ReadFromFileNegativeAction(String filename) {415this.filename = filename;416}417418@Override419public Object run() {420AccessControlContext acc = AccessController.getContext();421Subject subject = Subject.getSubject(acc);422System.out.println("principals = " + subject.getPrincipals());423424try {425Utils.readFile(filename);426throw new RuntimeException(427"Test failed: no AccessControlException thrown");428} catch (AccessControlException ace) {429System.out.println(430"AccessControlException thrown as expected: "431+ ace.getMessage());432}433434ReadPropertyNegativeAction readProperty =435new ReadPropertyNegativeAction();436return Subject.doAs(subject, readProperty);437}438439}440441class ReadPropertyNegativeAction implements PrivilegedAction {442443@Override444public java.lang.Object run() {445System.out.println("Try to read 'java.class.path' property");446447AccessControlContext acc = AccessController.getContext();448Subject s = Subject.getSubject(acc);449System.out.println("principals = " + s.getPrincipals());450451try {452System.out.println("java.class.path = "453+ System.getProperty("java.class.path"));454throw new RuntimeException(455"Test failed: no AccessControlException thrown");456} catch (AccessControlException ace) {457System.out.println(458"AccessControlException thrown as expected: "459+ ace.getMessage());460}461462return null;463}464465}466467class WriteToFileExceptionAction implements PrivilegedExceptionAction {468469private final String filename;470471WriteToFileExceptionAction(String filename) {472this.filename = filename;473}474475@Override476public Object run() throws Exception {477Utils.writeFile(filename);478AccessControlContext acc = AccessController.getContext();479Subject subject = Subject.getSubject(acc);480ReadFromFileExceptionAction readFromFile =481new ReadFromFileExceptionAction(filename);482return Subject.doAs(subject, readFromFile);483}484485}486487class ReadFromFileExceptionAction implements PrivilegedExceptionAction {488489private final String filename;490491ReadFromFileExceptionAction(String filename) {492this.filename = filename;493}494495@Override496public Object run() throws Exception {497Utils.readFile(filename);498AccessControlContext acc = AccessController.getContext();499Subject subject = Subject.getSubject(acc);500ReadPropertyExceptionAction readProperty =501new ReadPropertyExceptionAction();502return Subject.doAs(subject, readProperty);503}504505}506507class ReadPropertyExceptionAction implements PrivilegedExceptionAction {508509@Override510public java.lang.Object run() throws Exception {511System.out.println("Try to read 'java.class.path' property");512513AccessControlContext acc = AccessController.getContext();514Subject s = Subject.getSubject(acc);515System.out.println("principals = " + s.getPrincipals());516517try {518System.out.println("java.class.path = "519+ System.getProperty("java.class.path"));520throw new RuntimeException(521"Test failed: no AccessControlException thrown");522} catch (AccessControlException ace) {523System.out.println(524"AccessControlException thrown as expected: "525+ ace.getMessage());526throw new ReadPropertyException(ace);527}528}529530}531532class ReadPropertyException extends Exception {533534ReadPropertyException(Throwable cause) {535super(cause);536}537}538539540