| 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 | namespace xcl { |
| 29 | namespace test { |
| 30 | |
| 31 | TEST_F(Xcl_protocol_impl_tests, execute_close) { |
| 32 | using Send_desc = Client_message<::Mysqlx::Connection::Close>; |
| 33 | using Recv_desc = Server_message<::Mysqlx::Ok>; |
| 34 | |
| 35 | auto msg_send = Send_desc::make_required(); |
| 36 | auto msg_recv = Recv_desc::make_required(); |
| 37 | |
| 38 | expect_write_message_without_payload(msg_send); |
| 39 | expect_read_message_without_payload(msg_recv); |
| 40 | |
| 41 | auto error = m_sut->execute_close(); |
| 42 | |
| 43 | ASSERT_FALSE(error); |
| 44 | } |
| 45 | |
| 46 | TEST_F(Xcl_protocol_impl_tests, execute_close_fail_at_write) { |
| 47 | const uint32_t expected_header_size = 5; |
| 48 | const int expected_error_code = 1123; |
| 49 | |
| 50 | EXPECT_CALL(*m_mock_connection, write(_, expected_header_size)) |
| 51 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 52 | auto error = m_sut->execute_close(); |
| 53 | |
| 54 | ASSERT_EQ(expected_error_code, error.error()); |
| 55 | } |
| 56 | |
| 57 | TEST_F(Xcl_protocol_impl_tests, execute_close_fail_at_read) { |
| 58 | const uint32_t expected_header_size = 5; |
| 59 | const int expected_error_code = 1124; |
| 60 | |
| 61 | EXPECT_CALL(*m_mock_connection, write(_, expected_header_size)) |
| 62 | .WillOnce(Return(XError())); |
| 63 | EXPECT_CALL(*m_mock_connection, read(_, _)) |
| 64 | .WillOnce(Return(XError{expected_error_code, ""})); |
| 65 | auto error = m_sut->execute_close(); |
| 66 | |
| 67 | ASSERT_EQ(expected_error_code, error.error()); |
| 68 | } |
| 69 | |
| 70 | TEST_F(Xcl_protocol_impl_tests, execute_close_failed_because_of_error_msg) { |
| 71 | using Send_desc = Client_message<::Mysqlx::Connection::Close>; |
| 72 | using Recv_desc = Server_message<::Mysqlx::Error>; |
| 73 | |
| 74 | auto msg_send = Send_desc::make_required(); |
| 75 | auto msg_recv = Recv_desc::make_required(); |
| 76 | |
| 77 | const uint32_t expected_error_code = 1124; |
| 78 | msg_recv.set_code(expected_error_code); |
| 79 | |
| 80 | expect_write_message_without_payload(msg_send); |
| 81 | expect_read_message(msg_recv); |
| 82 | |
| 83 | auto error = m_sut->execute_close(); |
| 84 | |
| 85 | ASSERT_EQ(expected_error_code, error.error()); |
| 86 | } |
| 87 | |
| 88 | TEST_F(Xcl_protocol_impl_tests, execute_close_failed_because_recv_wrong_msg) { |
| 89 | using Send_desc = Client_message<::Mysqlx::Connection::Close>; |
| 90 | using Recv_desc = Server_message<::Mysqlx::Session::AuthenticateContinue>; |
| 91 | |
| 92 | auto msg_send = Send_desc::make_required(); |
| 93 | auto msg_recv = Recv_desc::make_required(); |
| 94 | |
| 95 | expect_write_message_without_payload(msg_send); |
| 96 | expect_read_message(msg_recv); |
| 97 | |
| 98 | auto error = m_sut->execute_close(); |
| 99 | |
| 100 | ASSERT_EQ(CR_MALFORMED_PACKET, error.error()); |
| 101 | } |
| 102 | |
| 103 | TEST_F(Xcl_protocol_impl_tests, execute_with_resultset_msg_without_payload) { |
| 104 | using Send_desc = Client_message<::Mysqlx::Session::Close>; |
| 105 | auto msg_send = Send_desc::make_required(); |
| 106 | XError out_error; |
| 107 | |
| 108 | XQuery_result *expected_result = expect_factory_new_result(); |
| 109 | expect_write_message_without_payload(msg_send); |
| 110 | |
| 111 | auto result = |
| 112 | m_sut->execute_with_resultset(Send_desc::get_id(), msg_send, &out_error); |
| 113 | |
| 114 | ASSERT_FALSE(out_error); |
| 115 | ASSERT_EQ(expected_result, result.get()); |
| 116 | } |
| 117 | |
| 118 | TEST_F(Xcl_protocol_impl_tests, execute_with_resultset_msg_with_payload) { |
| 119 | using Send_desc = Client_message<::Mysqlx::Crud::CreateView>; |
| 120 | auto msg_send = Send_desc::make_required(); |
| 121 | XError out_error; |
| 122 | |
| 123 | XQuery_result *expected_result = expect_factory_new_result(); |
| 124 | expect_write_message(msg_send); |
| 125 | |
| 126 | auto result = |
| 127 | m_sut->execute_with_resultset(Send_desc::get_id(), msg_send, &out_error); |
| 128 | |
| 129 | ASSERT_FALSE(out_error); |
| 130 | ASSERT_EQ(expected_result, result.get()); |
| 131 | } |
| 132 | |
| 133 | TEST_F(Xcl_protocol_impl_tests, execute_with_resultset_fails_at_header_write) { |
| 134 | using Close_desc = Client_message<::Mysqlx::Session::Close>; |
| 135 | |
| 136 | auto msg_close = Close_desc::make_required(); |
| 137 | const int expected_error_code = 23324; |
| 138 | XError out_error; |
| 139 | |
| 140 | expect_write_message_without_payload(msg_close, expected_error_code); |
| 141 | |
| 142 | auto result = m_sut->execute_with_resultset(Close_desc::get_id(), msg_close, |
| 143 | &out_error); |
| 144 | |
| 145 | ASSERT_EQ(expected_error_code, out_error.error()); |
| 146 | } |
| 147 | |
| 148 | template <typename Msg> |
| 149 | class xcl_protocol_impl_tests_execute_msg |
| 150 | : public Xcl_protocol_impl_tests_with_msg<Msg> { |
| 151 | public: |
| 152 | void SetUp() override { |
| 153 | Xcl_protocol_impl_tests_with_msg<Msg>::SetUp(); |
| 154 | this->setup_required_fields_in_message(); |
| 155 | } |
| 156 | |
| 157 | std::unique_ptr<XQuery_result> do_execute(const Mysqlx::Sql::StmtExecute &m, |
| 158 | XError *out_error) { |
| 159 | return this->m_sut->execute_stmt(m, out_error); |
| 160 | } |
| 161 | |
| 162 | std::unique_ptr<XQuery_result> do_execute(const Mysqlx::Crud::Find &m, |
| 163 | XError *out_error) { |
| 164 | return this->m_sut->execute_find(m, out_error); |
| 165 | } |
| 166 | |
| 167 | std::unique_ptr<XQuery_result> do_execute(const Mysqlx::Crud::Update &m, |
| 168 | XError *out_error) { |
| 169 | return this->m_sut->execute_update(m, out_error); |
| 170 | } |
| 171 | |
| 172 | std::unique_ptr<XQuery_result> do_execute(const Mysqlx::Crud::Insert &m, |
| 173 | XError *out_error) { |
| 174 | return this->m_sut->execute_insert(m, out_error); |
| 175 | } |
| 176 | |
| 177 | std::unique_ptr<XQuery_result> do_execute(const Mysqlx::Crud::Delete &m, |
| 178 | XError *out_error) { |
| 179 | return this->m_sut->execute_delete(m, out_error); |
| 180 | } |
| 181 | |
| 182 | std::unique_ptr<XQuery_result> do_execute(const Mysqlx::Cursor::Open &m, |
| 183 | XError *out_error) { |
| 184 | return this->m_sut->execute_cursor_open(m, out_error); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | TEST_F(Xcl_protocol_impl_tests, cursor_fetch_msg_with_payload) { |
| 189 | using Send_desc = Client_message<::Mysqlx::Cursor::Fetch>; |
| 190 | auto msg_send = Send_desc::make_required(); |
| 191 | XError out_error; |
| 192 | |
| 193 | mock::XQuery_result *expected_result = new mock::XQuery_result(); |
| 194 | expect_write_message(msg_send); |
| 195 | |
| 196 | XQuery_result::Metadata metadata; |
| 197 | std::unique_ptr<mock::XQuery_result> open_result(new mock::XQuery_result()); |
| 198 | EXPECT_CALL(*open_result.get(), get_metadata(_)) |
| 199 | .WillOnce(ReturnRef(metadata)); |
| 200 | EXPECT_CALL(m_mock_factory, create_result_raw(_, _, _)) |
| 201 | .WillOnce(Return(expected_result)); |
| 202 | EXPECT_CALL(*expected_result, set_metadata(_)); |
| 203 | auto result = |
| 204 | m_sut->execute_cursor_fetch(msg_send, std::move(open_result), &out_error); |
| 205 | |
| 206 | ASSERT_FALSE(out_error); |
| 207 | ASSERT_EQ(expected_result, result.get()); |
| 208 | } |
| 209 | |
| 210 | using Msg_vs_method_types = |
| 211 | ::testing::Types<::Mysqlx::Sql::StmtExecute, ::Mysqlx::Crud::Find, |
| 212 | ::Mysqlx::Crud::Insert, ::Mysqlx::Crud::Update, |
| 213 | ::Mysqlx::Crud::Delete, ::Mysqlx::Cursor::Open>; |
| 214 | |
| 215 | TYPED_TEST_SUITE(xcl_protocol_impl_tests_execute_msg, Msg_vs_method_types); |
| 216 | |
| 217 | TYPED_TEST(xcl_protocol_impl_tests_execute_msg, msg_with_payload) { |
| 218 | XError out_error; |
| 219 | |
| 220 | XQuery_result *expected_result = this->expect_factory_new_result(); |
| 221 | this->expect_write_message(*this->m_message); |
| 222 | |
| 223 | auto result = this->do_execute(*this->m_message, &out_error); |
| 224 | |
| 225 | ASSERT_FALSE(out_error); |
| 226 | ASSERT_EQ(expected_result, result.get()); |
| 227 | } |
| 228 | |
| 229 | TYPED_TEST(xcl_protocol_impl_tests_execute_msg, fails_at_message_write) { |
| 230 | const uint32_t expected_error_code = 23324; |
| 231 | XError out_error; |
| 232 | |
| 233 | this->expect_write_message(*this->m_message, expected_error_code); |
| 234 | |
| 235 | auto result = this->do_execute(*this->m_message, &out_error); |
| 236 | |
| 237 | ASSERT_EQ(expected_error_code, out_error.error()); |
| 238 | } |
| 239 | |
| 240 | TEST_F(Xcl_protocol_impl_tests, execute_fetch_capabilities) { |
| 241 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesGet>; |
| 242 | using Recv_desc = Server_message<::Mysqlx::Connection::Capabilities>; |
| 243 | |
| 244 | auto msg_send = Send_desc::make_required(); |
| 245 | auto msg_recv = Recv_desc::make_required(); |
| 246 | XError out_error; |
| 247 | |
| 248 | auto cap_value = msg_recv.mutable_capabilities()->Add(); |
| 249 | cap_value->set_name("CapName"); |
| 250 | auto any = cap_value->mutable_value(); |
| 251 | any->set_type(::Mysqlx::Datatypes::Any_Type_SCALAR); |
| 252 | auto scalar = any->mutable_scalar(); |
| 253 | scalar->set_type(::Mysqlx::Datatypes::Scalar_Type_V_BOOL); |
| 254 | scalar->set_v_bool(true); |
| 255 | |
| 256 | expect_write_message_without_payload(msg_send); |
| 257 | expect_read_message(msg_recv); |
| 258 | |
| 259 | auto result = m_sut->execute_fetch_capabilities(&out_error); |
| 260 | |
| 261 | ASSERT_FALSE(out_error); |
| 262 | ASSERT_EQ(Message_compare<::Mysqlx::Connection::Capabilities>(msg_recv), |
| 263 | *result); |
| 264 | } |
| 265 | |
| 266 | TEST_F(Xcl_protocol_impl_tests, |
| 267 | execute_fetch_capabilities_fails_at_header_write) { |
| 268 | const uint32_t expected_error = 2300; |
| 269 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesGet>; |
| 270 | auto msg_send = Send_desc::make_required(); |
| 271 | XError out_error; |
| 272 | |
| 273 | expect_write_message_without_payload(msg_send, expected_error); |
| 274 | |
| 275 | auto result = m_sut->execute_fetch_capabilities(&out_error); |
| 276 | |
| 277 | ASSERT_EQ(expected_error, out_error.error()); |
| 278 | ASSERT_FALSE(result.get()); |
| 279 | } |
| 280 | |
| 281 | TEST_F(Xcl_protocol_impl_tests, execute_fetch_capabilities_fails_at_read) { |
| 282 | const uint32_t expected_error = 2301; |
| 283 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesGet>; |
| 284 | using Recv_desc = Server_message<::Mysqlx::Connection::Capabilities>; |
| 285 | |
| 286 | auto msg_send = Send_desc::make_required(); |
| 287 | auto msg_recv = Recv_desc::make_required(); |
| 288 | XError out_error; |
| 289 | |
| 290 | expect_write_message_without_payload(msg_send); |
| 291 | expect_read_message_without_payload(msg_recv, expected_error); |
| 292 | |
| 293 | auto result = m_sut->execute_fetch_capabilities(&out_error); |
| 294 | |
| 295 | ASSERT_EQ(expected_error, out_error.error()); |
| 296 | ASSERT_FALSE(result.get()); |
| 297 | } |
| 298 | |
| 299 | TEST_F(Xcl_protocol_impl_tests, |
| 300 | execute_fetch_capabilities_fails_because_recv_error_msg) { |
| 301 | const uint32_t expected_error = 2302; |
| 302 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesGet>; |
| 303 | using Recv_desc = Server_message<::Mysqlx::Error>; |
| 304 | |
| 305 | auto msg_send = Send_desc::make_required(); |
| 306 | auto msg_recv = Recv_desc::make_required(); |
| 307 | XError out_error; |
| 308 | |
| 309 | msg_recv.set_code(expected_error); |
| 310 | expect_write_message_without_payload(msg_send); |
| 311 | expect_read_message(msg_recv); |
| 312 | |
| 313 | auto result = m_sut->execute_fetch_capabilities(&out_error); |
| 314 | |
| 315 | ASSERT_EQ(expected_error, out_error.error()); |
| 316 | ASSERT_FALSE(result.get()); |
| 317 | } |
| 318 | |
| 319 | TEST_F(Xcl_protocol_impl_tests, |
| 320 | execute_fetch_capabilities_fails_because_wrong_msg) { |
| 321 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesGet>; |
| 322 | using Recv_desc = Server_message<::Mysqlx::Ok>; |
| 323 | |
| 324 | auto msg_send = Send_desc::make_required(); |
| 325 | auto msg_recv = Recv_desc::make_required(); |
| 326 | XError out_error; |
| 327 | |
| 328 | expect_write_message_without_payload(msg_send); |
| 329 | expect_read_message_without_payload(msg_recv); |
| 330 | |
| 331 | auto result = m_sut->execute_fetch_capabilities(&out_error); |
| 332 | |
| 333 | ASSERT_EQ(CR_MALFORMED_PACKET, out_error.error()); |
| 334 | ASSERT_FALSE(result.get()); |
| 335 | } |
| 336 | |
| 337 | TEST_F(Xcl_protocol_impl_tests, execute_set_capability) { |
| 338 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesSet>; |
| 339 | using Recv_desc = Server_message<::Mysqlx::Ok>; |
| 340 | |
| 341 | auto msg_send = Send_desc::make_required(); |
| 342 | auto msg_recv = Recv_desc::make_required(); |
| 343 | |
| 344 | expect_write_message(msg_send); |
| 345 | expect_read_message_without_payload(msg_recv); |
| 346 | |
| 347 | auto error = m_sut->execute_set_capability(msg_send); |
| 348 | |
| 349 | ASSERT_FALSE(error); |
| 350 | } |
| 351 | |
| 352 | TEST_F(Xcl_protocol_impl_tests, |
| 353 | execute_set_capability_failed_because_of_recv_error_msg) { |
| 354 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesSet>; |
| 355 | using Recv_desc = Server_message<::Mysqlx::Error>; |
| 356 | |
| 357 | const uint32_t expected_error_code = 1001; |
| 358 | auto msg_send = Send_desc::make_required(); |
| 359 | auto msg_recv = Recv_desc::make_required(); |
| 360 | |
| 361 | msg_recv.set_code(expected_error_code); |
| 362 | expect_write_message(msg_send); |
| 363 | expect_read_message(msg_recv); |
| 364 | |
| 365 | auto error = m_sut->execute_set_capability(msg_send); |
| 366 | |
| 367 | ASSERT_EQ(expected_error_code, error.error()); |
| 368 | } |
| 369 | |
| 370 | TEST_F(Xcl_protocol_impl_tests, |
| 371 | execute_set_capability_failed_because_of_send_error) { |
| 372 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesSet>; |
| 373 | |
| 374 | const uint32_t expected_error_code = 1002; |
| 375 | auto msg_send = Send_desc::make_required(); |
| 376 | |
| 377 | expect_write_message(msg_send, expected_error_code); |
| 378 | |
| 379 | auto error = m_sut->execute_set_capability(msg_send); |
| 380 | |
| 381 | ASSERT_EQ(expected_error_code, error.error()); |
| 382 | } |
| 383 | |
| 384 | TEST_F(Xcl_protocol_impl_tests, |
| 385 | execute_set_capability_failed_because_of_wrong_msg_recv) { |
| 386 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesSet>; |
| 387 | using Recv_desc = Server_message<::Mysqlx::Session::AuthenticateContinue>; |
| 388 | |
| 389 | auto msg_send = Send_desc::make_required(); |
| 390 | auto msg_recv = Recv_desc::make_required(); |
| 391 | |
| 392 | expect_write_message(msg_send); |
| 393 | expect_read_message(msg_recv); |
| 394 | |
| 395 | auto error = m_sut->execute_set_capability(msg_send); |
| 396 | |
| 397 | ASSERT_EQ(CR_MALFORMED_PACKET, error.error()); |
| 398 | } |
| 399 | |
| 400 | TEST_F(Xcl_protocol_impl_tests, |
| 401 | execute_set_capability_failed_because_of_io_error_recv) { |
| 402 | using Send_desc = Client_message<::Mysqlx::Connection::CapabilitiesSet>; |
| 403 | using Recv_desc = Server_message<::Mysqlx::Ok>; |
| 404 | |
| 405 | const uint32_t expected_error_code = 1003; |
| 406 | auto msg_send = Send_desc::make_required(); |
| 407 | auto msg_recv = Recv_desc::make_required(); |
| 408 | |
| 409 | expect_write_message(msg_send); |
| 410 | expect_read_message_without_payload(msg_recv, expected_error_code); |
| 411 | |
| 412 | auto error = m_sut->execute_set_capability(msg_send); |
| 413 | |
| 414 | ASSERT_EQ(expected_error_code, error.error()); |
| 415 | } |
| 416 | |
| 417 | } // namespace test |
| 418 | } // namespace xcl |