Debian Codegraph @

unittest/gunit/group_replication/group_replication_gcs_mysql_network_provider-t.cc

raw symbols SCIP
1 /* Copyright (c) 2021, 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 "plugin/group_replication/include/gcs_mysql_network_provider.h"
25
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28
29 void handle_group_replication_incoming_connection(THD *thd, int fd,
30 SSL *ssl_ctx) {
31 (void)thd;
32 (void)fd;
33 (void)ssl_ctx;
34 }
35
36 // To fool the compiler
37 ulong get_components_stop_timeout_var() { return 0; }
38
39 namespace group_replication_gcs_mysql_networkprovidertest {
40
41 class mock_gcs_mysql_network_provider_auth_interface
42 : public Gcs_mysql_network_provider_auth_interface {
43 public:
44 MOCK_METHOD(bool, get_credentials,
45 (std::string & username, std::string &password), (override));
46 };
47
48 class mock_gcs_mysql_network_provider_native_interface
49 : public Gcs_mysql_network_provider_native_interface {
50 public:
51 MOCK_METHOD(MYSQL *, mysql_real_connect,
52 (MYSQL * mysql, const char *host, const char *user,
53 const char *passwd, const char *db, unsigned int port,
54 const char *unix_socket, unsigned long clientflag),
55 (override));
56 MOCK_METHOD(bool, send_command,
57 (MYSQL * mysql, enum enum_server_command command,
58 const unsigned char *arg, size_t length, bool skip_check),
59 (override));
60 MOCK_METHOD(MYSQL *, mysql_init, (MYSQL * sock), (override));
61 MOCK_METHOD(void, mysql_close, (MYSQL * sock), (override));
62 MOCK_METHOD(void, mysql_free, (void *ptr), (override));
63 MOCK_METHOD(int, channel_get_network_namespace, (std::string & net_ns),
64 (override));
65 MOCK_METHOD(bool, set_network_namespace,
66 (const std::string &network_namespace), (override));
67 MOCK_METHOD(bool, restore_original_network_namespace, (), (override));
68
69 MOCK_METHOD(int, mysql_options,
70 (MYSQL * mysql, enum mysql_option option, const void *arg),
71 (override));
72 MOCK_METHOD(bool, mysql_ssl_set,
73 (MYSQL * mysql, const char *key, const char *cert, const char *ca,
74 const char *capath, const char *cipher),
75 (override));
76 };
77
78 /**
79 * @brief Unit Test Fixture for Gcs_mysql_network_provider.
80 *
81 * It is intended to provide as much coverage as possible to
82 * Gcs_mysql_network_provider. For that, it uses a series of mocks and
83 * dependecy injection mechanisms to reach its goal.
84 *
85 * The tests are:
86 *
87 * MySQLNetworkProviderTest.StartAndStopTest
88 * Tests if we are able to start and stop the provider
89 *
90 * MySQLNetworkProviderTest.StartAgainAndStopTest
91 * Tests if we are able to start the provider twice and stop it
92 *
93 * MySQLNetworkProviderTest.CreateConnectionToSelfTest
94 * Test if one is able to connect successfully
95 *
96 * MySQLNetworkProviderTest.CreateConnectionToSelfWithSSLTest
97 * Test if one is able to connect successfully using SSL
98 *
99 * MySQLNetworkProviderTest.CreateConnectionToSelfCredentialsErrorTest
100 * Test error case when getting credentials
101 *
102 * MySQLNetworkProviderTest.CreateConnectionToSelfRealConnectErrorTest
103 * Test error case when connecting
104 *
105 * MySQLNetworkProviderTest.CreateConnectionToSelfSendCommandErrorTest
106 * Test error case when sending a command
107 *
108 * MySQLNetworkProviderTest.NewServerConnectionTest
109 * Test receiving a new connection from the outside
110 */
111
112 class MySQLNetworkProviderTest : public ::testing::Test {
113 protected:
114 MySQLNetworkProviderTest() {}
115
116 virtual void SetUp() {
117 auth_interface = new mock_gcs_mysql_network_provider_auth_interface();
118 native_interface = new mock_gcs_mysql_network_provider_native_interface();
119 }
120
121 virtual void TearDown() {
122 delete auth_interface;
123 delete native_interface;
124 }
125
126 mock_gcs_mysql_network_provider_auth_interface *auth_interface;
127 mock_gcs_mysql_network_provider_native_interface *native_interface;
128 };
129
130 TEST_F(MySQLNetworkProviderTest, StartAndStopTest) {
131 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
132
133 ASSERT_FALSE(net_provider.start().first);
134 net_provider.stop();
135 }
136
137 TEST_F(MySQLNetworkProviderTest, StartAgainAndStopTest) {
138 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
139
140 ASSERT_FALSE(net_provider.start().first);
141 ASSERT_FALSE(net_provider.start().first);
142
143 net_provider.stop();
144 }
145
146 TEST_F(MySQLNetworkProviderTest, CreateConnectionToSelfTest) {
147 EXPECT_CALL(*auth_interface, get_credentials(testing::_, testing::_))
148 .Times(1)
149 .WillRepeatedly(testing::Return(false));
150
151 MYSQL *fake_conn = (MYSQL *)malloc(sizeof(MYSQL));
152
153 EXPECT_CALL(*native_interface, mysql_init(testing::_))
154 .Times(1)
155 .WillRepeatedly(testing::Return(fake_conn));
156
157 EXPECT_CALL(
158 *native_interface,
159 mysql_real_connect(testing::_, testing::_, testing::_, testing::_,
160 testing::_, testing::_, testing::_, testing::_))
161 .Times(1)
162 .WillRepeatedly(testing::Return(fake_conn));
163
164 EXPECT_CALL(
165 *native_interface,
166 send_command(testing::_, testing::_, testing::_, testing::_, testing::_))
167 .Times(1)
168 .WillRepeatedly(testing::Return(false));
169
170 EXPECT_CALL(*native_interface, channel_get_network_namespace(testing::_))
171 .Times(1)
172 .WillRepeatedly(testing::Return(false));
173
174 EXPECT_CALL(*native_interface, set_network_namespace(testing::_)).Times(0);
175
176 EXPECT_CALL(*native_interface, restore_original_network_namespace()).Times(0);
177
178 EXPECT_CALL(*native_interface, mysql_close(testing::_));
179
180 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
181
182 ASSERT_FALSE(net_provider.start().first);
183
184 auto new_connection =
185 net_provider.open_connection("localhost", 12345, {"", "", false});
186
187 ASSERT_NE(-1, new_connection.get()->fd);
188
189 int close_connection_retval = net_provider.close_connection(*new_connection);
190
191 ASSERT_EQ(0, close_connection_retval);
192
193 net_provider.stop();
194
195 free(fake_conn);
196 }
197
198 TEST_F(MySQLNetworkProviderTest, CreateConnectionToSelfWithNameSpaceTest) {
199 EXPECT_CALL(*auth_interface, get_credentials(testing::_, testing::_))
200 .Times(1)
201 .WillRepeatedly(testing::Return(false));
202
203 MYSQL *fake_conn = (MYSQL *)malloc(sizeof(MYSQL));
204
205 EXPECT_CALL(*native_interface, mysql_init(testing::_))
206 .Times(1)
207 .WillRepeatedly(testing::Return(fake_conn));
208
209 EXPECT_CALL(
210 *native_interface,
211 mysql_real_connect(testing::_, testing::_, testing::_, testing::_,
212 testing::_, testing::_, testing::_, testing::_))
213 .Times(1)
214 .WillRepeatedly(testing::Return(fake_conn));
215
216 EXPECT_CALL(
217 *native_interface,
218 send_command(testing::_, testing::_, testing::_, testing::_, testing::_))
219 .Times(1)
220 .WillRepeatedly(testing::Return(false));
221
222 EXPECT_CALL(*native_interface, channel_get_network_namespace(testing::_))
223 .Times(1)
224 .WillRepeatedly(
225 testing::DoAll([](std::string &net_ns) { net_ns.assign("test_ns"); },
226 testing::Return(false)));
227
228 EXPECT_CALL(*native_interface,
229 set_network_namespace(testing::StrEq("test_ns")))
230 .Times(1)
231 .WillRepeatedly(testing::Return(false));
232
233 EXPECT_CALL(*native_interface, restore_original_network_namespace())
234 .Times(1)
235 .WillRepeatedly(testing::Return(false));
236
237 EXPECT_CALL(*native_interface, mysql_close(testing::_));
238
239 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
240
241 ASSERT_FALSE(net_provider.start().first);
242
243 auto new_connection =
244 net_provider.open_connection("localhost", 12345, {"", "", false});
245
246 ASSERT_NE(-1, new_connection.get()->fd);
247
248 int close_connection_retval = net_provider.close_connection(*new_connection);
249
250 ASSERT_EQ(0, close_connection_retval);
251
252 net_provider.stop();
253
254 free(fake_conn);
255 }
256
257 TEST_F(MySQLNetworkProviderTest, CreateConnectionToSelfWithSSLTest) {
258 EXPECT_CALL(*auth_interface, get_credentials(testing::_, testing::_))
259 .Times(1)
260 .WillRepeatedly(testing::Return(false));
261
262 MYSQL *fake_conn = (MYSQL *)malloc(sizeof(MYSQL));
263
264 EXPECT_CALL(*native_interface, mysql_init(testing::_))
265 .Times(1)
266 .WillRepeatedly(testing::Return(fake_conn));
267
268 EXPECT_CALL(
269 *native_interface,
270 mysql_real_connect(testing::_, testing::_, testing::_, testing::_,
271 testing::_, testing::_, testing::_, testing::_))
272 .Times(1)
273 .WillRepeatedly(testing::DoAll(
274 [](MYSQL *mysql, const char *host, const char *user,
275 const char *passwd, const char *db, unsigned int port,
276 const char *unix_socket, unsigned long clientflag) {
277 (void)host;
278 (void)user;
279 (void)passwd;
280 (void)db;
281 (void)port;
282 (void)unix_socket;
283 (void)clientflag;
284 mysql->net.vio = (Vio *)malloc(sizeof(Vio));
285 SSL *fake_ssl_connection = (SSL *)malloc(sizeof(SSL *));
286 mysql->net.vio->ssl_arg = fake_ssl_connection;
287 },
288 testing::Return(fake_conn)));
289
290 EXPECT_CALL(
291 *native_interface,
292 send_command(testing::_, testing::_, testing::_, testing::_, testing::_))
293 .Times(1)
294 .WillRepeatedly(testing::Return(false));
295
296 EXPECT_CALL(*native_interface, mysql_close(testing::_));
297
298 Network_configuration_parameters net_provider_security_params;
299 net_provider_security_params.ssl_params = {
300 SSL_REQUIRED, nullptr, nullptr, nullptr, nullptr,
301 nullptr, nullptr, nullptr, nullptr, nullptr};
302 net_provider_security_params.tls_params = {nullptr, nullptr};
303 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
304
305 net_provider.configure_secure_connections(net_provider_security_params);
306
307 ASSERT_FALSE(net_provider.start().first);
308
309 auto new_connection =
310 net_provider.open_connection("localhost", 12345, {"", "", true});
311
312 ASSERT_NE(-1, new_connection.get()->fd);
313
314 int close_connection_retval = net_provider.close_connection(*new_connection);
315
316 ASSERT_EQ(0, close_connection_retval);
317
318 net_provider.stop();
319
320 free(fake_conn->net.vio->ssl_arg);
321 free(fake_conn->net.vio);
322 free(fake_conn);
323 }
324
325 TEST_F(MySQLNetworkProviderTest, CreateConnectionToSelfCredentialsErrorTest) {
326 EXPECT_CALL(*auth_interface, get_credentials(testing::_, testing::_))
327 .Times(1)
328 .WillOnce(testing::Return(true));
329
330 MYSQL *fake_conn = (MYSQL *)malloc(sizeof(MYSQL));
331
332 EXPECT_CALL(*native_interface, mysql_init(testing::_))
333 .Times(1)
334 .WillRepeatedly(testing::Return(fake_conn));
335
336 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
337
338 ASSERT_FALSE(net_provider.start().first);
339
340 auto new_connection =
341 net_provider.open_connection("localhost", 12345, {"", "", false});
342
343 ASSERT_EQ(-1, new_connection.get()->fd);
344
345 net_provider.stop();
346
347 free(fake_conn);
348 }
349
350 TEST_F(MySQLNetworkProviderTest, CreateConnectionToSelfRealConnectErrorTest) {
351 EXPECT_CALL(*auth_interface, get_credentials(testing::_, testing::_))
352 .Times(1)
353 .WillRepeatedly(testing::Return(false));
354
355 MYSQL *fake_conn = (MYSQL *)malloc(sizeof(MYSQL));
356
357 EXPECT_CALL(*native_interface, mysql_init(testing::_))
358 .Times(1)
359 .WillRepeatedly(testing::Return(fake_conn));
360
361 EXPECT_CALL(
362 *native_interface,
363 mysql_real_connect(testing::_, testing::_, testing::_, testing::_,
364 testing::_, testing::_, testing::_, testing::_))
365 .Times(1)
366 .WillOnce(testing::Return(nullptr));
367
368 EXPECT_CALL(*native_interface, mysql_close(testing::_)).Times(1);
369
370 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
371
372 ASSERT_FALSE(net_provider.start().first);
373
374 auto new_connection =
375 net_provider.open_connection("localhost", 12345, {"", "", false});
376
377 ASSERT_EQ(-1, new_connection.get()->fd);
378
379 net_provider.stop();
380
381 free(fake_conn);
382 }
383
384 TEST_F(MySQLNetworkProviderTest, CreateConnectionToSelfSendCommandErrorTest) {
385 EXPECT_CALL(*auth_interface, get_credentials(testing::_, testing::_))
386 .Times(1)
387 .WillRepeatedly(testing::Return(false));
388
389 MYSQL *fake_conn = (MYSQL *)malloc(sizeof(MYSQL));
390
391 EXPECT_CALL(*native_interface, mysql_init(testing::_))
392 .Times(1)
393 .WillRepeatedly(testing::Return(fake_conn));
394
395 EXPECT_CALL(
396 *native_interface,
397 mysql_real_connect(testing::_, testing::_, testing::_, testing::_,
398 testing::_, testing::_, testing::_, testing::_))
399 .Times(1)
400 .WillRepeatedly(testing::Return(fake_conn));
401
402 EXPECT_CALL(
403 *native_interface,
404 send_command(testing::_, testing::_, testing::_, testing::_, testing::_))
405 .Times(1)
406 .WillRepeatedly(testing::Return(true));
407
408 EXPECT_CALL(*native_interface, mysql_close(testing::_));
409
410 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
411
412 ASSERT_FALSE(net_provider.start().first);
413
414 auto new_connection =
415 net_provider.open_connection("localhost", 12345, {"", "", false});
416
417 ASSERT_EQ(-1, new_connection.get()->fd);
418
419 net_provider.stop();
420
421 free(fake_conn);
422 }
423
424 TEST_F(MySQLNetworkProviderTest, NewServerConnectionTest) {
425 Gcs_mysql_network_provider net_provider(auth_interface, native_interface);
426
427 ASSERT_FALSE(net_provider.start().first);
428
429 constexpr int socket_to_use = 42;
430
431 THD fake_thd(false);
432 // Vio *active_vio = (Vio *)malloc(sizeof(Vio));
433 MYSQL_VIO active_vio = vio_new(socket_to_use, VIO_TYPE_TCPIP, 0);
434 active_vio->mysql_socket.fd = socket_to_use;
435 active_vio->vioshutdown = [](Vio *) { return 0; };
436
437 fake_thd.set_active_vio(active_vio);
438
439 Network_connection fake_network_conn(socket_to_use);
440
441 net_provider.set_new_connection(&fake_thd, &fake_network_conn);
442
443 Network_connection *retrieved_network_connection =
444 net_provider.get_new_connection();
445
446 ASSERT_NE(retrieved_network_connection, nullptr);
447 ASSERT_EQ(fake_network_conn.fd, retrieved_network_connection->fd);
448
449 net_provider.stop();
450
451 fake_thd.clear_active_vio();
452 vio_delete(active_vio);
453 }
454
455 TEST_F(MySQLNetworkProviderTest, LogMappingTest) {
456 // Test for default return
457 int coded_log_level = ERROR_LEVEL;
458 network_provider_dynamic_log_level provided_log_level =
459 network_provider_dynamic_log_level::PROVIDED;
460
461 ASSERT_EQ(ERROR_LEVEL, Gcs_mysql_network_provider_util::log_level_adaptation(
462 coded_log_level, provided_log_level));
463
464 // Test for transformed return
465 coded_log_level = ERROR_LEVEL;
466 provided_log_level = network_provider_dynamic_log_level::WARNING;
467
468 ASSERT_EQ(WARNING_LEVEL,
469 Gcs_mysql_network_provider_util::log_level_adaptation(
470 coded_log_level, provided_log_level));
471
472 // Test for out of range transformed return
473 coded_log_level = ERROR_LEVEL;
474 provided_log_level = network_provider_dynamic_log_level::DEBUG;
475
476 ASSERT_EQ(Gcs_mysql_network_provider_util::OUT_OF_RANGE_LOG_LEVEL,
477 Gcs_mysql_network_provider_util::log_level_adaptation(
478 coded_log_level, provided_log_level));
479 }
480
481 } // namespace group_replication_gcs_mysql_networkprovidertest