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 | |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 146 | #define CHECK_WOULD_LOG_DISABLED(severity) \ |
| 147 | static_assert(android::base::severity < android::base::FATAL, "Bad input"); \ |
| 148 | for (size_t i = static_cast<size_t>(android::base::severity) + 1; \ |
| 149 | i <= static_cast<size_t>(android::base::FATAL); \ |
| 150 | ++i) { \ |
| 151 | { \ |
| 152 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 153 | EXPECT_FALSE(WOULD_LOG(severity)) << i; \ |
| 154 | } \ |
| 155 | { \ |
| 156 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 157 | EXPECT_FALSE(WOULD_LOG(::android::base::severity)) << i; \ |
| 158 | } \ |
| 159 | } \ |
| 160 | |
| 161 | #define CHECK_WOULD_LOG_ENABLED(severity) \ |
| 162 | for (size_t i = static_cast<size_t>(android::base::VERBOSE); \ |
| 163 | i <= static_cast<size_t>(android::base::severity); \ |
| 164 | ++i) { \ |
| 165 | { \ |
| 166 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 167 | EXPECT_TRUE(WOULD_LOG(severity)) << i; \ |
| 168 | } \ |
| 169 | { \ |
| 170 | android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \ |
| 171 | EXPECT_TRUE(WOULD_LOG(::android::base::severity)) << i; \ |
| 172 | } \ |
| 173 | } \ |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 174 | |
| 175 | TEST(logging, WOULD_LOG_FATAL) { |
| 176 | CHECK_WOULD_LOG_ENABLED(FATAL); |
| 177 | } |
| 178 | |
| 179 | TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) { |
| 180 | CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 181 | } |
| 182 | |
| 183 | TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) { |
| 184 | CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 185 | } |
| 186 | |
| 187 | TEST(logging, WOULD_LOG_ERROR_disabled) { |
| 188 | CHECK_WOULD_LOG_DISABLED(ERROR); |
| 189 | } |
| 190 | |
| 191 | TEST(logging, WOULD_LOG_ERROR_enabled) { |
| 192 | CHECK_WOULD_LOG_ENABLED(ERROR); |
| 193 | } |
| 194 | |
| 195 | TEST(logging, WOULD_LOG_WARNING_disabled) { |
| 196 | CHECK_WOULD_LOG_DISABLED(WARNING); |
| 197 | } |
| 198 | |
| 199 | TEST(logging, WOULD_LOG_WARNING_enabled) { |
| 200 | CHECK_WOULD_LOG_ENABLED(WARNING); |
| 201 | } |
| 202 | |
| 203 | TEST(logging, WOULD_LOG_INFO_disabled) { |
| 204 | CHECK_WOULD_LOG_DISABLED(INFO); |
| 205 | } |
| 206 | |
| 207 | TEST(logging, WOULD_LOG_INFO_enabled) { |
| 208 | CHECK_WOULD_LOG_ENABLED(INFO); |
| 209 | } |
| 210 | |
| 211 | TEST(logging, WOULD_LOG_DEBUG_disabled) { |
| 212 | CHECK_WOULD_LOG_DISABLED(DEBUG); |
| 213 | } |
| 214 | |
| 215 | TEST(logging, WOULD_LOG_DEBUG_enabled) { |
| 216 | CHECK_WOULD_LOG_ENABLED(DEBUG); |
| 217 | } |
| 218 | |
| 219 | TEST(logging, WOULD_LOG_VERBOSE_disabled) { |
| 220 | CHECK_WOULD_LOG_DISABLED(VERBOSE); |
| 221 | } |
| 222 | |
| 223 | TEST(logging, WOULD_LOG_VERBOSE_enabled) { |
| 224 | CHECK_WOULD_LOG_ENABLED(VERBOSE); |
| 225 | } |
| 226 | |
| 227 | #undef CHECK_WOULD_LOG_DISABLED |
| 228 | #undef CHECK_WOULD_LOG_ENABLED |
| 229 | |
| 230 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 231 | static std::string make_log_pattern(android::base::LogSeverity severity, |
| 232 | const char* message) { |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 233 | static const char log_characters[] = "VDIWEFF"; |
| 234 | static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1, |
| 235 | "Mismatch in size of log_characters and values in LogSeverity"); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 236 | char log_char = log_characters[severity]; |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 237 | std::string holder(__FILE__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 238 | return android::base::StringPrintf( |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 239 | "%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] | 240 | log_char, basename(&holder[0]), message); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 243 | static void CheckMessage(const CapturedStderr& cap, |
| 244 | android::base::LogSeverity severity, const char* expected) { |
| 245 | std::string output; |
| 246 | ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET)); |
| 247 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 248 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 249 | // We can't usefully check the output of any of these on Windows because we |
| 250 | // don't have std::regex, but we can at least make sure we printed at least as |
| 251 | // many characters are in the log message. |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 252 | ASSERT_GT(output.length(), strlen(expected)); |
| 253 | ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output; |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 254 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 255 | #if !defined(_WIN32) |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 256 | std::regex message_regex(make_log_pattern(severity, expected)); |
| 257 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 258 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 261 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 262 | #define CHECK_LOG_STREAM_DISABLED(severity) \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 263 | { \ |
| 264 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 265 | CapturedStderr cap1; \ |
| 266 | LOG_STREAM(severity) << "foo bar"; \ |
| 267 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 268 | } \ |
| 269 | { \ |
| 270 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 271 | CapturedStderr cap1; \ |
| 272 | LOG_STREAM(::android::base::severity) << "foo bar"; \ |
| 273 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 274 | } \ |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 275 | |
| 276 | #define CHECK_LOG_STREAM_ENABLED(severity) \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 277 | { \ |
| 278 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 279 | CapturedStderr cap2; \ |
| 280 | LOG_STREAM(severity) << "foobar"; \ |
| 281 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 282 | } \ |
| 283 | { \ |
| 284 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 285 | CapturedStderr cap2; \ |
| 286 | LOG_STREAM(::android::base::severity) << "foobar"; \ |
| 287 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 288 | } \ |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 289 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 290 | TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_disabled) { |
| 291 | CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 294 | TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) { |
| 295 | CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 298 | TEST(logging, LOG_STREAM_ERROR_disabled) { |
| 299 | CHECK_LOG_STREAM_DISABLED(ERROR); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 302 | TEST(logging, LOG_STREAM_ERROR_enabled) { |
| 303 | CHECK_LOG_STREAM_ENABLED(ERROR); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 306 | TEST(logging, LOG_STREAM_WARNING_disabled) { |
| 307 | CHECK_LOG_STREAM_DISABLED(WARNING); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 310 | TEST(logging, LOG_STREAM_WARNING_enabled) { |
| 311 | CHECK_LOG_STREAM_ENABLED(WARNING); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 314 | TEST(logging, LOG_STREAM_INFO_disabled) { |
| 315 | CHECK_LOG_STREAM_DISABLED(INFO); |
| 316 | } |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 317 | |
Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 318 | TEST(logging, LOG_STREAM_INFO_enabled) { |
| 319 | CHECK_LOG_STREAM_ENABLED(INFO); |
| 320 | } |
| 321 | |
| 322 | TEST(logging, LOG_STREAM_DEBUG_disabled) { |
| 323 | CHECK_LOG_STREAM_DISABLED(DEBUG); |
| 324 | } |
| 325 | |
| 326 | TEST(logging, LOG_STREAM_DEBUG_enabled) { |
| 327 | CHECK_LOG_STREAM_ENABLED(DEBUG); |
| 328 | } |
| 329 | |
| 330 | TEST(logging, LOG_STREAM_VERBOSE_disabled) { |
| 331 | CHECK_LOG_STREAM_DISABLED(VERBOSE); |
| 332 | } |
| 333 | |
| 334 | TEST(logging, LOG_STREAM_VERBOSE_enabled) { |
| 335 | CHECK_LOG_STREAM_ENABLED(VERBOSE); |
| 336 | } |
| 337 | |
| 338 | #undef CHECK_LOG_STREAM_DISABLED |
| 339 | #undef CHECK_LOG_STREAM_ENABLED |
| 340 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 341 | |
| 342 | #define CHECK_LOG_DISABLED(severity) \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 343 | { \ |
| 344 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 345 | CapturedStderr cap1; \ |
| 346 | LOG(severity) << "foo bar"; \ |
| 347 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 348 | } \ |
| 349 | { \ |
| 350 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 351 | CapturedStderr cap1; \ |
| 352 | LOG(::android::base::severity) << "foo bar"; \ |
| 353 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 354 | } \ |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 355 | |
| 356 | #define CHECK_LOG_ENABLED(severity) \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 357 | { \ |
| 358 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 359 | CapturedStderr cap2; \ |
| 360 | LOG(severity) << "foobar"; \ |
| 361 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 362 | } \ |
| 363 | { \ |
| 364 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 365 | CapturedStderr cap2; \ |
| 366 | LOG(::android::base::severity) << "foobar"; \ |
| 367 | CheckMessage(cap2, android::base::severity, "foobar"); \ |
| 368 | } \ |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 369 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 370 | TEST(logging, LOG_FATAL) { |
| 371 | ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 372 | ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar"); |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 373 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 374 | |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 375 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) { |
| 376 | CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 377 | } |
| 378 | |
| 379 | TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) { |
| 380 | CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 381 | } |
| 382 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 383 | TEST(logging, LOG_ERROR_disabled) { |
| 384 | CHECK_LOG_DISABLED(ERROR); |
| 385 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 386 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 387 | TEST(logging, LOG_ERROR_enabled) { |
| 388 | CHECK_LOG_ENABLED(ERROR); |
| 389 | } |
| 390 | |
| 391 | TEST(logging, LOG_WARNING_disabled) { |
| 392 | CHECK_LOG_DISABLED(WARNING); |
| 393 | } |
| 394 | |
| 395 | TEST(logging, LOG_WARNING_enabled) { |
| 396 | CHECK_LOG_ENABLED(WARNING); |
| 397 | } |
| 398 | |
| 399 | TEST(logging, LOG_INFO_disabled) { |
| 400 | CHECK_LOG_DISABLED(INFO); |
| 401 | } |
| 402 | |
| 403 | TEST(logging, LOG_INFO_enabled) { |
| 404 | CHECK_LOG_ENABLED(INFO); |
| 405 | } |
| 406 | |
| 407 | TEST(logging, LOG_DEBUG_disabled) { |
| 408 | CHECK_LOG_DISABLED(DEBUG); |
| 409 | } |
| 410 | |
| 411 | TEST(logging, LOG_DEBUG_enabled) { |
| 412 | CHECK_LOG_ENABLED(DEBUG); |
| 413 | } |
| 414 | |
| 415 | TEST(logging, LOG_VERBOSE_disabled) { |
| 416 | CHECK_LOG_DISABLED(VERBOSE); |
| 417 | } |
| 418 | |
| 419 | TEST(logging, LOG_VERBOSE_enabled) { |
| 420 | CHECK_LOG_ENABLED(VERBOSE); |
| 421 | } |
| 422 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 423 | #undef CHECK_LOG_DISABLED |
| 424 | #undef CHECK_LOG_ENABLED |
| 425 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 426 | |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 427 | TEST(logging, LOG_complex_param) { |
| 428 | #define CHECK_LOG_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \ |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 429 | { \ |
| 430 | android::base::ScopedLogSeverity sls( \ |
| 431 | (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \ |
| 432 | CapturedStderr cap; \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 433 | LOG((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \ |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 434 | << "foobar"; \ |
| 435 | if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \ |
| 436 | CheckMessage(cap, \ |
| 437 | (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \ |
| 438 | "foobar"); \ |
| 439 | } else { \ |
| 440 | ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \ |
| 441 | } \ |
| 442 | } |
| 443 | |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 444 | CHECK_LOG_COMBINATION(false,false); |
| 445 | CHECK_LOG_COMBINATION(false,true); |
| 446 | CHECK_LOG_COMBINATION(true,false); |
| 447 | CHECK_LOG_COMBINATION(true,true); |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 448 | |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 449 | #undef CHECK_LOG_COMBINATION |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 453 | TEST(logging, LOG_does_not_clobber_errno) { |
| 454 | CapturedStderr cap; |
| 455 | errno = 12345; |
| 456 | LOG(INFO) << (errno = 67890); |
| 457 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 458 | |
| 459 | CheckMessage(cap, android::base::INFO, "67890"); |
| 460 | } |
| 461 | |
| 462 | TEST(logging, PLOG_does_not_clobber_errno) { |
| 463 | CapturedStderr cap; |
| 464 | errno = 12345; |
| 465 | PLOG(INFO) << (errno = 67890); |
| 466 | EXPECT_EQ(12345, errno) << "errno was not restored"; |
| 467 | |
| 468 | CheckMessage(cap, android::base::INFO, "67890"); |
| 469 | } |
| 470 | |
| 471 | TEST(logging, LOG_does_not_have_dangling_if) { |
| 472 | CapturedStderr cap; // So the logging below has no side-effects. |
| 473 | |
| 474 | // Do the test two ways: once where we hypothesize that LOG()'s if |
| 475 | // will evaluate to true (when severity is high enough) and once when we |
| 476 | // expect it to evaluate to false (when severity is not high enough). |
| 477 | bool flag = false; |
| 478 | if (true) |
| 479 | LOG(INFO) << "foobar"; |
| 480 | else |
| 481 | flag = true; |
| 482 | |
| 483 | EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; |
| 484 | |
| 485 | flag = false; |
| 486 | if (true) |
| 487 | LOG(VERBOSE) << "foobar"; |
| 488 | else |
| 489 | flag = true; |
| 490 | |
| 491 | EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else"; |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 492 | } |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 493 | |
| 494 | #define CHECK_PLOG_DISABLED(severity) \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 495 | { \ |
| 496 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 497 | CapturedStderr cap1; \ |
| 498 | PLOG(severity) << "foo bar"; \ |
| 499 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 500 | } \ |
| 501 | { \ |
| 502 | android::base::ScopedLogSeverity sls1(android::base::FATAL); \ |
| 503 | CapturedStderr cap1; \ |
| 504 | PLOG(severity) << "foo bar"; \ |
| 505 | ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \ |
| 506 | } \ |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 507 | |
| 508 | #define CHECK_PLOG_ENABLED(severity) \ |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 509 | { \ |
| 510 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 511 | CapturedStderr cap2; \ |
| 512 | errno = ENOENT; \ |
| 513 | PLOG(severity) << "foobar"; \ |
| 514 | CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ |
| 515 | } \ |
| 516 | { \ |
| 517 | android::base::ScopedLogSeverity sls2(android::base::severity); \ |
| 518 | CapturedStderr cap2; \ |
| 519 | errno = ENOENT; \ |
| 520 | PLOG(severity) << "foobar"; \ |
| 521 | CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \ |
| 522 | } \ |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 523 | |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 524 | TEST(logging, PLOG_FATAL) { |
| 525 | ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar"); |
Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 526 | ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar"); |
Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) { |
| 530 | CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT); |
| 531 | } |
| 532 | |
| 533 | TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) { |
| 534 | CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT); |
| 535 | } |
| 536 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 537 | TEST(logging, PLOG_ERROR_disabled) { |
| 538 | CHECK_PLOG_DISABLED(ERROR); |
| 539 | } |
| 540 | |
| 541 | TEST(logging, PLOG_ERROR_enabled) { |
| 542 | CHECK_PLOG_ENABLED(ERROR); |
| 543 | } |
| 544 | |
| 545 | TEST(logging, PLOG_WARNING_disabled) { |
| 546 | CHECK_PLOG_DISABLED(WARNING); |
| 547 | } |
| 548 | |
| 549 | TEST(logging, PLOG_WARNING_enabled) { |
| 550 | CHECK_PLOG_ENABLED(WARNING); |
| 551 | } |
| 552 | |
| 553 | TEST(logging, PLOG_INFO_disabled) { |
| 554 | CHECK_PLOG_DISABLED(INFO); |
| 555 | } |
| 556 | |
| 557 | TEST(logging, PLOG_INFO_enabled) { |
| 558 | CHECK_PLOG_ENABLED(INFO); |
| 559 | } |
| 560 | |
| 561 | TEST(logging, PLOG_DEBUG_disabled) { |
| 562 | CHECK_PLOG_DISABLED(DEBUG); |
| 563 | } |
| 564 | |
| 565 | TEST(logging, PLOG_DEBUG_enabled) { |
| 566 | CHECK_PLOG_ENABLED(DEBUG); |
| 567 | } |
| 568 | |
| 569 | TEST(logging, PLOG_VERBOSE_disabled) { |
| 570 | CHECK_PLOG_DISABLED(VERBOSE); |
| 571 | } |
| 572 | |
| 573 | TEST(logging, PLOG_VERBOSE_enabled) { |
| 574 | CHECK_PLOG_ENABLED(VERBOSE); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Andreas Gampe | 436f5a0 | 2016-09-22 10:15:01 -0700 | [diff] [blame] | 577 | #undef CHECK_PLOG_DISABLED |
| 578 | #undef CHECK_PLOG_ENABLED |
| 579 | |
| 580 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 581 | TEST(logging, UNIMPLEMENTED) { |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 582 | std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 583 | |
Elliott Hughes | 13d78e4 | 2016-09-07 16:22:40 -0700 | [diff] [blame] | 584 | CapturedStderr cap; |
| 585 | errno = ENOENT; |
| 586 | UNIMPLEMENTED(ERROR); |
| 587 | CheckMessage(cap, android::base::ERROR, expected.c_str()); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 588 | } |
Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 589 | |
| 590 | static void NoopAborter(const char* msg ATTRIBUTE_UNUSED) { |
| 591 | LOG(ERROR) << "called noop"; |
| 592 | } |
| 593 | |
| 594 | TEST(logging, LOG_FATAL_NOOP_ABORTER) { |
| 595 | { |
| 596 | android::base::SetAborter(NoopAborter); |
| 597 | |
| 598 | android::base::ScopedLogSeverity sls(android::base::ERROR); |
| 599 | CapturedStderr cap; |
| 600 | LOG(FATAL) << "foobar"; |
| 601 | CheckMessage(cap, android::base::FATAL, "foobar"); |
| 602 | CheckMessage(cap, android::base::ERROR, "called noop"); |
| 603 | |
| 604 | android::base::SetAborter(android::base::DefaultAborter); |
| 605 | } |
| 606 | |
| 607 | ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar"); |
| 608 | } |
Andreas Gampe | b4e32f3 | 2016-10-04 19:17:07 -0700 | [diff] [blame^] | 609 | |
| 610 | struct CountLineAborter { |
| 611 | static void CountLineAborterFunction(const char* msg) { |
| 612 | while (*msg != 0) { |
| 613 | if (*msg == '\n') { |
| 614 | newline_count++; |
| 615 | } |
| 616 | msg++; |
| 617 | } |
| 618 | } |
| 619 | static size_t newline_count; |
| 620 | }; |
| 621 | size_t CountLineAborter::newline_count = 0; |
| 622 | |
| 623 | TEST(logging, LOG_FATAL_ABORTER_MESSAGE) { |
| 624 | CountLineAborter::newline_count = 0; |
| 625 | android::base::SetAborter(CountLineAborter::CountLineAborterFunction); |
| 626 | |
| 627 | android::base::ScopedLogSeverity sls(android::base::ERROR); |
| 628 | CapturedStderr cap; |
| 629 | LOG(FATAL) << "foo\nbar"; |
| 630 | |
| 631 | EXPECT_EQ(CountLineAborter::newline_count, 1U + 1U); // +1 for final '\n'. |
| 632 | } |