Path: blob/master/test/jdk/java/lang/SecurityManager/modules/CustomSecurityManagerTest.java
41152 views
/*1* Copyright (c) 2015, 2020, 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*/2223import jdk.test.lib.Utils;24import jdk.test.lib.process.OutputAnalyzer;25import jdk.test.lib.process.ProcessTools;26import org.testng.annotations.DataProvider;27import org.testng.annotations.Test;28import java.nio.file.Paths;29import java.util.List;3031/*32* @test33* @summary Basic test of -Djava.security.manager to a class in named module.34* @library /test/lib35* @build jdk.test.lib.process.*36* m/*37* @run testng/othervm CustomSecurityManagerTest38*/39public class CustomSecurityManagerTest {4041private static final String MODULE_PATH = Paths.get(Utils.TEST_CLASSES).resolve("modules").toString();42private static final String POLICY_PATH = Paths.get(Utils.TEST_SRC).resolve("test.policy").toString();4344@DataProvider45public Object[][] testCases() {46return new Object[][]{47new Object[] { List.of(48"--module-path", MODULE_PATH,49"--add-modules", "m",50"-Djava.security.manager",51String.format("-Djava.security.policy=%s", POLICY_PATH),52"RunTest"53) },54new Object[] { List.of(55"--module-path", MODULE_PATH,56"--add-modules", "m",57"-Djava.security.manager=p.CustomSecurityManager",58String.format("-Djava.security.policy=%s", POLICY_PATH),59"RunTest"60) }61};62}6364@Test(dataProvider = "testCases")65public void testProvider(List<String> args) throws Throwable {66ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(args);67OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(processBuilder);68outputAnalyzer.shouldHaveExitValue(0);69}7071}7273class RunTest {74public static void main(String... args) {75SecurityManager sm = System.getSecurityManager();76Module module = sm.getClass().getModule();77String s = System.getProperty("java.security.manager");78String expected = s.isEmpty() ? "java.base" : "m";79if (!module.isNamed() || !module.getName().equals(expected)) {80throw new RuntimeException(module + " expected module m instead");81}82}83}848586