blob: c12dfa5f3e876451bddb0f31858281b1cde3a5ff [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
Elliott Hughes2e5ae002015-08-13 20:09:29 -070019#include <libgen.h>
20
Dan Albert58310b42015-03-13 23:06:01 -070021#include <regex>
22#include <string>
23
24#include "base/file.h"
25#include "base/stringprintf.h"
Alex Vallée47d67c92015-05-06 16:26:00 -040026#include "base/test_utils.h"
Dan Albert58310b42015-03-13 23:06:01 -070027
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
36class 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 Albertb547c852015-03-27 11:20:14 -070066TEST(logging, CHECK) {
Dan Albert58310b42015-03-13 23:06:01 -070067 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
77std::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 Lowbdab59a2015-08-11 16:00:13 -070081 std::string holder(__FILE__);
Dan Albert58310b42015-03-13 23:06:01 -070082 return android::base::StringPrintf(
Spencer Lowbdab59a2015-08-11 16:00:13 -070083 "%c[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+ %s:[[:digit:]]+] %s",
84 log_char, basename(&holder[0]), message);
Dan Albert58310b42015-03-13 23:06:01 -070085}
86
Dan Albertb547c852015-03-27 11:20:14 -070087TEST(logging, LOG) {
Dan Albert58310b42015-03-13 23:06:01 -070088 ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar");
89
Dan Albert5c190402015-04-29 11:32:23 -070090 // 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 Albert58310b42015-03-13 23:06:01 -070093 {
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 Albert5c190402015-04-29 11:32:23 -0700100 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700101
Dan Albert5c190402015-04-29 11:32:23 -0700102#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700103 std::regex message_regex(
104 make_log_pattern(android::base::WARNING, "foobar"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700105 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700106#endif
Dan Albert58310b42015-03-13 23:06:01 -0700107 }
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 Albert5c190402015-04-29 11:32:23 -0700116 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700117
Dan Albert5c190402015-04-29 11:32:23 -0700118#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700119 std::regex message_regex(
120 make_log_pattern(android::base::INFO, "foobar"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700121 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700122#endif
Dan Albert58310b42015-03-13 23:06:01 -0700123 }
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 Albert5c190402015-04-29 11:32:23 -0700143 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700144
Dan Albert5c190402015-04-29 11:32:23 -0700145#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700146 std::regex message_regex(
147 make_log_pattern(android::base::DEBUG, "foobar"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700148 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700149#endif
Dan Albert58310b42015-03-13 23:06:01 -0700150 }
151}
152
Dan Albertb547c852015-03-27 11:20:14 -0700153TEST(logging, PLOG) {
Dan Albert58310b42015-03-13 23:06:01 -0700154 {
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 Albert5c190402015-04-29 11:32:23 -0700162 ASSERT_GT(output.length(), strlen("foobar"));
Dan Albert58310b42015-03-13 23:06:01 -0700163
Dan Albert5c190402015-04-29 11:32:23 -0700164#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700165 std::regex message_regex(make_log_pattern(
166 android::base::INFO, "foobar: No such file or directory"));
Spencer Lowbdab59a2015-08-11 16:00:13 -0700167 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700168#endif
Dan Albert58310b42015-03-13 23:06:01 -0700169 }
170}
171
Dan Albertb547c852015-03-27 11:20:14 -0700172TEST(logging, UNIMPLEMENTED) {
Dan Albert58310b42015-03-13 23:06:01 -0700173 {
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 Albert5c190402015-04-29 11:32:23 -0700181 ASSERT_GT(output.length(), strlen("unimplemented"));
Dan Albert58310b42015-03-13 23:06:01 -0700182
Dan Albert5c190402015-04-29 11:32:23 -0700183#if !defined(_WIN32)
Dan Albert58310b42015-03-13 23:06:01 -0700184 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 Lowbdab59a2015-08-11 16:00:13 -0700188 ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
Dan Albert5c190402015-04-29 11:32:23 -0700189#endif
Dan Albert58310b42015-03-13 23:06:01 -0700190 }
191}