|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * 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 | +import static jdk.test.lib.Asserts.*; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.lang.management.ManagementFactory; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.Paths; |
| 31 | +import java.util.jar.Attributes; |
| 32 | +import java.util.jar.Manifest; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import com.sun.management.HotSpotDiagnosticMXBean; |
| 36 | +import com.sun.management.VMOption; |
| 37 | +import jdk.test.lib.process.OutputAnalyzer; |
| 38 | +import jdk.test.lib.Utils; |
| 39 | +import jdk.test.lib.util.JarUtils; |
| 40 | + |
| 41 | +/* |
| 42 | + * @test id=default |
| 43 | + * @bug 8304438 |
| 44 | + * @summary JVMTI.agent_load should obey EnableDynamicAgentLoading (by default) |
| 45 | + * @modules jdk.attach jdk.jcmd |
| 46 | + * @library /test/lib |
| 47 | + * @build JavaAgent |
| 48 | + * @run main/othervm TestJcmdNoAgentLoad |
| 49 | + */ |
| 50 | + |
| 51 | +/* |
| 52 | + * @test id=disabled |
| 53 | + * @bug 8304438 |
| 54 | + * @summary JVMTI.agent_load should obey EnableDynamicAgentLoading (disabled) |
| 55 | + * @modules jdk.attach jdk.jcmd |
| 56 | + * @library /test/lib |
| 57 | + * @build JavaAgent |
| 58 | + * @run main/othervm -XX:-EnableDynamicAgentLoading TestJcmdNoAgentLoad |
| 59 | + */ |
| 60 | + |
| 61 | +/* |
| 62 | + * @test id=enabled |
| 63 | + * @bug 8304438 |
| 64 | + * @summary JVMTI.agent_load should obey EnableDynamicAgentLoading (enabled) |
| 65 | + * @modules jdk.attach jdk.jcmd |
| 66 | + * @library /test/lib |
| 67 | + * @build JavaAgent |
| 68 | + * @run main/othervm -XX:+EnableDynamicAgentLoading TestJcmdNoAgentLoad |
| 69 | + */ |
| 70 | + |
| 71 | + |
| 72 | +public class TestJcmdNoAgentLoad { |
| 73 | + private static final String PTRN = "Dynamic agent loading is not enabled"; |
| 74 | + private static boolean dynamicLoadingEnabled = true; |
| 75 | + private static final String TEST_CLASSES = System.getProperty("test.classes"); |
| 76 | + private static String javaAgent; |
| 77 | + |
| 78 | + static { |
| 79 | + // get VM option EnableDynamicAgentLoading value |
| 80 | + HotSpotDiagnosticMXBean bean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); |
| 81 | + VMOption dynamicLoadingEnabledOpt = bean.getVMOption("EnableDynamicAgentLoading"); |
| 82 | + dynamicLoadingEnabled = dynamicLoadingEnabledOpt.getValue().equals("true"); |
| 83 | + } |
| 84 | + |
| 85 | + public static void main(String[] args) throws Exception { |
| 86 | + setup(); |
| 87 | + testNoAgentLoad(new String[] { "JVMTI.agent_load", javaAgent }); |
| 88 | + } |
| 89 | + |
| 90 | + private static void setup() throws Exception { |
| 91 | + // create JAR file with Java agent |
| 92 | + Manifest man = new Manifest(); |
| 93 | + Attributes attrs = man.getMainAttributes(); |
| 94 | + attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
| 95 | + attrs.put(new Attributes.Name("Agent-Class"), "JavaAgent"); |
| 96 | + Path jarfile = Path.of("javaagent.jar"); |
| 97 | + Path classes = Path.of(TEST_CLASSES); |
| 98 | + JarUtils.createJarFile(jarfile, man, classes, Path.of("JavaAgent.class")); |
| 99 | + javaAgent = jarfile.toString(); |
| 100 | + } |
| 101 | + |
| 102 | + private static void testNoAgentLoad(String... jcmdArgs) throws Exception { |
| 103 | + OutputAnalyzer output = JcmdBase.jcmd(jcmdArgs); |
| 104 | + |
| 105 | + output.shouldHaveExitValue(0); |
| 106 | + if (dynamicLoadingEnabled) { |
| 107 | + output.shouldNotContain(PTRN); |
| 108 | + } else { |
| 109 | + output.shouldContain(PTRN); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments