Path: blob/master/test/jdk/java/lang/SecurityManager/CheckPackageAccess.java
41149 views
/*1* Copyright (c) 2012, 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 6741606 7146431 8000450 8019830 8022945 8027144 8041633 8078427 805520626* @summary Check that various restricted packages that are supposed to be27* restricted by default or are listed in the package.access28* property in the java.security file are blocked29* @run main/othervm CheckPackageAccess30*/3132import java.lang.module.ModuleFinder;33import java.lang.module.ModuleReference;34import java.util.Arrays;35import java.util.List;36import java.util.Optional;3738public class CheckPackageAccess {3940private static final SecurityManager sm = new SecurityManager();41private static final ModuleFinder mf = ModuleFinder.ofSystem();4243/*44* The expected list of restricted packages of the package.access property.45*46* This array should be updated whenever new packages are added to the47* package.access property in the java.security file48* NOTE: it should be in the same order as the java.security file49*/50private static final String[] EXPECTED = {51"sun.misc.",52"sun.reflect.",53};5455/**56* Tests access to various packages of a module.57*/58private static class Test {59String moduleName; // name of module60ModuleReference moduleRef; // module reference61String exports; // exported pkg62Optional<String> opens; // opened pkg63String conceals; // concealed pkg64Optional<String> qualExports; // qualified export pkg65Optional<String> qualOpens; // qualified open pkg66// qual open and non-qualified export pkg67Optional<String> qualOpensAndExports;68Test(String module, String exports, String opens, String conceals,69String qualExports, String qualOpens, String qualOpensAndExports) {70this.moduleName = module;71this.moduleRef = mf.find(moduleName).get();72this.exports = exports;73this.opens = Optional.ofNullable(opens);74this.conceals = conceals;75this.qualExports = Optional.ofNullable(qualExports);76this.qualOpens = Optional.ofNullable(qualOpens);77this.qualOpensAndExports = Optional.ofNullable(qualOpensAndExports);78}7980void test() {81final boolean isModulePresent =82ModuleLayer.boot().findModule(moduleName).isPresent();83System.out.format("Testing module: %1$s. Module is%2$s present.\n",84moduleName, isModulePresent ? "" : " NOT");8586if (isModulePresent) {8788// access to exported pkg should pass89testNonRestricted(exports);9091// access to opened pkg should pass92opens.ifPresent(Test::testNonRestricted);9394// access to concealed pkg should fail95testRestricted(conceals);9697// access to qualified export pkg should fail98qualExports.ifPresent(Test::testRestricted);99100// access to qualified open pkg should fail101qualOpens.ifPresent(Test::testRestricted);102103// access to qualified opened pkg that is also exported should pass104qualOpensAndExports.ifPresent(Test::testNonRestricted);105} else {106System.out.println("Skipping tests for module.");107}108}109110private static void testRestricted(String pkg) {111try {112sm.checkPackageAccess(pkg);113throw new RuntimeException("Able to access restricted package: "114+ pkg);115} catch (SecurityException se) {}116try {117sm.checkPackageDefinition(pkg);118throw new RuntimeException("Able to access restricted package: "119+ pkg);120} catch (SecurityException se) {}121}122123private static void testNonRestricted(String pkg) {124try {125sm.checkPackageAccess(pkg);126} catch (SecurityException se) {127throw new RuntimeException("Unable to access exported package: "128+ pkg, se);129}130try {131sm.checkPackageDefinition(pkg);132} catch (SecurityException se) {133throw new RuntimeException("Unable to access exported package: "134+ pkg, se);135}136}137}138139private static final Test[] tests = new Test[] {140// java.base module loaded by boot loader141new Test("java.base", "java.security", null, "jdk.internal.jrtfs",142"jdk.internal.loader", null, null),143// java.desktop module loaded by boot loader and has an openQual pkg144// that is exported145new Test("java.desktop", "java.applet", null, "sun.font",146"sun.awt", null, "javax.swing.plaf.basic"),147// java.security.jgss module loaded by platform loader148new Test("java.security.jgss", "org.ietf.jgss", null,149"sun.security.krb5.internal.crypto", "sun.security.krb5",150null, null),151};152153public static void main(String[] args) throws Exception {154155// check expected list of restricted packages in java.security file156checkPackages(Arrays.asList(EXPECTED));157158// check access to each module's packages159for (Test test : tests) {160test.test();161}162163System.out.println("Test passed");164}165166private static void checkPackages(List<String> pkgs) {167for (String pkg : pkgs) {168try {169sm.checkPackageAccess(pkg);170throw new RuntimeException("Able to access " + pkg +171" package");172} catch (SecurityException se) { }173try {174sm.checkPackageDefinition(pkg);175throw new RuntimeException("Able to define class in " + pkg +176" package");177} catch (SecurityException se) { }178String subpkg = pkg + "foo";179try {180sm.checkPackageAccess(subpkg);181throw new RuntimeException("Able to access " + subpkg +182" package");183} catch (SecurityException se) { }184try {185sm.checkPackageDefinition(subpkg);186throw new RuntimeException("Able to define class in " +187subpkg + " package");188} catch (SecurityException se) { }189}190}191}192193194