blob: 8ecd8fd6897a4e7cc1f870697aef13eff419cbae [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
123std::string make_log_pattern(android::base::LogSeverity severity,
124 const char* message) {
125 static const char* log_characters = "VDIWEF";
126 char log_char = log_characters[severity];
Spencer Lowbdab59a2015-08-11 16:00:13 -0700127 std::string holder(__FILE__);
Dan Albert58310b42015-03-13 23:06:01 -0700128 return android::base::StringPrintf(
Andreas Gampe7c1cdd72016-09-07 11:31:37 -0700129 "%c[[:space:]]+"
130 "[[:digit:]]+-[[:digit:]]+[[:space:]]+"
131 "[[:digit:]]+:[[:digit:]]+:[[:digit:]]+[[:space:]]+"
132 "[[:digit:]]+[[:space:]]+"
133 "[[:digit:]]+[[:space:]]+"
134 "%s:[[:digit:]]+] %s",
Spencer Lowbdab59a2015-08-11 16:00:13 -0700135 log_char, basename(&holder[0]), message);
Dan Albert58310b42015-03-13 23:06:01 -0700136}
137
Dan Albertb547c852015-03-27 11:20:14 -0700138TEST(logging, LOG) {
Spencer Low765ae6b2015-09-17 19:36:10 -0700139 ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
Dan Albert58310b42015-03-13 23:06:01 -0700140
Dan Albert5c190402015-04-29 11:32:23 -0700141 // We can't usefully check the output of any of these on Windows because we
142 // don't have std::regex, but we can at least make sure we printed at least as
143 // many characters are in the log message.
Dan Albert58310b42015-03-13 23:06:01 -0700144 {
145 CapturedStderr cap;
146 LOG(WARNING) << "foobar";
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700147 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Dan Albert58310b42015-03-13 23:06:01 -0700148
149 std::string output;
150 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert5c190402015-04-29 11:32:23 -0700151 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700152
Dan Albert5c190402015-04-29 11:32:23 -0700153#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700154 std::regex message_regex(
155 make_log_pattern(android::base::WARNING, "foobar"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700156 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700157#endif
Dan Albert58310b42015-03-13 23:06:01 -0700158 }
159
160 {
161 CapturedStderr cap;
162 LOG(INFO) << "foobar";
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700163 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Dan Albert58310b42015-03-13 23:06:01 -0700164
165 std::string output;
166 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert5c190402015-04-29 11:32:23 -0700167 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700168
Dan Albert5c190402015-04-29 11:32:23 -0700169#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700170 std::regex message_regex(
171 make_log_pattern(android::base::INFO, "foobar"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700172 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700173#endif
Dan Albert58310b42015-03-13 23:06:01 -0700174 }
175
176 {
177 CapturedStderr cap;
178 LOG(DEBUG) << "foobar";
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700179 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Dan Albert58310b42015-03-13 23:06:01 -0700180
181 std::string output;
182 android::base::ReadFdToString(cap.fd(), &output);
183 ASSERT_TRUE(output.empty());
184 }
185
186 {
187 android::base::ScopedLogSeverity severity(android::base::DEBUG);
188 CapturedStderr cap;
189 LOG(DEBUG) << "foobar";
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700190 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Dan Albert58310b42015-03-13 23:06:01 -0700191
192 std::string output;
193 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert5c190402015-04-29 11:32:23 -0700194 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700195
Dan Albert5c190402015-04-29 11:32:23 -0700196#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700197 std::regex message_regex(
198 make_log_pattern(android::base::DEBUG, "foobar"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700199 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700200#endif
Dan Albert58310b42015-03-13 23:06:01 -0700201 }
Spencer Low765ae6b2015-09-17 19:36:10 -0700202
203 // Test whether LOG() saves and restores errno.
204 {
205 CapturedStderr cap;
206 errno = 12345;
207 LOG(INFO) << (errno = 67890);
208 EXPECT_EQ(12345, errno) << "errno was not restored";
209
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700210 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Spencer Low765ae6b2015-09-17 19:36:10 -0700211
212 std::string output;
213 android::base::ReadFdToString(cap.fd(), &output);
214 EXPECT_NE(nullptr, strstr(output.c_str(), "67890")) << output;
215
216#if !defined(_WIN32)
217 std::regex message_regex(
218 make_log_pattern(android::base::INFO, "67890"));
219 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
220#endif
221 }
222
223 // Test whether LOG() has a dangling if with no else.
224 {
225 CapturedStderr cap;
226
227 // Do the test two ways: once where we hypothesize that LOG()'s if
228 // will evaluate to true (when severity is high enough) and once when we
229 // expect it to evaluate to false (when severity is not high enough).
230 bool flag = false;
231 if (true)
232 LOG(INFO) << "foobar";
233 else
234 flag = true;
235
236 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
237
238 flag = false;
239 if (true)
240 LOG(VERBOSE) << "foobar";
241 else
242 flag = true;
243
244 EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
245 }
Dan Albert58310b42015-03-13 23:06:01 -0700246}
247
Dan Albertb547c852015-03-27 11:20:14 -0700248TEST(logging, PLOG) {
Dan Albert58310b42015-03-13 23:06:01 -0700249 {
250 CapturedStderr cap;
251 errno = ENOENT;
252 PLOG(INFO) << "foobar";
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700253 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Dan Albert58310b42015-03-13 23:06:01 -0700254
255 std::string output;
256 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert5c190402015-04-29 11:32:23 -0700257 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700258
Dan Albert5c190402015-04-29 11:32:23 -0700259#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700260 std::regex message_regex(make_log_pattern(
261 android::base::INFO, "foobar: No such file or directory"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700262 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700263#endif
Dan Albert58310b42015-03-13 23:06:01 -0700264 }
265}
266
Dan Albertb547c852015-03-27 11:20:14 -0700267TEST(logging, UNIMPLEMENTED) {
Dan Albert58310b42015-03-13 23:06:01 -0700268 {
269 CapturedStderr cap;
270 errno = ENOENT;
271 UNIMPLEMENTED(ERROR);
Elliott Hughesc96de0d2015-10-20 13:18:22 -0700272 ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
Dan Albert58310b42015-03-13 23:06:01 -0700273
274 std::string output;
275 android::base::ReadFdToString(cap.fd(), &output);
Dan Albert5c190402015-04-29 11:32:23 -0700276 ASSERT_GT(output.length(), strlen("unimplemented"));
Dan Albert58310b42015-03-13 23:06:01 -0700277
Dan Albert5c190402015-04-29 11:32:23 -0700278#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700279 std::string expected_message =
280 android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);
281 std::regex message_regex(
282 make_log_pattern(android::base::ERROR, expected_message.c_str()));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700283 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700284#endif
Dan Albert58310b42015-03-13 23:06:01 -0700285 }
286}