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