Debian Codegraph @

unittest/gunit/xplugin/xpl/capabilities_handlers_t.cc

raw symbols SCIP
1 /* Copyright (c) 2015, 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 <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 #include "my_config.h" // NOLINT(build/include_subdir)
28
29 #include "plugin/x/src/account_verification_handler.h"
30 #include "plugin/x/src/capabilities/handler_auth_mech.h"
31 #include "plugin/x/src/capabilities/handler_client_interactive.h"
32 #include "plugin/x/src/capabilities/handler_connection_attributes.h"
33 #include "plugin/x/src/capabilities/handler_tls.h"
34 #include "plugin/x/src/ngs/client_list.h"
35 #include "plugin/x/src/sql_user_require.h"
36 #include "unittest/gunit/xplugin/xpl/assert_error_code.h"
37 #include "unittest/gunit/xplugin/xpl/mock/authentication_container.h"
38 #include "unittest/gunit/xplugin/xpl/mock/client.h"
39 #include "unittest/gunit/xplugin/xpl/mock/server.h"
40 #include "unittest/gunit/xplugin/xpl/mock/ssl_context.h"
41 #include "unittest/gunit/xplugin/xpl/mock/vio.h"
42 #include "unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h"
43
44 namespace xpl {
45 namespace test {
46
47 using mock::Authentication_container;
48 using ::testing::_;
49 using ::testing::Return;
50 using ::testing::ReturnRef;
51 using ::testing::SetArgPointee;
52 using ::testing::StrictMock;
53 using ::testing::Test;
54 using ::testing::WithParamInterface;
55
56 class CapabilityHanderTlsTestSuite : public Test {
57 public:
58 CapabilityHanderTlsTestSuite() : sut(&mock_client) {
59 EXPECT_CALL(mock_client, connection())
60 .WillRepeatedly(ReturnRef(mock_connection));
61 EXPECT_CALL(mock_client, server()).WillRepeatedly(ReturnRef(mock_server));
62 EXPECT_CALL(mock_server, ssl_context())
63 .WillRepeatedly(Return(&mock_ssl_context));
64 }
65
66 StrictMock<mock::Vio> mock_connection;
67 StrictMock<mock::Client> mock_client;
68 StrictMock<mock::Ssl_context> mock_ssl_context;
69 StrictMock<mock::Server> mock_server;
70
71 Capability_tls sut;
72 };
73
74 TEST_F(
75 CapabilityHanderTlsTestSuite,
76 isSupported_returnsCurrentConnectionOption_on_supported_connection_type) {
77 EXPECT_CALL(mock_ssl_context, has_ssl())
78 .WillOnce(Return(true))
79 .WillOnce(Return(false));
80 EXPECT_CALL(mock_connection, get_type())
81 .WillOnce(Return(xpl::Connection_tcpip))
82 .WillOnce(Return(xpl::Connection_tcpip));
83
84 ASSERT_TRUE(sut.is_supported());
85 ASSERT_FALSE(sut.is_supported());
86 }
87
88 TEST_F(CapabilityHanderTlsTestSuite,
89 isSupported_returnsFailure_on_unsupported_connection_type) {
90 EXPECT_CALL(mock_ssl_context, has_ssl())
91 .WillOnce(Return(true))
92 .WillOnce(Return(false));
93 EXPECT_CALL(mock_connection, get_type())
94 .WillOnce(Return(xpl::Connection_namedpipe))
95 .WillOnce(Return(xpl::Connection_namedpipe));
96
97 ASSERT_FALSE(sut.is_supported());
98 ASSERT_FALSE(sut.is_supported());
99 }
100
101 TEST_F(CapabilityHanderTlsTestSuite, name_returnsTls_always) {
102 ASSERT_STREQ("tls", sut.name().c_str());
103 }
104
105 TEST_F(CapabilityHanderTlsTestSuite,
106 get_returnsCurrentConnectionOption_always) {
107 const bool expected_result = true;
108 ::Mysqlx::Datatypes::Any any;
109
110 EXPECT_CALL(mock_connection, get_type())
111 .WillOnce(Return(xpl::Connection_type::Connection_tls));
112
113 sut.get(&any);
114
115 ASSERT_EQ(::Mysqlx::Datatypes::Any::SCALAR, any.type());
116 ASSERT_EQ(::Mysqlx::Datatypes::Scalar::V_BOOL, any.scalar().type());
117 ASSERT_EQ(expected_result, any.scalar().v_bool());
118 }
119
120 class Set_params {
121 public:
122 template <typename T>
123 Set_params(T any, bool tls) : m_tls_active(tls) {
124 m_any = Any{Scalar{any}};
125
126 m_tls_active = tls;
127 }
128
129 Any m_any;
130 bool m_tls_active;
131 };
132
133 ::std::ostream &operator<<(::std::ostream &os, const Set_params &set_param) {
134 return os << "tls-active:" << set_param.m_tls_active << std::endl;
135 }
136
137 class SuccessSetCapabilityHanderTlsTestSuite
138 : public CapabilityHanderTlsTestSuite,
139 public WithParamInterface<Set_params> {
140 public:
141 };
142
143 TEST_P(SuccessSetCapabilityHanderTlsTestSuite,
144 get_success_forValidParametersAndTlsSupportedOnTcpip) {
145 auto s = GetParam();
146
147 EXPECT_CALL(mock_ssl_context, has_ssl()).WillOnce(Return(true));
148 EXPECT_CALL(mock_connection, get_type())
149 .WillRepeatedly(Return(xpl::Connection_tcpip));
150
151 ASSERT_EQ(ER_X_SUCCESS, sut.set(s.m_any).error);
152
153 EXPECT_CALL(mock_client, activate_tls_void());
154
155 sut.commit();
156 }
157
158 TEST_P(SuccessSetCapabilityHanderTlsTestSuite,
159 get_failure_forValidParametersAndTlsSupportedOnNamedPipe) {
160 Set_params s = GetParam();
161
162 EXPECT_CALL(mock_ssl_context, has_ssl()).WillOnce(Return(true));
163 EXPECT_CALL(mock_connection, get_type())
164 .WillRepeatedly(Return(xpl::Connection_namedpipe));
165
166 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut.set(s.m_any).error);
167 }
168
169 TEST_P(SuccessSetCapabilityHanderTlsTestSuite,
170 get_failure_forValidParametersAndTlsIsntSupported) {
171 Set_params s = GetParam();
172
173 EXPECT_CALL(mock_ssl_context, has_ssl()).WillOnce(Return(false));
174 EXPECT_CALL(mock_connection, get_type())
175 .WillRepeatedly(Return(xpl::Connection_tcpip));
176
177 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut.set(s.m_any).error);
178 }
179
180 INSTANTIATE_TEST_SUITE_P(
181 SuccessInstantiation, SuccessSetCapabilityHanderTlsTestSuite,
182 ::testing::Values(Set_params(true, false), Set_params(1, false),
183 Set_params(2, false), Set_params(3u, false),
184 Set_params(1.0, false)));
185
186 class FaildSetCapabilityHanderTlsTestSuite
187 : public SuccessSetCapabilityHanderTlsTestSuite {};
188
189 TEST_P(FaildSetCapabilityHanderTlsTestSuite, get_failure_forValidParameters) {
190 Set_params s = GetParam();
191
192 EXPECT_CALL(mock_connection, get_type())
193 .WillRepeatedly(
194 Return(s.m_tls_active ? xpl::Connection_tls : xpl::Connection_tcpip));
195
196 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut.set(s.m_any).error);
197
198 sut.commit();
199 }
200
201 INSTANTIATE_TEST_SUITE_P(
202 FaildInstantiationAlreadySet, FaildSetCapabilityHanderTlsTestSuite,
203 ::testing::Values(Set_params(true, true), Set_params(1, true),
204 Set_params(2, true), Set_params(3u, true),
205 Set_params(1.0, true)));
206
207 INSTANTIATE_TEST_SUITE_P(
208 FaildInstantiationCantDisable, FaildSetCapabilityHanderTlsTestSuite,
209 ::testing::Values(Set_params(false, true), Set_params(0, true),
210 Set_params(0u, true), Set_params(0.0, true)));
211
212 INSTANTIATE_TEST_SUITE_P(FaildInstantiationAlreadyDisabled,
213 FaildSetCapabilityHanderTlsTestSuite,
214 ::testing::Values(Set_params(0, false),
215 Set_params(false, false)));
216
217 class CapabilityHanderAuthMechTestSuite : public Test {
218 public:
219 CapabilityHanderAuthMechTestSuite() : sut(&mock_client) {
220 mock_server = std::make_shared<StrictMock<mock::Server>>();
221
222 EXPECT_CALL(mock_client, connection())
223 .WillRepeatedly(ReturnRef(mock_connection));
224 EXPECT_CALL(mock_client, server()).WillRepeatedly(ReturnRef(*mock_server));
225 }
226
227 std::shared_ptr<StrictMock<mock::Server>> mock_server;
228
229 StrictMock<mock::Vio> mock_connection;
230 StrictMock<mock::Client> mock_client;
231
232 Capability_auth_mech sut;
233 };
234
235 TEST_F(CapabilityHanderAuthMechTestSuite, isSupported_returnsTrue_always) {
236 ASSERT_TRUE(sut.is_supported());
237 }
238
239 TEST_F(CapabilityHanderAuthMechTestSuite, set_returnsFalse_always) {
240 Set_params set(1, false);
241
242 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut.set(set.m_any).error);
243 }
244
245 TEST_F(CapabilityHanderAuthMechTestSuite, commit_doesNothing_always) {
246 sut.commit();
247 }
248
249 TEST_F(CapabilityHanderAuthMechTestSuite, name) {
250 ASSERT_STREQ("authentication.mechanisms", sut.name().c_str());
251 }
252
253 TEST_F(CapabilityHanderAuthMechTestSuite, get_doesNothing_whenEmptySetReceive) {
254 std::vector<std::string> names{};
255 ::Mysqlx::Datatypes::Any any;
256 mock::Authentication_container mock_auth;
257
258 EXPECT_CALL(*mock_server, get_authentications())
259 .WillOnce(ReturnRef(mock_auth));
260
261 EXPECT_CALL(mock_auth, get_authentication_mechanisms(&mock_client))
262 .WillOnce(Return(names));
263 sut.get(&any);
264
265 ASSERT_EQ(::Mysqlx::Datatypes::Any::ARRAY, any.type());
266 EXPECT_EQ(0, any.array().value_size());
267 }
268
269 TEST_F(CapabilityHanderAuthMechTestSuite,
270 get_returnAuthMethodsFromServer_always) {
271 std::vector<std::string> names{"first", "second"};
272 ::Mysqlx::Datatypes::Any any;
273
274 StrictMock<mock::Authentication_container> mock_auth;
275
276 EXPECT_CALL(*mock_server, get_authentications())
277 .WillOnce(ReturnRef(mock_auth));
278
279 EXPECT_CALL(mock_auth, get_authentication_mechanisms(_))
280 .WillOnce(Return(names));
281
282 sut.get(&any);
283
284 ASSERT_EQ(::Mysqlx::Datatypes::Any::ARRAY, any.type());
285 ASSERT_EQ(static_cast<int>(names.size()), any.array().value_size());
286
287 for (std::size_t i = 0; i < names.size(); ++i) {
288 const auto &a = any.array().value(static_cast<int>(i));
289
290 ASSERT_EQ(::Mysqlx::Datatypes::Any::SCALAR, a.type());
291 ASSERT_EQ(::Mysqlx::Datatypes::Scalar::V_STRING, a.scalar().type());
292 ASSERT_STREQ(names[i].c_str(), a.scalar().v_string().value().c_str());
293 }
294 }
295
296 class Capability_hander_client_interactive_test_suite : public Test {
297 public:
298 Capability_hander_client_interactive_test_suite() = default;
299
300 void SetUp() override {
301 EXPECT_CALL(mock_client, is_interactive()).WillOnce(Return(false));
302 sut.reset(new Capability_client_interactive(&mock_client));
303 }
304
305 std::unique_ptr<Capability_client_interactive> sut;
306 StrictMock<mock::Client> mock_client;
307 };
308
309 TEST_F(Capability_hander_client_interactive_test_suite,
310 is_supported_returns_true_always) {
311 ASSERT_TRUE(sut->is_supported());
312 }
313
314 TEST_F(Capability_hander_client_interactive_test_suite,
315 name_returns_client_interactive_always) {
316 ASSERT_STREQ("client.interactive", sut->name().c_str());
317 }
318
319 TEST_F(Capability_hander_client_interactive_test_suite,
320 get_when_client_is_interactive) {
321 EXPECT_CALL(mock_client, is_interactive()).WillOnce(Return(true));
322 sut.reset(new Capability_client_interactive(&mock_client));
323
324 const bool expected_result = true;
325 ::Mysqlx::Datatypes::Any any;
326
327 sut->get(&any);
328
329 ASSERT_EQ(::Mysqlx::Datatypes::Any::SCALAR, any.type());
330 ASSERT_EQ(::Mysqlx::Datatypes::Scalar::V_BOOL, any.scalar().type());
331 ASSERT_EQ(expected_result, any.scalar().v_bool());
332 }
333
334 TEST_F(Capability_hander_client_interactive_test_suite,
335 get_when_client_is_not_interactive) {
336 EXPECT_CALL(mock_client, is_interactive()).WillOnce(Return(false));
337 sut.reset(new Capability_client_interactive(&mock_client));
338
339 const bool expected_result = false;
340 ::Mysqlx::Datatypes::Any any;
341
342 sut->get(&any);
343
344 ASSERT_EQ(::Mysqlx::Datatypes::Any::SCALAR, any.type());
345 ASSERT_EQ(::Mysqlx::Datatypes::Scalar::V_BOOL, any.scalar().type());
346 ASSERT_EQ(expected_result, any.scalar().v_bool());
347 }
348
349 TEST_F(Capability_hander_client_interactive_test_suite,
350 set_and_commit_valid_type) {
351 Any any{Scalar{true}};
352
353 ASSERT_EQ(ER_X_SUCCESS, sut->set(any).error);
354
355 EXPECT_CALL(mock_client, set_is_interactive(true));
356
357 sut->commit();
358 }
359
360 TEST_F(Capability_hander_client_interactive_test_suite,
361 set_and_commit_invalid_type) {
362 Any any{Scalar{"invalid"}};
363
364 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut->set(any).error);
365
366 EXPECT_CALL(mock_client, set_is_interactive(false));
367
368 sut->commit();
369 }
370
371 class Capability_handler_connection_attributes : public Test {
372 public:
373 void SetUp() override {
374 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, m_sut.set(m_any).error);
375 }
376
377 Any m_any;
378 Capability_connection_attributes m_sut;
379 };
380
381 TEST_F(Capability_handler_connection_attributes, is_supported) {
382 ASSERT_TRUE(m_sut.is_supported());
383 }
384
385 TEST_F(Capability_handler_connection_attributes,
386 set_invalid_capability_empty_object) {
387 const auto empty_obj = Any::Object{};
388 m_any = Any{empty_obj};
389 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, m_sut.set(m_any).error);
390 }
391 TEST_F(Capability_handler_connection_attributes,
392 set_invalid_capability_empty_field) {
393 const Any::Object::Fld field{};
394 const Any::Object obj{field};
395 m_any = Any{obj};
396 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, m_sut.set(m_any).error);
397 }
398
399 TEST_F(Capability_handler_connection_attributes,
400 set_invalid_capability_field_with_empty_value) {
401 const Any::Object::Fld field{"some_key", {}};
402 const Any::Object obj{field};
403 m_any = Any{obj};
404 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, m_sut.set(m_any).error);
405 }
406
407 TEST_F(Capability_handler_connection_attributes,
408 set_invalid_capability_field_with_empty_scalar) {
409 Scalar empty_scalar;
410 Any value{empty_scalar};
411 const Any::Object::Fld field{"some_key", value};
412 const Any::Object obj{field};
413 m_any = Any{obj};
414 ASSERT_EQ(ER_X_BAD_CONNECTION_SESSION_ATTRIBUTE_TYPE, m_sut.set(m_any).error);
415 }
416
417 TEST_F(Capability_handler_connection_attributes,
418 set_invalid_capability_field_with_scalar_other_than_string) {
419 Scalar scalar{true};
420 Any value{scalar};
421 const Any::Object::Fld field{"some_key", value};
422 const Any::Object obj{field};
423 m_any = Any{obj};
424 ASSERT_EQ(ER_X_BAD_CONNECTION_SESSION_ATTRIBUTE_TYPE, m_sut.set(m_any).error);
425 }
426
427 TEST_F(Capability_handler_connection_attributes,
428 set_invalid_capability_field_with_empty_string_scalar) {
429 ::Mysqlx::Datatypes::Object obj;
430 ::Mysqlx::Datatypes::Any value;
431 ::Mysqlx::Datatypes::Scalar scalar;
432 ::Mysqlx::Datatypes::Scalar_String empty_scalar_string;
433 scalar.mutable_v_string()->CopyFrom(empty_scalar_string);
434 value.mutable_scalar()->CopyFrom(scalar);
435 auto field = obj.add_fld();
436 field->set_key("some_key");
437 field->mutable_value()->CopyFrom(value);
438 ::Mysqlx::Datatypes::Any any;
439 any.mutable_obj()->CopyFrom(obj);
440 ASSERT_EQ(ER_X_BAD_CONNECTION_SESSION_ATTRIBUTE_TYPE, m_sut.set(any).error);
441 }
442
443 TEST_F(Capability_handler_connection_attributes,
444 set_invalid_capability_field_without_a_key) {
445 ::Mysqlx::Datatypes::Object obj;
446 ::Mysqlx::Datatypes::Any value;
447 ::Mysqlx::Datatypes::Scalar scalar;
448 ::Mysqlx::Datatypes::Scalar_String scalar_string;
449 scalar_string.set_value("some value");
450 scalar.mutable_v_string()->CopyFrom(scalar_string);
451 value.mutable_scalar()->CopyFrom(scalar);
452 auto field = obj.add_fld();
453 field->mutable_value()->CopyFrom(value);
454 ::Mysqlx::Datatypes::Any any;
455 any.mutable_obj()->CopyFrom(obj);
456 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, m_sut.set(any).error);
457 }
458
459 TEST_F(Capability_handler_connection_attributes, key_max_size_exceeded) {
460 Scalar::String scalar_string{"some value"};
461 Scalar scalar{scalar_string};
462 Any value{scalar};
463 std::string long_string(128, 'x');
464 const Any::Object::Fld field{long_string, value};
465 const Any::Object obj{field};
466 m_any = Any{obj};
467 ASSERT_EQ(ER_X_BAD_CONNECTION_SESSION_ATTRIBUTE_KEY_LENGTH,
468 m_sut.set(m_any).error);
469 }
470
471 TEST_F(Capability_handler_connection_attributes, empty_key) {
472 Scalar::String scalar_string{"some value"};
473 Scalar scalar{scalar_string};
474 Any value{scalar};
475 const Any::Object::Fld field{"", value};
476 const Any::Object obj{field};
477 m_any = Any{obj};
478 ASSERT_EQ(ER_X_BAD_CONNECTION_SESSION_ATTRIBUTE_EMPTY_KEY,
479 m_sut.set(m_any).error);
480 }
481
482 TEST_F(Capability_handler_connection_attributes, value_max_size_exceeded) {
483 std::string long_string(2048, 'x');
484 Scalar::String long_scalar_string{long_string};
485 Scalar scalar{long_scalar_string};
486 Any value{scalar};
487 const Any::Object::Fld field{"some key", value};
488 const Any::Object obj{field};
489 m_any = Any{obj};
490 ASSERT_EQ(ER_X_BAD_CONNECTION_SESSION_ATTRIBUTE_VALUE_LENGTH,
491 m_sut.set(m_any).error);
492 }
493
494 TEST_F(Capability_handler_connection_attributes, set_one_key_value_pair) {
495 Scalar::String scalar_string{"some value"};
496 Scalar scalar{scalar_string};
497 Any value{scalar};
498 const Any::Object::Fld field{"some key", value};
499 const Any::Object obj{field};
500 m_any = Any{obj};
501 ASSERT_EQ(ER_X_SUCCESS, m_sut.set(m_any).error);
502 }
503
504 TEST_F(Capability_handler_connection_attributes, set_two_key_value_pairs) {
505 Scalar::String scalar_string1{"some value"};
506 Scalar::String scalar_string2{"other value"};
507 Scalar scalar1{scalar_string1};
508 Scalar scalar2{scalar_string1};
509 Any value1{scalar1};
510 Any value2{scalar2};
511 const Any::Object::Fld field1{"some key", value1};
512 const Any::Object::Fld field2{"other key", value2};
513 const Any::Object obj{{field1, field2}};
514 m_any = Any{obj};
515 ASSERT_EQ(ER_X_SUCCESS, m_sut.set(m_any).error);
516 }
517
518 } // namespace test
519
520 } // namespace xpl