Path: blob/master/test/jdk/java/rmi/RMISecurityManager/checkPackageAccess/CheckPackageAccess.java
41152 views
/*1* Copyright (c) 1999, 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/* @test24* @bug 418039225* @summary When an instance of java.rmi.RMISecurityManager is set the VM's26* security manager, the same package access restrictions should be in effect27* as when the default java.lang.SecurityManager is set, which with the28* default "java.security" file in the JDK means that access to packages in29* the sun.* package hierarchy is denied (without explicit runtime permission30* "accessClassInPackage.*").31* @author Peter Jones32*33* @run main/othervm -Djava.security.manager=allow CheckPackageAccess34*/3536import java.rmi.RMISecurityManager;3738public class CheckPackageAccess {3940/*41* This test assumes that the default security manager protects untrusted42* access to classes in the sun.* hierarchy, which is what is specified43* in the JDK's default java.security file.44*/45private final static String restrictedClassName = "sun.misc.Cache";4647public static void main(String[] args) {4849System.err.println("\nRegression test for bug 4180392\n");5051System.err.println("Setting RMISecurityManager.");52System.setSecurityManager(new RMISecurityManager());5354try {55System.err.println("Attempting to acquire restricted class " +56restrictedClassName);57Class restrictedClass = Class.forName(restrictedClassName);58throw new RuntimeException(59"TEST FAILED: successfully acquired restricted class " +60restrictedClass);61} catch (ClassNotFoundException e) {62throw new RuntimeException(63"TEST FAILED: couldn't find (but was allowed to look for) " +64"restricted class " + restrictedClassName);65} catch (SecurityException e) {66System.err.println("TEST PASSED: ");67e.printStackTrace();68}69}70}717273