Path: blob/master/test/hotspot/jtreg/serviceability/logging/TestQuotedLogOutputs.java
64474 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestQuotedLogOutputs25* @summary Ensure proper parsing of quoted output names for -Xlog arguments.26* @requires vm.flagless27* @modules java.base/jdk.internal.misc28* @library /test/lib29* @comment after JDK-8224505, this has to be run in othervm mode30* @run main/othervm TestQuotedLogOutputs31*/3233import java.io.File;34import java.nio.file.Path;35import java.nio.file.Paths;3637import jdk.test.lib.Asserts;38import jdk.test.lib.process.ProcessTools;39import jdk.test.lib.process.OutputAnalyzer;4041public class TestQuotedLogOutputs {4243public static void main(String[] args) throws Exception {44// Ensure log files can be specified with full path.45// On windows, this means that the file name will contain46// a colon ('C:\log.txt' for example), which is used to47// separate -Xlog: options (-Xlog:tags:filename:decorators).48// Try to log to a file in our current directory, using its absolute path.49String baseName = "test file.log";50Path filePath = Paths.get(baseName).toAbsolutePath();51String fileName = filePath.toString();52File file = filePath.toFile();5354// In case the file already exists, attempt to delete it before running the test55file.delete();5657// Depending on if we're on Windows or not the quotation marks must be escaped,58// otherwise they will be stripped from the command line arguments.59String quote;60if (System.getProperty("os.name").toLowerCase().contains("windows")) {61quote = "\\\""; // quote should be \" (escaped quote)62} else {63quote = "\""; // quote should be " (no escape needed)64}6566// Test a few variations with valid log output specifiers67String[] validOutputs = new String[] {68quote + fileName + quote,69"file=" + quote + fileName + quote,70quote + fileName + quote + ":",71quote + fileName + quote + "::"72};73for (String logOutput : validOutputs) {74// Run with logging=trace on stdout so that we can verify the log configuration afterwards.75ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:logging=trace",76"-Xlog:all=trace:" + logOutput,77"-version");78OutputAnalyzer output = new OutputAnalyzer(pb.start());79output.shouldHaveExitValue(0);80Asserts.assertTrue(file.exists());81file.deleteOnExit(); // Clean up after test82output.shouldMatch("\\[logging *\\].*" + baseName); // Expect to see the log output listed83}8485// Test a bunch of invalid output specifications and ensure the VM fails with these86String[] invalidOutputs = new String[] {87quote,88quote + quote, // should fail because the VM will try to create a file without a name89quote + quote + quote,90quote + quote + quote + quote,91quote + quote + quote + quote + quote,92"prefix" + quote + quote + "suffix",93"prefix" + quote + quote,94quote + quote + "suffix",95quote + "A" + quote + quote + "B" + quote,96quote + "A" + quote + "B" + quote + "C" + quote,97"A" + quote + quote + "B"98};99for (String logOutput : invalidOutputs) {100ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:logging=trace",101"-Xlog:all=trace:" + logOutput,102"-version");103OutputAnalyzer output = new OutputAnalyzer(pb.start());104output.shouldHaveExitValue(1);105// Ensure error message was logged106output.shouldMatch("([Mm]issing terminating quote)"107+ "|(Error opening log file '')"108+ "|(Output name can not be partially quoted)");109}110}111}112113114115