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 /*
25 * @test
26 * @bug 6741606 7146431 8000450 8019830 8022945 8027144 8041633 8078427 8055206
27 * @summary Check that various restricted packages that are supposed to be
28 * restricted by default or are listed in the package.access
29 * property in the java.security file are blocked
30 * @modules java.xml.ws java.corba
31 * @run main/othervm CheckPackageAccess
32 */
33
34 import java.lang.module.ModuleFinder;
35 import java.lang.module.ModuleReference;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Optional;
39
40 public class CheckPackageAccess {
41
42 private static final SecurityManager sm = new SecurityManager();
43 private static final ModuleFinder mf = ModuleFinder.ofSystem();
44
45 /*
46 * The expected list of restricted packages of the package.access property.
47 *
48 * This array should be updated whenever new packages are added to the
49 * package.access property in the java.security file
50 * NOTE: it should be in the same order as the java.security file
51 */
52 private static final String[] EXPECTED = {
53 "sun.misc.",
54 "sun.reflect.",
55 };
56
57 /**
58 * Tests access to various packages of a module.
59 */
60 private static class Test {
61 String moduleName; // name of module
62 ModuleReference moduleRef; // module reference
63 String exports; // exported pkg
64 Optional<String> opens; // opened pkg
65 String conceals; // concealed pkg
66 Optional<String> qualExports; // qualified export pkg
67 Optional<String> qualOpens; // qualified open pkg
68 // qual open and non-qualified export pkg
69 Optional<String> qualOpensAndExports;
70 Test(String module, String exports, String opens, String conceals,
71 String qualExports, String qualOpens, String qualOpensAndExports) {
72 this.moduleName = module;
73 this.moduleRef = mf.find(moduleName).get();
74 this.exports = exports;
75 this.opens = Optional.ofNullable(opens);
76 this.conceals = conceals;
77 this.qualExports = Optional.ofNullable(qualExports);
78 this.qualOpens = Optional.ofNullable(qualOpens);
79 this.qualOpensAndExports = Optional.ofNullable(qualOpensAndExports);
80 }
81
82 void test() {
83 System.out.println("Testing module " + moduleName);
84
85 // access to exported pkg should pass
86 testNonRestricted(exports);
87
88 // access to opened pkg should pass
89 opens.ifPresent(Test::testNonRestricted);
90
91 // access to concealed pkg should fail
92 testRestricted(conceals);
93
94 // access to qualified export pkg should fail
95 qualExports.ifPresent(Test::testRestricted);
96
97 // access to qualified open pkg should fail
98 qualOpens.ifPresent(Test::testRestricted);
99
100 // access to qualified opened pkg that is also exported should pass
101 qualOpensAndExports.ifPresent(Test::testNonRestricted);
102 }
103
104 private static void testRestricted(String pkg) {
105 try {
106 sm.checkPackageAccess(pkg);
107 throw new RuntimeException("Able to access restricted package: "
108 + pkg);
109 } catch (SecurityException se) {}
110 try {
111 sm.checkPackageDefinition(pkg);
112 throw new RuntimeException("Able to access restricted package: "
113 + pkg);
114 } catch (SecurityException se) {}
115 }
116
117 private static void testNonRestricted(String pkg) {
118 try {
119 sm.checkPackageAccess(pkg);
120 } catch (SecurityException se) {
121 throw new RuntimeException("Unable to access exported package: "
|
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 /*
25 * @test
26 * @bug 6741606 7146431 8000450 8019830 8022945 8027144 8041633 8078427 8055206
27 * @summary Check that various restricted packages that are supposed to be
28 * restricted by default or are listed in the package.access
29 * property in the java.security file are blocked
30 * @modules java.xml.ws java.corba
31 * @run main/othervm CheckPackageAccess
32 */
33
34 import java.lang.module.ModuleFinder;
35 import java.lang.module.ModuleReference;
36 import java.lang.reflect.Layer;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Optional;
40
41 public class CheckPackageAccess {
42
43 private static final SecurityManager sm = new SecurityManager();
44 private static final ModuleFinder mf = ModuleFinder.ofSystem();
45 private static final String MODULE_TEXT = "Testing module: %1$s. Module is%2$s present.";
46
47 /*
48 * The expected list of restricted packages of the package.access property.
49 *
50 * This array should be updated whenever new packages are added to the
51 * package.access property in the java.security file
52 * NOTE: it should be in the same order as the java.security file
53 */
54 private static final String[] EXPECTED = {
55 "sun.misc.",
56 "sun.reflect.",
57 };
58
59 /**
60 * Tests access to various packages of a module.
61 */
62 private static class Test {
63 String moduleName; // name of module
64 ModuleReference moduleRef; // module reference
65 String exports; // exported pkg
66 Optional<String> opens; // opened pkg
67 String conceals; // concealed pkg
68 Optional<String> qualExports; // qualified export pkg
69 Optional<String> qualOpens; // qualified open pkg
70 // qual open and non-qualified export pkg
71 Optional<String> qualOpensAndExports;
72 Test(String module, String exports, String opens, String conceals,
73 String qualExports, String qualOpens, String qualOpensAndExports) {
74 this.moduleName = module;
75 this.moduleRef = mf.find(moduleName).get();
76 this.exports = exports;
77 this.opens = Optional.ofNullable(opens);
78 this.conceals = conceals;
79 this.qualExports = Optional.ofNullable(qualExports);
80 this.qualOpens = Optional.ofNullable(qualOpens);
81 this.qualOpensAndExports = Optional.ofNullable(qualOpensAndExports);
82 }
83
84 void test() {
85 final boolean isModulePresent = Layer.boot().findModule(moduleName).isPresent();
86 System.out.println(String.format(MODULE_TEXT, moduleName, isModulePresent ? "" : " NOT"));
87
88 if (isModulePresent) {
89
90 // access to exported pkg should pass
91 testNonRestricted(exports);
92
93 // access to opened pkg should pass
94 opens.ifPresent(Test::testNonRestricted);
95
96 // access to concealed pkg should fail
97 testRestricted(conceals);
98
99 // access to qualified export pkg should fail
100 qualExports.ifPresent(Test::testRestricted);
101
102 // access to qualified open pkg should fail
103 qualOpens.ifPresent(Test::testRestricted);
104
105 // access to qualified opened pkg that is also exported should pass
106 qualOpensAndExports.ifPresent(Test::testNonRestricted);
107 } else {
108 System.out.println("Skipping tests for module.");
109 }
110 }
111
112 private static void testRestricted(String pkg) {
113 try {
114 sm.checkPackageAccess(pkg);
115 throw new RuntimeException("Able to access restricted package: "
116 + pkg);
117 } catch (SecurityException se) {}
118 try {
119 sm.checkPackageDefinition(pkg);
120 throw new RuntimeException("Able to access restricted package: "
121 + pkg);
122 } catch (SecurityException se) {}
123 }
124
125 private static void testNonRestricted(String pkg) {
126 try {
127 sm.checkPackageAccess(pkg);
128 } catch (SecurityException se) {
129 throw new RuntimeException("Unable to access exported package: "
|