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