Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/logging.h" |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 2e5ae00 | 2015-08-13 20:09:29 -0700 | [diff] [blame] | 19 | #include <libgen.h> |
| 20 | |
Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 21 | #if defined(_WIN32) |
| 22 | #include <signal.h> |
| 23 | #endif |
| 24 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 25 | #include <regex> |
| 26 | #include <string> |
| 27 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include "android-base/file.h" |
| 29 | #include "android-base/stringprintf.h" |
| 30 | #include "android-base/test_utils.h" |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 31 | |
| 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 | |
| 40 | class 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 Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 56 | #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 Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 61 | 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 Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 69 | // Note: cannot restore prior setvbuf() setting. |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TemporaryFile temp_file_; |
| 73 | int old_stderr_; |
| 74 | }; |
| 75 | |
Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 76 | #if defined(_WIN32) |
| 77 | static void ExitSignalAbortHandler(int) { |
| 78 | _exit(3); |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | static 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 Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 96 | TEST(logging, CHECK) { |
Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 97 | ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false "); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 98 | CHECK(true); |
| 99 | |
Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 100 | ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 "); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 101 | CHECK_EQ(0, 0); |
| 102 | |
Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 103 | ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");}, |
| 104 | R"(Check failed: "foo" == "bar")"); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 105 | CHECK_STREQ("foo", "foo"); |
Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 106 | |
| 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 Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Andreas Gampe | d8f26e2 | 2016-09-13 10:44:46 -0700 | [diff] [blame^] | 123 | TEST(logging, DCHECK) { |
| 124 | if (android::base::kEnableDChecks) { |
| 125 | ASSERT_DEATH({SuppressAbortUI(); DCHECK(false);}, "DCheck failed: false "); |
| 126 | } |
| 127 | DCHECK(true); |
| 128 | |
| 129 | if (android::base::kEnableDChecks) { |
| 130 | ASSERT_DEATH({SuppressAbortUI(); DCHECK_EQ(0, 1);}, "DCheck failed: 0 == 1 "); |
| 131 | } |
| 132 | DCHECK_EQ(0, 0); |
| 133 | |
| 134 | if (android::base::kEnableDChecks) { |
| 135 | ASSERT_DEATH({SuppressAbortUI(); DCHECK_STREQ("foo", "bar");}, |
| 136 | R"(DCheck failed: "foo" == "bar")"); |
| 137 | } |
| 138 | DCHECK_STREQ("foo", "foo"); |
| 139 | |
| 140 | // No testing whether we have a dangling else, possibly. That's inherent to the if (constexpr) |
| 141 | // setup we intentionally chose to force type-checks of debug code even in release builds (so |
| 142 | // we don't get more bit-rot). |
| 143 | } |
| 144 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 145 | static std::string make_log_pattern(android::base::LogSeverity severity, |
| 146 | const char* message) { |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 147 | static const char log_characters[] = "VDIWEFF"; |
| 148 | static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1, |
| 149 | "Mismatch in size of log_characters and values in LogSeverity"); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 150 | char log_char = log_characters[severity]; |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 151 | std::string holder(__FILE__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 152 | return android::base::StringPrintf( |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 153 | "%c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s:\\d+] %s", |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 154 | log_char, basename(&holder[0]), message); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 157 | #define CHECK_LOG_DISABLED(severity) \ |
| 158 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 159 | CapturedStderr cap1; \ |
| 160 | LOG(severity) << "foo bar"; \ |
| 161 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 162 | |
| 163 | #define CHECK_LOG_ENABLED(severity) \ |
| 164 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 165 | CapturedStderr cap2; \ |
| 166 | LOG(severity) << "foobar"; \ |
| 167 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 168 | |
| 169 | static void CheckMessage(const CapturedStderr& cap, |
| 170 | android::base::LogSeverity severity, const char* expected) { |
| 171 | std::string output; |
| 172 | ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); |
| 173 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 174 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 175 | // We can't usefully check the output of any of these on Windows because we |
| 176 | // don't have std::regex, but we can at least make sure we printed at least as |
| 177 | // many characters are in the log message. |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 178 | ASSERT_GT(output.length(), strlen(expected)); |
| 179 | ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output; |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 180 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 181 | #if !defined(_WIN32) |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 182 | std::regex message_regex(make_log_pattern(severity, expected)); |
| 183 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 184 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 187 | TEST(logging, LOG_FATAL) { |
| 188 | ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); |
| 189 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 190 | |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 191 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) { |
| 192 | CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 193 | } |
| 194 | |
| 195 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) { |
| 196 | CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 197 | } |
| 198 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 199 | TEST(logging, LOG_ERROR_disabled) { |
| 200 | CHECK_LOG_DISABLED(ERROR); |
| 201 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 202 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 203 | TEST(logging, LOG_ERROR_enabled) { |
| 204 | CHECK_LOG_ENABLED(ERROR); |
| 205 | } |
| 206 | |
| 207 | TEST(logging, LOG_WARNING_disabled) { |
| 208 | CHECK_LOG_DISABLED(WARNING); |
| 209 | } |
| 210 | |
| 211 | TEST(logging, LOG_WARNING_enabled) { |
| 212 | CHECK_LOG_ENABLED(WARNING); |
| 213 | } |
| 214 | |
| 215 | TEST(logging, LOG_INFO_disabled) { |
| 216 | CHECK_LOG_DISABLED(INFO); |
| 217 | } |
| 218 | |
| 219 | TEST(logging, LOG_INFO_enabled) { |
| 220 | CHECK_LOG_ENABLED(INFO); |
| 221 | } |
| 222 | |
| 223 | TEST(logging, LOG_DEBUG_disabled) { |
| 224 | CHECK_LOG_DISABLED(DEBUG); |
| 225 | } |
| 226 | |
| 227 | TEST(logging, LOG_DEBUG_enabled) { |
| 228 | CHECK_LOG_ENABLED(DEBUG); |
| 229 | } |
| 230 | |
| 231 | TEST(logging, LOG_VERBOSE_disabled) { |
| 232 | CHECK_LOG_DISABLED(VERBOSE); |
| 233 | } |
| 234 | |
| 235 | TEST(logging, LOG_VERBOSE_enabled) { |
| 236 | CHECK_LOG_ENABLED(VERBOSE); |
| 237 | } |
| 238 | |
| 239 | TEST(logging, LOG_does_not_clobber_errno) { |
| 240 | CapturedStderr cap; |
| 241 | errno = 12345; |
| 242 | LOG(INFO) << (errno = 67890); |
| 243 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 244 | |
| 245 | CheckMessage(cap, android::base::INFO, "67890"); |
| 246 | } |
| 247 | |
| 248 | TEST(logging, PLOG_does_not_clobber_errno) { |
| 249 | CapturedStderr cap; |
| 250 | errno = 12345; |
| 251 | PLOG(INFO) << (errno = 67890); |
| 252 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 253 | |
| 254 | CheckMessage(cap, android::base::INFO, "67890"); |
| 255 | } |
| 256 | |
| 257 | TEST(logging, LOG_does_not_have_dangling_if) { |
| 258 | CapturedStderr cap; // So the logging below has no side-effects. |
| 259 | |
| 260 | // Do the test two ways: once where we hypothesize that LOG()'s if |
| 261 | // will evaluate to true (when severity is high enough) and once when we |
| 262 | // expect it to evaluate to false (when severity is not high enough). |
| 263 | bool flag = false; |
| 264 | if (true) |
| 265 | LOG(INFO) << "foobar"; |
| 266 | else |
| 267 | flag = true; |
| 268 | |
| 269 | EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; |
| 270 | |
| 271 | flag = false; |
| 272 | if (true) |
| 273 | LOG(VERBOSE) << "foobar"; |
| 274 | else |
| 275 | flag = true; |
| 276 | |
| 277 | EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; |
| 278 | } |
| 279 | |
| 280 | #define CHECK_PLOG(severity) \ |
| 281 | |
| 282 | #define CHECK_PLOG_DISABLED(severity) \ |
| 283 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 284 | CapturedStderr cap1; \ |
| 285 | PLOG(severity) << "foo bar"; \ |
| 286 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 287 | |
| 288 | #define CHECK_PLOG_ENABLED(severity) \ |
| 289 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 290 | CapturedStderr cap2; \ |
| 291 | errno = ENOENT; \ |
| 292 | PLOG(severity) << "foobar"; \ |
| 293 | CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ |
| 294 | |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 295 | TEST(logging, PLOG_FATAL) { |
| 296 | ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar"); |
| 297 | } |
| 298 | |
| 299 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) { |
| 300 | CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 301 | } |
| 302 | |
| 303 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) { |
| 304 | CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 305 | } |
| 306 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 307 | TEST(logging, PLOG_ERROR_disabled) { |
| 308 | CHECK_PLOG_DISABLED(ERROR); |
| 309 | } |
| 310 | |
| 311 | TEST(logging, PLOG_ERROR_enabled) { |
| 312 | CHECK_PLOG_ENABLED(ERROR); |
| 313 | } |
| 314 | |
| 315 | TEST(logging, PLOG_WARNING_disabled) { |
| 316 | CHECK_PLOG_DISABLED(WARNING); |
| 317 | } |
| 318 | |
| 319 | TEST(logging, PLOG_WARNING_enabled) { |
| 320 | CHECK_PLOG_ENABLED(WARNING); |
| 321 | } |
| 322 | |
| 323 | TEST(logging, PLOG_INFO_disabled) { |
| 324 | CHECK_PLOG_DISABLED(INFO); |
| 325 | } |
| 326 | |
| 327 | TEST(logging, PLOG_INFO_enabled) { |
| 328 | CHECK_PLOG_ENABLED(INFO); |
| 329 | } |
| 330 | |
| 331 | TEST(logging, PLOG_DEBUG_disabled) { |
| 332 | CHECK_PLOG_DISABLED(DEBUG); |
| 333 | } |
| 334 | |
| 335 | TEST(logging, PLOG_DEBUG_enabled) { |
| 336 | CHECK_PLOG_ENABLED(DEBUG); |
| 337 | } |
| 338 | |
| 339 | TEST(logging, PLOG_VERBOSE_disabled) { |
| 340 | CHECK_PLOG_DISABLED(VERBOSE); |
| 341 | } |
| 342 | |
| 343 | TEST(logging, PLOG_VERBOSE_enabled) { |
| 344 | CHECK_PLOG_ENABLED(VERBOSE); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 347 | TEST(logging, UNIMPLEMENTED) { |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 348 | std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 349 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 350 | CapturedStderr cap; |
| 351 | errno = ENOENT; |
| 352 | UNIMPLEMENTED(ERROR); |
| 353 | CheckMessage(cap, android::base::ERROR, expected.c_str()); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 354 | } |
Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 355 | |
| 356 | static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) { |
| 357 | LOG(ERROR) << "called noop"; |
| 358 | } |
| 359 | |
| 360 | TEST(logging, LOG_FATAL_NOOP_ABORTER) { |
| 361 | { |
| 362 | android::base::SetAborter(NoopAborter); |
| 363 | |
| 364 | android::base::ScopedLogSeverity sls(android::base::ERROR); |
| 365 | CapturedStderr cap; |
| 366 | LOG(FATAL) << "foobar"; |
| 367 | CheckMessage(cap, android::base::FATAL, "foobar"); |
| 368 | CheckMessage(cap, android::base::ERROR, "called noop"); |
| 369 | |
| 370 | android::base::SetAborter(android::base::DefaultAborter); |
| 371 | } |
| 372 | |
| 373 | ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); |
| 374 | } |