86 ResourceMark rm;
87 stringStream ss;
88 LogFileOutput fo(name);
89 EXPECT_FALSE(fo.initialize(invalid_options[i], &ss))
90 << "Accepted invalid option(s) '" << invalid_options[i] << "': " << ss.as_string();
91 }
92 }
93
94 // Test for overflows with filesize
95 TEST_VM(LogFileOutput, filesize_overflow) {
96 char buf[256];
97 int ret = jio_snprintf(buf, sizeof(buf), "filesize=" SIZE_FORMAT "K", SIZE_MAX);
98 ASSERT_GT(ret, 0) << "Buffer too small";
99
100 ResourceMark rm;
101 stringStream ss;
102 LogFileOutput fo(name);
103 EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";
104 }
105
106 TEST(LogFileOutput, startup_rotation) {
107 const size_t rotations = 5;
108 const char* filename = "start-rotate-test";
109 char* rotated_file[rotations];
110
111 ResourceMark rm;
112 for (size_t i = 0; i < rotations; i++) {
113 size_t len = strlen(filename) + 3;
114 rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
115 int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
116 ASSERT_NE(-1, ret);
117 delete_file(rotated_file[i]);
118 }
119
120 delete_file(filename);
121 init_log_file(filename);
122 ASSERT_TRUE(file_exists(filename))
123 << "configured logging to file '" << filename << "' but file was not found";
124
125 // Initialize the same file a bunch more times to trigger rotations
126 for (size_t i = 0; i < rotations; i++) {
127 init_log_file(filename);
128 EXPECT_TRUE(file_exists(rotated_file[i]));
129 }
130
131 // Remove a file and expect its slot to be re-used
132 delete_file(rotated_file[1]);
133 init_log_file(filename);
134 EXPECT_TRUE(file_exists(rotated_file[1]));
135
136 // Clean up after test
137 delete_file(filename);
138 for (size_t i = 0; i < rotations; i++) {
139 delete_file(rotated_file[i]);
140 }
141 }
142
143 TEST(LogFileOutput, startup_truncation) {
144 const char* filename = "start-truncate-test";
145 const char* archived_filename = "start-truncate-test.0";
146
147 delete_file(filename);
148 delete_file(archived_filename);
149
150 // Use the same log file twice and expect it to be overwritten/truncated
151 init_log_file(filename, "filecount=0");
152 ASSERT_TRUE(file_exists(filename))
153 << "configured logging to file '" << filename << "' but file was not found";
154
155 init_log_file(filename, "filecount=0");
156 ASSERT_TRUE(file_exists(filename))
157 << "configured logging to file '" << filename << "' but file was not found";
158 EXPECT_FALSE(file_exists(archived_filename))
159 << "existing log file was not properly truncated when filecount was 0";
160
161 // Verify that the file was really truncated and not just appended
162 EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL));
163 const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL };
164 EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated))
165 << "log file " << filename << " appended rather than truncated";
166
167 delete_file(filename);
168 delete_file(archived_filename);
169 }
170
171 TEST(LogFileOutput, invalid_file) {
172 ResourceMark rm;
173 stringStream ss;
174
175 // Attempt to log to a directory (existing log not a regular file)
176 create_directory("tmplogdir");
177 LogFileOutput bad_file("file=tmplogdir");
178 EXPECT_FALSE(bad_file.initialize("", &ss))
179 << "file was initialized when there was an existing directory with the same name";
180 EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file"))
181 << "missing expected error message, received msg: %s" << ss.as_string();
182 remove("tmplogdir");
183 }
|
86 ResourceMark rm;
87 stringStream ss;
88 LogFileOutput fo(name);
89 EXPECT_FALSE(fo.initialize(invalid_options[i], &ss))
90 << "Accepted invalid option(s) '" << invalid_options[i] << "': " << ss.as_string();
91 }
92 }
93
94 // Test for overflows with filesize
95 TEST_VM(LogFileOutput, filesize_overflow) {
96 char buf[256];
97 int ret = jio_snprintf(buf, sizeof(buf), "filesize=" SIZE_FORMAT "K", SIZE_MAX);
98 ASSERT_GT(ret, 0) << "Buffer too small";
99
100 ResourceMark rm;
101 stringStream ss;
102 LogFileOutput fo(name);
103 EXPECT_FALSE(fo.initialize(buf, &ss)) << "Accepted filesize that overflows";
104 }
105
106 TEST_VM(LogFileOutput, startup_rotation) {
107 const size_t rotations = 5;
108 const char* filename = "start-rotate-test";
109 char* rotated_file[rotations];
110
111 ResourceMark rm;
112 for (size_t i = 0; i < rotations; i++) {
113 size_t len = strlen(filename) + 3;
114 rotated_file[i] = NEW_RESOURCE_ARRAY(char, len);
115 int ret = jio_snprintf(rotated_file[i], len, "%s." SIZE_FORMAT, filename, i);
116 ASSERT_NE(-1, ret);
117 delete_file(rotated_file[i]);
118 }
119
120 delete_file(filename);
121 init_log_file(filename);
122 ASSERT_TRUE(file_exists(filename))
123 << "configured logging to file '" << filename << "' but file was not found";
124
125 // Initialize the same file a bunch more times to trigger rotations
126 for (size_t i = 0; i < rotations; i++) {
127 init_log_file(filename);
128 EXPECT_TRUE(file_exists(rotated_file[i]));
129 }
130
131 // Remove a file and expect its slot to be re-used
132 delete_file(rotated_file[1]);
133 init_log_file(filename);
134 EXPECT_TRUE(file_exists(rotated_file[1]));
135
136 // Clean up after test
137 delete_file(filename);
138 for (size_t i = 0; i < rotations; i++) {
139 delete_file(rotated_file[i]);
140 }
141 }
142
143 TEST_VM(LogFileOutput, startup_truncation) {
144 const char* filename = "start-truncate-test";
145 const char* archived_filename = "start-truncate-test.0";
146
147 delete_file(filename);
148 delete_file(archived_filename);
149
150 // Use the same log file twice and expect it to be overwritten/truncated
151 init_log_file(filename, "filecount=0");
152 ASSERT_TRUE(file_exists(filename))
153 << "configured logging to file '" << filename << "' but file was not found";
154
155 init_log_file(filename, "filecount=0");
156 ASSERT_TRUE(file_exists(filename))
157 << "configured logging to file '" << filename << "' but file was not found";
158 EXPECT_FALSE(file_exists(archived_filename))
159 << "existing log file was not properly truncated when filecount was 0";
160
161 // Verify that the file was really truncated and not just appended
162 EXPECT_TRUE(file_contains_substring(filename, LOG_TEST_STRING_LITERAL));
163 const char* repeated[] = { LOG_TEST_STRING_LITERAL, LOG_TEST_STRING_LITERAL };
164 EXPECT_FALSE(file_contains_substrings_in_order(filename, repeated))
165 << "log file " << filename << " appended rather than truncated";
166
167 delete_file(filename);
168 delete_file(archived_filename);
169 }
170
171 TEST_VM(LogFileOutput, invalid_file) {
172 ResourceMark rm;
173 stringStream ss;
174
175 // Attempt to log to a directory (existing log not a regular file)
176 create_directory("tmplogdir");
177 LogFileOutput bad_file("file=tmplogdir");
178 EXPECT_FALSE(bad_file.initialize("", &ss))
179 << "file was initialized when there was an existing directory with the same name";
180 EXPECT_TRUE(string_contains_substring(ss.as_string(), "tmplogdir is not a regular file"))
181 << "missing expected error message, received msg: %s" << ss.as_string();
182 delete_directory("tmplogdir");
183 }
|