Path: blob/master/test/jdk/java/security/AccessController/LimitedDoPrivilegedWithThread.java
41149 views
/*1* Copyright (c) 2013, 2014, 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 805028126* @summary Test limited doprivileged action with trhead calls.27* @run main/othervm/policy=policy LimitedDoPrivilegedWithThread28*/29import java.io.FilePermission;30import java.security.AccessControlContext;31import java.security.AccessControlException;32import java.security.AccessController;33import java.security.Permission;34import java.security.PrivilegedAction;35import java.security.ProtectionDomain;36import java.util.PropertyPermission;3738public class LimitedDoPrivilegedWithThread {3940private static final Permission PROPERTYPERM41= new PropertyPermission("user.name", "read");42private static final Permission FILEPERM43= new FilePermission("*", "read");44private static final AccessControlContext ACC45= new AccessControlContext(46new ProtectionDomain[]{new ProtectionDomain(null, null)});4748public static void main(String args[]) {49//parent thread without any permission50AccessController.doPrivileged(51(PrivilegedAction) () -> {52Thread ct = new Thread(53new ChildThread(PROPERTYPERM, FILEPERM));54ct.start();55try {56ct.join();57} catch (InterruptedException ie) {58Thread.currentThread().interrupt();59ie.printStackTrace();60throw new RuntimeException("Unexpected InterruptedException");61}62return null;63}, ACC);64}65}6667class ChildThread implements Runnable {6869private final Permission P1;70private final Permission P2;71private boolean catchACE = false;7273public ChildThread(Permission p1, Permission p2) {74this.P1 = p1;75this.P2 = p2;76}7778@Override79public void run() {80//Verified that child thread has permission p1,81runTest(null, P1, false, 1);82//Verified that child thread inherits parent thread's access control context83AccessControlContext childAcc = AccessController.getContext();84runTest(childAcc, P1, true, 2);85//Verified that we can give permision p2 to limit the "privilege" of the86//class calling doprivileged action, stack walk will continue87runTest(null, P2, true, 3);8889}9091public void runTest(AccessControlContext acc, Permission perm,92boolean expectACE, int id) {9394AccessController.doPrivileged(95(PrivilegedAction) () -> {96try {97AccessController.getContext().checkPermission(P1);98} catch (AccessControlException ace) {99catchACE = true;100}101if (catchACE ^ expectACE) {102throw new RuntimeException("test" + id + " failed");103}104return null;105}, acc, perm);106}107}108109110