Debian Codegraph @

unittest/gunit/xplugin/xcl/protocol_send_recv_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 "unittest/gunit/xplugin/xcl/protocol_t.h"
27
28 #include "plugin/x/protocol/stream/compression/compression_algorithm_interface.h"
29
30 namespace xcl {
31 namespace test {
32
33 using Msg_types = ::testing::Types<
34 ::Mysqlx::Session::AuthenticateStart,
35 ::Mysqlx::Session::AuthenticateContinue, ::Mysqlx::Session::Reset,
36 ::Mysqlx::Session::Close, ::Mysqlx::Sql::StmtExecute, ::Mysqlx::Crud::Find,
37 ::Mysqlx::Crud::Insert, ::Mysqlx::Crud::Update, ::Mysqlx::Crud::Delete,
38 ::Mysqlx::Crud::CreateView, ::Mysqlx::Crud::ModifyView,
39 ::Mysqlx::Crud::DropView, ::Mysqlx::Expect::Open, ::Mysqlx::Expect::Close,
40 ::Mysqlx::Connection::CapabilitiesGet,
41 ::Mysqlx::Connection::CapabilitiesSet, ::Mysqlx::Connection::Close,
42 ::Mysqlx::Prepare::Prepare, ::Mysqlx::Prepare::Execute,
43 ::Mysqlx::Prepare::Deallocate>;
44
45 TYPED_TEST_SUITE(Xcl_protocol_impl_tests_with_msg, Msg_types);
46
47 TYPED_TEST(Xcl_protocol_impl_tests_with_msg,
48 connection_send_successful_send_handler_consumed_msg_was_ignored) {
49 this->setup_required_fields_in_message();
50
51 const auto id = this->m_sut->add_send_message_handler(
52 this->m_mock_handlers.get_mock_lambda_send_message_handler());
53
54 {
55 InSequence s;
56 EXPECT_CALL(this->m_mock_handlers,
57 send_message_handler(_, TestFixture::Msg_descriptor::get_id(),
58 Ref(*this->m_message.get())))
59 .WillOnce(Return(Handler_result::Consumed));
60
61 EXPECT_CALL(*this->m_mock_connection, write(_, _))
62 .WillOnce(
63 Invoke([this](const uchar *data, const std::size_t size) -> XError {
64 return this->assert_write_size(data, size);
65 }));
66 }
67
68 auto error = this->m_sut->send(*this->m_message.get());
69
70 ASSERT_FALSE(error);
71
72 this->m_sut->remove_send_message_handler(id);
73 }
74
75 TYPED_TEST(Xcl_protocol_impl_tests_with_msg,
76 connection_send_successful_send_handler_ignored_msg) {
77 this->setup_required_fields_in_message();
78
79 const auto id = this->m_sut->add_send_message_handler(
80 this->m_mock_handlers.get_mock_lambda_send_message_handler());
81
82 {
83 InSequence s;
84 EXPECT_CALL(this->m_mock_handlers,
85 send_message_handler(_, TestFixture::Msg_descriptor::get_id(),
86 Ref(*this->m_message.get())))
87 .WillOnce(Return(Handler_result::Continue));
88
89 EXPECT_CALL(*this->m_mock_connection, write(_, _))
90 .WillOnce(
91 Invoke([this](const uchar *data, const std::size_t size) -> XError {
92 return this->assert_write_size(data, size);
93 }));
94 }
95
96 auto error = this->m_sut->send(*this->m_message.get());
97
98 ASSERT_FALSE(error);
99
100 this->m_sut->remove_send_message_handler(id);
101 }
102
103 TYPED_TEST(Xcl_protocol_impl_tests_with_msg,
104 connection_send_successful_multiple_send_handlers_called) {
105 const auto first_handler_didnt_consume = Handler_result::Continue;
106
107 this->assert_multiple_handlers(first_handler_didnt_consume);
108 }
109
110 TYPED_TEST(Xcl_protocol_impl_tests_with_msg,
111 connection_send_successful_first_handler_doesnt_blok_others) {
112 const auto first_handler_consumed = Handler_result::Consumed;
113
114 this->assert_multiple_handlers(first_handler_consumed);
115 }
116
117 TYPED_TEST(Xcl_protocol_impl_tests_with_msg, connection_send_successful) {
118 this->setup_required_fields_in_message();
119
120 {
121 InSequence s;
122
123 EXPECT_CALL(*this->m_mock_connection, write(_, _))
124 .WillOnce(
125 Invoke([this](const uchar *data, const std::size_t size) -> XError {
126 return this->assert_write_size(data, size);
127 }));
128 }
129
130 auto error = this->m_sut->send(*this->m_message.get());
131
132 ASSERT_FALSE(error);
133 }
134
135 TEST_F(Xcl_protocol_impl_tests, recv_fails_at_header) {
136 const int64_t expected_error_code = 3000;
137 const int32_t expected_payload_size = 10;
138 XProtocol::Server_message_type_id out_id;
139 XError out_error;
140
141 expect_read_header(::Mysqlx::ServerMessages::OK, expected_payload_size,
142 expected_error_code);
143 auto result = m_sut->recv_single_message(&out_id, &out_error);
144 ASSERT_FALSE(result.get());
145 ASSERT_EQ(expected_error_code, out_error.error());
146 }
147
148 TEST_F(Xcl_protocol_impl_tests, recv_fails_at_payload) {
149 const int expected_error_code = 3000;
150 const int32_t expected_payload_size = 10;
151 XProtocol::Server_message_type_id out_id;
152 XError out_error;
153
154 expect_read_header(::Mysqlx::ServerMessages::OK, expected_payload_size);
155
156 EXPECT_CALL(*m_mock_connection, read(_, expected_payload_size))
157 .WillOnce(Return(XError(expected_error_code, "")));
158 auto result = m_sut->recv_single_message(&out_id, &out_error);
159 ASSERT_FALSE(result.get());
160 ASSERT_EQ(expected_error_code, out_error.error());
161 }
162
163 TEST_F(Xcl_protocol_impl_tests, recv_large_message) {
164 std::string message_payload(1024 * 64, 'x');
165 int offset = 0;
166 expect_read_header(::Mysqlx::ServerMessages::OK,
167 static_cast<int32>(message_payload.size()));
168
169 {
170 InSequence s;
171 const int k_internal_buffer = 4 * 1024;
172 size_t count_data_in_reads = 0;
173
174 while (count_data_in_reads < message_payload.size()) {
175 EXPECT_CALL(*m_mock_connection, read(_, k_internal_buffer))
176 .WillOnce(Invoke(
177 [&offset, message_payload](
178 uchar *data, const std::size_t data_length MY_ATTRIBUTE(
179 (unused))) -> XError {
180 auto i_start = message_payload.begin() + offset;
181 std::copy(i_start, i_start + data_length, data);
182 offset += data_length;
183 return {};
184 }));
185 count_data_in_reads += k_internal_buffer;
186 }
187 }
188 XProtocol::Server_message_type_id out_id;
189 XError out_error;
190 auto result = m_sut->recv_single_message(&out_id, &out_error);
191 ASSERT_TRUE(result.get());
192 ASSERT_EQ(0, out_error.error());
193 }
194
195 TEST_F(Xcl_protocol_impl_tests, recv_unknown_msg_type) {
196 const int32_t invalid_message_id = 255;
197 const int32_t expected_payload_size = 10;
198 XProtocol::Server_message_type_id out_id;
199 XError out_error;
200
201 expect_read_header(invalid_message_id, expected_payload_size);
202
203 EXPECT_CALL(*m_mock_connection, read(_, expected_payload_size))
204 .WillOnce(Return(XError()));
205 auto result = m_sut->recv_single_message(&out_id, &out_error);
206 ASSERT_FALSE(result.get());
207 ASSERT_EQ(CR_MALFORMED_PACKET, out_error.error());
208 }
209
210 TEST_F(Xcl_protocol_impl_tests, recv_in_single_read_op) {
211 const int32_t expected_payload_size = 0;
212 const std::string expected_message_name = "Mysqlx.Ok";
213 XProtocol::Server_message_type_id out_id;
214 XError out_error;
215
216 expect_read_header(::Mysqlx::ServerMessages::OK, expected_payload_size);
217
218 auto result = m_sut->recv_single_message(&out_id, &out_error);
219 ASSERT_TRUE(result.get());
220 ASSERT_EQ(expected_message_name, result->GetTypeName());
221 ASSERT_FALSE(out_error);
222 }
223
224 TEST_F(Xcl_protocol_impl_tests, recv_in_two_read_op) {
225 using Auth_continue = ::Mysqlx::Session::AuthenticateContinue;
226 const auto message = Server_message<Auth_continue>::make_required();
227
228 assert_read_message(message);
229 }
230
231 TEST_F(Xcl_protocol_impl_tests, recv_in_two_read_op_call_one_handler) {
232 using Auth_continue_desc =
233 Server_message<::Mysqlx::Session::AuthenticateContinue>;
234
235 const auto message = Auth_continue_desc::make_required();
236
237 m_sut->add_received_message_handler(
238 m_mock_handlers.get_mock_lambda_received_message_handler());
239 EXPECT_CALL(m_mock_handlers,
240 received_message_handler(_, Auth_continue_desc::get_id(),
241 Cmp_msg(message)))
242 .WillOnce(Return(Handler_result::Continue));
243
244 assert_read_message(message);
245 }
246
247 TEST_F(Xcl_protocol_impl_tests, recv_in_two_read_op_call_multiple_handlers) {
248 using Auth_continue_desc =
249 Server_message<::Mysqlx::Session::AuthenticateContinue>;
250
251 const auto message = Auth_continue_desc::make_required();
252 StrictMock<mock::Message_handlers> mock_handlers[2];
253
254 m_sut->add_received_message_handler(
255 m_mock_handlers.get_mock_lambda_received_message_handler());
256 m_sut->add_received_message_handler(
257 mock_handlers[0].get_mock_lambda_received_message_handler());
258 m_sut->add_received_message_handler(
259 mock_handlers[1].get_mock_lambda_received_message_handler());
260
261 // The call sequence must be opposite to push sequence
262 {
263 InSequence s;
264 EXPECT_CALL(mock_handlers[1],
265 received_message_handler(_, Auth_continue_desc::get_id(),
266 Cmp_msg(message)))
267 .WillOnce(Return(Handler_result::Continue));
268 EXPECT_CALL(mock_handlers[0],
269 received_message_handler(_, Auth_continue_desc::get_id(),
270 Cmp_msg(message)))
271 .WillOnce(Return(Handler_result::Continue));
272 EXPECT_CALL(m_mock_handlers,
273 received_message_handler(_, Auth_continue_desc::get_id(),
274 Cmp_msg(message)))
275 .WillOnce(Return(Handler_result::Continue));
276 }
277
278 assert_read_message(message);
279 }
280
281 TEST_F(
282 Xcl_protocol_impl_tests,
283 recv_in_two_read_op_call_first_handler_consumes_next_msg_on_all_handlers) {
284 using Auth_continue_desc =
285 Server_message<::Mysqlx::Session::AuthenticateContinue>;
286 using Error_desc = Server_message<::Mysqlx::Error>;
287
288 const auto msg_continue = Auth_continue_desc::make_required();
289 const auto msg_error = Error_desc::make_required();
290 StrictMock<mock::Message_handlers> mock_handlers[2];
291
292 m_sut->add_received_message_handler(
293 m_mock_handlers.get_mock_lambda_received_message_handler());
294 m_sut->add_received_message_handler(
295 mock_handlers[0].get_mock_lambda_received_message_handler());
296 m_sut->add_received_message_handler(
297 mock_handlers[1].get_mock_lambda_received_message_handler());
298
299 // The call sequence must be opposite to push sequence
300 {
301 InSequence s1;
302 // Get first transmitted message
303 // and consume it inside the handler
304 EXPECT_CALL(mock_handlers[1],
305 received_message_handler(_, Auth_continue_desc::get_id(),
306 Cmp_msg(msg_continue)))
307 .WillOnce(Return(Handler_result::Consumed));
308
309 // Message was consumed, thus read_single_message
310 // is going to read next one from XConnection.
311 // We are expecting that next message is going to
312 // processed on all handlers without consuming it !
313 EXPECT_CALL(
314 mock_handlers[1],
315 received_message_handler(_, Error_desc::get_id(), Cmp_msg(msg_error)))
316 .WillOnce(Return(Handler_result::Continue));
317 EXPECT_CALL(
318 mock_handlers[0],
319 received_message_handler(_, Error_desc::get_id(), Cmp_msg(msg_error)))
320 .WillOnce(Return(Handler_result::Continue));
321 EXPECT_CALL(
322 m_mock_handlers,
323 received_message_handler(_, Error_desc::get_id(), Cmp_msg(msg_error)))
324 .WillOnce(Return(Handler_result::Continue));
325 }
326
327 {
328 InSequence s2;
329 expect_read_message(msg_continue); // Message to be read at first read op
330 assert_read_message(msg_error); // Message to be read at second read op
331 }
332 }
333
334 TEST_F(Xcl_protocol_impl_tests, recv_ok_fails_on_other_msg) {
335 using Auth_continue_desc =
336 Server_message<::Mysqlx::Session::AuthenticateContinue>;
337
338 const auto msg_continue = Auth_continue_desc::make_required();
339
340 expect_read_message(msg_continue);
341 auto error = m_sut->recv_ok();
342
343 ASSERT_EQ(CR_MALFORMED_PACKET, error.error());
344 }
345
346 TEST_F(Xcl_protocol_impl_tests, recv_ok_fails_error_msg) {
347 using Error_desc = Server_message<::Mysqlx::Error>;
348
349 const uint32_t expected_error_code = 23332;
350 const char *expected_msg = "expected error message";
351 const char *expected_sql_state = "expected sql state";
352
353 auto msg_error = Error_desc::make_required();
354
355 msg_error.set_code(expected_error_code);
356 msg_error.set_msg(expected_msg);
357 msg_error.set_sql_state(expected_sql_state);
358 msg_error.set_severity(::Mysqlx::Error::FATAL);
359
360 expect_read_message(msg_error);
361 XError error = m_sut->recv_ok();
362
363 ASSERT_EQ(expected_error_code, error.error());
364 ASSERT_STREQ(expected_msg, error.what());
365 ASSERT_STREQ(expected_sql_state, error.sql_state());
366 ASSERT_TRUE(error.is_fatal());
367 }
368
369 TEST_F(Xcl_protocol_impl_tests, recv_ok) {
370 using Ok_desc = Server_message<::Mysqlx::Ok>;
371
372 auto msg_ok = Ok_desc::make_required();
373
374 expect_read_message_without_payload(msg_ok);
375 auto error = m_sut->recv_ok();
376
377 ASSERT_FALSE(error);
378 }
379
380 TEST_F(Xcl_protocol_impl_tests, recv_resultset) {
381 mock::XQuery_result *query_result = new mock::XQuery_result();
382 static XQuery_result::Metadata metadata;
383
384 EXPECT_CALL(m_mock_factory, create_result_raw(_, _, _))
385 .WillOnce(Return(query_result));
386
387 auto result = m_sut->recv_resultset();
388
389 ASSERT_EQ(query_result, result.get());
390 }
391
392 TEST_F(Xcl_protocol_impl_tests, send_compressed) {
393 m_sut->use_compression(Compression_algorithm::k_deflate, 1);
394 expect_write_payload(Mysqlx::ClientMessages::COMPRESSION, 17, 0);
395
396 auto result = m_sut->send_compressed_frame(
397 Mysqlx::ClientMessages::EXPECT_OPEN, Mysqlx::Expect::Open());
398
399 ASSERT_EQ(0, result.error());
400 }
401
402 } // namespace test
403 } // namespace xcl