Path: blob/master/test/jdk/java/security/ProtectionDomain/AllPerm.java
41152 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 625673426* @summary ProtectionDomain could optimize implies by first checking for27* AllPermission in internal collection28* @run main/othervm -Djava.security.manager=allow AllPerm29*/3031import java.io.*;32import java.net.*;33import java.security.*;34import java.lang.reflect.*;3536public class AllPerm {3738private static final Class[] ARGS = new Class[] { };39private static ProtectionDomain allPermClassDomain;4041public static void main(String[]args) throws Exception {4243// create custom class loader that assigns AllPermission to44// classes it loads4546File file = new File(System.getProperty("test.src"), "AllPerm.jar");47URL[] urls = new URL[] { file.toURL() };48ClassLoader parent = Thread.currentThread().getContextClassLoader();49AllPermLoader loader = new AllPermLoader(urls, parent);5051// load a class from AllPerm.jar using custom loader5253Object o = loader.loadClass("AllPermClass").newInstance();54Method doCheck = o.getClass().getMethod("doCheck", ARGS);55allPermClassDomain = o.getClass().getProtectionDomain();5657// set a custom Policy and set the SecurityManager5859Policy.setPolicy(new AllPermPolicy());60System.setSecurityManager(new SecurityManager());6162// invoke method on loaded class, which will perform a63// security-sensitive operation. custom policy will check64// to see if it is called (it should not be called)6566doCheck.invoke(o, ARGS);67}6869/**70* this class loader assigns AllPermission to classes that it loads71*/72private static class AllPermLoader extends URLClassLoader {7374public AllPermLoader(URL[] urls, ClassLoader parent) {75super(urls, parent);76}7778protected PermissionCollection getPermissions(CodeSource codesource) {79Permissions perms = new Permissions();80perms.add(new AllPermission());81return perms;82}83}8485/**86* this policy should not be called if domain is allPermClassDomain87*/88private static class AllPermPolicy extends Policy {89public boolean implies(ProtectionDomain domain, Permission permission) {90if (domain == allPermClassDomain) {91throw new SecurityException92("Unexpected call into AllPermPolicy");93}94return true;95}96}97}9899/**100* here is the source code for AllPermClass inside AllPerm.jar101*/102/*103public class AllPermClass {104public void doCheck() {105System.getProperty("user.name");106}107}108*/109110111