xref: /aosp_15_r20/external/cronet/base/fuchsia/fuchsia_logging_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/fuchsia/fuchsia_logging.h"
6 
7 #include <fidl/base.testfidl/cpp/fidl.h>
8 #include <fidl/fuchsia.logger/cpp/fidl.h>
9 #include <lib/async/default.h>
10 #include <lib/fidl/cpp/binding.h>
11 #include <lib/sys/cpp/component_context.h>
12 
13 #include <string_view>
14 
15 #include "base/fuchsia/fuchsia_component_connect.h"
16 #include "base/fuchsia/scoped_fx_logger.h"
17 #include "base/fuchsia/test_component_context_for_process.h"
18 #include "base/fuchsia/test_log_listener_safe.h"
19 #include "base/logging.h"
20 #include "base/test/scoped_logging_settings.h"
21 #include "base/test/task_environment.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 
25 namespace base {
26 
27 namespace {
28 
29 class MockLogSource {
30  public:
31   MOCK_METHOD0(Log, const char*());
32 };
33 
34 }  // namespace
35 
36 // Verifies that calling the log macro goes to the Fuchsia system logs, by
37 // default.
TEST(FuchsiaLoggingTest,SystemLogging)38 TEST(FuchsiaLoggingTest, SystemLogging) {
39   constexpr char kLogMessage[] = "This is FuchsiaLoggingTest.SystemLogging!";
40 
41   test::SingleThreadTaskEnvironment task_environment_{
42       test::SingleThreadTaskEnvironment::MainThreadType::IO};
43   SimpleTestLogListener listener;
44   ListenFilteredByCurrentProcessId(listener);
45 
46   // Ensure that logging is directed to the system debug log.
47   logging::ScopedLoggingSettings scoped_logging_settings;
48   CHECK(logging::InitLogging({.logging_dest = logging::LOG_DEFAULT}));
49 
50   // Emit the test log message, and spin the loop until it is reported to the
51   // test listener.
52   LOG(ERROR) << kLogMessage;
53 
54   std::optional<fuchsia_logger::LogMessage> logged_message =
55       listener.RunUntilMessageReceived(kLogMessage);
56 
57   ASSERT_TRUE(logged_message.has_value());
58   EXPECT_EQ(logged_message->severity(),
59             static_cast<int32_t>(fuchsia_logger::LogLevelFilter::kError));
60   ASSERT_EQ(logged_message->tags().size(), 1u);
61 
62   EXPECT_EQ(logged_message->tags()[0], "base_unittests__exec");
63 }
64 
65 // Verifies that configuring a system logger with multiple tags works.
TEST(FuchsiaLoggingTest,SystemLoggingMultipleTags)66 TEST(FuchsiaLoggingTest, SystemLoggingMultipleTags) {
67   constexpr char kLogMessage[] =
68       "This is FuchsiaLoggingTest.SystemLoggingMultipleTags!";
69   const std::vector<std::string_view> kTags = {"tag1", "tag2"};
70 
71   test::SingleThreadTaskEnvironment task_environment_{
72       test::SingleThreadTaskEnvironment::MainThreadType::IO};
73   SimpleTestLogListener listener;
74   ListenFilteredByCurrentProcessId(listener);
75 
76   // Connect the test LogListenerSafe to the Log.
77   auto log_sink_client_end =
78       fuchsia_component::Connect<fuchsia_logger::LogSink>();
79   EXPECT_TRUE(log_sink_client_end.is_ok())
80       << FidlConnectionErrorMessage(log_sink_client_end);
81 
82   // Create a logger with multiple tags and emit a message to it.
83   ScopedFxLogger logger = ScopedFxLogger::CreateFromLogSink(
84       std::move(log_sink_client_end.value()), kTags);
85   logger.LogMessage("", 0, kLogMessage, FUCHSIA_LOG_ERROR);
86 
87   std::optional<fuchsia_logger::LogMessage> logged_message =
88       listener.RunUntilMessageReceived(kLogMessage);
89 
90   ASSERT_TRUE(logged_message.has_value());
91   auto tags = std::vector<std::string_view>(logged_message->tags().begin(),
92                                             logged_message->tags().end());
93   EXPECT_EQ(tags, kTags);
94 }
95 
96 // Verifies the Fuchsia-specific ZX_*() logging macros.
TEST(FuchsiaLoggingTest,FuchsiaLogging)97 TEST(FuchsiaLoggingTest, FuchsiaLogging) {
98   MockLogSource mock_log_source;
99   EXPECT_CALL(mock_log_source, Log())
100       .Times(DCHECK_IS_ON() ? 2 : 1)
101       .WillRepeatedly(testing::Return("log message"));
102 
103   logging::ScopedLoggingSettings scoped_logging_settings;
104   logging::SetMinLogLevel(logging::LOGGING_INFO);
105 
106   EXPECT_TRUE(LOG_IS_ON(INFO));
107   EXPECT_EQ(DCHECK_IS_ON(), DLOG_IS_ON(INFO));
108 
109   ZX_LOG(INFO, ZX_ERR_INTERNAL) << mock_log_source.Log();
110   ZX_DLOG(INFO, ZX_ERR_INTERNAL) << mock_log_source.Log();
111 
112   ZX_CHECK(true, ZX_ERR_INTERNAL);
113   ZX_DCHECK(true, ZX_ERR_INTERNAL);
114 }
115 
TEST(FuchsiaLoggingTest,ConnectionErrorMessage)116 TEST(FuchsiaLoggingTest, ConnectionErrorMessage) {
117   zx::result<fidl::ClientEnd<base_testfidl::TestInterface>> result =
118       zx::error_result{ZX_ERR_PEER_CLOSED};
119 
120   EXPECT_EQ(
121       "Failed to connect to base.testfidl.TestInterface: "
122       "ZX_ERR_PEER_CLOSED",
123       base::FidlConnectionErrorMessage(result));
124 }
125 
TEST(FuchsiaLoggingTest,FidlMethodErrorMessage_TwoWay)126 TEST(FuchsiaLoggingTest, FidlMethodErrorMessage_TwoWay) {
127   fidl::Result<base_testfidl::TestInterface::Add> result =
128       fit::error(fidl::Status::Unbound());
129 
130   EXPECT_EQ(
131       "Error calling Add: FIDL operation failed due to user initiated unbind, "
132       "status: ZX_ERR_CANCELED (-23), detail: unbound endpoint",
133       base::FidlMethodResultErrorMessage(result, "Add"));
134 }
135 
TEST(FuchsiaLoggingTest,FidlMethodErrorMessage_OneWay)136 TEST(FuchsiaLoggingTest, FidlMethodErrorMessage_OneWay) {
137   fit::result<fidl::OneWayError> result = fit::error(fidl::Status::Unbound());
138 
139   EXPECT_EQ(
140       "Error calling Add: FIDL operation failed due to user initiated unbind, "
141       "status: ZX_ERR_CANCELED (-23), detail: unbound endpoint",
142       base::FidlMethodResultErrorMessage(result, "Add"));
143 }
144 
TEST(FuchsiaLoggingTest,FidlBindingClosureWarningLogger)145 TEST(FuchsiaLoggingTest, FidlBindingClosureWarningLogger) {
146   test::SingleThreadTaskEnvironment task_environment{
147       test::SingleThreadTaskEnvironment::MainThreadType::IO};
148   SimpleTestLogListener listener;
149 
150   // Ensure that logging is directed to the system debug log.
151   logging::ScopedLoggingSettings scoped_logging_settings;
152   TestComponentContextForProcess test_context;
153   test_context.AddService(fidl::DiscoverableProtocolName<fuchsia_logger::Log>);
154   ListenFilteredByCurrentProcessId(listener);
155 
156   // Initialize logging in the `scoped_logging_settings_`.
157   CHECK(logging::InitLogging({.logging_dest = logging::LOG_DEFAULT}));
158 
159   base::FidlBindingClosureWarningLogger<base_testfidl::TestInterface>()(
160       fidl::UnbindInfo::PeerClosed(ZX_ERR_PEER_CLOSED));
161 
162   std::optional<fuchsia_logger::LogMessage> logged_message =
163       listener.RunUntilMessageReceived(
164           "base.testfidl.TestInterface unbound: ZX_ERR_PEER_CLOSED (-24)");
165 
166   ASSERT_TRUE(logged_message.has_value());
167   EXPECT_EQ(logged_message->severity(),
168             static_cast<int32_t>(fuchsia_logger::LogLevelFilter::kWarn));
169 }
170 
171 }  // namespace base
172