Path: blob/master/test/jdk/java/lang/ModuleTests/WithSecurityManager.java
41149 views
/*1* Copyright (c) 2016, 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* @modules jdk.compiler26* @summary Test java.lang.Module methods that specify permission checks27* @run main/othervm -Djava.security.manager=allow -Djava.security.policy=${test.src}/allow.policy WithSecurityManager allow28* @run main/othervm -Djava.security.manager=allow WithSecurityManager deny29*/3031import java.io.IOException;32import java.io.InputStream;33import java.lang.module.Configuration;34import java.lang.module.ModuleFinder;35import java.lang.module.ModuleReference;36import java.util.Collections;37import java.util.Optional;38import java.util.Set;3940/**41* Test java.lang.Module methods that specify permission checks.42*/4344public class WithSecurityManager {4546// a module that will be loaded into a child layer47static final String ANOTHER_MODULE = "jdk.compiler";48static final String ANOTHER_MODULE_RESOURCE = "com/sun/tools/javac/Main.class";4950public static void main(String[] args) throws IOException {51boolean allow = args[0].equals("allow");5253// base module, in the boot layer54Module base = Object.class.getModule();5556// another module, in a child layer57Module other = loadModuleInChildLayer(ANOTHER_MODULE);58assertTrue(other.getLayer() != ModuleLayer.boot());5960System.setSecurityManager(new SecurityManager());6162test(base, "java/lang/Object.class", allow);63test(other, ANOTHER_MODULE_RESOURCE, allow);64}6566/**67* Test the permission checks by invoking methods on the given module.68*69* If {@code allow} is {@code true} then the permission checks should succeed.70*/71static void test(Module m, String name, boolean allow) throws IOException {7273// test Module::getClassLoader74System.out.format("Test getClassLoader on %s ...%n", m);75try {76ClassLoader cl = m.getClassLoader();77System.out.println(cl);78if (!allow)79assertTrue("getClassLoader should have failed", false);80} catch (SecurityException e) {81System.out.println(e + " thrown");82if (allow)83throw e;84}8586// test Module::getResourceAsStream87System.out.format("Test getResourceAsStream(\"%s\") on %s ...%n", name, m);88try (InputStream in = m.getResourceAsStream(name)) {89System.out.println(in);90if (allow && (in == null))91assertTrue(name + " not found", false);92if (!allow && (in != null))93assertTrue(name + " should not be found", false);94}9596}9798/**99* Create a module layer that contains the given system module.100*/101static Module loadModuleInChildLayer(String mn) {102Optional<ModuleReference> omref = ModuleFinder.ofSystem().find(mn);103assertTrue("module " + mn + " not a system module", omref.isPresent());104105// create a ModuleFinder that only finds this module106ModuleReference mref = omref.get();107ModuleFinder finder = new ModuleFinder() {108@Override109public Optional<ModuleReference> find(String name) {110if (name.equals(mn))111return Optional.of(mref);112else113return Optional.empty();114}115116@Override117public Set<ModuleReference> findAll() {118return Collections.singleton(mref);119}120};121122// create a child configuration and layer with this module123ModuleLayer bootLayer = ModuleLayer.boot();124Configuration cf = bootLayer125.configuration()126.resolve(finder, ModuleFinder.of(), Set.of(ANOTHER_MODULE));127ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, null);128129Optional<Module> om = layer.findModule(mn);130assertTrue("module " + mn + " not in child layer", om.isPresent());131return om.get();132}133134static void assertTrue(String msg, boolean e) {135if (!e)136throw new RuntimeException(msg);137}138139static void assertTrue(boolean e) {140if (!e)141throw new RuntimeException();142}143144}145146147