blob: 94920e7f1a06afda61c88a970d0c5fb2a89a4c4a [file] [log] [blame]
Yavor Goulishev9c08e842020-04-29 14:03:33 -07001// Copyright 2015 The Crashpad Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "util/thread/thread_log_messages.h"
16
17#include <string.h>
18#include <sys/types.h>
19
20#include "base/logging.h"
21#include "base/stl_util.h"
22#include "base/strings/stringprintf.h"
23#include "gtest/gtest.h"
24#include "util/thread/thread.h"
25
26namespace crashpad {
27namespace test {
28namespace {
29
30TEST(ThreadLogMessages, Empty) {
31 ThreadLogMessages thread_log_messages;
32
33 const std::vector<std::string>& log_messages =
34 thread_log_messages.log_messages();
35
36 EXPECT_TRUE(log_messages.empty());
37}
38
39// For a message formatted like "[preamble] message\n", returns just "message".
40// If the message is not formatted as expected, a gtest expectation failure will
41// be recorded and this function will return an empty string.
42std::string MessageString(const std::string& log_message) {
43 if (log_message.size() < 1) {
44 EXPECT_GE(log_message.size(), 1u);
45 return std::string();
46 }
47
48 constexpr char kStartChar = '[';
49 if (log_message[0] != kStartChar) {
50 EXPECT_EQ(log_message[0], kStartChar);
51 return std::string();
52 }
53
54 static constexpr char kFindString[] = "] ";
55 size_t pos = log_message.find(kFindString);
56 if (pos == std::string::npos) {
57 EXPECT_NE(pos, std::string::npos);
58 return std::string();
59 }
60
61 std::string message_string = log_message.substr(pos + strlen(kFindString));
62 if (message_string.size() < 1) {
63 EXPECT_GE(message_string.size(), 1u);
64 return std::string();
65 }
66
67 constexpr char kEndChar = '\n';
68 if (message_string[message_string.size() - 1] != kEndChar) {
69 EXPECT_NE(message_string[message_string.size() - 1], kEndChar);
70 return std::string();
71 }
72
73 message_string.resize(message_string.size() - 1);
74 return message_string;
75}
76
77TEST(ThreadLogMessages, Basic) {
78 // Logging must be enabled at least at this level for this test to work.
79 ASSERT_TRUE(LOG_IS_ON(INFO));
80
81 {
82 static constexpr const char* kMessages[] = {
83 "An info message",
84 "A warning message",
85 "An error message",
86 };
87
88 ThreadLogMessages thread_log_messages;
89
90 LOG(INFO) << kMessages[0];
91 LOG(WARNING) << kMessages[1];
92 LOG(ERROR) << kMessages[2];
93
94 const std::vector<std::string>& log_messages =
95 thread_log_messages.log_messages();
96
97 EXPECT_EQ(log_messages.size(), base::size(kMessages));
98 for (size_t index = 0; index < base::size(kMessages); ++index) {
99 EXPECT_EQ(MessageString(log_messages[index]), kMessages[index])
100 << "index " << index;
101 }
102 }
103
104 {
105 static constexpr char kMessage[] = "Sample error message";
106
107 ThreadLogMessages thread_log_messages;
108
109 LOG(ERROR) << kMessage;
110
111 const std::vector<std::string>& log_messages =
112 thread_log_messages.log_messages();
113
114 EXPECT_EQ(log_messages.size(), 1u);
115 EXPECT_EQ(MessageString(log_messages[0]), kMessage);
116 }
117
118 {
119 ThreadLogMessages thread_log_messages;
120
121 LOG(INFO) << "I can't believe I " << "streamed" << " the whole thing.";
122
123 const std::vector<std::string>& log_messages =
124 thread_log_messages.log_messages();
125
126 EXPECT_EQ(log_messages.size(), 1u);
127 EXPECT_EQ(MessageString(log_messages[0]),
128 "I can't believe I streamed the whole thing.");
129 }
130}
131
132class LoggingTestThread : public Thread {
133 public:
134 LoggingTestThread() : thread_number_(0), start_(0), count_(0) {}
135 ~LoggingTestThread() override {}
136
137 void Initialize(size_t thread_number, int start, int count) {
138 thread_number_ = thread_number;
139 start_ = start;
140 count_ = count;
141 }
142
143 private:
144 void ThreadMain() override {
145 ThreadLogMessages thread_log_messages;
146
147 std::vector<std::string> expected_messages;
148 for (int index = start_; index < start_ + count_; ++index) {
149 std::string message = base::StringPrintf("message %d", index);
150 expected_messages.push_back(message);
151 LOG(WARNING) << message;
152 }
153
154 const std::vector<std::string>& log_messages =
155 thread_log_messages.log_messages();
156
157 ASSERT_EQ(log_messages.size(), static_cast<size_t>(count_));
158 for (size_t index = 0; index < log_messages.size(); ++index) {
159 EXPECT_EQ(MessageString(log_messages[index]), expected_messages[index])
160 << "thread_number_ " << thread_number_ << ", index " << index;
161 }
162 }
163
164 size_t thread_number_;
165 int start_;
166 int count_;
167
168 DISALLOW_COPY_AND_ASSIGN(LoggingTestThread);
169};
170
171TEST(ThreadLogMessages, Multithreaded) {
172 // Logging must be enabled at least at this level for this test to work.
173 ASSERT_TRUE(LOG_IS_ON(WARNING));
174
175 LoggingTestThread threads[20];
176 int start = 0;
177 for (size_t index = 0; index < base::size(threads); ++index) {
178 threads[index].Initialize(
179 index, static_cast<int>(start), static_cast<int>(index));
180 start += static_cast<int>(index);
181
182 ASSERT_NO_FATAL_FAILURE(threads[index].Start());
183 }
184
185 for (LoggingTestThread& thread : threads) {
186 thread.Join();
187 }
188}
189
190} // namespace
191} // namespace test
192} // namespace crashpad