Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/SecurityManager/CheckPackageAccess.java
47182 views
/*1* Copyright (c) 2012, 2017, 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 817942326* 818608027* @summary Make sure all restricted packages listed in the package.access28* property in the java.security file are blocked29* @run main/othervm CheckPackageAccess30*/3132import java.security.Security;33import java.util.Collections;34import java.util.Arrays;35import java.util.ArrayList;36import java.util.List;37import java.util.StringTokenizer;3839/*40* The main benefit of this test is to catch merge errors or other types41* of issues where one or more of the packages are accidentally42* removed. This is why the packages that are known to be restricted have to43* be explicitly listed below.44*/45public class CheckPackageAccess {4647/*48* This array should be updated whenever new packages are added to the49* package.access property in the java.security file50* NOTE: it should be in the same order as the java.security file51*/52private static final String[] packages = {53"sun.",54"com.sun.xml.internal.",55"com.sun.imageio.",56"com.sun.istack.internal.",57"com.sun.jmx.",58"com.sun.media.sound.",59"com.sun.naming.internal.",60"com.sun.proxy.",61"com.sun.corba.se.",62"com.sun.org.apache.bcel.internal.",63"com.sun.org.apache.regexp.internal.",64"com.sun.org.apache.xerces.internal.",65"com.sun.org.apache.xpath.internal.",66"com.sun.org.apache.xalan.internal.extensions.",67"com.sun.org.apache.xalan.internal.lib.",68"com.sun.org.apache.xalan.internal.res.",69"com.sun.org.apache.xalan.internal.templates.",70"com.sun.org.apache.xalan.internal.utils.",71"com.sun.org.apache.xalan.internal.xslt.",72"com.sun.org.apache.xalan.internal.xsltc.cmdline.",73"com.sun.org.apache.xalan.internal.xsltc.compiler.",74"com.sun.org.apache.xalan.internal.xsltc.trax.",75"com.sun.org.apache.xalan.internal.xsltc.util.",76"com.sun.org.apache.xml.internal.res.",77"com.sun.org.apache.xml.internal.resolver.helpers.",78"com.sun.org.apache.xml.internal.resolver.readers.",79"com.sun.org.apache.xml.internal.security.",80"com.sun.org.apache.xml.internal.serializer.utils.",81"com.sun.org.apache.xml.internal.utils.",82"com.sun.org.glassfish.",83"com.oracle.xmlns.internal.",84"com.oracle.webservices.internal.",85"oracle.jrockit.jfr.",86"org.jcp.xml.dsig.internal.",87"jdk.internal.",88"jdk.jfr.events.",89"jdk.jfr.internal.",90"jdk.management.jfr.internal.",91"jdk.nashorn.internal.",92"jdk.nashorn.tools.",93"jdk.xml.internal.",94"com.sun.activation.registries."95};9697public static void main(String[] args) throws Exception {98List<String> pkgs = new ArrayList<>(Arrays.asList(packages));99String osName = System.getProperty("os.name");100if (osName.contains("OS X")) {101pkgs.add("apple."); // add apple package for OS X102} else if (osName.startsWith("Windows")) {103pkgs.add("com.sun.java.accessibility.");104}105106List<String> jspkgs =107getPackages(Security.getProperty("package.access"));108109if (!isOpenJDKOnly()) {110String lastPkg = pkgs.get(pkgs.size() - 1);111112// Remove any closed packages from list before comparing113int index = jspkgs.indexOf(lastPkg);114if (index != -1 && index != jspkgs.size() - 1) {115jspkgs.subList(index + 1, jspkgs.size()).clear();116}117}118119// Sort to ensure lists are comparable120Collections.sort(pkgs);121Collections.sort(jspkgs);122123if (!pkgs.equals(jspkgs)) {124for (String p : pkgs)125if (!jspkgs.contains(p))126System.out.println("In golden set, but not in j.s file: " + p);127for (String p : jspkgs)128if (!pkgs.contains(p))129System.out.println("In j.s file, but not in golden set: " + p);130131132throw new RuntimeException("restricted packages are not " +133"consistent with java.security file");134}135System.setSecurityManager(new SecurityManager());136SecurityManager sm = System.getSecurityManager();137for (String pkg : packages) {138String subpkg = pkg + "foo";139try {140sm.checkPackageAccess(pkg);141throw new RuntimeException("Able to access " + pkg +142" package");143} catch (SecurityException se) { }144try {145sm.checkPackageAccess(subpkg);146throw new RuntimeException("Able to access " + subpkg +147" package");148} catch (SecurityException se) { }149try {150sm.checkPackageDefinition(pkg);151throw new RuntimeException("Able to define class in " + pkg +152" package");153} catch (SecurityException se) { }154try {155sm.checkPackageDefinition(subpkg);156throw new RuntimeException("Able to define class in " + subpkg +157" package");158} catch (SecurityException se) { }159}160System.out.println("Test passed");161}162163private static List<String> getPackages(String p) {164List<String> packages = new ArrayList<>();165if (p != null && !p.equals("")) {166StringTokenizer tok = new StringTokenizer(p, ",");167while (tok.hasMoreElements()) {168String s = tok.nextToken().trim();169packages.add(s);170}171}172return packages;173}174175private static boolean isOpenJDKOnly() {176String prop = System.getProperty("java.runtime.name");177return prop != null && prop.startsWith("OpenJDK");178}179}180181182