blob: c91857a0a636c5093003e7c42692a139c55a2b04 [file] [log] [blame]
Dan Albert58310b42015-03-13 23:06:01 -07001/*
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"
24#include "test_utils.h"
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
34class 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 Albertb547c852015-03-27 11:20:14 -070064TEST(logging, CHECK) {
Dan Albert58310b42015-03-13 23:06:01 -070065 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
75std::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];
79 return android::base::StringPrintf(
80 "%c[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+ " __FILE__
81 ":[[:digit:]]+] %s",
82 log_char, message);
83}
84
Dan Albertb547c852015-03-27 11:20:14 -070085TEST(logging, LOG) {
Dan Albert58310b42015-03-13 23:06:01 -070086 ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar");
87
Dan Albert5c190402015-04-29 11:32:23 -070088 // 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 Albert58310b42015-03-13 23:06:01 -070091 {
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 Albert5c190402015-04-29 11:32:23 -070098 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -070099
Dan Albert5c190402015-04-29 11:32:23 -0700100#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700101 std::regex message_regex(
102 make_log_pattern(android::base::WARNING, "foobar"));
103 ASSERT_TRUE(std::regex_search(output, message_regex));
Dan Albert5c190402015-04-29 11:32:23 -0700104#endif
Dan Albert58310b42015-03-13 23:06:01 -0700105 }
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 Albert5c190402015-04-29 11:32:23 -0700114 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700115
Dan Albert5c190402015-04-29 11:32:23 -0700116#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700117 std::regex message_regex(
118 make_log_pattern(android::base::INFO, "foobar"));
119 ASSERT_TRUE(std::regex_search(output, message_regex));
Dan Albert5c190402015-04-29 11:32:23 -0700120#endif
Dan Albert58310b42015-03-13 23:06:01 -0700121 }
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 Albert5c190402015-04-29 11:32:23 -0700141 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700142
Dan Albert5c190402015-04-29 11:32:23 -0700143#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700144 std::regex message_regex(
145 make_log_pattern(android::base::DEBUG, "foobar"));
146 ASSERT_TRUE(std::regex_search(output, message_regex));
Dan Albert5c190402015-04-29 11:32:23 -0700147#endif
Dan Albert58310b42015-03-13 23:06:01 -0700148 }
149}
150
Dan Albertb547c852015-03-27 11:20:14 -0700151TEST(logging, PLOG) {
Dan Albert58310b42015-03-13 23:06:01 -0700152 {
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 Albert5c190402015-04-29 11:32:23 -0700160 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700161
Dan Albert5c190402015-04-29 11:32:23 -0700162#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700163 std::regex message_regex(make_log_pattern(
164 android::base::INFO, "foobar: No such file or directory"));
165 ASSERT_TRUE(std::regex_search(output, message_regex));
Dan Albert5c190402015-04-29 11:32:23 -0700166#endif
Dan Albert58310b42015-03-13 23:06:01 -0700167 }
168}
169
Dan Albertb547c852015-03-27 11:20:14 -0700170TEST(logging, UNIMPLEMENTED) {
Dan Albert58310b42015-03-13 23:06:01 -0700171 {
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 Albert5c190402015-04-29 11:32:23 -0700179 ASSERT_GT(output.length(), strlen("unimplemented"));
Dan Albert58310b42015-03-13 23:06:01 -0700180
Dan Albert5c190402015-04-29 11:32:23 -0700181#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700182 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()));
186 ASSERT_TRUE(std::regex_search(output, message_regex));
Dan Albert5c190402015-04-29 11:32:23 -0700187#endif
Dan Albert58310b42015-03-13 23:06:01 -0700188 }
189}