Path: blob/master/test/jdk/java/lang/System/SecurityManagerWarnings.java
66644 views
/*1* Copyright (c) 2021, 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* @bug 8266459 8268349 826954326* @summary check various warnings27* @library /test/lib28*/2930import jdk.test.lib.JDKToolFinder;31import jdk.test.lib.process.OutputAnalyzer;32import jdk.test.lib.process.ProcessTools;33import jdk.test.lib.util.JarUtils;3435import java.io.ByteArrayOutputStream;36import java.io.File;37import java.io.PrintStream;38import java.nio.file.Files;39import java.nio.file.Path;4041public class SecurityManagerWarnings {4243public static void main(String args[]) throws Exception {44if (args.length == 0) {45Files.writeString(Path.of("policy"), """46grant {47permission java.lang.RuntimePermission "setIO";48permission java.lang.RuntimePermission "createSecurityManager";49permission java.lang.RuntimePermission "setSecurityManager";50};51""");5253String testClasses = System.getProperty("test.classes");5455allowTest(null, testClasses);56allowTest("allow", testClasses);57disallowTest("disallow", testClasses);58enableTest("", testClasses);59enableTest("default", testClasses);60enableTest("java.lang.SecurityManager", testClasses);6162JarUtils.createJarFile(Path.of("a.jar"),63Path.of(testClasses),64Path.of("SecurityManagerWarnings.class"),65Path.of("A.class"),66Path.of("B.class"));6768allowTest(null, "a.jar");69} else {70System.out.println("SM is enabled: " + (System.getSecurityManager() != null));71PrintStream oldErr = System.err;72// Modify System.err, thus make sure warnings are always printed73// to the original System.err and will not be swallowed.74System.setErr(new PrintStream(new ByteArrayOutputStream()));75try {76// Run A.run() twice will show only one warning77// (setSecurityManager(null) to ensure the next set is permitted)78// Run B.run() and a new warning will appear79A.run(); // System.setSecurityManager(null);80A.run(); // System.setSecurityManager(null);81B.run(); // System.setSecurityManager(new SecurityManager());82} catch (Exception e) {83// Exception messages must show in original stderr84e.printStackTrace(oldErr);85throw e;86}87}88}8990// When SM is allowed, no startup warning, has setSM warning91static void allowTest(String prop, String cp) throws Exception {92checkInstallMessage(run(prop, cp), cp)93.shouldHaveExitValue(0)94.stdoutShouldContain("SM is enabled: false")95.shouldNotContain("A command line option");96}9798// When SM is disallowed, no startup warning, setSM fails99static void disallowTest(String prop, String cp) throws Exception {100run(prop, cp)101.shouldNotHaveExitValue(0)102.stdoutShouldContain("SM is enabled: false")103.shouldNotContain("A command line option")104.shouldNotContain("A terminally deprecated method")105.stderrShouldContain("UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release");106}107108// When SM is allowed, has startup warning, has setSM warning109static void enableTest(String prop, String cp) throws Exception {110checkInstallMessage(run(prop, cp), cp)111.shouldHaveExitValue(0)112.stdoutShouldContain("SM is enabled: true")113.stderrShouldContain("WARNING: A command line option has enabled the Security Manager")114.stderrShouldContain("WARNING: The Security Manager is deprecated and will be removed in a future release");115}116117// Check the setSM warning118static OutputAnalyzer checkInstallMessage(OutputAnalyzer oa, String cp) {119String uri = new File(cp).toURI().toString();120return oa121.stderrShouldContain("WARNING: A terminally deprecated method in java.lang.System has been called")122.stderrShouldContain("WARNING: System::setSecurityManager has been called by A (" + uri + ")")123.stderrShouldContain("WARNING: System::setSecurityManager has been called by B (" + uri + ")")124.stderrShouldContain("WARNING: Please consider reporting this to the maintainers of A")125.stderrShouldContain("WARNING: Please consider reporting this to the maintainers of B")126.stderrShouldContain("WARNING: System::setSecurityManager will be removed in a future release")127.stderrShouldNotMatch("(?s)by A.*by A"); // "by A" appears only once128}129130static OutputAnalyzer run(String prop, String cp) throws Exception {131ProcessBuilder pb;132if (prop == null) {133pb = new ProcessBuilder(134JDKToolFinder.getJDKTool("java"),135"-cp", cp,136"SecurityManagerWarnings", "run");137} else {138pb = new ProcessBuilder(139JDKToolFinder.getJDKTool("java"),140"-cp", cp,141"-Djava.security.manager=" + prop,142"-Djava.security.policy=policy",143"SecurityManagerWarnings", "run");144}145return ProcessTools.executeProcess(pb)146.stderrShouldNotContain("AccessControlException");147}148}149150class A {151static void run() {152System.setSecurityManager(null);153}154}155156class B {157static void run() {158System.setSecurityManager(new SecurityManager());159}160}161162163