blob: 10752b94ba42772f51b88bef7a8477b4678065e2 [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
mullan506b2d32013-10-11 08:43:07 -040032import java.io.File;
33import java.nio.charset.StandardCharsets;
34import java.nio.file.Files;
coffeys52be22a2013-07-09 16:00:41 +010035import java.security.Security;
36import java.util.Collections;
37import java.util.Arrays;
38import java.util.ArrayList;
39import java.util.List;
40import java.util.StringTokenizer;
41
42/*
43 * The main benefit of this test is to catch merge errors or other types
44 * of issues where one or more of the packages are accidentally
45 * removed. This is why the packages that are known to be restricted have to
46 * be explicitly listed below.
47 */
joehw08a68c42012-04-17 11:21:35 -070048public class CheckPackageAccess {
49
coffeys52be22a2013-07-09 16:00:41 +010050 /*
51 * This array should be updated whenever new packages are added to the
52 * package.access property in the java.security file
53 */
54 private static final String[] packages = {
55 "sun.",
coffeys52be22a2013-07-09 16:00:41 +010056 "com.sun.xml.internal.",
57 "com.sun.imageio.",
58 "com.sun.istack.internal.",
59 "com.sun.jmx.",
mullan40a8bb12013-08-27 12:04:32 -040060 "com.sun.media.sound.",
coffeys52be22a2013-07-09 16:00:41 +010061 "com.sun.proxy.",
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.security.",
78 "com.sun.org.apache.xml.internal.serializer.utils.",
79 "com.sun.org.apache.xml.internal.utils.",
80 "com.sun.org.glassfish.",
81 "com.oracle.xmlns.internal.",
82 "com.oracle.webservices.internal.",
83 "oracle.jrockit.jfr.",
84 "org.jcp.xml.dsig.internal.",
85 "jdk.internal.",
86 "jdk.nashorn.internal.",
87 "jdk.nashorn.tools."
88 };
joehw08a68c42012-04-17 11:21:35 -070089
coffeys52be22a2013-07-09 16:00:41 +010090 public static void main(String[] args) throws Exception {
91 List<String> pkgs = new ArrayList<>(Arrays.asList(packages));
92 String osName = System.getProperty("os.name");
93 if (osName.contains("OS X")) {
94 pkgs.add("apple."); // add apple package for OS X
95 } else if (osName.startsWith("Windows")) {
96 pkgs.add("com.sun.java.accessibility.");
97 }
98
99 List<String> jspkgs =
100 getPackages(Security.getProperty("package.access"));
101
mullan506b2d32013-10-11 08:43:07 -0400102 // get closed restricted packages
103 File f = new File(System.getProperty("test.src"),
104 "../../../../src/closed/share/lib/security/restricted.pkgs");
105 if (f.exists()) {
106 List<String> ipkgs = Files.readAllLines(f.toPath(),
107 StandardCharsets.UTF_8);
108 // Remove any closed packages from list before comparing
109 jspkgs.removeAll(ipkgs);
110 }
111
coffeys52be22a2013-07-09 16:00:41 +0100112 // Sort to ensure lists are comparable
113 Collections.sort(pkgs);
114 Collections.sort(jspkgs);
115
116 if (!pkgs.equals(jspkgs)) {
117 for (String p : pkgs)
118 if (!jspkgs.contains(p))
119 System.out.println("In golden set, but not in j.s file: " + p);
120 for (String p : jspkgs)
121 if (!pkgs.contains(p))
122 System.out.println("In j.s file, but not in golden set: " + p);
123
124
125 throw new RuntimeException("restricted packages are not " +
126 "consistent with java.security file");
127 }
128 System.setSecurityManager(new SecurityManager());
129 SecurityManager sm = System.getSecurityManager();
130 for (String pkg : packages) {
131 String subpkg = pkg + "foo";
joehw08a68c42012-04-17 11:21:35 -0700132 try {
133 sm.checkPackageAccess(pkg);
coffeys52be22a2013-07-09 16:00:41 +0100134 throw new RuntimeException("Able to access " + pkg +
135 " package");
136 } catch (SecurityException se) { }
137 try {
138 sm.checkPackageAccess(subpkg);
139 throw new RuntimeException("Able to access " + subpkg +
140 " package");
coffeys55ddfb12013-06-06 14:10:44 +0100141 } catch (SecurityException se) { }
142 try {
143 sm.checkPackageDefinition(pkg);
coffeys52be22a2013-07-09 16:00:41 +0100144 throw new RuntimeException("Able to define class in " + pkg +
145 " package");
146 } catch (SecurityException se) { }
147 try {
148 sm.checkPackageDefinition(subpkg);
149 throw new RuntimeException("Able to define class in " + subpkg +
150 " package");
joehw08a68c42012-04-17 11:21:35 -0700151 } catch (SecurityException se) { }
152 }
coffeys52be22a2013-07-09 16:00:41 +0100153 System.out.println("Test passed");
154 }
155
156 private static List<String> getPackages(String p) {
157 List<String> packages = new ArrayList<>();
158 if (p != null && !p.equals("")) {
159 StringTokenizer tok = new StringTokenizer(p, ",");
160 while (tok.hasMoreElements()) {
161 String s = tok.nextToken().trim();
162 packages.add(s);
163 }
164 }
165 return packages;
joehw08a68c42012-04-17 11:21:35 -0700166 }
167}