1 /*
   2  * Copyright (c) 2016, 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 import java.io.*;
  24 import java.nio.file.*;
  25 import java.util.jar.Attributes;
  26 import java.util.jar.JarEntry;
  27 import java.util.jar.JarOutputStream;
  28 import java.util.jar.Manifest;
  29 import jdk.test.lib.*;
  30 import jdk.test.lib.dcmd.*;
  31 import org.testng.annotations.Test;
  32 
  33 /*
  34  * Test to attach JVMTI java agent.
  35  *
  36  * @test
  37  * @bug 8147388
  38  * @library /testlibrary
  39  * @modules java.base/jdk.internal.misc
  40  *          java.compiler
  41  *          java.instrument
  42  *          java.management
  43  *          jdk.jvmstat/sun.jvmstat.monitor
  44  * @build ClassFileInstaller jdk.test.lib.* SimpleJvmtiAgent
  45  * @ignore 8150318
  46  * @run main ClassFileInstaller SimpleJvmtiAgent
  47  * @run testng LoadAgentDcmdTest
  48  */
  49 public class LoadAgentDcmdTest {
  50 
  51     public String getLibInstrumentPath() throws FileNotFoundException {
  52         String jdkPath = System.getProperty("test.jdk");
  53 
  54         if (jdkPath == null) {
  55             throw new RuntimeException(
  56                       "System property 'test.jdk' not set. " +
  57                       "This property is normally set by jtreg. " +
  58                       "When running test separately, set this property using " +
  59                       "'-Dtest.jdk=/path/to/jdk'.");
  60         }
  61 
  62         Path libpath = Paths.get(jdkPath, Platform.jdkLibPath(), Platform.sharedObjectName("instrument"));
  63 
  64         if (!libpath.toFile().exists()) {
  65             throw new FileNotFoundException(
  66                       "Could not find " + libpath.toAbsolutePath());
  67         }
  68 
  69         return libpath.toAbsolutePath().toString();
  70     }
  71 
  72 
  73     public void createJarFileForAgent()
  74       throws IOException {
  75 
  76         final String jarName = "agent.jar";
  77         final String agentClass = "SimpleJvmtiAgent";
  78 
  79         Manifest manifest = new Manifest();
  80 
  81         manifest.getMainAttributes().put(
  82              Attributes.Name.MANIFEST_VERSION, "1.0");
  83 
  84         manifest.getMainAttributes().put(
  85              new Attributes.Name("Agent-Class"), agentClass);
  86 
  87         JarOutputStream target = null;
  88 
  89         try {
  90             target = new
  91               JarOutputStream(new FileOutputStream(jarName), manifest);
  92             JarEntry entry = new JarEntry(agentClass + ".class");
  93             target.putNextEntry(entry);
  94             target.closeEntry();
  95         } finally {
  96             target.close();
  97         }
  98     }
  99 
 100     public void run(CommandExecutor executor)  {
 101         try{
 102 
 103             createJarFileForAgent();
 104 
 105             String libpath = getLibInstrumentPath();
 106             OutputAnalyzer output = null;
 107 
 108             // Test 1: Native agent, no arguments
 109             output = executor.execute("JVMTI.agent_load " +
 110                                           libpath + " agent.jar");
 111             output.stderrShouldBeEmpty();
 112 
 113             // Test 2: Native agent, with arguments
 114             output = executor.execute("JVMTI.agent_load " +
 115                                           libpath + " \"agent.jar=foo=bar\"");
 116             output.stderrShouldBeEmpty();
 117 
 118             // Test 3: Java agent, no arguments
 119             output = executor.execute("JVMTI.agent_load " +
 120                                           "agent.jar");
 121             output.stderrShouldBeEmpty();
 122 
 123             // Test 4: Java agent, with arguments
 124             output = executor.execute("JVMTI.agent_load " +
 125                                           "\"agent.jar=foo=bar\"");
 126             output.stderrShouldBeEmpty();
 127 
 128         } catch (Exception e) {
 129             throw new RuntimeException(e);
 130         }
 131     }
 132 
 133     @Test
 134     public void jmx() throws Throwable {
 135         run(new JMXExecutor());
 136     }
 137 
 138     @Test
 139     public void cli() throws Throwable {
 140         run(new PidJcmdExecutor());
 141     }
 142 }