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