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 | |
| 17 | #include "base/logging.h" |
| 18 | |
Elliott Hughes | 2e5ae00 | 2015-08-13 20:09:29 -0700 | [diff] [blame^] | 19 | #include <libgen.h> |
| 20 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 21 | #include <regex> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "base/file.h" |
| 25 | #include "base/stringprintf.h" |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 26 | #include "base/test_utils.h" |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #ifdef __ANDROID__ |
| 31 | #define HOST_TEST(suite, name) TEST(suite, DISABLED_ ## name) |
| 32 | #else |
| 33 | #define HOST_TEST(suite, name) TEST(suite, name) |
| 34 | #endif |
| 35 | |
| 36 | class CapturedStderr { |
| 37 | public: |
| 38 | CapturedStderr() : old_stderr_(-1) { |
| 39 | init(); |
| 40 | } |
| 41 | |
| 42 | ~CapturedStderr() { |
| 43 | reset(); |
| 44 | } |
| 45 | |
| 46 | int fd() const { |
| 47 | return temp_file_.fd; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | void init() { |
| 52 | old_stderr_ = dup(STDERR_FILENO); |
| 53 | ASSERT_NE(-1, old_stderr_); |
| 54 | ASSERT_NE(-1, dup2(fd(), STDERR_FILENO)); |
| 55 | } |
| 56 | |
| 57 | void reset() { |
| 58 | ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO)); |
| 59 | ASSERT_EQ(0, close(old_stderr_)); |
| 60 | } |
| 61 | |
| 62 | TemporaryFile temp_file_; |
| 63 | int old_stderr_; |
| 64 | }; |
| 65 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 66 | TEST(logging, CHECK) { |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 67 | ASSERT_DEATH(CHECK(false), "Check failed: false "); |
| 68 | CHECK(true); |
| 69 | |
| 70 | ASSERT_DEATH(CHECK_EQ(0, 1), "Check failed: 0 == 1 "); |
| 71 | CHECK_EQ(0, 0); |
| 72 | |
| 73 | ASSERT_DEATH(CHECK_STREQ("foo", "bar"), R"(Check failed: "foo" == "bar")"); |
| 74 | CHECK_STREQ("foo", "foo"); |
| 75 | } |
| 76 | |
| 77 | std::string make_log_pattern(android::base::LogSeverity severity, |
| 78 | const char* message) { |
| 79 | static const char* log_characters = "VDIWEF"; |
| 80 | char log_char = log_characters[severity]; |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 81 | std::string holder(__FILE__); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 82 | return android::base::StringPrintf( |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 83 | "%c[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+ %s:[[:digit:]]+] %s", |
| 84 | log_char, basename(&holder[0]), message); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 87 | TEST(logging, LOG) { |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 88 | ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar"); |
| 89 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 90 | // We can't usefully check the output of any of these on Windows because we |
| 91 | // don't have std::regex, but we can at least make sure we printed at least as |
| 92 | // many characters are in the log message. |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 93 | { |
| 94 | CapturedStderr cap; |
| 95 | LOG(WARNING) << "foobar"; |
| 96 | ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); |
| 97 | |
| 98 | std::string output; |
| 99 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 100 | ASSERT_GT(output.length(), strlen("foobar")); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 101 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 102 | #if !defined(_WIN32) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 103 | std::regex message_regex( |
| 104 | make_log_pattern(android::base::WARNING, "foobar")); |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 105 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 106 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | { |
| 110 | CapturedStderr cap; |
| 111 | LOG(INFO) << "foobar"; |
| 112 | ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); |
| 113 | |
| 114 | std::string output; |
| 115 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 116 | ASSERT_GT(output.length(), strlen("foobar")); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 117 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 118 | #if !defined(_WIN32) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 119 | std::regex message_regex( |
| 120 | make_log_pattern(android::base::INFO, "foobar")); |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 122 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | { |
| 126 | CapturedStderr cap; |
| 127 | LOG(DEBUG) << "foobar"; |
| 128 | ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); |
| 129 | |
| 130 | std::string output; |
| 131 | android::base::ReadFdToString(cap.fd(), &output); |
| 132 | ASSERT_TRUE(output.empty()); |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | android::base::ScopedLogSeverity severity(android::base::DEBUG); |
| 137 | CapturedStderr cap; |
| 138 | LOG(DEBUG) << "foobar"; |
| 139 | ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); |
| 140 | |
| 141 | std::string output; |
| 142 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 143 | ASSERT_GT(output.length(), strlen("foobar")); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 144 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 145 | #if !defined(_WIN32) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 146 | std::regex message_regex( |
| 147 | make_log_pattern(android::base::DEBUG, "foobar")); |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 148 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 149 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 153 | TEST(logging, PLOG) { |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 154 | { |
| 155 | CapturedStderr cap; |
| 156 | errno = ENOENT; |
| 157 | PLOG(INFO) << "foobar"; |
| 158 | ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); |
| 159 | |
| 160 | std::string output; |
| 161 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 162 | ASSERT_GT(output.length(), strlen("foobar")); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 163 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 164 | #if !defined(_WIN32) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 165 | std::regex message_regex(make_log_pattern( |
| 166 | android::base::INFO, "foobar: No such file or directory")); |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 167 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 168 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 172 | TEST(logging, UNIMPLEMENTED) { |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 173 | { |
| 174 | CapturedStderr cap; |
| 175 | errno = ENOENT; |
| 176 | UNIMPLEMENTED(ERROR); |
| 177 | ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); |
| 178 | |
| 179 | std::string output; |
| 180 | android::base::ReadFdToString(cap.fd(), &output); |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 181 | ASSERT_GT(output.length(), strlen("unimplemented")); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 182 | |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 183 | #if !defined(_WIN32) |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 184 | std::string expected_message = |
| 185 | android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__); |
| 186 | std::regex message_regex( |
| 187 | make_log_pattern(android::base::ERROR, expected_message.c_str())); |
Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(std::regex_search(output, message_regex)) << output; |
Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 189 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 190 | } |
| 191 | } |