Path: blob/master/test/jdk/java/lang/SecurityManager/PackageAccessTest.java
41149 views
/*1* Copyright (c) 1999, 2018, 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 4256530 413624526* @summary Check that restricted packages that are supposed to be restricted27* and explicit grants accessClassInPackage permission overridden in28* privileged block29* @modules java.base/sun.security.x50930* @run main/othervm PackageAccessTest31* @run main/othervm/java.security.policy=test.policy PackageAccessTest access32* @run main/othervm/java.security.policy=empty.policy PackageAccessTest deny33*/3435import java.security.AccessControlException;36import java.security.AccessController;37import java.security.PrivilegedExceptionAction;3839public class PackageAccessTest {4041public static void main(String[] args) {42boolean access = true;4344if (args != null && args.length > 0) {45switch (args[0]) {46case "access":47access = true;48break;49case "deny":50access = false;51break;52default:53throw new RuntimeException(54"Invalid input parameter " + args[0]);55}56}5758testPkgAccess(access);59testPkgAccessWithPrivileged(access);60}6162private static void testPkgAccess(boolean access) {63try {64sun.security.x509.X509CertInfo x = new sun.security.x509.X509CertInfo();65if (!access) {66throw new RuntimeException(67"application unexpectedly able to access the internal package");68}69} catch (SecurityException se) {70if (access) {71throw new RuntimeException("Unexpected security exception", se);72}73}74}7576private static void testPkgAccessWithPrivileged(boolean access) {77sun.security.x509.X509CertInfo o = null;78try {79o = (sun.security.x509.X509CertInfo) AccessController.doPrivileged(80(PrivilegedExceptionAction) () -> new sun.security.x509.X509CertInfo());81if (!access) {82throw new RuntimeException(83"application unexpectedly able to access the internal package");84}85} catch (AccessControlException ace) {86if (access) {87throw new RuntimeException("Unexpected AccessControlException", ace);88}89} catch (Exception ex) {90throw new RuntimeException("Test failed with unexpected exception", ex);91}92if (access && o == null)93throw new RuntimeException(94"Test failed: unable to instantiate object");95}9697}9899100