1 /*
   2  * Copyright (c) 2011, 2018, 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.testlibrary.Asserts.*;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.List;
  32 
  33 import jdk.testlibrary.OutputAnalyzer;
  34 import jdk.testlibrary.ProcessTools;
  35 import jdk.testlibrary.Utils;
  36 
  37 /*
  38  * @test
  39  * @bug 7104647 7154822
  40  * @summary Unit test for jcmd utility. The test will send different diagnostic
  41  * command requests to the current java process.
  42  *
  43  * @library /lib/testlibrary
  44  *
  45  * @build jdk.testlibrary.*
  46  * @run main/othervm -XX:+UsePerfData TestJcmdSanity
  47  */
  48 public class TestJcmdSanity {
  49 
  50     private static final String TEST_SRC = System.getProperty("test.src").trim();
  51     private static final String[] VM_ARGS = new String[] { "-XX:+UsePerfData" };
  52     private static final String JCMD_COMMAND_REGEX = "(\\w|\\.)*";
  53     private static final String PERF_COUNTER_REGEX = "(\\w|\\.)*\\=.*";
  54 
  55     public static void main(String[] args) throws Exception {
  56         testJcmdPidHelp();
  57         testJcmdPidHelpHelp();
  58         testJcmdPid_f();
  59         testJcmdPidPerfCounterPrint();
  60         testJcmdPidBigScript();
  61     }
  62 
  63     /**
  64      * jcmd -J-XX:+UsePerfData pid help
  65      */
  66     private static void testJcmdPidHelp() throws Exception {
  67         OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
  68                 new String[] {"help"});
  69 
  70         output.shouldHaveExitValue(0);
  71         output.shouldNotContain("Exception");
  72         output.shouldContain(Long.toString(ProcessTools.getProcessId()) + ":");
  73         matchJcmdCommands(output);
  74         output.shouldContain("For more information about a specific command use 'help <command>'.");
  75     }
  76 
  77     /**
  78      * jcmd -J-XX:+UsePerfData pid help help
  79      */
  80     private static void testJcmdPidHelpHelp() throws Exception {
  81         OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
  82                 new String[] {"help", "help"});
  83 
  84         output.shouldHaveExitValue(0);
  85         verifyOutputAgainstFile(output);
  86     }
  87 
  88     /**
  89      * jcmd -J-XX:+UsePerfData pid PerfCounter.print
  90      */
  91     private static void testJcmdPidPerfCounterPrint() throws Exception {
  92         OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
  93                 new String[] {"PerfCounter.print"});
  94 
  95         output.shouldHaveExitValue(0);
  96         matchPerfCounters(output);
  97     }
  98 
  99     /**
 100      * jcmd -J-XX:+UsePerfData pid -f dcmd-script.txt
 101      */
 102     private static void testJcmdPid_f() throws Exception {
 103         File scrpitFile = new File(TEST_SRC, "dcmd-script.txt");
 104         OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
 105                 new String[] {"-f", scrpitFile.getAbsolutePath()});
 106 
 107         output.shouldHaveExitValue(0);
 108         verifyOutputAgainstFile(output);
 109     }
 110 
 111     /**
 112      * Tests that it possible send a file over 1024 bytes large via jcmd -f.
 113      *
 114      * jcmd -J-XX:+UsePerfData pid -f dcmd-big-script.txt
 115      */
 116     private static void testJcmdPidBigScript() throws Exception {
 117         File scrpitFile = new File(TEST_SRC, "dcmd-big-script.txt");
 118         OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
 119                 new String[] {"-f", scrpitFile.getAbsolutePath()});
 120 
 121         output.shouldHaveExitValue(0);
 122         output.shouldNotContain("Exception");
 123         output.shouldContain(System.getProperty("java.vm.name").trim());
 124     }
 125 
 126     /**
 127      * Verifies the listed jcmd commands match a certain pattern.
 128      *
 129      * The output of the jcmd commands should look like:
 130      * VM.uptime
 131      * VM.flags
 132      * VM.system_properties
 133      *
 134      * @param output The generated output from the jcmd.
 135      * @throws Exception
 136      */
 137     private static void matchJcmdCommands(OutputAnalyzer output) throws Exception {
 138         int matchedCount = output.shouldMatchByLine(JCMD_COMMAND_REGEX,
 139                 "help",
 140                 JCMD_COMMAND_REGEX);
 141         assertGreaterThan(matchedCount , 0,
 142                 "Found no lines matching pattern: " + JCMD_COMMAND_REGEX);
 143     }
 144 
 145     /**
 146      * Verifies the generated output from the PerfCounter.print command
 147      * matches a certain pattern.
 148      *
 149      * The output of perf counters should look like:
 150      * java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM"
 151      * java.threads.daemon=7
 152      * sun.rt.javaCommand="com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdSanity.jta"
 153      *
 154      * @param output The generated output from the PerfCounter.print command.
 155      * @throws Exception
 156      */
 157     private static void matchPerfCounters(OutputAnalyzer output) throws Exception {
 158         int matchedCount = output.shouldMatchByLineFrom(PERF_COUNTER_REGEX,
 159                 PERF_COUNTER_REGEX);
 160         assertGreaterThan(matchedCount , 0,
 161                 "Found no lines matching pattern: " + PERF_COUNTER_REGEX);
 162     }
 163 
 164     private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {
 165         Path path = Paths.get(TEST_SRC, "help_help.out");
 166         List<String> fileOutput = Files.readAllLines(path);
 167         List<String> outputAsLines = output.asLines();
 168         assertTrue(outputAsLines.containsAll(fileOutput),
 169                 "The ouput should contain all content of " + path.toAbsolutePath());
 170     }
 171 
 172 }