blob: 3fde302a3ccdf669ae4eb8557691b9c6be100c6d [file] [log] [blame]
Dan Albert58310b42015-03-13 23:06:01 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes4f713192015-12-04 22:00:26 -080017#include "android-base/logging.h"
Dan Albert58310b42015-03-13 23:06:01 -070018
Elliott Hughes2e5ae002015-08-13 20:09:29 -070019#include <libgen.h>
20
Spencer Low765ae6b2015-09-17 19:36:10 -070021#if defined(_WIN32)
22#include <signal.h>
23#endif
24
Dan Albert58310b42015-03-13 23:06:01 -070025#include <regex>
26#include <string>
27
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include "android-base/file.h"
29#include "android-base/stringprintf.h"
30#include "android-base/test_utils.h"
Dan Albert58310b42015-03-13 23:06:01 -070031
32#include <gtest/gtest.h>
33
34#ifdef __ANDROID__
35#define HOST_TEST(suite, name) TEST(suite, DISABLED_ ## name)
36#else
37#define HOST_TEST(suite, name) TEST(suite, name)
38#endif
39
40class CapturedStderr {
41 public:
42 CapturedStderr() : old_stderr_(-1) {
43 init();
44 }
45
46 ~CapturedStderr() {
47 reset();
48 }
49
50 int fd() const {
51 return temp_file_.fd;
52 }
53
54 private:
55 void init() {
Spencer Low765ae6b2015-09-17 19:36:10 -070056#if defined(_WIN32)
57 // On Windows, stderr is often buffered, so make sure it is unbuffered so
58 // that we can immediately read back what was written to stderr.
59 ASSERT_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
60#endif
Dan Albert58310b42015-03-13 23:06:01 -070061 old_stderr_ = dup(STDERR_FILENO);
62 ASSERT_NE(-1, old_stderr_);
63 ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
64 }
65
66 void reset() {
67 ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
68 ASSERT_EQ(0, close(old_stderr_));
Spencer Low765ae6b2015-09-17 19:36:10 -070069 // Note: cannot restore prior setvbuf() setting.
Dan Albert58310b42015-03-13 23:06:01 -070070 }
71
72 TemporaryFile temp_file_;
73 int old_stderr_;
74};
75
Spencer Low765ae6b2015-09-17 19:36:10 -070076#if defined(_WIN32)
77static void ExitSignalAbortHandler(int) {
78 _exit(3);
79}
80#endif
81
82static void SuppressAbortUI() {
83#if defined(_WIN32)
84 // We really just want to call _set_abort_behavior(0, _CALL_REPORTFAULT) to
85 // suppress the Windows Error Reporting dialog box, but that API is not
86 // available in the OS-supplied C Runtime, msvcrt.dll, that we currently
87 // use (it is available in the Visual Studio C runtime).
88 //
89 // Instead, we setup a SIGABRT handler, which is called in abort() right
90 // before calling Windows Error Reporting. In the handler, we exit the
91 // process just like abort() does.
92 ASSERT_NE(SIG_ERR, signal(SIGABRT, ExitSignalAbortHandler));
93#endif
94}
95
Dan Albertb547c852015-03-27 11:20:14 -070096TEST(logging, CHECK) {
Spencer Low765ae6b2015-09-17 19:36:10 -070097 ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false ");
Dan Albert58310b42015-03-13 23:06:01 -070098 CHECK(true);
99
Spencer Low765ae6b2015-09-17 19:36:10 -0700100 ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 ");
Dan Albert58310b42015-03-13 23:06:01 -0700101 CHECK_EQ(0, 0);
102
Spencer Low765ae6b2015-09-17 19:36:10 -0700103 ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");},
104 R"(Check failed: "foo" == "bar")");
Dan Albert58310b42015-03-13 23:06:01 -0700105 CHECK_STREQ("foo", "foo");
Spencer Low765ae6b2015-09-17 19:36:10 -0700106
107 // Test whether CHECK() and CHECK_STREQ() have a dangling if with no else.
108 bool flag = false;
109 if (true)
110 CHECK(true);
111 else
112 flag = true;
113 EXPECT_FALSE(flag) << "CHECK macro probably has a dangling if with no else";
114
115 flag = false;
116 if (true)
117 CHECK_STREQ("foo", "foo");
118 else
119 flag = true;
120 EXPECT_FALSE(flag) << "CHECK_STREQ probably has a dangling if with no else";
Dan Albert58310b42015-03-13 23:06:01 -0700121}
122
Elliott Hughes13d78e42016-09-07 16:22:40 -0700123static std::string make_log_pattern(android::base::LogSeverity severity,
124 const char* message) {
Andreas Gampe550829d2016-09-07 10:10:50 -0700125 static const char log_characters[] = "VDIWEFF";
126 static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1,
127 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albert58310b42015-03-13 23:06:01 -0700128 char log_char = log_characters[severity];
Spencer Lowbdab59a2015-08-11 16:00:13 -0700129 std::string holder(__FILE__);
Dan Albert58310b42015-03-13 23:06:01 -0700130 return android::base::StringPrintf(
Elliott Hughes13d78e42016-09-07 16:22:40 -0700131 "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s",
Spencer Lowbdab59a2015-08-11 16:00:13 -0700132 log_char, basename(&holder[0]), message);
Dan Albert58310b42015-03-13 23:06:01 -0700133}
134
Elliott Hughes13d78e42016-09-07 16:22:40 -0700135#define CHECK_LOG_DISABLED(severity) \
136 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
137 CapturedStderr cap1; \
138 LOG(severity) << "foo bar"; \
139 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
140
141#define CHECK_LOG_ENABLED(severity) \
142 android::base::ScopedLogSeverity sls2(android::base::severity); \
143 CapturedStderr cap2; \
144 LOG(severity) << "foobar"; \
145 CheckMessage(cap2, android::base::severity, "foobar"); \
146
147static void CheckMessage(const CapturedStderr& cap,
148 android::base::LogSeverity severity, const char* expected) {
149 std::string output;
150 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
151 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert58310b42015-03-13 23:06:01 -0700152
Dan Albert5c190402015-04-29 11:32:23 -0700153 // We can't usefully check the output of any of these on Windows because we
154 // don't have std::regex, but we can at least make sure we printed at least as
155 // many characters are in the log message.
Elliott Hughes13d78e42016-09-07 16:22:40 -0700156 ASSERT_GT(output.length(), strlen(expected));
157 ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
Dan Albert58310b42015-03-13 23:06:01 -0700158
Dan Albert5c190402015-04-29 11:32:23 -0700159#if !defined(_WIN32)
Elliott Hughes13d78e42016-09-07 16:22:40 -0700160 std::regex message_regex(make_log_pattern(severity, expected));
161 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700162#endif
Dan Albert58310b42015-03-13 23:06:01 -0700163}
164
Elliott Hughes13d78e42016-09-07 16:22:40 -0700165TEST(logging, LOG_FATAL) {
166 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
167}
Dan Albert58310b42015-03-13 23:06:01 -0700168
Andreas Gampe550829d2016-09-07 10:10:50 -0700169TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
170 CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
171}
172
173TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
174 CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT);
175}
176
Elliott Hughes13d78e42016-09-07 16:22:40 -0700177TEST(logging, LOG_ERROR_disabled) {
178 CHECK_LOG_DISABLED(ERROR);
179}
Dan Albert58310b42015-03-13 23:06:01 -0700180
Elliott Hughes13d78e42016-09-07 16:22:40 -0700181TEST(logging, LOG_ERROR_enabled) {
182 CHECK_LOG_ENABLED(ERROR);
183}
184
185TEST(logging, LOG_WARNING_disabled) {
186 CHECK_LOG_DISABLED(WARNING);
187}
188
189TEST(logging, LOG_WARNING_enabled) {
190 CHECK_LOG_ENABLED(WARNING);
191}
192
193TEST(logging, LOG_INFO_disabled) {
194 CHECK_LOG_DISABLED(INFO);
195}
196
197TEST(logging, LOG_INFO_enabled) {
198 CHECK_LOG_ENABLED(INFO);
199}
200
201TEST(logging, LOG_DEBUG_disabled) {
202 CHECK_LOG_DISABLED(DEBUG);
203}
204
205TEST(logging, LOG_DEBUG_enabled) {
206 CHECK_LOG_ENABLED(DEBUG);
207}
208
209TEST(logging, LOG_VERBOSE_disabled) {
210 CHECK_LOG_DISABLED(VERBOSE);
211}
212
213TEST(logging, LOG_VERBOSE_enabled) {
214 CHECK_LOG_ENABLED(VERBOSE);
215}
216
217TEST(logging, LOG_does_not_clobber_errno) {
218 CapturedStderr cap;
219 errno = 12345;
220 LOG(INFO) << (errno = 67890);
221 EXPECT_EQ(12345, errno) << "errno was not restored";
222
223 CheckMessage(cap, android::base::INFO, "67890");
224}
225
226TEST(logging, PLOG_does_not_clobber_errno) {
227 CapturedStderr cap;
228 errno = 12345;
229 PLOG(INFO) << (errno = 67890);
230 EXPECT_EQ(12345, errno) << "errno was not restored";
231
232 CheckMessage(cap, android::base::INFO, "67890");
233}
234
235TEST(logging, LOG_does_not_have_dangling_if) {
236 CapturedStderr cap; // So the logging below has no side-effects.
237
238 // Do the test two ways: once where we hypothesize that LOG()'s if
239 // will evaluate to true (when severity is high enough) and once when we
240 // expect it to evaluate to false (when severity is not high enough).
241 bool flag = false;
242 if (true)
243 LOG(INFO) << "foobar";
244 else
245 flag = true;
246
247 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
248
249 flag = false;
250 if (true)
251 LOG(VERBOSE) << "foobar";
252 else
253 flag = true;
254
255 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
256}
257
258#define CHECK_PLOG(severity) \
259
260#define CHECK_PLOG_DISABLED(severity) \
261 android::base::ScopedLogSeverity sls1(android::base::FATAL); \
262 CapturedStderr cap1; \
263 PLOG(severity) << "foo bar"; \
264 ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
265
266#define CHECK_PLOG_ENABLED(severity) \
267 android::base::ScopedLogSeverity sls2(android::base::severity); \
268 CapturedStderr cap2; \
269 errno = ENOENT; \
270 PLOG(severity) << "foobar"; \
271 CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
272
Andreas Gampe550829d2016-09-07 10:10:50 -0700273TEST(logging, PLOG_FATAL) {
274 ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
275}
276
277TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
278 CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
279}
280
281TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
282 CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT);
283}
284
Elliott Hughes13d78e42016-09-07 16:22:40 -0700285TEST(logging, PLOG_ERROR_disabled) {
286 CHECK_PLOG_DISABLED(ERROR);
287}
288
289TEST(logging, PLOG_ERROR_enabled) {
290 CHECK_PLOG_ENABLED(ERROR);
291}
292
293TEST(logging, PLOG_WARNING_disabled) {
294 CHECK_PLOG_DISABLED(WARNING);
295}
296
297TEST(logging, PLOG_WARNING_enabled) {
298 CHECK_PLOG_ENABLED(WARNING);
299}
300
301TEST(logging, PLOG_INFO_disabled) {
302 CHECK_PLOG_DISABLED(INFO);
303}
304
305TEST(logging, PLOG_INFO_enabled) {
306 CHECK_PLOG_ENABLED(INFO);
307}
308
309TEST(logging, PLOG_DEBUG_disabled) {
310 CHECK_PLOG_DISABLED(DEBUG);
311}
312
313TEST(logging, PLOG_DEBUG_enabled) {
314 CHECK_PLOG_ENABLED(DEBUG);
315}
316
317TEST(logging, PLOG_VERBOSE_disabled) {
318 CHECK_PLOG_DISABLED(VERBOSE);
319}
320
321TEST(logging, PLOG_VERBOSE_enabled) {
322 CHECK_PLOG_ENABLED(VERBOSE);
Dan Albert58310b42015-03-13 23:06:01 -0700323}
324
Dan Albertb547c852015-03-27 11:20:14 -0700325TEST(logging, UNIMPLEMENTED) {
Elliott Hughes13d78e42016-09-07 16:22:40 -0700326 std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
Dan Albert58310b42015-03-13 23:06:01 -0700327
Elliott Hughes13d78e42016-09-07 16:22:40 -0700328 CapturedStderr cap;
329 errno = ENOENT;
330 UNIMPLEMENTED(ERROR);
331 CheckMessage(cap, android::base::ERROR, expected.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700332}