Path: blob/master/test/jdk/java/security/AccessController/LimitedDoPrivileged.java
41149 views
/*1* Copyright (c) 2013, 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 801409726* @summary Test the limited privilege scope version of doPrivileged27*/2829import java.security.*;30import java.util.*;3132public class LimitedDoPrivileged {33/*34* Test variations of doPrivileged() and doPrivileged() with a limited privilege scope35* in a sandbox with the usual default permission to read the system properties for the36* file and path separators.37*38* By passing in an "assigned" AccessControlContext that has39* no default permissions we can test how code privileges are being scoped.40*/4142private static final ProtectionDomain domain =43new ProtectionDomain(null, null, null, null);44private static final AccessControlContext acc =45new AccessControlContext(new ProtectionDomain[] { domain });46private static final PropertyPermission pathPerm =47new PropertyPermission("path.separator", "read");48private static final PropertyPermission filePerm =49new PropertyPermission("file.separator", "read");5051public static void main(String[] args) throws Exception {52/*53* Verify that we have the usual default property read permission.54*/55AccessController.getContext().checkPermission(filePerm);56AccessController.getContext().checkPermission(pathPerm);57System.out.println("test 1 passed");5859/*60* Inject the "no permission" AccessControlContext.61*/62AccessController.doPrivileged(new PrivilegedAction() {63public Object run() {6465/*66* Verify that we no longer have the "file.separator" permission.67*/68try {69AccessController.getContext().checkPermission(pathPerm);70} catch (AccessControlException ace) {71System.out.println("test 2 passed");72}7374/*75* Verify that we can give ourselves limited privilege to read76* any system property starting with "path.".77*/78AccessController.doPrivileged79(new PrivilegedAction() {80public Object run() {81AccessController.getContext().checkPermission(pathPerm);82return null;83}84}, null, new PropertyPermission("path.*", "read"));85System.out.println("test 3 passed");8687/*88* Verify that if we give ourselves limited privilege to read89* any system property starting with "path." it won't give us the90* the ability to read "file.separator".91*/92try {93AccessController.doPrivileged94(new PrivilegedAction() {95public Object run() {96AccessController.getContext().checkPermission(filePerm);97return null;98}99}, null, new PropertyPermission("path.*", "read"));100} catch (AccessControlException ace) {101System.out.println("test 4 passed");102}103104/*105* Verify that capturing and passing in the context with no default106* system property permission grants will prevent access that succeeded107* earlier without the context assignment.108*/109final AccessControlContext context = AccessController.getContext();110try {111AccessController.doPrivileged112(new PrivilegedAction() {113public Object run() {114AccessController.getContext().checkPermission(pathPerm);115return null;116}117}, context, new PropertyPermission("path.*", "read"));118} catch (AccessControlException ace) {119System.out.println("test 5 passed");120}121122/*123* Verify that we can give ourselves full privilege to read124* any system property starting with "path.".125*/126AccessController.doPrivileged127(new PrivilegedAction() {128public Object run() {129AccessController.getContext().checkPermission(pathPerm);130return null;131}132});133System.out.println("test 6 passed");134135/*136* Verify that capturing and passing in the context with no default137* system property permission grants will prevent access that succeeded138* earlier without the context assignment.139*/140try {141AccessController.doPrivileged142(new PrivilegedAction() {143public Object run() {144AccessController.getContext().checkPermission(pathPerm);145return null;146}147}, context);148} catch (AccessControlException ace) {149System.out.println("test 7 passed");150}151152/*153* Verify that we can give ourselves limited privilege to read154* any system property starting with "path." when a limited155* privilege scope context is captured and passed to a regular156* doPrivileged() as an assigned context.157*/158AccessController.doPrivileged159(new PrivilegedAction() {160public Object run() {161162/*163* Capture the limited privilege scope and inject it into the164* regular doPrivileged().165*/166final AccessControlContext limitedContext = AccessController.getContext();167AccessController.doPrivileged168(new PrivilegedAction() {169public Object run() {170AccessController.getContext().checkPermission(pathPerm);171return null;172}173}, limitedContext);174return null;175}176}, null, new PropertyPermission("path.*", "read"));177System.out.println("test 8 passed");178179/*180* Verify that we can give ourselves limited privilege to read181* any system property starting with "path." it won't give us the182* the ability to read "file.separator" when a limited183* privilege scope context is captured and passed to a regular184* doPrivileged() as an assigned context.185*/186AccessController.doPrivileged187(new PrivilegedAction() {188public Object run() {189190/*191* Capture the limited privilege scope and inject it into the192* regular doPrivileged().193*/194final AccessControlContext limitedContext = AccessController.getContext();195try {196AccessController.doPrivileged197(new PrivilegedAction() {198public Object run() {199AccessController.getContext().checkPermission(filePerm);200return null;201}202}, limitedContext);203} catch (AccessControlException ace) {204System.out.println("test 9 passed");205}206return null;207}208}, null, new PropertyPermission("path.*", "read"));209210return null;211}212}, acc);213}214}215216217