1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/proximity_auth/logging/logging.h"
7 #include "base/lazy_instance.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/proximity_auth/logging/log_buffer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace proximity_auth
{
17 const char kLog1
[] = "Mahogony destined to make a sturdy table";
18 const char kLog2
[] = "Construction grade cedar";
19 const char kLog3
[] = "Pine infested by hungry beetles";
21 // Called for every log message added to the standard logging system. The new
22 // log is saved in |g_standard_logs| and consumed so it does not flood stdout.
23 base::LazyInstance
<std::vector
<std::string
>> g_standard_logs
=
24 LAZY_INSTANCE_INITIALIZER
;
25 bool HandleStandardLogMessage(int severity
,
29 const std::string
& str
) {
30 g_standard_logs
.Get().push_back(str
);
36 class ProximityAuthLoggingTest
: public testing::Test
{
38 ProximityAuthLoggingTest() : previous_handler_(NULL
) {}
40 void SetUp() override
{
41 LogBuffer::GetInstance()->Clear();
42 g_standard_logs
.Get().clear();
44 previous_handler_
= logging::GetLogMessageHandler();
45 logging::SetLogMessageHandler(&HandleStandardLogMessage
);
48 void TearDown() override
{ logging::SetLogMessageHandler(previous_handler_
); }
51 logging::LogMessageHandlerFunction previous_handler_
;
54 TEST_F(ProximityAuthLoggingTest
, LogsSavedToBuffer
) {
55 int base_line_number
= __LINE__
;
56 PA_LOG(INFO
) << kLog1
;
57 PA_LOG(WARNING
) << kLog2
;
58 PA_LOG(ERROR
) << kLog3
;
60 auto logs
= LogBuffer::GetInstance()->logs();
61 ASSERT_EQ(3u, logs
->size());
63 auto iterator
= logs
->begin();
64 const LogBuffer::LogMessage
& log_message1
= *iterator
;
65 EXPECT_EQ(kLog1
, log_message1
.text
);
66 EXPECT_EQ(__FILE__
, log_message1
.file
);
67 EXPECT_EQ(base_line_number
+ 1, log_message1
.line
);
68 EXPECT_EQ(logging::LOG_INFO
, log_message1
.severity
);
71 const LogBuffer::LogMessage
& log_message2
= *iterator
;
72 EXPECT_EQ(kLog2
, log_message2
.text
);
73 EXPECT_EQ(__FILE__
, log_message2
.file
);
74 EXPECT_EQ(base_line_number
+ 2, log_message2
.line
);
75 EXPECT_EQ(logging::LOG_WARNING
, log_message2
.severity
);
78 const LogBuffer::LogMessage
& log_message3
= *iterator
;
79 EXPECT_EQ(kLog3
, log_message3
.text
);
80 EXPECT_EQ(__FILE__
, log_message3
.file
);
81 EXPECT_EQ(base_line_number
+ 3, log_message3
.line
);
82 EXPECT_EQ(logging::LOG_ERROR
, log_message3
.severity
);
85 TEST_F(ProximityAuthLoggingTest
, LogWhenBufferIsFull
) {
86 LogBuffer
* log_buffer
= LogBuffer::GetInstance();
87 EXPECT_EQ(0u, log_buffer
->logs()->size());
89 for (size_t i
= 0; i
< log_buffer
->MaxBufferSize(); ++i
) {
90 PA_LOG(INFO
) << "log " << i
;
93 EXPECT_EQ(log_buffer
->MaxBufferSize(), log_buffer
->logs()->size());
94 PA_LOG(INFO
) << kLog1
;
95 EXPECT_EQ(log_buffer
->MaxBufferSize(), log_buffer
->logs()->size());
97 auto iterator
= log_buffer
->logs()->begin();
98 for (size_t i
= 0; i
< log_buffer
->MaxBufferSize() - 1; ++iterator
, ++i
) {
99 std::string expected_text
=
100 "log " + base::IntToString(base::saturated_cast
<int>(i
+ 1));
101 EXPECT_EQ(expected_text
, (*iterator
).text
);
103 EXPECT_EQ(kLog1
, (*iterator
).text
);
106 TEST_F(ProximityAuthLoggingTest
, StandardLogsCreated
) {
107 PA_LOG(INFO
) << kLog1
;
108 PA_LOG(WARNING
) << kLog2
;
109 PA_LOG(ERROR
) << kLog3
;
111 ASSERT_EQ(3u, g_standard_logs
.Get().size());
112 EXPECT_NE(std::string::npos
, g_standard_logs
.Get()[0].find(kLog1
));
113 EXPECT_NE(std::string::npos
, g_standard_logs
.Get()[1].find(kLog2
));
114 EXPECT_NE(std::string::npos
, g_standard_logs
.Get()[2].find(kLog3
));
117 } // namespace proximity_auth