1 /*
  2  * Copyright (c) 2023, 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 java.io.BufferedReader;
 25 import java.io.File;
 26 import java.io.IOException;
 27 import java.nio.file.Path;
 28 import java.util.List;
 29 import java.util.logging.LogRecord;
 30 import java.util.logging.Logger;
 31 import java.util.logging.StreamHandler;
 32 import java.util.stream.Stream;
 33 
 34 import org.junit.jupiter.params.provider.Arguments;
 35 import org.junit.jupiter.params.provider.MethodSource;
 36 import org.junit.jupiter.params.ParameterizedTest;
 37 import static org.junit.jupiter.api.Assertions.assertEquals;
 38 import static org.junit.jupiter.api.Assertions.fail;
 39 
 40 /*
 41  * @test
 42  * @summary verify logging of ProcessBuilder.start()
 43  * @requires vm.flagless
 44  * @run junit/othervm ProcessStartLoggingTest
 45  */
 46 public class ProcessStartLoggingTest {
 47 
 48     private final static int NORMAL_STATUS = 0;
 49     private final static int ERROR_STATUS = 1;
 50 
 51     private static final String TEST_JDK = System.getProperty("test.jdk");
 52     private static final String TEST_SRC = System.getProperty("test.src");
 53 
 54     private static Object HOLD_LOGGER;
 55 
 56     /**
 57      * Launch a process with the arguments.
 58      * @param args 1 or strings passed directly to ProcessBuilder as command and arguments.
 59      */
 60     public static void main(String[] args) throws InterruptedException {
 61         if (System.getProperty("ThrowingHandler") != null) {
 62             HOLD_LOGGER = ProcessStartLoggingTest.ThrowingHandler.installHandler();
 63         }
 64         String directory = System.getProperty("directory");
 65         try {
 66             ProcessBuilder pb = new ProcessBuilder(args);
 67             pb.directory((directory == null) ? null : new File(directory));
 68             Process p = pb.start();
 69             int status = p.waitFor();
 70             if (status != 0) {
 71                 System.out.println("exitValue: " + status);
 72             }
 73         } catch (IOException ioe) {
 74             System.out.println("ProcessBuilder.start() threw IOException: " + ioe);
 75         }
 76     }
 77 
 78     /**
 79      * Test various log level settings, and none.
 80      * @return a stream of arguments for parameterized test
 81      */
 82     private static Stream<Arguments> logParamProvider() {
 83 
 84         return Stream.of(
 85                 // Logging enabled with level TRACE
 86                 Arguments.of(List.of("-Djava.util.logging.config.file=" +
 87                                         Path.of(TEST_SRC, "ProcessLogging-FINER.properties").toString(),
 88                                 "-Ddirectory=."),
 89                         List.of("echo", "echo0"),
 90                         NORMAL_STATUS,
 91                         "dir: ., cmd: \"echo\" \"echo0\""),
 92                 // Logging enabled with level DEBUG
 93                 Arguments.of(List.of("-Djava.util.logging.config.file=" +
 94                                         Path.of(TEST_SRC, "ProcessLogging-FINE.properties").toString(),
 95                                 "-Ddirectory=."),
 96                         List.of("echo", "echo1"),
 97                         NORMAL_STATUS,
 98                         "dir: ., cmd: \"echo\""),
 99                 // Logging disabled due to level INFO
100                 Arguments.of(List.of("-Djava.util.logging.config.file=" +
101                                 Path.of(TEST_SRC, "ProcessLogging-INFO.properties").toString()),
102                         List.of("echo", "echo2"),
103                         NORMAL_STATUS,
104                         ""),
105                 // Console logger DEBUG
106                 Arguments.of(List.of("--limit-modules", "java.base",
107                                 "-Djdk.system.logger.level=DEBUG"),
108                         List.of("echo", "echo3"),
109                         NORMAL_STATUS,
110                         "dir: null, cmd: \"echo\""),
111                 // Console logger TRACE
112                 Arguments.of(List.of("--limit-modules", "java.base",
113                                 "-Djdk.system.logger.level=TRACE",
114                                 "-Ddirectory=."),
115                         List.of("echo", "echo4"),
116                         NORMAL_STATUS,
117                         "dir: ., cmd: \"echo\" \"echo4\""),
118                 // No Logging configured
119                 Arguments.of(List.of(),
120                         List.of("echo", "echo5"),
121                         NORMAL_STATUS,
122                         ""),
123                 // Throwing Handler
124                 Arguments.of(List.of("-DThrowingHandler",
125                                 "-Djava.util.logging.config.file=" +
126                                         Path.of(TEST_SRC, "ProcessLogging-FINE.properties").toString()),
127                         List.of("echo", "echo6"),
128                         ERROR_STATUS,
129                         "Exception in thread \"main\" java.lang.RuntimeException: Exception in publish")
130         );
131     }
132 
133     /**
134      * Check that the logger output of a launched process contains the expected message.
135      *
136      * @param logArgs       Arguments to configure logging in the java test process
137      * @param childArgs     the args passed to the child to be invoked as a Process
138      * @param expectMessage log should contain the message
139      */
140     @ParameterizedTest
141     @MethodSource("logParamProvider")
142     public void checkLogger(List<String> logArgs, List<String> childArgs,
143                             int expectedStatus, String expectMessage) {
144         ProcessBuilder pb = new ProcessBuilder();
145         pb.redirectErrorStream(true);
146 
147         List<String> cmd = pb.command();
148         cmd.add(Path.of(TEST_JDK,"bin", "java").toString());
149         cmd.addAll(logArgs);
150         cmd.add(this.getClass().getName());
151         cmd.addAll(childArgs);
152         try {
153             Process process = pb.start();
154             try (BufferedReader reader = process.inputReader()) {
155                 List<String> lines = reader.lines().toList();
156                 boolean match = (expectMessage.isEmpty())
157                         ? lines.size() == 0
158                         : lines.stream().filter(s -> s.contains(expectMessage)).findFirst().isPresent();
159                 if (!match) {
160                     // Output lines for debug
161                     System.err.println("Expected> \"" + expectMessage + "\"");
162                     lines.forEach(l -> System.err.println("Actual>   \"" + l+ "\""));
163                     fail("Unexpected log contents");
164                 }
165             }
166             int result = process.waitFor();
167             assertEquals(expectedStatus, result, "Exit status");
168         } catch (IOException | InterruptedException ex) {
169             fail(ex);
170         }
171     }
172 
173     /**
174      * A LoggingHandler that throws an Exception.
175      */
176     public static class ThrowingHandler extends StreamHandler {
177 
178         // Install this handler for java.lang.ProcessBuilder
179         public static Logger installHandler() {
180             Logger logger = Logger.getLogger("java.lang.ProcessBuilder");
181             logger.addHandler(new ProcessStartLoggingTest.ThrowingHandler());
182             return logger;
183         }
184 
185         @Override
186         public synchronized void publish(LogRecord record) {
187             throw new RuntimeException("Exception in publish");
188         }
189     }
190 }