Path: blob/master/test/hotspot/jtreg/serviceability/logging/TestQuotedLogOutputs.java
41152 views
/*1* Copyright (c) 2015, 2020, 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* @modules java.base/jdk.internal.misc27* @library /test/lib28* @comment after JDK-8224505, this has to be run in othervm mode29* @run main/othervm TestQuotedLogOutputs30*/3132import java.io.File;33import java.nio.file.Path;34import java.nio.file.Paths;3536import jdk.test.lib.Asserts;37import jdk.test.lib.process.ProcessTools;38import jdk.test.lib.process.OutputAnalyzer;3940public class TestQuotedLogOutputs {4142public static void main(String[] args) throws Exception {43// Ensure log files can be specified with full path.44// On windows, this means that the file name will contain45// a colon ('C:\log.txt' for example), which is used to46// separate -Xlog: options (-Xlog:tags:filename:decorators).47// Try to log to a file in our current directory, using its absolute path.48String baseName = "test file.log";49Path filePath = Paths.get(baseName).toAbsolutePath();50String fileName = filePath.toString();51File file = filePath.toFile();5253// In case the file already exists, attempt to delete it before running the test54file.delete();5556// Depending on if we're on Windows or not the quotation marks must be escaped,57// otherwise they will be stripped from the command line arguments.58String quote;59if (System.getProperty("os.name").toLowerCase().contains("windows")) {60quote = "\\\""; // quote should be \" (escaped quote)61} else {62quote = "\""; // quote should be " (no escape needed)63}6465// Test a few variations with valid log output specifiers66String[] validOutputs = new String[] {67quote + fileName + quote,68"file=" + quote + fileName + quote,69quote + fileName + quote + ":",70quote + fileName + quote + "::"71};72for (String logOutput : validOutputs) {73// Run with logging=trace on stdout so that we can verify the log configuration afterwards.74ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:logging=trace",75"-Xlog:all=trace:" + logOutput,76"-version");77OutputAnalyzer output = new OutputAnalyzer(pb.start());78output.shouldHaveExitValue(0);79Asserts.assertTrue(file.exists());80file.deleteOnExit(); // Clean up after test81output.shouldMatch("\\[logging *\\].*" + baseName); // Expect to see the log output listed82}8384// Test a bunch of invalid output specifications and ensure the VM fails with these85String[] invalidOutputs = new String[] {86quote,87quote + quote, // should fail because the VM will try to create a file without a name88quote + quote + quote,89quote + quote + quote + quote,90quote + quote + quote + quote + quote,91"prefix" + quote + quote + "suffix",92"prefix" + quote + quote,93quote + quote + "suffix",94quote + "A" + quote + quote + "B" + quote,95quote + "A" + quote + "B" + quote + "C" + quote,96"A" + quote + quote + "B"97};98for (String logOutput : invalidOutputs) {99ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:logging=trace",100"-Xlog:all=trace:" + logOutput,101"-version");102OutputAnalyzer output = new OutputAnalyzer(pb.start());103output.shouldHaveExitValue(1);104// Ensure error message was logged105output.shouldMatch("([Mm]issing terminating quote)"106+ "|(Error opening log file '')"107+ "|(Output name can not be partially quoted)");108}109}110}111112113114