Debian Codegraph @

unittest/gunit/xplugin/xcl/session_negotiation_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2017, 2026, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gtest/gtest.h>
27 #include <initializer_list>
28 #include <memory>
29 #include <set>
30
31 #include "mysqlx_error.h"
32 #include "unittest/gunit/xplugin/xcl/message_helpers.h"
33 #include "unittest/gunit/xplugin/xcl/session_t.h"
34
35 namespace xcl {
36 namespace test {
37
38 template <typename E>
39 class Flags {
40 public:
41 Flags(const std::initializer_list<E> &set) : m_set(set) {}
42
43 bool has(const E e) const { return 0 != m_set.count(e); }
44
45 private:
46 std::set<E> m_set;
47 };
48
49 class Xcl_session_negotiation_tests : public Xcl_session_impl_tests {
50 public:
51 enum Action_session {
52 Action_ssl_configured,
53 Action_connect,
54 Action_compression,
55 };
56
57 void setup_state(const Flags<Action_session> config,
58 const std::string ssl_mode,
59 const std::string compression_mode) {
60 EXPECT_CALL(m_mock_connection_state, is_ssl_configured())
61 .WillRepeatedly(Return(config.has(Action_ssl_configured)));
62
63 EXPECT_CALL(m_mock_connection_state, is_ssl_activated())
64 .WillRepeatedly(Return(false));
65 EXPECT_CALL(m_mock_connection_state, get_connection_type())
66 .WillOnce(Return(Connection_type::Tcp));
67 EXPECT_CALL(*m_mock_protocol, add_send_message_handler(_, _, _));
68 EXPECT_CALL(*m_mock_protocol, remove_send_message_handler(_))
69 .Times(testing::AtLeast(1));
70 EXPECT_CALL(m_mock_connection, set_read_timeout(_))
71 .WillRepeatedly(Return(XError{}));
72 EXPECT_CALL(m_mock_connection, set_write_timeout(_))
73 .WillRepeatedly(Return(XError{}));
74 EXPECT_CALL(*m_mock_protocol, add_notice_handler(_, Handler_position::Begin,
75 Handler_priority_low))
76 .Times(testing::AtLeast(1));
77 EXPECT_CALL(*m_mock_protocol, remove_notice_handler(_))
78 .Times(testing::AtLeast(1));
79 EXPECT_CALL(*m_mock_protocol, reset_buffering()).Times(testing::AtLeast(1));
80
81 EXPECT_CALL(m_mock_connection, connect(_, 33060, _))
82 .WillOnce(Return(XError{0, ""}));
83
84 if (config.has(Action_connect)) {
85 EXPECT_CALL(*m_mock_protocol,
86 use_compression(config.has(Action_compression)
87 ? Compression_algorithm::k_deflate
88 : Compression_algorithm::k_none));
89
90 EXPECT_CALL(*m_mock_protocol, execute_authenticate("", "", "", "MYSQL41"))
91 .WillOnce(Return(XError{}));
92 }
93
94 ASSERT_FALSE(
95 m_sut->set_mysql_option(XSession::Mysqlx_option::Ssl_mode, ssl_mode));
96 ASSERT_FALSE(m_sut->set_mysql_option(
97 XSession::Mysqlx_option::Compression_negotiation_mode,
98 compression_mode));
99 }
100
101 void setup_level(const int64_t level) {
102 ASSERT_FALSE(m_sut->set_mysql_option(
103 XSession::Mysqlx_option::Compression_level_server, level));
104 }
105
106 void expect_tls(const int error_code = 0) {
107 EXPECT_CALL(*m_mock_protocol,
108 execute_set_capability(Cmp_msg(m_capability_set_tls.get())))
109 .WillOnce(Return(XError(error_code, "")));
110
111 if (0 == error_code) {
112 EXPECT_CALL(m_mock_connection, activate_tls());
113 }
114 }
115
116 void expect_fetch_capabilities(Mysqlx::Connection::Capabilities &src_caps) {
117 auto capabilities = src_caps.New();
118 capabilities->CopyFrom(src_caps);
119 EXPECT_CALL(*m_mock_protocol, execute_fetch_capabilities_raw)
120 .WillOnce(Return(capabilities));
121 }
122
123 void expect_set_capabilities(Mysqlx::Connection::CapabilitiesSet &cap_set,
124 int error_code = 0,
125 const bool is_fatal = false) {
126 EXPECT_CALL(*m_mock_protocol, execute_set_capability(Cmp_msg(cap_set)))
127 .WillOnce(Return(XError(error_code, "", is_fatal)));
128 }
129
130 Message_from_str<Mysqlx::Connection::CapabilitiesSet>
131 m_capability_set_compression{
132 "capabilities {"
133 " capabilities {"
134 " name: \"compression\""
135 " value {"
136 " type: OBJECT"
137 " obj {"
138 " fld {"
139 " key: \"algorithm\""
140 " value {"
141 " type: SCALAR"
142 " scalar {"
143 " type: V_STRING"
144 " v_string {"
145 " value: \"DEFLATE_STREAM\""
146 " }"
147 " }"
148 " }"
149 " }"
150 " fld {"
151 " key: \"server_combine_mixed_messages\""
152 " value {"
153 " type: SCALAR"
154 " scalar {"
155 " type: V_BOOL"
156 " v_bool: true"
157 " }"
158 " }"
159 " }"
160 " fld {"
161 " key: \"server_max_combine_messages\""
162 " value {"
163 " type: SCALAR"
164 " scalar {"
165 " type: V_SINT"
166 " v_signed_int: 0"
167 " }"
168 " }"
169 " }"
170 " }"
171 " }"
172 " }"
173 "}"};
174
175 Message_from_str<Mysqlx::Connection::CapabilitiesSet>
176 m_capability_set_compression_with_level{
177 " capabilities {"
178 " capabilities {"
179 " name: \"compression\""
180 " value {"
181 " type: OBJECT"
182 " obj {"
183 " fld {"
184 " key: \"algorithm\""
185 " value {"
186 " type: SCALAR"
187 " scalar {"
188 " type: V_STRING"
189 " v_string {"
190 " value: \"DEFLATE_STREAM\""
191 " }"
192 " }"
193 " }"
194 " }"
195 " fld {"
196 " key: \"level\""
197 " value {"
198 " type: SCALAR"
199 " scalar {"
200 " type: V_SINT"
201 " v_signed_int: 3"
202 " }"
203 " }"
204 " }"
205 " fld {"
206 " key: \"server_combine_mixed_messages\""
207 " value {"
208 " type: SCALAR"
209 " scalar {"
210 " type: V_BOOL"
211 " v_bool: true"
212 " }"
213 " }"
214 " }"
215 " fld {"
216 " key: \"server_max_combine_messages\""
217 " value {"
218 " type: SCALAR"
219 " scalar {"
220 " type: V_SINT"
221 " v_signed_int: 0"
222 " }"
223 " }"
224 " }"
225 " }"
226 " }"
227 " }"
228 " }"};
229
230 Message_from_str<Mysqlx::Connection::CapabilitiesSet> m_capability_set_tls{
231 "capabilities {"
232 " capabilities {"
233 " name: \"tls\""
234 " value {"
235 " type: SCALAR"
236 " scalar {"
237 " type: V_BOOL"
238 " v_bool: true"
239 " }"
240 " }"
241 " }"
242 "}"};
243
244 Message_from_str<Mysqlx::Connection::Capabilities> m_capabilities{
245 " capabilities {"
246 " name: \"authentication.mechanisms\""
247 " value {"
248 " type: ARRAY"
249 " array {"
250 " value {"
251 " type: SCALAR"
252 " scalar {"
253 " type: V_STRING"
254 " v_string {"
255 " value: \"MYSQL41\""
256 " }"
257 " }"
258 " }"
259 " value {"
260 " type: SCALAR"
261 " scalar {"
262 " type: V_STRING"
263 " v_string {"
264 " value: \"SHA256_MEMORY\""
265 " }"
266 " }"
267 " }"
268 " }"
269 " }"
270 " }"
271 " capabilities {"
272 " name: \"doc.formats\""
273 " value {"
274 " type: SCALAR"
275 " scalar {"
276 " type: V_STRING"
277 " v_string {"
278 " value: \"text\""
279 " }"
280 " }"
281 " }"
282 " }"
283 " capabilities {"
284 " name: \"client.interactive\""
285 " value {"
286 " type: SCALAR"
287 " scalar {"
288 " type: V_BOOL"
289 " v_bool: false"
290 " }"
291 " }"
292 " }"
293 " capabilities {"
294 " name: \"compression\""
295 " value {"
296 " type: OBJECT"
297 " obj {"
298 " fld {"
299 " key: \"algorithm\""
300 " value {"
301 " type: ARRAY"
302 " array {"
303 " value {"
304 " type: SCALAR"
305 " scalar {"
306 " type: V_STRING"
307 " v_string {"
308 " value: \"deflate_stream\""
309 " }"
310 " }"
311 " }"
312 " value {"
313 " type: SCALAR"
314 " scalar {"
315 " type: V_STRING"
316 " v_string {"
317 " value: \"lz4_message\""
318 " }"
319 " }"
320 " }"
321 " value {"
322 " type: SCALAR"
323 " scalar {"
324 " type: V_STRING"
325 " v_string {"
326 " value: \"zstd_stream\""
327 " }"
328 " }"
329 " }"
330 " }"
331 " }"
332 " }"
333 " }"
334 " }"
335 " }"};
336
337 Message_from_str<Mysqlx::Connection::Capabilities>
338 m_capabilities_no_compression{
339 " capabilities {"
340 " name: \"authentication.mechanisms\""
341 " value {"
342 " type: ARRAY"
343 " array {"
344 " value {"
345 " type: SCALAR"
346 " scalar {"
347 " type: V_STRING"
348 " v_string {"
349 " value: \"MYSQL41\""
350 " }"
351 " }"
352 " }"
353 " value {"
354 " type: SCALAR"
355 " scalar {"
356 " type: V_STRING"
357 " v_string {"
358 " value: \"SHA256_MEMORY\""
359 " }"
360 " }"
361 " }"
362 " }"
363 " }"
364 " }"
365 " capabilities {"
366 " name: \"doc.formats\""
367 " value {"
368 " type: SCALAR"
369 " scalar {"
370 " type: V_STRING"
371 " v_string {"
372 " value: \"text\""
373 " }"
374 " }"
375 " }"
376 " }"
377 " capabilities {"
378 " name: \"client.interactive\""
379 " value {"
380 " type: SCALAR"
381 " scalar {"
382 " type: V_BOOL"
383 " v_bool: false"
384 " }"
385 " }"
386 " }"};
387 };
388
389 TEST_F(Xcl_session_negotiation_tests, xsession_no_negotiation) {
390 setup_state({Action_connect}, "DISABLED", "DISABLED");
391
392 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
393
394 ASSERT_FALSE(error);
395 }
396
397 TEST_F(Xcl_session_negotiation_tests, xsession_SSL_REQUIRED_server_support) {
398 setup_state({Action_connect, Action_ssl_configured}, "REQUIRED", "DISABLED");
399
400 expect_tls();
401
402 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
403
404 ASSERT_FALSE(error);
405 }
406
407 TEST_F(Xcl_session_negotiation_tests,
408 xsession_SSL_REQUIRED_server_doesnt_support) {
409 setup_state({Action_ssl_configured}, "REQUIRED", "DISABLED");
410
411 expect_tls(ER_X_CAPABILITIES_PREPARE_FAILED);
412
413 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
414
415 ASSERT_TRUE(error);
416 }
417
418 TEST_F(Xcl_session_negotiation_tests, xsession_SSL_PREFERRED_server_support) {
419 setup_state({Action_connect, Action_ssl_configured}, "PREFERRED", "DISABLED");
420
421 expect_tls();
422
423 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
424
425 ASSERT_FALSE(error);
426 }
427
428 TEST_F(Xcl_session_negotiation_tests,
429 xsession_SSL_PREFERRED_server_doesnt_support) {
430 setup_state({Action_connect, Action_ssl_configured}, "PREFERRED", "DISABLED");
431
432 expect_tls(ER_X_CAPABILITIES_PREPARE_FAILED);
433
434 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
435
436 ASSERT_FALSE(error);
437 }
438
439 TEST_F(Xcl_session_negotiation_tests,
440 xsession_COMPRESSION_REQUIRED_server_doesnt_support) {
441 setup_state({}, "DISABLED", "REQUIRED");
442
443 expect_fetch_capabilities(m_capabilities_no_compression.get());
444
445 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
446
447 ASSERT_TRUE(error);
448 }
449
450 TEST_F(Xcl_session_negotiation_tests,
451 xsession_COMPRESSION_REQUIRED_server_support) {
452 setup_state({Action_connect, Action_compression}, "DISABLED", "REQUIRED");
453
454 expect_fetch_capabilities(m_capabilities.get());
455 expect_set_capabilities(m_capability_set_compression.get());
456
457 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
458
459 ASSERT_FALSE(error);
460 }
461
462 TEST_F(Xcl_session_negotiation_tests,
463 xsession_COMPRESSION_PREFERRED_server_doesnt_support) {
464 setup_state({Action_connect}, "DISABLED", "PREFERRED");
465
466 expect_fetch_capabilities(m_capabilities_no_compression.get());
467
468 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
469
470 ASSERT_FALSE(error);
471 }
472
473 TEST_F(Xcl_session_negotiation_tests,
474 xsession_COMPRESSION_PREFERRED_server_support) {
475 setup_state({Action_connect, Action_compression}, "DISABLED", "PREFERRED");
476
477 expect_fetch_capabilities(m_capabilities.get());
478 expect_set_capabilities(m_capability_set_compression.get());
479
480 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
481
482 ASSERT_FALSE(error);
483 }
484
485 TEST_F(Xcl_session_negotiation_tests,
486 xsession_COMPRESSION_REQUIRED_WITH_LEVEL_server_doesnt_support_LEVEL) {
487 setup_state({Action_connect, Action_compression}, "DISABLED", "REQUIRED");
488 setup_level(3);
489
490 expect_fetch_capabilities(m_capabilities.get());
491 expect_set_capabilities(m_capability_set_compression_with_level.get(),
492 ER_X_CAPABILITIES_PREPARE_FAILED);
493 expect_set_capabilities(m_capability_set_compression.get());
494
495 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
496
497 ASSERT_FALSE(error);
498 }
499
500 TEST_F(Xcl_session_negotiation_tests,
501 xsession_COMPRESSION_REQUIRED_WITH_LEVEL_server_fatal_error) {
502 setup_state({}, "DISABLED", "REQUIRED");
503 setup_level(3);
504
505 expect_fetch_capabilities(m_capabilities.get());
506 expect_set_capabilities(m_capability_set_compression_with_level.get(),
507 ER_X_CAPABILITIES_PREPARE_FAILED, true);
508
509 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
510
511 ASSERT_TRUE(error);
512 }
513
514 TEST_F(Xcl_session_negotiation_tests,
515 xsession_COMPRESSION_PREFERRED_WITH_LEVEL_server_doesnt_support_LEVEL) {
516 setup_state({Action_connect, Action_compression}, "DISABLED", "PREFERRED");
517 setup_level(3);
518
519 expect_fetch_capabilities(m_capabilities.get());
520 expect_set_capabilities(m_capability_set_compression_with_level.get(),
521 ER_X_CAPABILITIES_PREPARE_FAILED);
522 expect_set_capabilities(m_capability_set_compression.get());
523
524 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
525
526 ASSERT_FALSE(error);
527 }
528
529 TEST_F(
530 Xcl_session_negotiation_tests,
531 xsession_COMPRESSION_PREFERRED_WITH_LEVEL_server_doesnt_support_compression) {
532 setup_state({Action_connect}, "DISABLED", "PREFERRED");
533 setup_level(3);
534
535 expect_fetch_capabilities(m_capabilities_no_compression.get());
536
537 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
538
539 ASSERT_FALSE(error);
540 }
541
542 TEST_F(Xcl_session_negotiation_tests,
543 xsession_SSL_REQUIRED_and_COMPRESSION_REQUIRED_server_support) {
544 setup_state({Action_connect, Action_compression, Action_ssl_configured},
545 "REQUIRED", "REQUIRED");
546
547 expect_fetch_capabilities(m_capabilities.get());
548 expect_set_capabilities(m_capability_set_compression.get());
549 expect_tls();
550
551 const auto error = m_sut->connect(nullptr, 0, nullptr, nullptr, nullptr);
552
553 ASSERT_FALSE(error);
554 }
555
556 } // namespace test
557 } // namespace xcl