1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, SAP. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 /*
  27  * @test
  28  * @bug 8220786
  29  * @summary Test ErrorFileToStderr and ErrorFileToStdout
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  * @requires (vm.debug == true)
  33  */
  34 
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 
  38 import java.io.BufferedReader;
  39 import java.io.File;
  40 import java.io.FileInputStream;
  41 import java.io.InputStreamReader;
  42 import java.util.Map;
  43 import java.util.regex.Pattern;
  44 
  45 public class ErrorFileRedirectTest {
  46 
  47   public static void do_test(boolean redirectStdout, boolean redirectStderr) throws Exception {
  48 
  49     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  50             "-Xmx64M",
  51             "-XX:-CreateCoredumpOnCrash",
  52             "-XX:ErrorHandlerTest=14",
  53             "-XX:" + (redirectStdout ? "+" : "-") + "ErrorFileToStdout",
  54             "-XX:" + (redirectStderr ? "+" : "-") + "ErrorFileToStderr",
  55             "-version");
  56 
  57     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  58 
  59     // we should have crashed with a SIGSEGV
  60     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  61     output_detail.shouldMatch("# +(?:SIGSEGV|SIGBUS|EXCEPTION_ACCESS_VIOLATION).*");
  62 
  63     // If no redirection happened, we should find a mention of the file in the output.
  64     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  65     if (redirectStdout == false && redirectStderr == false) {
  66       if (hs_err_file == null) {
  67         throw new RuntimeException("Expected hs-err file but none found.");
  68       } else {
  69         System.out.println("Found hs error file mentioned as expected: " + hs_err_file);
  70       }
  71     } else {
  72       if (hs_err_file != null) {
  73         throw new RuntimeException("Found unexpected mention of hs-err file (we did redirect the output so no file should have been written).");
  74       } else {
  75         System.out.println("No mention of an hs-err file - ok! ");
  76       }
  77     }
  78 
  79     // Check the output. Note that since stderr was specified last it has preference if both are set.
  80     if (redirectStdout == true && redirectStderr == false) {
  81       output_detail.stdoutShouldContain("---------------  S U M M A R Y ------------");
  82       output_detail.stderrShouldNotContain("---------------  S U M M A R Y ------------");
  83       System.out.println("Found report on stderr - ok! ");
  84     } else if (redirectStderr == true) {
  85       output_detail.stderrShouldContain("---------------  S U M M A R Y ------------");
  86       output_detail.stdoutShouldNotContain("---------------  S U M M A R Y ------------");
  87       System.out.println("Found report on stdout - ok! ");
  88     }
  89 
  90     System.out.println("OK.");
  91 
  92   }
  93 
  94   public static void main(String[] args) throws Exception {
  95     do_test(false, false);
  96     do_test(false, true);
  97     do_test(true, false);
  98     do_test(true, true);
  99   }
 100 
 101 }
 102 
 103