author | coleenp |
Tue, 31 Oct 2017 11:55:09 -0400 | |
changeset 47765 | b7c7428eaab9 |
parent 47216 | 71c04702a3d5 |
child 53845 | 1807da9ad196 |
permissions | -rw-r--r-- |
40656 | 1 |
/* |
46560
388aa8d67c80
8181449: Fix debug.hpp / globalDefinitions.hpp dependency inversion
kbarrett
parents:
42909
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
40656 | 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 |
*/ |
|
41705
332239c052cc
8165687: Fix license and copyright headers in jd9 under hotspot/test
stsmirno
parents:
41678
diff
changeset
|
23 |
|
40656 | 24 |
#include "precompiled.hpp" |
47765
b7c7428eaab9
8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents:
47216
diff
changeset
|
25 |
#include "jvm.h" |
41678
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
26 |
#include "logTestUtils.inline.hpp" |
40656 | 27 |
#include "logging/logFileOutput.hpp" |
28 |
#include "memory/resourceArea.hpp" |
|
29 |
#include "runtime/os.hpp" |
|
30 |
#include "unittest.hpp" |
|
31 |
#include "utilities/globalDefinitions.hpp" |
|
32 |
#include "utilities/ostream.hpp" |
|
33 |
||
40902
395e1f3ec886
8157948: UL allows same log file with multiple file=
mlarsson
parents:
40656
diff
changeset
|
34 |
static const char* name = "file=testlog.pid%p.%t.log"; |
40656 | 35 |
|
36 |
// Test parsing a bunch of valid file output options |
|
41671
9e0c6db4918a
8166925: several native TESTs should be changed to TEST_VM
iignatyev
parents:
40902
diff
changeset
|
37 |
TEST_VM(LogFileOutput, parse_valid) { |
40656 | 38 |
const char* valid_options[] = { |
39 |
"", "filecount=10", "filesize=512", |
|
40 |
"filecount=11,filesize=256", |
|
41 |
"filesize=256,filecount=11", |
|
42 |
"filesize=0", "filecount=1", |
|
43 |
"filesize=1m", "filesize=1M", |
|
44 |
"filesize=1k", "filesize=1G" |
|
45 |
}; |
|
46 |
||
47 |
// Override LogOutput's vm_start time to get predictable file name |
|
48 |
LogFileOutput::set_file_name_parameters(0); |
|
49 |
char expected_filename[1 * K]; |
|
50 |
int ret = jio_snprintf(expected_filename, sizeof(expected_filename), |
|
51 |
"testlog.pid%d.1970-01-01_01-00-00.log", |
|
52 |
os::current_process_id()); |
|
53 |
ASSERT_GT(ret, 0) << "Buffer too small"; |
|
54 |
||
55 |
for (size_t i = 0; i < ARRAY_SIZE(valid_options); i++) { |
|
56 |
ResourceMark rm; |
|
57 |
stringStream ss; |
|
58 |
{ |
|
59 |
LogFileOutput fo(name); |
|
60 |
EXPECT_STREQ(name, fo.name()); |
|
61 |
EXPECT_TRUE(fo.initialize(valid_options[i], &ss)) |
|
62 |
<< "Did not accept valid option(s) '" << valid_options[i] << "': " << ss.as_string(); |
|
63 |
} |
|
64 |
remove(expected_filename); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
// Test parsing a bunch of invalid file output options |
|
41671
9e0c6db4918a
8166925: several native TESTs should be changed to TEST_VM
iignatyev
parents:
40902
diff
changeset
|
69 |
TEST_VM(LogFileOutput, parse_invalid) { |
40656 | 70 |
const char* invalid_options[] = { |
71 |
"invalidopt", "filecount=", |
|
72 |
"filesize=,filecount=10", |
|
73 |
"fileco=10", "ilesize=512", |
|
74 |
"filecount=11,,filesize=256", |
|
75 |
",filesize=256,filecount=11", |
|
76 |
"filesize=256,filecount=11,", |
|
77 |
"filesize=-1", "filecount=0.1", |
|
78 |
"filecount=-2", "filecount=2.0", |
|
79 |
"filecount= 2", "filesize=2 ", |
|
80 |
"filecount=ab", "filesize=0xz", |
|
81 |
"filecount=1MB", "filesize=99bytes", |
|
82 |
"filesize=9999999999999999999999999" |
|
83 |
"filecount=9999999999999999999999999" |
|
84 |
}; |
|
85 |
||
86 |
for (size_t i = 0; i < ARRAY_SIZE(invalid_options); i++) { |
|
87 |
ResourceMark rm; |
|
88 |
stringStream ss; |
|
89 |
LogFileOutput fo(name); |
|
90 |
EXPECT_FALSE(fo.initialize(invalid_options[i], &ss)) |
|
91 |
<< "Accepted invalid option(s) '" << invalid_options[i] << "': " << ss.as_string(); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
// Test for overflows with filesize |
|
41671
9e0c6db4918a
8166925: several native TESTs should be changed to TEST_VM
iignatyev
parents:
40902
diff
changeset
|
96 |
TEST_VM(LogFileOutput, filesize_overflow) { |
40656 | 97 |
char buf[256]; |
98 |
int ret = jio_snprintf(buf, sizeof(buf), "filesize=" SIZE_FORMAT "K", SIZE_MAX); |
|
99 |
ASSERT_GT(ret, 0) << "Buffer too small"; |
|
100 |
||
101 |
ResourceMark rm; |
|
102 |
stringStream ss; |
|
103 |
LogFileOutput fo(name); |
|
104 |
EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows"; |
|
105 |
} |
|
41678
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
106 |
|
42909
e440bfad5d9a
8170936: Logging: LogFileOutput.invalid_file_test crashes when executed twice.
kzhaldyb
parents:
41705
diff
changeset
|
107 |
TEST_VM(LogFileOutput, startup_rotation) { |
41678
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
108 |
const size_t rotations = 5; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
109 |
const char* filename = "start-rotate-test"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
110 |
char* rotated_file[rotations]; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
111 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
112 |
ResourceMark rm; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
113 |
for (size_t i = 0; i < rotations; i++) { |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
114 |
size_t len = strlen(filename) + 3; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
115 |
rotated_file[i] = NEW_RESOURCE_ARRAY(char, len); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
116 |
int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
117 |
ASSERT_NE(-1, ret); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
118 |
delete_file(rotated_file[i]); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
119 |
} |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
120 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
121 |
delete_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
122 |
init_log_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
123 |
ASSERT_TRUE(file_exists(filename)) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
124 |
<< "configured logging to file '" << filename << "' but file was not found"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
125 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
126 |
// Initialize the same file a bunch more times to trigger rotations |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
127 |
for (size_t i = 0; i < rotations; i++) { |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
128 |
init_log_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
129 |
EXPECT_TRUE(file_exists(rotated_file[i])); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
130 |
} |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
131 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
132 |
// Remove a file and expect its slot to be re-used |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
133 |
delete_file(rotated_file[1]); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
134 |
init_log_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
135 |
EXPECT_TRUE(file_exists(rotated_file[1])); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
136 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
137 |
// Clean up after test |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
138 |
delete_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
139 |
for (size_t i = 0; i < rotations; i++) { |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
140 |
delete_file(rotated_file[i]); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
141 |
} |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
142 |
} |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
143 |
|
42909
e440bfad5d9a
8170936: Logging: LogFileOutput.invalid_file_test crashes when executed twice.
kzhaldyb
parents:
41705
diff
changeset
|
144 |
TEST_VM(LogFileOutput, startup_truncation) { |
41678
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
145 |
const char* filename = "start-truncate-test"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
146 |
const char* archived_filename = "start-truncate-test.0"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
147 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
148 |
delete_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
149 |
delete_file(archived_filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
150 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
151 |
// Use the same log file twice and expect it to be overwritten/truncated |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
152 |
init_log_file(filename, "filecount=0"); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
153 |
ASSERT_TRUE(file_exists(filename)) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
154 |
<< "configured logging to file '" << filename << "' but file was not found"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
155 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
156 |
init_log_file(filename, "filecount=0"); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
157 |
ASSERT_TRUE(file_exists(filename)) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
158 |
<< "configured logging to file '" << filename << "' but file was not found"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
159 |
EXPECT_FALSE(file_exists(archived_filename)) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
160 |
<< "existing log file was not properly truncated when filecount was 0"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
161 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
162 |
// Verify that the file was really truncated and not just appended |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
163 |
EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL)); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
164 |
const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL }; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
165 |
EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated)) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
166 |
<< "log file " << filename << " appended rather than truncated"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
167 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
168 |
delete_file(filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
169 |
delete_file(archived_filename); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
170 |
} |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
171 |
|
42909
e440bfad5d9a
8170936: Logging: LogFileOutput.invalid_file_test crashes when executed twice.
kzhaldyb
parents:
41705
diff
changeset
|
172 |
TEST_VM(LogFileOutput, invalid_file) { |
41678
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
173 |
ResourceMark rm; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
174 |
stringStream ss; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
175 |
|
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
176 |
// Attempt to log to a directory (existing log not a regular file) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
177 |
create_directory("tmplogdir"); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
178 |
LogFileOutput bad_file("file=tmplogdir"); |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
179 |
EXPECT_FALSE(bad_file.initialize("", &ss)) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
180 |
<< "file was initialized when there was an existing directory with the same name"; |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
181 |
EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file")) |
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
182 |
<< "missing expected error message, received msg: %s" << ss.as_string(); |
42909
e440bfad5d9a
8170936: Logging: LogFileOutput.invalid_file_test crashes when executed twice.
kzhaldyb
parents:
41705
diff
changeset
|
183 |
delete_empty_directory("tmplogdir"); |
41678
d51adf949bcd
8165702: Convert LogFileOutput internal tests to GTest
mlarsson
parents:
41671
diff
changeset
|
184 |
} |