Debian Codegraph @

unittest/gunit/xplugin/xcl/message_helpers.h

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 #ifndef UNITTEST_GUNIT_XPLUGIN_XCL_MESSAGE_HELPERS_H_
27 #define UNITTEST_GUNIT_XPLUGIN_XCL_MESSAGE_HELPERS_H_
28
29 #include <gmock/gmock.h>
30 #include <gtest/gtest.h>
31 #include <string>
32 #include <type_traits>
33
34 #include "plugin/x/client/mysqlxclient/xmessage.h"
35 #include "plugin/x/client/mysqlxclient/xprotocol.h"
36
37 namespace xcl {
38 namespace test {
39
40 template <typename Message_type>
41 class Message_from_str {
42 Message_type m_protobuf_message;
43
44 public:
45 explicit Message_from_str(const std::string &text_message) {
46 EXPECT_TRUE(::google::protobuf::TextFormat::ParseFromString(
47 text_message, &m_protobuf_message));
48 }
49 Message_type &get() { return m_protobuf_message; }
50 const Message_type &get() const { return m_protobuf_message; }
51 };
52
53 template <typename M>
54 class Message_compare {
55 public:
56 explicit Message_compare(const std::string &text_message)
57 : m_expected_text_reformated(reformat_text_message(text_message)) {}
58
59 explicit Message_compare(const M &message) {
60 ::google::protobuf::TextFormat::PrintToString(message,
61 &m_expected_text_reformated);
62 }
63
64 bool operator==(const xcl::XProtocol::Message &msg) const {
65 std::string msg_text;
66 ::google::protobuf::TextFormat::PrintToString(msg, &msg_text);
67
68 return msg_text == m_expected_text_reformated;
69 }
70
71 std::string explain_failure(const xcl::XProtocol::Message &msg) const {
72 std::string msg_text;
73
74 ::google::protobuf::TextFormat::PrintToString(msg, &msg_text);
75
76 std::string result = "\nFollowing message doesn't match ";
77 result += "with expectations:\n" + msg_text;
78 result += "\nExpected message:\n " + m_expected_text_reformated;
79
80 return result;
81 }
82
83 private:
84 std::string reformat_text_message(const std::string &text_message) const {
85 M m;
86 std::string text_message_reformated;
87
88 EXPECT_TRUE(
89 ::google::protobuf::TextFormat::ParseFromString(text_message, &m));
90 ::google::protobuf::TextFormat::PrintToString(m, &text_message_reformated);
91
92 return text_message_reformated;
93 }
94
95 // User entered message reformatted by protobuf
96 std::string m_expected_text_reformated;
97 };
98
99 // If Your code requires fields from this template, You need
100 // to create a specialization of this class for concrete message
101 struct Message_encoder {
102 static std::string encode(const XProtocol::Message &msg) {
103 std::string result;
104
105 msg.SerializeToString(&result);
106
107 return result;
108 }
109 };
110
111 template <typename Msg>
112 struct Client_message {};
113
114 template <>
115 struct Client_message<::Mysqlx::Session::AuthenticateStart> {
116 enum { id = ::Mysqlx::ClientMessages::SESS_AUTHENTICATE_START };
117
118 static ::Mysqlx::ClientMessages::Type get_id() {
119 return static_cast<::Mysqlx::ClientMessages::Type>(id);
120 }
121
122 static ::Mysqlx::Session::AuthenticateStart make_required() {
123 ::Mysqlx::Session::AuthenticateStart msg;
124 msg.set_mech_name("");
125 return msg;
126 }
127 };
128
129 template <>
130 struct Client_message<::Mysqlx::Session::AuthenticateContinue> {
131 enum { id = ::Mysqlx::ClientMessages::SESS_AUTHENTICATE_CONTINUE };
132
133 static ::Mysqlx::ClientMessages::Type get_id() {
134 return static_cast<::Mysqlx::ClientMessages::Type>(id);
135 }
136
137 static ::Mysqlx::Session::AuthenticateContinue make_required() {
138 ::Mysqlx::Session::AuthenticateContinue msg;
139 msg.set_auth_data("");
140 return msg;
141 }
142 };
143
144 template <>
145 struct Client_message<::Mysqlx::Session::Reset> {
146 enum { id = ::Mysqlx::ClientMessages::SESS_RESET };
147
148 static ::Mysqlx::ClientMessages::Type get_id() {
149 return static_cast<::Mysqlx::ClientMessages::Type>(id);
150 }
151
152 static ::Mysqlx::Session::Reset make_required() { return {}; }
153 };
154
155 template <>
156 struct Client_message<::Mysqlx::Session::Close> {
157 enum { id = ::Mysqlx::ClientMessages::SESS_CLOSE };
158
159 static ::Mysqlx::ClientMessages::Type get_id() {
160 return static_cast<::Mysqlx::ClientMessages::Type>(id);
161 }
162
163 static ::Mysqlx::Session::Close make_required() { return {}; }
164 };
165
166 template <>
167 struct Client_message<::Mysqlx::Sql::StmtExecute> {
168 enum { id = ::Mysqlx::ClientMessages::SQL_STMT_EXECUTE };
169
170 static ::Mysqlx::ClientMessages::Type get_id() {
171 return static_cast<::Mysqlx::ClientMessages::Type>(id);
172 }
173
174 static ::Mysqlx::Sql::StmtExecute make_required() {
175 ::Mysqlx::Sql::StmtExecute msg;
176 msg.set_stmt("");
177 return msg;
178 }
179 };
180
181 template <>
182 struct Client_message<::Mysqlx::Crud::Find> {
183 enum { id = ::Mysqlx::ClientMessages::CRUD_FIND };
184
185 static ::Mysqlx::ClientMessages::Type get_id() {
186 return static_cast<::Mysqlx::ClientMessages::Type>(id);
187 }
188
189 static ::Mysqlx::Crud::Find make_required() {
190 ::Mysqlx::Crud::Find msg;
191 msg.mutable_collection()->set_name("");
192 return msg;
193 }
194 };
195
196 template <>
197 struct Client_message<::Mysqlx::Crud::Insert> {
198 enum { id = ::Mysqlx::ClientMessages::CRUD_INSERT };
199
200 static ::Mysqlx::ClientMessages::Type get_id() {
201 return static_cast<::Mysqlx::ClientMessages::Type>(id);
202 }
203
204 static ::Mysqlx::Crud::Insert make_required() {
205 ::Mysqlx::Crud::Insert msg;
206 msg.mutable_collection()->set_name("");
207 return msg;
208 }
209 };
210
211 template <>
212 struct Client_message<::Mysqlx::Crud::Update> {
213 enum { id = ::Mysqlx::ClientMessages::CRUD_UPDATE };
214
215 static ::Mysqlx::ClientMessages::Type get_id() {
216 return static_cast<::Mysqlx::ClientMessages::Type>(id);
217 }
218
219 static ::Mysqlx::Crud::Update make_required() {
220 ::Mysqlx::Crud::Update msg;
221 msg.mutable_collection()->set_name("");
222 return msg;
223 }
224 };
225
226 template <>
227 struct Client_message<::Mysqlx::Crud::Delete> {
228 enum { id = ::Mysqlx::ClientMessages::CRUD_DELETE };
229
230 static ::Mysqlx::ClientMessages::Type get_id() {
231 return static_cast<::Mysqlx::ClientMessages::Type>(id);
232 }
233
234 static ::Mysqlx::Crud::Delete make_required() {
235 ::Mysqlx::Crud::Delete msg;
236 msg.mutable_collection()->set_name("");
237 return msg;
238 }
239 };
240
241 template <>
242 struct Client_message<::Mysqlx::Crud::CreateView> {
243 enum { id = ::Mysqlx::ClientMessages::CRUD_CREATE_VIEW };
244
245 static ::Mysqlx::ClientMessages::Type get_id() {
246 return static_cast<::Mysqlx::ClientMessages::Type>(id);
247 }
248
249 static ::Mysqlx::Crud::CreateView make_required() {
250 ::Mysqlx::Crud::CreateView msg;
251 msg.mutable_collection()->set_name("");
252 msg.mutable_stmt()->mutable_collection()->set_name("");
253 return msg;
254 }
255 };
256
257 template <>
258 struct Client_message<::Mysqlx::Crud::ModifyView> {
259 enum { id = ::Mysqlx::ClientMessages::CRUD_MODIFY_VIEW };
260
261 static ::Mysqlx::ClientMessages::Type get_id() {
262 return static_cast<::Mysqlx::ClientMessages::Type>(id);
263 }
264
265 static ::Mysqlx::Crud::ModifyView make_required() {
266 ::Mysqlx::Crud::ModifyView msg;
267 msg.mutable_collection()->set_name("");
268 return msg;
269 }
270 };
271
272 template <>
273 struct Client_message<::Mysqlx::Crud::DropView> {
274 enum { id = ::Mysqlx::ClientMessages::CRUD_DROP_VIEW };
275
276 static ::Mysqlx::ClientMessages::Type get_id() {
277 return static_cast<::Mysqlx::ClientMessages::Type>(id);
278 }
279
280 static ::Mysqlx::Crud::DropView make_required() {
281 ::Mysqlx::Crud::DropView msg;
282 msg.mutable_collection()->set_name("");
283 return msg;
284 }
285 };
286
287 template <>
288 struct Client_message<::Mysqlx::Expect::Open> {
289 enum { id = ::Mysqlx::ClientMessages::EXPECT_OPEN };
290
291 static ::Mysqlx::ClientMessages::Type get_id() {
292 return static_cast<::Mysqlx::ClientMessages::Type>(id);
293 }
294
295 static ::Mysqlx::Expect::Open make_required() { return {}; }
296 };
297
298 template <>
299 struct Client_message<::Mysqlx::Expect::Close> {
300 enum { id = ::Mysqlx::ClientMessages::EXPECT_CLOSE };
301
302 static ::Mysqlx::ClientMessages::Type get_id() {
303 return static_cast<::Mysqlx::ClientMessages::Type>(id);
304 }
305
306 static ::Mysqlx::Expect::Close make_required() { return {}; }
307 };
308
309 template <>
310 struct Client_message<::Mysqlx::Connection::CapabilitiesGet> {
311 enum { id = ::Mysqlx::ClientMessages::CON_CAPABILITIES_GET };
312
313 static ::Mysqlx::ClientMessages::Type get_id() {
314 return static_cast<::Mysqlx::ClientMessages::Type>(id);
315 }
316
317 static ::Mysqlx::Connection::CapabilitiesGet make_required() { return {}; }
318 };
319
320 template <>
321 struct Client_message<::Mysqlx::Connection::CapabilitiesSet> {
322 enum { id = ::Mysqlx::ClientMessages::CON_CAPABILITIES_SET };
323
324 static ::Mysqlx::ClientMessages::Type get_id() {
325 return static_cast<::Mysqlx::ClientMessages::Type>(id);
326 }
327
328 static ::Mysqlx::Connection::CapabilitiesSet make_required() {
329 ::Mysqlx::Connection::CapabilitiesSet msg;
330 msg.mutable_capabilities();
331 return msg;
332 }
333 };
334
335 template <>
336 struct Client_message<::Mysqlx::Connection::Close> {
337 enum { id = ::Mysqlx::ClientMessages::CON_CLOSE };
338
339 static ::Mysqlx::ClientMessages::Type get_id() {
340 return static_cast<::Mysqlx::ClientMessages::Type>(id);
341 }
342
343 static ::Mysqlx::Connection::Close make_required() { return {}; }
344 };
345
346 template <>
347 struct Client_message<::Mysqlx::Prepare::Prepare> {
348 enum { id = ::Mysqlx::ClientMessages::PREPARE_PREPARE };
349
350 static ::Mysqlx::ClientMessages::Type get_id() {
351 return static_cast<::Mysqlx::ClientMessages::Type>(id);
352 }
353
354 static ::Mysqlx::Prepare::Prepare make_required() {
355 ::Mysqlx::Prepare::Prepare msg;
356 msg.set_stmt_id(1);
357 auto stmt = msg.mutable_stmt();
358 stmt->set_type(Mysqlx::Prepare::Prepare_OneOfMessage::FIND);
359 *stmt->mutable_find() =
360 Client_message<Mysqlx::Crud::Find>().make_required();
361 return msg;
362 }
363 };
364
365 template <>
366 struct Client_message<::Mysqlx::Prepare::Execute> {
367 enum { id = ::Mysqlx::ClientMessages::PREPARE_EXECUTE };
368
369 static ::Mysqlx::ClientMessages::Type get_id() {
370 return static_cast<::Mysqlx::ClientMessages::Type>(id);
371 }
372
373 static ::Mysqlx::Prepare::Execute make_required() {
374 ::Mysqlx::Prepare::Execute msg;
375 msg.set_stmt_id(1);
376 return msg;
377 }
378 };
379
380 template <>
381 struct Client_message<::Mysqlx::Prepare::Deallocate> {
382 enum { id = ::Mysqlx::ClientMessages::PREPARE_DEALLOCATE };
383
384 static ::Mysqlx::ClientMessages::Type get_id() {
385 return static_cast<::Mysqlx::ClientMessages::Type>(id);
386 }
387
388 static ::Mysqlx::Prepare::Deallocate make_required() {
389 ::Mysqlx::Prepare::Deallocate msg;
390 msg.set_stmt_id(1);
391 return msg;
392 }
393 };
394
395 template <>
396 struct Client_message<::Mysqlx::Cursor::Open> {
397 enum { id = ::Mysqlx::ClientMessages::CURSOR_OPEN };
398
399 static ::Mysqlx::ClientMessages::Type get_id() {
400 return static_cast<::Mysqlx::ClientMessages::Type>(id);
401 }
402
403 static ::Mysqlx::Cursor::Open make_required() {
404 ::Mysqlx::Cursor::Open msg;
405 msg.set_cursor_id(1);
406 auto statement = msg.mutable_stmt();
407 statement->set_type(Mysqlx::Cursor::Open::OneOfMessage::PREPARE_EXECUTE);
408 *statement->mutable_prepare_execute() =
409 Client_message<Mysqlx::Prepare::Execute>().make_required();
410 return msg;
411 }
412 };
413
414 template <>
415 struct Client_message<::Mysqlx::Cursor::Fetch> {
416 enum { id = ::Mysqlx::ClientMessages::CURSOR_FETCH };
417
418 static ::Mysqlx::ClientMessages::Type get_id() {
419 return static_cast<::Mysqlx::ClientMessages::Type>(id);
420 }
421
422 static ::Mysqlx::Cursor::Fetch make_required() {
423 ::Mysqlx::Cursor::Fetch msg;
424 msg.set_cursor_id(1);
425 return msg;
426 }
427 };
428
429 template <typename Msg>
430 struct Server_message {};
431
432 template <>
433 struct Server_message<::Mysqlx::Ok> {
434 enum { id = ::Mysqlx::ServerMessages::OK };
435
436 static ::Mysqlx::ServerMessages::Type get_id() {
437 return static_cast<::Mysqlx::ServerMessages::Type>(id);
438 }
439
440 static ::Mysqlx::Ok make_required() { return {}; }
441 };
442
443 template <>
444 struct Server_message<::Mysqlx::Error> {
445 enum { id = ::Mysqlx::ServerMessages::ERROR };
446
447 static ::Mysqlx::ServerMessages::Type get_id() {
448 return static_cast<::Mysqlx::ServerMessages::Type>(id);
449 }
450
451 static ::Mysqlx::Error make_required() {
452 ::Mysqlx::Error result;
453 result.set_code(1);
454 result.set_sql_state("");
455 result.set_msg("");
456 return result;
457 }
458 };
459
460 template <>
461 struct Server_message<::Mysqlx::Connection::Capabilities> {
462 enum { id = ::Mysqlx::ServerMessages::CONN_CAPABILITIES };
463
464 static ::Mysqlx::ServerMessages::Type get_id() {
465 return static_cast<::Mysqlx::ServerMessages::Type>(id);
466 }
467
468 static ::Mysqlx::Connection::Capabilities make_required() { return {}; }
469 };
470
471 template <>
472 struct Server_message<::Mysqlx::Session::AuthenticateContinue> {
473 enum { id = ::Mysqlx::ServerMessages::SESS_AUTHENTICATE_CONTINUE };
474
475 static ::Mysqlx::ServerMessages::Type get_id() {
476 return static_cast<::Mysqlx::ServerMessages::Type>(id);
477 }
478
479 static ::Mysqlx::Session::AuthenticateContinue make_required() {
480 ::Mysqlx::Session::AuthenticateContinue result;
481 result.set_auth_data("123456789012345678901234567890");
482 return result;
483 }
484 };
485
486 template <>
487 struct Server_message<::Mysqlx::Session::AuthenticateOk> {
488 enum { id = ::Mysqlx::ServerMessages::SESS_AUTHENTICATE_OK };
489
490 static ::Mysqlx::ServerMessages::Type get_id() {
491 return static_cast<::Mysqlx::ServerMessages::Type>(id);
492 }
493
494 static ::Mysqlx::Session::AuthenticateOk make_required() { return {}; }
495 };
496
497 template <>
498 struct Server_message<::Mysqlx::Notice::Frame> {
499 enum { id = ::Mysqlx::ServerMessages::NOTICE };
500
501 static ::Mysqlx::ServerMessages::Type get_id() {
502 return static_cast<::Mysqlx::ServerMessages::Type>(id);
503 }
504
505 static ::Mysqlx::Notice::Frame make_required() {
506 ::Mysqlx::Notice::Frame result;
507 result.set_type(::Mysqlx::Notice::Frame_Type_WARNING);
508 return result;
509 }
510 };
511
512 template <>
513 struct Server_message<::Mysqlx::Resultset::ColumnMetaData> {
514 enum { id = ::Mysqlx::ServerMessages::RESULTSET_COLUMN_META_DATA };
515
516 static ::Mysqlx::ServerMessages::Type get_id() {
517 return static_cast<::Mysqlx::ServerMessages::Type>(id);
518 }
519
520 static ::Mysqlx::Resultset::ColumnMetaData make_required() {
521 ::Mysqlx::Resultset::ColumnMetaData result;
522 result.set_type(Mysqlx::Resultset::ColumnMetaData_FieldType_DECIMAL);
523 return {};
524 }
525 };
526
527 template <>
528 struct Server_message<::Mysqlx::Resultset::Row> {
529 enum { id = ::Mysqlx::ServerMessages::RESULTSET_ROW };
530
531 static ::Mysqlx::ServerMessages::Type get_id() {
532 return static_cast<::Mysqlx::ServerMessages::Type>(id);
533 }
534
535 static ::Mysqlx::Resultset::Row make_required() { return {}; }
536 };
537
538 template <>
539 struct Server_message<::Mysqlx::Resultset::FetchDone> {
540 enum { id = ::Mysqlx::ServerMessages::RESULTSET_FETCH_DONE };
541
542 static ::Mysqlx::ServerMessages::Type get_id() {
543 return static_cast<::Mysqlx::ServerMessages::Type>(id);
544 }
545
546 static ::Mysqlx::Resultset::FetchDone make_required() { return {}; }
547 };
548
549 template <>
550 struct Server_message<::Mysqlx::Resultset::FetchSuspended> {
551 enum { id = ::Mysqlx::ServerMessages::RESULTSET_FETCH_SUSPENDED };
552
553 static ::Mysqlx::ServerMessages::Type get_id() {
554 return static_cast<::Mysqlx::ServerMessages::Type>(id);
555 }
556
557 static ::Mysqlx::Resultset::FetchSuspended make_required() { return {}; }
558 };
559
560 template <>
561 struct Server_message<::Mysqlx::Resultset::FetchDoneMoreResultsets> {
562 enum { id = ::Mysqlx::ServerMessages::RESULTSET_FETCH_DONE_MORE_RESULTSETS };
563
564 static ::Mysqlx::ServerMessages::Type get_id() {
565 return static_cast<::Mysqlx::ServerMessages::Type>(id);
566 }
567
568 static ::Mysqlx::Resultset::FetchDoneMoreResultsets make_required() {
569 return {};
570 }
571 };
572
573 template <>
574 struct Server_message<::Mysqlx::Sql::StmtExecuteOk> {
575 enum { id = ::Mysqlx::ServerMessages::SQL_STMT_EXECUTE_OK };
576
577 static ::Mysqlx::ServerMessages::Type get_id() {
578 return static_cast<::Mysqlx::ServerMessages::Type>(id);
579 }
580
581 static ::Mysqlx::Sql::StmtExecuteOk make_required() { return {}; }
582 };
583
584 template <>
585 struct Server_message<::Mysqlx::Resultset::FetchDoneMoreOutParams> {
586 enum { id = ::Mysqlx::ServerMessages::RESULTSET_FETCH_DONE_MORE_OUT_PARAMS };
587
588 static ::Mysqlx::ServerMessages::Type get_id() {
589 return static_cast<::Mysqlx::ServerMessages::Type>(id);
590 }
591
592 static ::Mysqlx::Resultset::FetchDoneMoreOutParams make_required() {
593 return {};
594 }
595 };
596
597 // parameter `n` can be either a string or XMessage
598 MATCHER_P(Cmp_msg, n, "") {
599 Message_compare<typename std::decay<decltype(arg)>::type> comparer(n);
600 const bool result = comparer == arg;
601
602 if (!result) {
603 *result_listener << comparer.explain_failure(arg);
604 }
605
606 return result;
607 }
608
609 } // namespace test
610 } // namespace xcl
611
612 #endif // UNITTEST_GUNIT_XPLUGIN_XCL_MESSAGE_HELPERS_H_