blob: c6b9a1e59be7295b10c5bdcae9c3a388a32ff9ce [file] [log] [blame]
joehw08a68c42012-04-17 11:21:35 -07001/*
coffeys55ddfb12013-06-06 14:10:44 +01002 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
joehw08a68c42012-04-17 11:21:35 -07003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
coffeys52be22a2013-07-09 16:00:41 +010025 * @test
mullan40a8bb12013-08-27 12:04:32 -040026 * @bug 6741606 7146431 8000450 8019830
coffeys52be22a2013-07-09 16:00:41 +010027 * @summary Make sure all restricted packages listed in the package.access
28 * property in the java.security file are blocked
29 * @run main/othervm CheckPackageAccess
joehw08a68c42012-04-17 11:21:35 -070030 */
31
coffeys52be22a2013-07-09 16:00:41 +010032import java.security.Security;
33import java.util.Collections;
34import java.util.Arrays;
35import java.util.ArrayList;
36import java.util.List;
37import java.util.StringTokenizer;
38
39/*
40 * The main benefit of this test is to catch merge errors or other types
41 * of issues where one or more of the packages are accidentally
42 * removed. This is why the packages that are known to be restricted have to
43 * be explicitly listed below.
44 */
joehw08a68c42012-04-17 11:21:35 -070045public class CheckPackageAccess {
46
coffeys52be22a2013-07-09 16:00:41 +010047 /*
48 * This array should be updated whenever new packages are added to the
49 * package.access property in the java.security file
50 */
51 private static final String[] packages = {
52 "sun.",
coffeys52be22a2013-07-09 16:00:41 +010053 "com.sun.xml.internal.",
54 "com.sun.imageio.",
55 "com.sun.istack.internal.",
56 "com.sun.jmx.",
mullan40a8bb12013-08-27 12:04:32 -040057 "com.sun.media.sound.",
coffeys52be22a2013-07-09 16:00:41 +010058 "com.sun.proxy.",
59 "com.sun.org.apache.bcel.internal.",
60 "com.sun.org.apache.regexp.internal.",
61 "com.sun.org.apache.xerces.internal.",
62 "com.sun.org.apache.xpath.internal.",
63 "com.sun.org.apache.xalan.internal.extensions.",
64 "com.sun.org.apache.xalan.internal.lib.",
65 "com.sun.org.apache.xalan.internal.res.",
66 "com.sun.org.apache.xalan.internal.templates.",
67 "com.sun.org.apache.xalan.internal.utils.",
68 "com.sun.org.apache.xalan.internal.xslt.",
69 "com.sun.org.apache.xalan.internal.xsltc.cmdline.",
70 "com.sun.org.apache.xalan.internal.xsltc.compiler.",
71 "com.sun.org.apache.xalan.internal.xsltc.trax.",
72 "com.sun.org.apache.xalan.internal.xsltc.util.",
73 "com.sun.org.apache.xml.internal.res.",
74 "com.sun.org.apache.xml.internal.security.",
75 "com.sun.org.apache.xml.internal.serializer.utils.",
76 "com.sun.org.apache.xml.internal.utils.",
77 "com.sun.org.glassfish.",
78 "com.oracle.xmlns.internal.",
79 "com.oracle.webservices.internal.",
80 "oracle.jrockit.jfr.",
81 "org.jcp.xml.dsig.internal.",
82 "jdk.internal.",
83 "jdk.nashorn.internal.",
84 "jdk.nashorn.tools."
85 };
joehw08a68c42012-04-17 11:21:35 -070086
coffeys52be22a2013-07-09 16:00:41 +010087 public static void main(String[] args) throws Exception {
88 List<String> pkgs = new ArrayList<>(Arrays.asList(packages));
89 String osName = System.getProperty("os.name");
90 if (osName.contains("OS X")) {
91 pkgs.add("apple."); // add apple package for OS X
92 } else if (osName.startsWith("Windows")) {
93 pkgs.add("com.sun.java.accessibility.");
94 }
95
96 List<String> jspkgs =
97 getPackages(Security.getProperty("package.access"));
98
99 // Sort to ensure lists are comparable
100 Collections.sort(pkgs);
101 Collections.sort(jspkgs);
102
103 if (!pkgs.equals(jspkgs)) {
104 for (String p : pkgs)
105 if (!jspkgs.contains(p))
106 System.out.println("In golden set, but not in j.s file: " + p);
107 for (String p : jspkgs)
108 if (!pkgs.contains(p))
109 System.out.println("In j.s file, but not in golden set: " + p);
110
111
112 throw new RuntimeException("restricted packages are not " +
113 "consistent with java.security file");
114 }
115 System.setSecurityManager(new SecurityManager());
116 SecurityManager sm = System.getSecurityManager();
117 for (String pkg : packages) {
118 String subpkg = pkg + "foo";
joehw08a68c42012-04-17 11:21:35 -0700119 try {
120 sm.checkPackageAccess(pkg);
coffeys52be22a2013-07-09 16:00:41 +0100121 throw new RuntimeException("Able to access " + pkg +
122 " package");
123 } catch (SecurityException se) { }
124 try {
125 sm.checkPackageAccess(subpkg);
126 throw new RuntimeException("Able to access " + subpkg +
127 " package");
coffeys55ddfb12013-06-06 14:10:44 +0100128 } catch (SecurityException se) { }
129 try {
130 sm.checkPackageDefinition(pkg);
coffeys52be22a2013-07-09 16:00:41 +0100131 throw new RuntimeException("Able to define class in " + pkg +
132 " package");
133 } catch (SecurityException se) { }
134 try {
135 sm.checkPackageDefinition(subpkg);
136 throw new RuntimeException("Able to define class in " + subpkg +
137 " package");
joehw08a68c42012-04-17 11:21:35 -0700138 } catch (SecurityException se) { }
139 }
coffeys52be22a2013-07-09 16:00:41 +0100140 System.out.println("Test passed");
141 }
142
143 private static List<String> getPackages(String p) {
144 List<String> packages = new ArrayList<>();
145 if (p != null && !p.equals("")) {
146 StringTokenizer tok = new StringTokenizer(p, ",");
147 while (tok.hasMoreElements()) {
148 String s = tok.nextToken().trim();
149 packages.add(s);
150 }
151 }
152 return packages;
joehw08a68c42012-04-17 11:21:35 -0700153 }
154}