Debian Codegraph @

unittest/gunit/libmysqlgcs/xcom/gcs_logging_system-t.cc

raw symbols SCIP
1 /* Copyright (c) 2016, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <sstream>
25
26 #include "gcs_base_test.h"
27 #include "my_io.h"
28
29 namespace gcs_logging_unittest {
30
31 class Mock_gcs_log_sink : public Sink_interface {
32 public:
33 MOCK_METHOD1(log_event, void(const std::string &message));
34 MOCK_METHOD2(log_event, void(const char *message, size_t message_size));
35 MOCK_METHOD0(initialize, enum_gcs_error());
36 MOCK_METHOD0(finalize, enum_gcs_error());
37 /* purecov: begin deadcode */
38 MOCK_CONST_METHOD0(get_information, const std::string());
39 /* purecov: end */
40 };
41
42 class LoggingDebuggingSystemTest : public GcsBaseTestNoLogging {
43 protected:
44 LoggingDebuggingSystemTest()
45 : common_sink(nullptr), logger(nullptr), debugger(nullptr) {}
46
47 void SetUp() override {
48 common_sink = new Gcs_async_buffer(new Mock_gcs_log_sink());
49 logger = new Gcs_default_logger(common_sink);
50 debugger = new Gcs_default_debugger(common_sink);
51 }
52
53 void TearDown() override {
54 Gcs_log_manager::finalize();
55
56 delete logger;
57 logger = nullptr;
58
59 Gcs_debug_manager::finalize();
60
61 delete debugger;
62 debugger = nullptr;
63
64 delete common_sink;
65 common_sink = nullptr;
66 }
67
68 Gcs_async_buffer *common_sink;
69 Gcs_default_logger *logger;
70 Gcs_default_debugger *debugger;
71 };
72
73 TEST_F(LoggingDebuggingSystemTest, DefaultLifecycle) {
74 Mock_gcs_log_sink *mock_sink = static_cast<Mock_gcs_log_sink *>(
75 static_cast<Gcs_async_buffer *>(common_sink)->get_sink());
76
77 ON_CALL(*mock_sink, initialize()).WillByDefault(Return(GCS_OK));
78 ON_CALL(*mock_sink, finalize()).WillByDefault(Return(GCS_OK));
79
80 // on some machines an info message will be displayed stating
81 // that a network interface was not successfully probed
82 // we cannot predict how many network interfaces are in the
83 // machine that cannot be probed
84 EXPECT_CALL(
85 *mock_sink,
86 log_event(ContainsRegex("\\[GCS\\] Unable to probe network interface .*"),
87 _))
88 .Times(AnyNumber());
89
90 /*
91 The following message is expected.
92 */
93 EXPECT_CALL(*mock_sink,
94 log_event(ContainsRegex("\\[GCS\\] SSL was not enabled.*"), _))
95 .Times(1);
96
97 /*
98 3 messages of this form, corresponding to the 3 logging levels, are
99 expected.
100 */
101 EXPECT_CALL(
102 *mock_sink,
103 log_event(ContainsRegex("This message belongs to logging level .*"), _))
104 .Times(3);
105
106 ASSERT_EQ(true, Gcs_log_manager::get_logger() == nullptr);
107 ASSERT_EQ(true, Gcs_debug_manager::get_debugger() == nullptr);
108
109 Gcs_log_manager::initialize(logger);
110 Gcs_debug_manager::initialize(debugger);
111
112 Gcs_group_identifier *group_id = new Gcs_group_identifier("only_group");
113 Gcs_interface_parameters if_params;
114
115 if_params.add_parameter("group_name", group_id->get_group_id());
116 if_params.add_parameter("peer_nodes", "127.0.0.1:12345");
117 if_params.add_parameter("local_node", "127.0.0.1:12345");
118 if_params.add_parameter("bootstrap_group", "true");
119 if_params.add_parameter("poll_spin_loops", "100");
120
121 // just to make the log entries count below deterministic, otherwise,
122 // there would be additional info messages due to automatically adding
123 // addresses to the allowlist
124 if_params.add_parameter("ip_allowlist", Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
125
126 Gcs_interface *xcom_if = Gcs_xcom_interface::get_interface();
127 enum_gcs_error initialized = xcom_if->initialize(if_params);
128
129 ASSERT_EQ(GCS_OK, initialized);
130
131 ASSERT_EQ(true, Gcs_log_manager::get_logger() != nullptr);
132 ASSERT_EQ(true, Gcs_debug_manager::get_debugger() != nullptr);
133
134 gcs_log_level_t level;
135
136 for (int i = GCS_ERROR; i <= GCS_INFO; i++) {
137 level = static_cast<gcs_log_level_t>(i);
138 std::string msg("This message belongs to logging level ");
139 msg += gcs_log_levels[level];
140
141 const char *c_msg = msg.c_str();
142
143 Gcs_log_manager::get_logger()->log_event(level, c_msg);
144 }
145
146 enum_gcs_error finalize_error = xcom_if->finalize();
147 ASSERT_EQ(GCS_OK, finalize_error);
148
149 Gcs_xcom_interface::cleanup();
150
151 delete group_id;
152
153 ASSERT_EQ(true, Gcs_log_manager::get_logger() == nullptr);
154 ASSERT_EQ(true, Gcs_debug_manager::get_debugger() == nullptr);
155 }
156
157 #ifndef XCOM_STANDALONE
158 class Wrapper_file_sink : public Sink_interface {
159 public:
160 Wrapper_file_sink(Gcs_file_sink *sink) : m_sink(sink) {
161 ON_CALL(*this, log_event(_))
162 .WillByDefault(Invoke(
163 m_sink, static_cast<void (Gcs_file_sink::*)(const std::string &)>(
164 &Gcs_file_sink::log_event)));
165 ON_CALL(*this, log_event(_, _))
166 .WillByDefault(Invoke(
167 m_sink, static_cast<void (Gcs_file_sink::*)(const char *, size_t)>(
168 &Gcs_file_sink::log_event)));
169 ON_CALL(*this, initialize())
170 .WillByDefault(Invoke(m_sink, &Gcs_file_sink::initialize));
171 ON_CALL(*this, finalize())
172 .WillByDefault(Invoke(m_sink, &Gcs_file_sink::finalize));
173 ON_CALL(*this, get_file_name(_))
174 .WillByDefault(Invoke(m_sink, &Gcs_file_sink::get_file_name));
175 ON_CALL(*this, get_information())
176 .WillByDefault(Invoke(m_sink, &Gcs_file_sink::get_information));
177 }
178
179 ~Wrapper_file_sink() { delete m_sink; }
180
181 MOCK_METHOD1(log_event, void(const std::string &message));
182 MOCK_METHOD2(log_event, void(const char *message, size_t message_size));
183 MOCK_METHOD0(initialize, enum_gcs_error());
184 MOCK_METHOD0(finalize, enum_gcs_error());
185 MOCK_METHOD1(get_file_name, enum_gcs_error(char *file_name_buffer));
186 MOCK_CONST_METHOD0(get_information, const std::string());
187
188 private:
189 Gcs_file_sink *m_sink;
190 };
191
192 class FileOutputSystemTest : public GcsBaseTestNoLogging {};
193
194 TEST_F(FileOutputSystemTest, SinkFileTest) {
195 /*
196 Definition of local variables.
197 */
198 char file_name_buffer[FN_REFLEN];
199 std::stringstream buffer;
200 std::string repeat("/unknown");
201 size_t base = repeat.length();
202 size_t size = 0;
203 File m_fd;
204 uchar read_buffer[10];
205 read_buffer[0] = '\0';
206 std::string info("testing");
207
208 /*
209 Try to create a file that exceeds the normal name's length.
210 */
211 *file_name_buffer = 0;
212 for (size = 0; size < FN_REFLEN; size += base) {
213 buffer << repeat;
214 }
215 buffer << "/";
216 Wrapper_file_sink wrapper_big(new Gcs_file_sink("unknown", buffer.str()));
217 ASSERT_EQ(wrapper_big.get_file_name(file_name_buffer), GCS_NOK);
218
219 /*
220 The sink will fail to initialize due to an invalid file
221 */
222 Wrapper_file_sink wrapper_unknown(
223 new Gcs_file_sink("unknown", "/unknown/unknown/"));
224 ASSERT_EQ(wrapper_unknown.initialize(), GCS_NOK);
225 ASSERT_EQ(wrapper_unknown.finalize(), GCS_OK);
226
227 /*
228 Then sink will succeed to initialize and will create a file name
229 gcs-communication.debug
230 */
231 Wrapper_file_sink wrapper(new Gcs_file_sink("gcs-communication", "."));
232 ASSERT_EQ(wrapper.initialize(), GCS_OK);
233 ASSERT_EQ(wrapper.get_file_name(file_name_buffer), GCS_OK);
234 ASSERT_TRUE((m_fd = my_open(file_name_buffer, 0 /* O_RDONLY */, MYF(0))) > 0);
235 wrapper.log_event(info);
236 ASSERT_EQ(my_read(m_fd, read_buffer, info.length(), MYF(0)), info.length());
237 read_buffer[info.length()] = '\0';
238 ASSERT_EQ(info.compare(0, info.length(), (char *)read_buffer), 0);
239 ASSERT_EQ(wrapper.finalize(), GCS_OK);
240 ASSERT_EQ(my_close(m_fd, MYF(0)), 0);
241 }
242 #endif /* XCOM_STANDALONE */
243
244 } // namespace gcs_logging_unittest