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 
  24 /*
  25  * @test
  26  * @bug 8163346
  27  * @summary Test hashing of extended characters in Serviceability Agent.
  28  * @library /test/lib
  29  * @library /lib/testlibrary
  30  * @compile -encoding utf8 HeapDumpTest.java
  31  * @run main/timeout=240 HeapDumpTest
  32  */
  33 
  34 import static jdk.testlibrary.Asserts.assertTrue;
  35 
  36 import java.io.IOException;
  37 import java.io.File;
  38 import java.util.List;
  39 import java.util.Arrays;
  40 import jdk.testlibrary.JDKToolLauncher;
  41 import jdk.testlibrary.OutputAnalyzer;
  42 import jdk.testlibrary.ProcessTools;
  43 import jdk.test.lib.apps.LingeredApp;
  44 import jdk.test.lib.Platform;
  45 
  46 public class HeapDumpTest {
  47 
  48     private static LingeredAppWithExtendedChars theApp = null;
  49 
  50     /**
  51      *
  52      * @param vmArgs  - tool arguments to launch jhsdb
  53      * @return exit code of tool
  54      */
  55     public static void launch(String expectedMessage, List<String> toolArgs)
  56         throws IOException {
  57 
  58         System.out.println("Starting LingeredApp");
  59         try {
  60             theApp = new LingeredAppWithExtendedChars();
  61             LingeredApp.startApp(Arrays.asList("-Xmx256m"), theApp);
  62 
  63             System.out.println(theApp.\u00CB);
  64             System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());
  65             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  66 
  67             for (String cmd : toolArgs) {
  68                 launcher.addToolArg(cmd);
  69             }
  70 
  71             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
  72 
  73             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  74             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  75             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  76             System.out.println("stdout:");
  77             System.out.println(output.getStdout());
  78             System.out.println("stderr:");
  79             System.out.println(output.getStderr());
  80             output.shouldContain(expectedMessage);
  81             output.shouldHaveExitValue(0);
  82 
  83         } catch (Exception ex) {
  84             throw new RuntimeException("Test ERROR " + ex, ex);
  85         } finally {
  86             LingeredApp.stopApp(theApp);
  87         }
  88     }
  89 
  90     public static void launch(String expectedMessage, String... toolArgs)
  91         throws IOException {
  92 
  93         launch(expectedMessage, Arrays.asList(toolArgs));
  94     }
  95 
  96     public static void testHeapDump() throws IOException {
  97         File dump = new File("jhsdb.jmap.heap." +
  98                              System.currentTimeMillis() + ".hprof");
  99         if (dump.exists()) {
 100             dump.delete();
 101         }
 102 
 103         launch("heap written to", "jmap",
 104                "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
 105 
 106         assertTrue(dump.exists() && dump.isFile(),
 107                    "Could not create dump file " + dump.getAbsolutePath());
 108 
 109         dump.delete();
 110     }
 111 
 112     public static void main(String[] args) throws Exception {
 113 
 114         if (!Platform.shouldSAAttach()) {
 115             // Silently skip the test if we don't have enough permissions to attach
 116             System.err.println("Error! Insufficient permissions to attach - test skipped.");
 117             return;
 118         }
 119 
 120 
 121         testHeapDump();
 122 
 123         // The test throws RuntimeException on error.
 124         // IOException is thrown if LingeredApp can't start because of some bad
 125         // environment condition
 126         System.out.println("Test PASSED");
 127     }
 128 }