Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/ProtectionDomain/AllPerm.java
38812 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*/2930import java.io.*;31import java.net.*;32import java.security.*;33import java.lang.reflect.*;3435public class AllPerm {3637private static final Class[] ARGS = new Class[] { };38private static ProtectionDomain allPermClassDomain;3940public static void main(String[]args) throws Exception {4142// create custom class loader that assigns AllPermission to43// classes it loads4445File file = new File(System.getProperty("test.src"), "AllPerm.jar");46URL[] urls = new URL[] { file.toURL() };47ClassLoader parent = Thread.currentThread().getContextClassLoader();48AllPermLoader loader = new AllPermLoader(urls, parent);4950// load a class from AllPerm.jar using custom loader5152Object o = loader.loadClass("AllPermClass").newInstance();53Method doCheck = o.getClass().getMethod("doCheck", ARGS);54allPermClassDomain = o.getClass().getProtectionDomain();5556// set a custom Policy and set the SecurityManager5758Policy.setPolicy(new AllPermPolicy());59System.setSecurityManager(new SecurityManager());6061// invoke method on loaded class, which will perform a62// security-sensitive operation. custom policy will check63// to see if it is called (it should not be called)6465doCheck.invoke(o, ARGS);66}6768/**69* this class loader assigns AllPermission to classes that it loads70*/71private static class AllPermLoader extends URLClassLoader {7273public AllPermLoader(URL[] urls, ClassLoader parent) {74super(urls, parent);75}7677protected PermissionCollection getPermissions(CodeSource codesource) {78Permissions perms = new Permissions();79perms.add(new AllPermission());80return perms;81}82}8384/**85* this policy should not be called if domain is allPermClassDomain86*/87private static class AllPermPolicy extends Policy {88public boolean implies(ProtectionDomain domain, Permission permission) {89if (domain == allPermClassDomain) {90throw new SecurityException91("Unexpected call into AllPermPolicy");92}93return true;94}95}96}9798/**99* here is the source code for AllPermClass inside AllPerm.jar100*/101/*102public class AllPermClass {103public void doCheck() {104System.getProperty("user.name");105}106}107*/108109110