Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/serviceability/logging/TestQuotedLogOutputs.java
64474 views
1
/*
2
* Copyright (c) 2015, 2021, 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 TestQuotedLogOutputs
26
* @summary Ensure proper parsing of quoted output names for -Xlog arguments.
27
* @requires vm.flagless
28
* @modules java.base/jdk.internal.misc
29
* @library /test/lib
30
* @comment after JDK-8224505, this has to be run in othervm mode
31
* @run main/othervm TestQuotedLogOutputs
32
*/
33
34
import java.io.File;
35
import java.nio.file.Path;
36
import java.nio.file.Paths;
37
38
import jdk.test.lib.Asserts;
39
import jdk.test.lib.process.ProcessTools;
40
import jdk.test.lib.process.OutputAnalyzer;
41
42
public class TestQuotedLogOutputs {
43
44
public static void main(String[] args) throws Exception {
45
// Ensure log files can be specified with full path.
46
// On windows, this means that the file name will contain
47
// a colon ('C:\log.txt' for example), which is used to
48
// separate -Xlog: options (-Xlog:tags:filename:decorators).
49
// Try to log to a file in our current directory, using its absolute path.
50
String baseName = "test file.log";
51
Path filePath = Paths.get(baseName).toAbsolutePath();
52
String fileName = filePath.toString();
53
File file = filePath.toFile();
54
55
// In case the file already exists, attempt to delete it before running the test
56
file.delete();
57
58
// Depending on if we're on Windows or not the quotation marks must be escaped,
59
// otherwise they will be stripped from the command line arguments.
60
String quote;
61
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
62
quote = "\\\""; // quote should be \" (escaped quote)
63
} else {
64
quote = "\""; // quote should be " (no escape needed)
65
}
66
67
// Test a few variations with valid log output specifiers
68
String[] validOutputs = new String[] {
69
quote + fileName + quote,
70
"file=" + quote + fileName + quote,
71
quote + fileName + quote + ":",
72
quote + fileName + quote + "::"
73
};
74
for (String logOutput : validOutputs) {
75
// Run with logging=trace on stdout so that we can verify the log configuration afterwards.
76
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:logging=trace",
77
"-Xlog:all=trace:" + logOutput,
78
"-version");
79
OutputAnalyzer output = new OutputAnalyzer(pb.start());
80
output.shouldHaveExitValue(0);
81
Asserts.assertTrue(file.exists());
82
file.deleteOnExit(); // Clean up after test
83
output.shouldMatch("\\[logging *\\].*" + baseName); // Expect to see the log output listed
84
}
85
86
// Test a bunch of invalid output specifications and ensure the VM fails with these
87
String[] invalidOutputs = new String[] {
88
quote,
89
quote + quote, // should fail because the VM will try to create a file without a name
90
quote + quote + quote,
91
quote + quote + quote + quote,
92
quote + quote + quote + quote + quote,
93
"prefix" + quote + quote + "suffix",
94
"prefix" + quote + quote,
95
quote + quote + "suffix",
96
quote + "A" + quote + quote + "B" + quote,
97
quote + "A" + quote + "B" + quote + "C" + quote,
98
"A" + quote + quote + "B"
99
};
100
for (String logOutput : invalidOutputs) {
101
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:logging=trace",
102
"-Xlog:all=trace:" + logOutput,
103
"-version");
104
OutputAnalyzer output = new OutputAnalyzer(pb.start());
105
output.shouldHaveExitValue(1);
106
// Ensure error message was logged
107
output.shouldMatch("([Mm]issing terminating quote)"
108
+ "|(Error opening log file '')"
109
+ "|(Output name can not be partially quoted)");
110
}
111
}
112
}
113
114
115