-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
fuchsia_logging_unittest.cc
141 lines (112 loc) · 4.91 KB
/
fuchsia_logging_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/fuchsia/scoped_fx_logger.h"
#include <fidl/base.testfidl/cpp/fidl.h>
#include <fidl/fuchsia.logger/cpp/fidl.h>
#include <lib/async/default.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/sys/cpp/component_context.h>
#include "base/fuchsia/fuchsia_component_connect.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/test_log_listener_safe.h"
#include "base/logging.h"
#include "base/process/process.h"
#include "base/test/scoped_logging_settings.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class MockLogSource {
public:
MOCK_METHOD0(Log, const char*());
};
// Configures `listener` to listen for messages from the current process.
void ListenFilteredByPid(SimpleTestLogListener& listener) {
// Connect the test LogListenerSafe to the Log.
auto log_client_end = fuchsia_component::Connect<fuchsia_logger::Log>();
EXPECT_TRUE(log_client_end.is_ok())
<< FidlConnectionErrorMessage(log_client_end);
fidl::Client log_client(std::move(log_client_end.value()),
async_get_default_dispatcher());
listener.ListenToLog(
log_client,
std::make_unique<fuchsia_logger::LogFilterOptions>(
fuchsia_logger::LogFilterOptions{
{.filter_by_pid = true,
.pid = Process::Current().Pid(),
.min_severity = fuchsia_logger::LogLevelFilter::kInfo}}));
}
} // namespace
// Verifies that calling the log macro goes to the Fuchsia system logs, by
// default.
TEST(FuchsiaLoggingTest, SystemLogging) {
constexpr char kLogMessage[] = "This is FuchsiaLoggingTest.SystemLogging!";
test::SingleThreadTaskEnvironment task_environment_{
test::SingleThreadTaskEnvironment::MainThreadType::IO};
SimpleTestLogListener listener;
ListenFilteredByPid(listener);
// Ensure that logging is directed to the system debug log.
logging::ScopedLoggingSettings scoped_logging_settings;
CHECK(logging::InitLogging({.logging_dest = logging::LOG_DEFAULT}));
// Emit the test log message, and spin the loop until it is reported to the
// test listener.
LOG(ERROR) << kLogMessage;
absl::optional<fuchsia_logger::LogMessage> logged_message =
listener.RunUntilMessageReceived(kLogMessage);
ASSERT_TRUE(logged_message.has_value());
EXPECT_EQ(logged_message->severity(),
static_cast<int32_t>(fuchsia_logger::LogLevelFilter::kError));
ASSERT_EQ(logged_message->tags().size(), 1u);
EXPECT_EQ(logged_message->tags()[0], "base_unittests__exec");
}
// Verifies that configuring a system logger with multiple tags works.
TEST(FuchsiaLoggingTest, SystemLoggingMultipleTags) {
constexpr char kLogMessage[] =
"This is FuchsiaLoggingTest.SystemLoggingMultipleTags!";
const std::vector<StringPiece> kTags = {"tag1", "tag2"};
test::SingleThreadTaskEnvironment task_environment_{
test::SingleThreadTaskEnvironment::MainThreadType::IO};
SimpleTestLogListener listener;
ListenFilteredByPid(listener);
// Connect the test LogListenerSafe to the Log.
auto log_sink_client_end =
fuchsia_component::Connect<fuchsia_logger::LogSink>();
EXPECT_TRUE(log_sink_client_end.is_ok())
<< FidlConnectionErrorMessage(log_sink_client_end);
// Create a logger with multiple tags and emit a message to it.
ScopedFxLogger logger = ScopedFxLogger::CreateFromLogSink(
std::move(log_sink_client_end.value()), kTags);
logger.LogMessage("", 0, kLogMessage, FUCHSIA_LOG_ERROR);
absl::optional<fuchsia_logger::LogMessage> logged_message =
listener.RunUntilMessageReceived(kLogMessage);
ASSERT_TRUE(logged_message.has_value());
auto tags = std::vector<StringPiece>(logged_message->tags().begin(),
logged_message->tags().end());
EXPECT_EQ(tags, kTags);
}
// Verifies the Fuchsia-specific ZX_*() logging macros.
TEST(FuchsiaLoggingTest, FuchsiaLogging) {
MockLogSource mock_log_source;
EXPECT_CALL(mock_log_source, Log())
.Times(DCHECK_IS_ON() ? 2 : 1)
.WillRepeatedly(testing::Return("log message"));
logging::ScopedLoggingSettings scoped_logging_settings;
logging::SetMinLogLevel(logging::LOGGING_INFO);
EXPECT_TRUE(LOG_IS_ON(INFO));
EXPECT_EQ(DCHECK_IS_ON(), DLOG_IS_ON(INFO));
ZX_LOG(INFO, ZX_ERR_INTERNAL) << mock_log_source.Log();
ZX_DLOG(INFO, ZX_ERR_INTERNAL) << mock_log_source.Log();
ZX_CHECK(true, ZX_ERR_INTERNAL);
ZX_DCHECK(true, ZX_ERR_INTERNAL);
}
TEST(FuchsiaLoggingTest, ConnectionErrorMessage) {
zx::result<fidl::ClientEnd<base_testfidl::TestInterface>> result =
zx::error_result{ZX_ERR_PEER_CLOSED};
EXPECT_EQ(
"Failed to connect to base.testfidl.TestInterface: "
"ZX_ERR_PEER_CLOSED",
base::FidlConnectionErrorMessage(result));
}
} // namespace base