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 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 145 | #define CHECK_WOULD_LOG_DISABLED(severity) \ |
| 146 | static_assert(android::base::severity < android::base::FATAL, "Bad input"); \ |
| 147 | for (size_t i = static_cast<size_t>(android::base::severity) + 1; \ |
| 148 | i <= static_cast<size_t>(android::base::FATAL); \ |
| 149 | ++i) { \ |
| 150 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 151 | EXPECT_FALSE(WOULD_LOG(severity)) << i; \ |
| 152 | } |
| 153 | |
| 154 | #define CHECK_WOULD_LOG_ENABLED(severity) \ |
| 155 | for (size_t i = static_cast<size_t>(android::base::VERBOSE); \ |
| 156 | i <= static_cast<size_t>(android::base::severity); \ |
| 157 | ++i) { \ |
| 158 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 159 | EXPECT_TRUE(WOULD_LOG(severity)) << i; \ |
| 160 | } |
| 161 | |
| 162 | TEST(logging, WOULD_LOG_FATAL) { |
| 163 | CHECK_WOULD_LOG_ENABLED(FATAL); |
| 164 | } |
| 165 | |
| 166 | TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) { |
| 167 | CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 168 | } |
| 169 | |
| 170 | TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) { |
| 171 | CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 172 | } |
| 173 | |
| 174 | TEST(logging, WOULD_LOG_ERROR_disabled) { |
| 175 | CHECK_WOULD_LOG_DISABLED(ERROR); |
| 176 | } |
| 177 | |
| 178 | TEST(logging, WOULD_LOG_ERROR_enabled) { |
| 179 | CHECK_WOULD_LOG_ENABLED(ERROR); |
| 180 | } |
| 181 | |
| 182 | TEST(logging, WOULD_LOG_WARNING_disabled) { |
| 183 | CHECK_WOULD_LOG_DISABLED(WARNING); |
| 184 | } |
| 185 | |
| 186 | TEST(logging, WOULD_LOG_WARNING_enabled) { |
| 187 | CHECK_WOULD_LOG_ENABLED(WARNING); |
| 188 | } |
| 189 | |
| 190 | TEST(logging, WOULD_LOG_INFO_disabled) { |
| 191 | CHECK_WOULD_LOG_DISABLED(INFO); |
| 192 | } |
| 193 | |
| 194 | TEST(logging, WOULD_LOG_INFO_enabled) { |
| 195 | CHECK_WOULD_LOG_ENABLED(INFO); |
| 196 | } |
| 197 | |
| 198 | TEST(logging, WOULD_LOG_DEBUG_disabled) { |
| 199 | CHECK_WOULD_LOG_DISABLED(DEBUG); |
| 200 | } |
| 201 | |
| 202 | TEST(logging, WOULD_LOG_DEBUG_enabled) { |
| 203 | CHECK_WOULD_LOG_ENABLED(DEBUG); |
| 204 | } |
| 205 | |
| 206 | TEST(logging, WOULD_LOG_VERBOSE_disabled) { |
| 207 | CHECK_WOULD_LOG_DISABLED(VERBOSE); |
| 208 | } |
| 209 | |
| 210 | TEST(logging, WOULD_LOG_VERBOSE_enabled) { |
| 211 | CHECK_WOULD_LOG_ENABLED(VERBOSE); |
| 212 | } |
| 213 | |
| 214 | #undef CHECK_WOULD_LOG_DISABLED |
| 215 | #undef CHECK_WOULD_LOG_ENABLED |
| 216 | |
| 217 | |
| 218 | #define CHECK_WOULD_LOG_SEVERITY_DISABLED(severity) \ |
| 219 | static_assert(android::base::severity < android::base::FATAL, "Bad input"); \ |
| 220 | for (size_t i = static_cast<size_t>(android::base::severity) + 1; \ |
| 221 | i <= static_cast<size_t>(android::base::FATAL); \ |
| 222 | ++i) { \ |
| 223 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 224 | EXPECT_FALSE(WOULD_LOG_SEVERITY(::android::base::severity)) << i; \ |
| 225 | } |
| 226 | |
| 227 | #define CHECK_WOULD_LOG_SEVERITY_ENABLED(severity) \ |
| 228 | for (size_t i = static_cast<size_t>(android::base::VERBOSE); \ |
| 229 | i <= static_cast<size_t>(android::base::severity); \ |
| 230 | ++i) { \ |
| 231 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 232 | EXPECT_TRUE(WOULD_LOG_SEVERITY(::android::base::severity)) << i; \ |
| 233 | } |
| 234 | |
| 235 | TEST(logging,WOULD_LOG_SEVERITY_FATAL) { |
| 236 | CHECK_WOULD_LOG_SEVERITY_ENABLED(FATAL); |
| 237 | } |
| 238 | |
| 239 | TEST(logging,WOULD_LOG_SEVERITY_FATAL_WITHOUT_ABORT_disabled) { |
| 240 | CHECK_WOULD_LOG_SEVERITY_DISABLED(FATAL_WITHOUT_ABORT); |
| 241 | } |
| 242 | |
| 243 | TEST(logging, WOULD_LOG_SEVERITY_FATAL_WITHOUT_ABORT_enabled) { |
| 244 | CHECK_WOULD_LOG_SEVERITY_ENABLED(FATAL_WITHOUT_ABORT); |
| 245 | } |
| 246 | |
| 247 | TEST(logging, WOULD_LOG_SEVERITY_ERROR_disabled) { |
| 248 | CHECK_WOULD_LOG_SEVERITY_DISABLED(ERROR); |
| 249 | } |
| 250 | |
| 251 | TEST(logging, WOULD_LOG_SEVERITY_ERROR_enabled) { |
| 252 | CHECK_WOULD_LOG_SEVERITY_ENABLED(ERROR); |
| 253 | } |
| 254 | |
| 255 | TEST(logging, WOULD_LOG_SEVERITY_WARNING_disabled) { |
| 256 | CHECK_WOULD_LOG_SEVERITY_DISABLED(WARNING); |
| 257 | } |
| 258 | |
| 259 | TEST(logging, WOULD_LOG_SEVERITY_WARNING_enabled) { |
| 260 | CHECK_WOULD_LOG_SEVERITY_ENABLED(WARNING); |
| 261 | } |
| 262 | |
| 263 | TEST(logging, WOULD_LOG_SEVERITY_INFO_disabled) { |
| 264 | CHECK_WOULD_LOG_SEVERITY_DISABLED(INFO); |
| 265 | } |
| 266 | |
| 267 | TEST(logging, WOULD_LOG_SEVERITY_INFO_enabled) { |
| 268 | CHECK_WOULD_LOG_SEVERITY_ENABLED(INFO); |
| 269 | } |
| 270 | |
| 271 | TEST(logging, WOULD_LOG_SEVERITY_DEBUG_disabled) { |
| 272 | CHECK_WOULD_LOG_SEVERITY_DISABLED(DEBUG); |
| 273 | } |
| 274 | |
| 275 | TEST(logging, WOULD_LOG_SEVERITY_DEBUG_enabled) { |
| 276 | CHECK_WOULD_LOG_SEVERITY_ENABLED(DEBUG); |
| 277 | } |
| 278 | |
| 279 | TEST(logging, WOULD_LOG_SEVERITYVERBOSE_disabled) { |
| 280 | CHECK_WOULD_LOG_SEVERITY_DISABLED(VERBOSE); |
| 281 | } |
| 282 | |
| 283 | TEST(logging, WOULD_LOG_SEVERITY_VERBOSE_enabled) { |
| 284 | CHECK_WOULD_LOG_SEVERITY_ENABLED(VERBOSE); |
| 285 | } |
| 286 | |
| 287 | #undef CHECK_WOULD_LOG_SEVERITY_DISABLED |
| 288 | #undef CHECK_WOULD_LOG_SEVERITY_ENABLED |
| 289 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 290 | static std::string make_log_pattern(android::base::LogSeverity severity, |
| 291 | const char* message) { |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 292 | static const char log_characters[] = "VDIWEFF"; |
| 293 | static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1, |
| 294 | "Mismatch in size of log_characters and values in LogSeverity"); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 295 | char log_char = log_characters[severity]; |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 296 | std::string holder(__FILE__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 297 | return android::base::StringPrintf( |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 298 | "%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] | 299 | log_char, basename(&holder[0]), message); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 302 | static void CheckMessage(const CapturedStderr& cap, |
| 303 | android::base::LogSeverity severity, const char* expected) { |
| 304 | std::string output; |
| 305 | ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); |
| 306 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 307 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 308 | // We can't usefully check the output of any of these on Windows because we |
| 309 | // don't have std::regex, but we can at least make sure we printed at least as |
| 310 | // many characters are in the log message. |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 311 | ASSERT_GT(output.length(), strlen(expected)); |
| 312 | ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output; |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 313 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 314 | #if !defined(_WIN32) |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 315 | std::regex message_regex(make_log_pattern(severity, expected)); |
| 316 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 317 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 320 | #define CHECK_LOG_STREAM(severity) \ |
| 321 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 322 | CapturedStderr cap2; \ |
| 323 | LOG_STREAM(severity) << "foobar"; \ |
| 324 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 325 | |
| 326 | TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT) { |
| 327 | CHECK_LOG_STREAM(FATAL_WITHOUT_ABORT); |
| 328 | } |
| 329 | |
| 330 | TEST(logging, LOG_STREAM_ERROR) { |
| 331 | CHECK_LOG_STREAM(ERROR); |
| 332 | } |
| 333 | |
| 334 | TEST(logging, LOG_STREAM_WARNING) { |
| 335 | CHECK_LOG_STREAM(WARNING); |
| 336 | } |
| 337 | |
| 338 | TEST(logging, LOG_STREAM_INFO) { |
| 339 | CHECK_LOG_STREAM(INFO); |
| 340 | } |
| 341 | |
| 342 | TEST(logging, LOG_STREAM_DEBUG) { |
| 343 | CHECK_LOG_STREAM(DEBUG); |
| 344 | } |
| 345 | |
| 346 | TEST(logging, LOG_STREAM_VERBOSE) { |
| 347 | CHECK_LOG_STREAM(VERBOSE); |
| 348 | } |
| 349 | |
| 350 | #undef CHECK_LOG_STREAM |
| 351 | |
| 352 | #define CHECK_LOG_STREAM_S(severity) \ |
| 353 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 354 | CapturedStderr cap2; \ |
| 355 | LOG_STREAM_S(android::base::severity) << "foobar"; \ |
| 356 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 357 | |
| 358 | TEST(logging, LOG_STREAM_S_FATAL_WITHOUT_ABORT) { |
| 359 | CHECK_LOG_STREAM_S(FATAL_WITHOUT_ABORT); |
| 360 | } |
| 361 | |
| 362 | TEST(logging, LOG_STREAM_S_ERROR) { |
| 363 | CHECK_LOG_STREAM_S(ERROR); |
| 364 | } |
| 365 | |
| 366 | TEST(logging, LOG_STREAM_S_WARNING) { |
| 367 | CHECK_LOG_STREAM_S(WARNING); |
| 368 | } |
| 369 | |
| 370 | TEST(logging, LOG_STREAM_S_INFO) { |
| 371 | CHECK_LOG_STREAM_S(INFO); |
| 372 | } |
| 373 | |
| 374 | TEST(logging, LOG_STREAM_S_DEBUG) { |
| 375 | CHECK_LOG_STREAM_S(DEBUG); |
| 376 | } |
| 377 | |
| 378 | TEST(logging, LOG_STREAM_S_VERBOSE) { |
| 379 | CHECK_LOG_STREAM_S(VERBOSE); |
| 380 | } |
| 381 | |
| 382 | #undef CHECK_LOG_STREAM_S |
| 383 | |
| 384 | |
| 385 | #define CHECK_LOG_DISABLED(severity) \ |
| 386 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 387 | CapturedStderr cap1; \ |
| 388 | LOG(severity) << "foo bar"; \ |
| 389 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 390 | |
| 391 | #define CHECK_LOG_ENABLED(severity) \ |
| 392 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 393 | CapturedStderr cap2; \ |
| 394 | LOG(severity) << "foobar"; \ |
| 395 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 396 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 397 | TEST(logging, LOG_FATAL) { |
| 398 | ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); |
| 399 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 400 | |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 401 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) { |
| 402 | CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 403 | } |
| 404 | |
| 405 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) { |
| 406 | CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 407 | } |
| 408 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 409 | TEST(logging, LOG_ERROR_disabled) { |
| 410 | CHECK_LOG_DISABLED(ERROR); |
| 411 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 412 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 413 | TEST(logging, LOG_ERROR_enabled) { |
| 414 | CHECK_LOG_ENABLED(ERROR); |
| 415 | } |
| 416 | |
| 417 | TEST(logging, LOG_WARNING_disabled) { |
| 418 | CHECK_LOG_DISABLED(WARNING); |
| 419 | } |
| 420 | |
| 421 | TEST(logging, LOG_WARNING_enabled) { |
| 422 | CHECK_LOG_ENABLED(WARNING); |
| 423 | } |
| 424 | |
| 425 | TEST(logging, LOG_INFO_disabled) { |
| 426 | CHECK_LOG_DISABLED(INFO); |
| 427 | } |
| 428 | |
| 429 | TEST(logging, LOG_INFO_enabled) { |
| 430 | CHECK_LOG_ENABLED(INFO); |
| 431 | } |
| 432 | |
| 433 | TEST(logging, LOG_DEBUG_disabled) { |
| 434 | CHECK_LOG_DISABLED(DEBUG); |
| 435 | } |
| 436 | |
| 437 | TEST(logging, LOG_DEBUG_enabled) { |
| 438 | CHECK_LOG_ENABLED(DEBUG); |
| 439 | } |
| 440 | |
| 441 | TEST(logging, LOG_VERBOSE_disabled) { |
| 442 | CHECK_LOG_DISABLED(VERBOSE); |
| 443 | } |
| 444 | |
| 445 | TEST(logging, LOG_VERBOSE_enabled) { |
| 446 | CHECK_LOG_ENABLED(VERBOSE); |
| 447 | } |
| 448 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 449 | #undef CHECK_LOG_DISABLED |
| 450 | #undef CHECK_LOG_ENABLED |
| 451 | |
| 452 | #define CHECK_LOG_S_DISABLED(severity) \ |
| 453 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 454 | CapturedStderr cap1; \ |
| 455 | LOG_S(::android::base::severity) << "foo bar"; \ |
| 456 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 457 | |
| 458 | #define CHECK_LOG_S_ENABLED(severity) \ |
| 459 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 460 | CapturedStderr cap2; \ |
| 461 | LOG_S(::android::base::severity) << "foobar"; \ |
| 462 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 463 | |
| 464 | TEST(logging, LOG_S_FATAL) { |
| 465 | ASSERT_DEATH({SuppressAbortUI(); LOG_S(::android::base::FATAL) << "foobar";}, "foobar"); |
| 466 | } |
| 467 | |
| 468 | TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_disabled) { |
| 469 | CHECK_LOG_S_DISABLED(FATAL_WITHOUT_ABORT); |
| 470 | } |
| 471 | |
| 472 | TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_enabled) { |
| 473 | CHECK_LOG_S_ENABLED(FATAL_WITHOUT_ABORT); |
| 474 | } |
| 475 | |
| 476 | TEST(logging, LOG_S_ERROR_disabled) { |
| 477 | CHECK_LOG_S_DISABLED(ERROR); |
| 478 | } |
| 479 | |
| 480 | TEST(logging, LOG_S_ERROR_enabled) { |
| 481 | CHECK_LOG_S_ENABLED(ERROR); |
| 482 | } |
| 483 | |
| 484 | TEST(logging, LOG_S_WARNING_disabled) { |
| 485 | CHECK_LOG_S_DISABLED(WARNING); |
| 486 | } |
| 487 | |
| 488 | TEST(logging, LOG_S_WARNING_enabled) { |
| 489 | CHECK_LOG_S_ENABLED(WARNING); |
| 490 | } |
| 491 | |
| 492 | TEST(logging, LOG_S_INFO_disabled) { |
| 493 | CHECK_LOG_S_DISABLED(INFO); |
| 494 | } |
| 495 | |
| 496 | TEST(logging, LOG_S_INFO_enabled) { |
| 497 | CHECK_LOG_S_ENABLED(INFO); |
| 498 | } |
| 499 | |
| 500 | TEST(logging, LOG_S_DEBUG_disabled) { |
| 501 | CHECK_LOG_S_DISABLED(DEBUG); |
| 502 | } |
| 503 | |
| 504 | TEST(logging, LOG_S_DEBUG_enabled) { |
| 505 | CHECK_LOG_S_ENABLED(DEBUG); |
| 506 | } |
| 507 | |
| 508 | TEST(logging, LOG_S_VERBOSE_disabled) { |
| 509 | CHECK_LOG_S_DISABLED(VERBOSE); |
| 510 | } |
| 511 | |
| 512 | TEST(logging, LOG_S_VERBOSE_enabled) { |
| 513 | CHECK_LOG_S_ENABLED(VERBOSE); |
| 514 | } |
| 515 | |
| 516 | #undef CHECK_LOG_S_DISABLED |
| 517 | #undef CHECK_LOG_S_ENABLED |
| 518 | |
| 519 | TEST(logging, LOG_S_complex_param) { |
| 520 | #define CHECK_LOG_S_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \ |
| 521 | { \ |
| 522 | android::base::ScopedLogSeverity sls( \ |
| 523 | (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \ |
| 524 | CapturedStderr cap; \ |
| 525 | LOG_S((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \ |
| 526 | << "foobar"; \ |
| 527 | if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \ |
| 528 | CheckMessage(cap, \ |
| 529 | (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \ |
| 530 | "foobar"); \ |
| 531 | } else { \ |
| 532 | ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \ |
| 533 | } \ |
| 534 | } |
| 535 | |
| 536 | CHECK_LOG_S_COMBINATION(false,false); |
| 537 | CHECK_LOG_S_COMBINATION(false,true); |
| 538 | CHECK_LOG_S_COMBINATION(true,false); |
| 539 | CHECK_LOG_S_COMBINATION(true,true); |
| 540 | |
| 541 | #undef CHECK_LOG_S_COMBINATION |
| 542 | } |
| 543 | |
| 544 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 545 | TEST(logging, LOG_does_not_clobber_errno) { |
| 546 | CapturedStderr cap; |
| 547 | errno = 12345; |
| 548 | LOG(INFO) << (errno = 67890); |
| 549 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 550 | |
| 551 | CheckMessage(cap, android::base::INFO, "67890"); |
| 552 | } |
| 553 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 554 | TEST(logging, LOG_S_does_not_clobber_errno) { |
| 555 | CapturedStderr cap; |
| 556 | errno = 12345; |
| 557 | LOG_S(::android::base::INFO) << (errno = 67890); |
| 558 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 559 | |
| 560 | CheckMessage(cap, android::base::INFO, "67890"); |
| 561 | } |
| 562 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 563 | TEST(logging, PLOG_does_not_clobber_errno) { |
| 564 | CapturedStderr cap; |
| 565 | errno = 12345; |
| 566 | PLOG(INFO) << (errno = 67890); |
| 567 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 568 | |
| 569 | CheckMessage(cap, android::base::INFO, "67890"); |
| 570 | } |
| 571 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 572 | TEST(logging, PLOG_S_does_not_clobber_errno) { |
| 573 | CapturedStderr cap; |
| 574 | errno = 12345; |
| 575 | PLOG_S(::android::base::INFO) << (errno = 67890); |
| 576 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 577 | |
| 578 | CheckMessage(cap, android::base::INFO, "67890"); |
| 579 | } |
| 580 | |
| 581 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 582 | TEST(logging, LOG_does_not_have_dangling_if) { |
| 583 | CapturedStderr cap; // So the logging below has no side-effects. |
| 584 | |
| 585 | // Do the test two ways: once where we hypothesize that LOG()'s if |
| 586 | // will evaluate to true (when severity is high enough) and once when we |
| 587 | // expect it to evaluate to false (when severity is not high enough). |
| 588 | bool flag = false; |
| 589 | if (true) |
| 590 | LOG(INFO) << "foobar"; |
| 591 | else |
| 592 | flag = true; |
| 593 | |
| 594 | EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; |
| 595 | |
| 596 | flag = false; |
| 597 | if (true) |
| 598 | LOG(VERBOSE) << "foobar"; |
| 599 | else |
| 600 | flag = true; |
| 601 | |
| 602 | EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 603 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 604 | flag = false; |
| 605 | if (true) |
| 606 | LOG_S(::android::base::INFO) << "foobar"; |
| 607 | else |
| 608 | flag = true; |
| 609 | |
| 610 | EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else"; |
| 611 | |
| 612 | flag = false; |
| 613 | if (true) |
| 614 | LOG_S(::android::base::VERBOSE) << "foobar"; |
| 615 | else |
| 616 | flag = true; |
| 617 | |
| 618 | EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else"; |
| 619 | } |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 620 | |
| 621 | #define CHECK_PLOG_DISABLED(severity) \ |
| 622 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 623 | CapturedStderr cap1; \ |
| 624 | PLOG(severity) << "foo bar"; \ |
| 625 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 626 | |
| 627 | #define CHECK_PLOG_ENABLED(severity) \ |
| 628 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 629 | CapturedStderr cap2; \ |
| 630 | errno = ENOENT; \ |
| 631 | PLOG(severity) << "foobar"; \ |
| 632 | CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ |
| 633 | |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 634 | TEST(logging, PLOG_FATAL) { |
| 635 | ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar"); |
| 636 | } |
| 637 | |
| 638 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) { |
| 639 | CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 640 | } |
| 641 | |
| 642 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) { |
| 643 | CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 644 | } |
| 645 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 646 | TEST(logging, PLOG_ERROR_disabled) { |
| 647 | CHECK_PLOG_DISABLED(ERROR); |
| 648 | } |
| 649 | |
| 650 | TEST(logging, PLOG_ERROR_enabled) { |
| 651 | CHECK_PLOG_ENABLED(ERROR); |
| 652 | } |
| 653 | |
| 654 | TEST(logging, PLOG_WARNING_disabled) { |
| 655 | CHECK_PLOG_DISABLED(WARNING); |
| 656 | } |
| 657 | |
| 658 | TEST(logging, PLOG_WARNING_enabled) { |
| 659 | CHECK_PLOG_ENABLED(WARNING); |
| 660 | } |
| 661 | |
| 662 | TEST(logging, PLOG_INFO_disabled) { |
| 663 | CHECK_PLOG_DISABLED(INFO); |
| 664 | } |
| 665 | |
| 666 | TEST(logging, PLOG_INFO_enabled) { |
| 667 | CHECK_PLOG_ENABLED(INFO); |
| 668 | } |
| 669 | |
| 670 | TEST(logging, PLOG_DEBUG_disabled) { |
| 671 | CHECK_PLOG_DISABLED(DEBUG); |
| 672 | } |
| 673 | |
| 674 | TEST(logging, PLOG_DEBUG_enabled) { |
| 675 | CHECK_PLOG_ENABLED(DEBUG); |
| 676 | } |
| 677 | |
| 678 | TEST(logging, PLOG_VERBOSE_disabled) { |
| 679 | CHECK_PLOG_DISABLED(VERBOSE); |
| 680 | } |
| 681 | |
| 682 | TEST(logging, PLOG_VERBOSE_enabled) { |
| 683 | CHECK_PLOG_ENABLED(VERBOSE); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame^] | 686 | #undef CHECK_PLOG_DISABLED |
| 687 | #undef CHECK_PLOG_ENABLED |
| 688 | |
| 689 | |
| 690 | #define CHECK_PLOG_S_DISABLED(severity) \ |
| 691 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 692 | CapturedStderr cap1; \ |
| 693 | PLOG_S(android::base::severity) << "foo bar"; \ |
| 694 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 695 | |
| 696 | #define CHECK_PLOG_S_ENABLED(severity) \ |
| 697 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 698 | CapturedStderr cap2; \ |
| 699 | errno = ENOENT; \ |
| 700 | PLOG_S(android::base::severity) << "foobar"; \ |
| 701 | CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ |
| 702 | |
| 703 | TEST(logging, PLOG_S_FATAL) { |
| 704 | ASSERT_DEATH({SuppressAbortUI(); PLOG_S(::android::base::FATAL) << "foobar";}, "foobar"); |
| 705 | } |
| 706 | |
| 707 | TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_disabled) { |
| 708 | CHECK_PLOG_S_DISABLED(FATAL_WITHOUT_ABORT); |
| 709 | } |
| 710 | |
| 711 | TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_enabled) { |
| 712 | CHECK_PLOG_S_ENABLED(FATAL_WITHOUT_ABORT); |
| 713 | } |
| 714 | |
| 715 | TEST(logging, PLOG_S_ERROR_disabled) { |
| 716 | CHECK_PLOG_S_DISABLED(ERROR); |
| 717 | } |
| 718 | |
| 719 | TEST(logging, PLOG_S_ERROR_enabled) { |
| 720 | CHECK_PLOG_S_ENABLED(ERROR); |
| 721 | } |
| 722 | |
| 723 | TEST(logging, PLOG_S_WARNING_disabled) { |
| 724 | CHECK_PLOG_S_DISABLED(WARNING); |
| 725 | } |
| 726 | |
| 727 | TEST(logging, PLOG_S_WARNING_enabled) { |
| 728 | CHECK_PLOG_S_ENABLED(WARNING); |
| 729 | } |
| 730 | |
| 731 | TEST(logging, PLOG_S_INFO_disabled) { |
| 732 | CHECK_PLOG_S_DISABLED(INFO); |
| 733 | } |
| 734 | |
| 735 | TEST(logging, PLOG_S_INFO_enabled) { |
| 736 | CHECK_PLOG_S_ENABLED(INFO); |
| 737 | } |
| 738 | |
| 739 | TEST(logging, PLOG_S_DEBUG_disabled) { |
| 740 | CHECK_PLOG_S_DISABLED(DEBUG); |
| 741 | } |
| 742 | |
| 743 | TEST(logging, PLOG_S_DEBUG_enabled) { |
| 744 | CHECK_PLOG_S_ENABLED(DEBUG); |
| 745 | } |
| 746 | |
| 747 | TEST(logging, PLOG_S_VERBOSE_disabled) { |
| 748 | CHECK_PLOG_S_DISABLED(VERBOSE); |
| 749 | } |
| 750 | |
| 751 | TEST(logging, PLOG_S_VERBOSE_enabled) { |
| 752 | CHECK_PLOG_S_ENABLED(VERBOSE); |
| 753 | } |
| 754 | |
| 755 | #undef CHECK_PLOG_S_DISABLED |
| 756 | #undef CHECK_PLOG_S_ENABLED |
| 757 | |
| 758 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 759 | TEST(logging, UNIMPLEMENTED) { |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 760 | std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 761 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 762 | CapturedStderr cap; |
| 763 | errno = ENOENT; |
| 764 | UNIMPLEMENTED(ERROR); |
| 765 | CheckMessage(cap, android::base::ERROR, expected.c_str()); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 766 | } |
Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 767 | |
| 768 | static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) { |
| 769 | LOG(ERROR) << "called noop"; |
| 770 | } |
| 771 | |
| 772 | TEST(logging, LOG_FATAL_NOOP_ABORTER) { |
| 773 | { |
| 774 | android::base::SetAborter(NoopAborter); |
| 775 | |
| 776 | android::base::ScopedLogSeverity sls(android::base::ERROR); |
| 777 | CapturedStderr cap; |
| 778 | LOG(FATAL) << "foobar"; |
| 779 | CheckMessage(cap, android::base::FATAL, "foobar"); |
| 780 | CheckMessage(cap, android::base::ERROR, "called noop"); |
| 781 | |
| 782 | android::base::SetAborter(android::base::DefaultAborter); |
| 783 | } |
| 784 | |
| 785 | ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); |
| 786 | } |