| 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 "gcs_base_test.h" |
| 25 | |
| 26 | #include <array> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "gcs_message_stage_lz4.h" |
| 30 | #include "gcs_message_stage_split.h" |
| 31 | #include "gcs_xcom_communication_interface.h" |
| 32 | #include "gcs_xcom_statistics_interface.h" |
| 33 | #include "mysql/gcs/gcs_message.h" |
| 34 | #include "template_utils.h" |
| 35 | |
| 36 | namespace gcs_xcom_communication_unittest { |
| 37 | |
| 38 | class mock_gcs_xcom_view_change_control_interface |
| 39 | : public Gcs_xcom_view_change_control_interface { |
| 40 | public: |
| 41 | MOCK_METHOD0(start_view_exchange, void()); |
| 42 | MOCK_METHOD0(end_view_exchange, void()); |
| 43 | MOCK_METHOD0(wait_for_view_change_end, void()); |
| 44 | MOCK_METHOD0(is_view_changing, bool()); |
| 45 | MOCK_METHOD0(start_leave, bool()); |
| 46 | MOCK_METHOD0(end_leave, void()); |
| 47 | MOCK_METHOD0(is_leaving, bool()); |
| 48 | MOCK_METHOD0(start_join, bool()); |
| 49 | MOCK_METHOD0(end_join, void()); |
| 50 | MOCK_METHOD0(is_joining, bool()); |
| 51 | |
| 52 | MOCK_METHOD1(set_current_view, void(Gcs_view *)); |
| 53 | MOCK_METHOD0(get_current_view, Gcs_view *()); |
| 54 | MOCK_METHOD0(belongs_to_group, bool()); |
| 55 | MOCK_METHOD1(set_belongs_to_group, void(bool)); |
| 56 | MOCK_METHOD1(set_unsafe_current_view, void(Gcs_view *)); |
| 57 | MOCK_METHOD0(get_unsafe_current_view, Gcs_view *()); |
| 58 | |
| 59 | MOCK_METHOD0(finalize, void()); |
| 60 | MOCK_METHOD0(is_finalized, bool()); |
| 61 | }; |
| 62 | |
| 63 | class mock_gcs_xcom_statistics_updater : public Gcs_xcom_statistics_updater { |
| 64 | public: |
| 65 | MOCK_METHOD1(update_message_sent, void(unsigned long long message_length)); |
| 66 | MOCK_METHOD1(update_message_received, void(long message_length)); |
| 67 | }; |
| 68 | |
| 69 | class mock_gcs_communication_event_listener |
| 70 | : public Gcs_communication_event_listener { |
| 71 | public: |
| 72 | MOCK_CONST_METHOD1(on_message_received, void(const Gcs_message &message)); |
| 73 | }; |
| 74 | |
| 75 | class mock_gcs_xcom_proxy : public Gcs_xcom_proxy_base { |
| 76 | public: |
| 77 | mock_gcs_xcom_proxy() { |
| 78 | ON_CALL(*this, xcom_open_handlers(_, _)).WillByDefault(Return(false)); |
| 79 | ON_CALL(*this, xcom_close_handlers()).WillByDefault(Return(false)); |
| 80 | ON_CALL(*this, xcom_client_add_node(_, _, _)).WillByDefault(Return(false)); |
| 81 | ON_CALL(*this, xcom_client_send_data(_, _)).WillByDefault(Return(false)); |
| 82 | } |
| 83 | |
| 84 | MOCK_METHOD3(new_node_address_uuid, |
| 85 | node_address *(unsigned int n, char const *names[], |
| 86 | blob uuids[])); |
| 87 | MOCK_METHOD2(delete_node_address, void(unsigned int n, node_address *na)); |
| 88 | MOCK_METHOD3(xcom_client_add_node, bool(connection_descriptor *con, |
| 89 | node_list *nl, uint32_t group_id)); |
| 90 | MOCK_METHOD2(xcom_client_remove_node, bool(node_list *nl, uint32_t group_id)); |
| 91 | MOCK_METHOD3(xcom_client_remove_node, bool(connection_descriptor *con, |
| 92 | node_list *nl, uint32_t group_id)); |
| 93 | MOCK_METHOD2(xcom_client_get_event_horizon, |
| 94 | bool(uint32_t group_id, xcom_event_horizon &event_horizon)); |
| 95 | MOCK_METHOD2(xcom_client_set_event_horizon, |
| 96 | bool(uint32_t group_id, xcom_event_horizon event_horizon)); |
| 97 | MOCK_METHOD2(xcom_client_set_max_leaders, |
| 98 | bool(uint32_t group_id, node_no max_leaders)); |
| 99 | MOCK_METHOD4(xcom_client_set_leaders, |
| 100 | bool(uint32_t group_id, u_int n, char const *names[], |
| 101 | node_no max_nr_leaders)); |
| 102 | MOCK_METHOD2(xcom_client_get_leaders, |
| 103 | bool(uint32_t gid, leader_info_data &leaders)); |
| 104 | MOCK_METHOD4(xcom_client_get_synode_app_data, |
| 105 | bool(connection_descriptor *con, uint32_t group_id_hash, |
| 106 | synode_no_array &synodes, synode_app_data_array &reply)); |
| 107 | MOCK_METHOD1(xcom_client_set_cache_size, bool(uint64_t)); |
| 108 | MOCK_METHOD2(xcom_client_boot, bool(node_list *nl, uint32_t group_id)); |
| 109 | MOCK_METHOD2(xcom_client_open_connection, |
| 110 | connection_descriptor *(std::string, xcom_port port)); |
| 111 | MOCK_METHOD1(xcom_client_close_connection, bool(connection_descriptor *con)); |
| 112 | MOCK_METHOD2(xcom_client_send_data, |
| 113 | bool(unsigned long long size, char *data)); |
| 114 | MOCK_METHOD1(xcom_init, void(xcom_port listen_port)); |
| 115 | MOCK_METHOD0(xcom_exit, void()); |
| 116 | MOCK_METHOD0(xcom_set_cleanup, void()); |
| 117 | MOCK_METHOD1(xcom_get_ssl_mode, int(const char *mode)); |
| 118 | MOCK_METHOD1(xcom_set_ssl_mode, int(int mode)); |
| 119 | MOCK_METHOD1(xcom_get_ssl_fips_mode, int(const char *mode)); |
| 120 | MOCK_METHOD1(xcom_set_ssl_fips_mode, int(int mode)); |
| 121 | MOCK_METHOD0(xcom_init_ssl, bool()); |
| 122 | MOCK_METHOD0(xcom_destroy_ssl, void()); |
| 123 | MOCK_METHOD0(xcom_use_ssl, bool()); |
| 124 | MOCK_METHOD2(xcom_set_ssl_parameters, |
| 125 | void(ssl_parameters ssl, tls_parameters tls)); |
| 126 | MOCK_METHOD1(find_site_def, site_def const *(synode_no synode)); |
| 127 | MOCK_METHOD2(xcom_open_handlers, bool(std::string saddr, xcom_port port)); |
| 128 | MOCK_METHOD0(xcom_close_handlers, bool()); |
| 129 | MOCK_METHOD0(xcom_acquire_handler, int()); |
| 130 | MOCK_METHOD1(xcom_release_handler, void(int index)); |
| 131 | MOCK_METHOD0(xcom_wait_ready, enum_gcs_error()); |
| 132 | MOCK_METHOD0(xcom_is_ready, bool()); |
| 133 | MOCK_METHOD1(xcom_set_ready, void(bool value)); |
| 134 | MOCK_METHOD0(xcom_signal_ready, void()); |
| 135 | MOCK_METHOD1(xcom_wait_for_xcom_comms_status_change, void(int &status)); |
| 136 | MOCK_METHOD0(xcom_has_comms_status_changed, bool()); |
| 137 | MOCK_METHOD1(xcom_set_comms_status, void(int status)); |
| 138 | MOCK_METHOD1(xcom_signal_comms_status_changed, void(int status)); |
| 139 | MOCK_METHOD0(xcom_wait_exit, enum_gcs_error()); |
| 140 | MOCK_METHOD0(xcom_is_exit, bool()); |
| 141 | MOCK_METHOD1(xcom_set_exit, void(bool)); |
| 142 | MOCK_METHOD0(xcom_signal_exit, void()); |
| 143 | MOCK_METHOD3(xcom_client_force_config, int(connection_descriptor *fd, |
| 144 | node_list *nl, uint32_t group_id)); |
| 145 | MOCK_METHOD2(xcom_client_force_config, |
| 146 | bool(node_list *nl, uint32_t group_id)); |
| 147 | |
| 148 | MOCK_METHOD0(get_should_exit, bool()); |
| 149 | MOCK_METHOD1(set_should_exit, void(bool should_exit)); |
| 150 | |
| 151 | MOCK_METHOD2(xcom_input_connect, |
| 152 | bool(std::string const &address, xcom_port port)); |
| 153 | MOCK_METHOD0(xcom_input_disconnect, void()); |
| 154 | MOCK_METHOD1(xcom_input_try_push, bool(app_data_ptr data)); |
| 155 | /* Mocking fails compilation on Windows. It attempts to copy the std::future |
| 156 | * which is non-copyable. */ |
| 157 | Gcs_xcom_input_queue::future_reply xcom_input_try_push_and_get_reply( |
| 158 | app_data_ptr) { |
| 159 | return std::future<std::unique_ptr<Gcs_xcom_input_queue::Reply>>(); |
| 160 | } |
| 161 | MOCK_METHOD0(xcom_input_try_pop, xcom_input_request_ptr()); |
| 162 | }; |
| 163 | |
| 164 | class mock_gcs_network_provider_management_interface |
| 165 | : public Network_provider_management_interface { |
| 166 | public: |
| 167 | MOCK_METHOD0(initialize, bool()); |
| 168 | MOCK_METHOD0(finalize, bool()); |
| 169 | MOCK_METHOD1(set_running_protocol, void(enum_transport_protocol new_value)); |
| 170 | MOCK_METHOD1(add_network_provider, |
| 171 | void(std::shared_ptr<Network_provider> provider)); |
| 172 | MOCK_CONST_METHOD0(get_running_protocol, enum_transport_protocol()); |
| 173 | MOCK_CONST_METHOD0(get_incoming_connections_protocol, |
| 174 | enum_transport_protocol()); |
| 175 | MOCK_CONST_METHOD0(is_xcom_using_ssl, int()); |
| 176 | MOCK_METHOD1(xcom_set_ssl_mode, int(int mode)); |
| 177 | MOCK_METHOD1(xcom_get_ssl_mode, int(const char *mode)); |
| 178 | MOCK_METHOD0(xcom_get_ssl_mode, int()); |
| 179 | MOCK_METHOD1(xcom_set_ssl_fips_mode, int(int mode)); |
| 180 | MOCK_METHOD1(xcom_get_ssl_fips_mode, int(const char *mode)); |
| 181 | MOCK_METHOD0(xcom_get_ssl_fips_mode, int()); |
| 182 | MOCK_METHOD0(cleanup_secure_connections_context, void()); |
| 183 | MOCK_METHOD0(delayed_cleanup_secure_connections_context, void()); |
| 184 | MOCK_METHOD0(finalize_secure_connections_context, void()); |
| 185 | MOCK_METHOD0(remove_all_network_provider, void()); |
| 186 | MOCK_METHOD1(remove_network_provider, void(enum_transport_protocol)); |
| 187 | }; |
| 188 | |
| 189 | class XComCommunicationTest : public GcsBaseTest { |
| 190 | protected: |
| 191 | void SetUp() override { |
| 192 | engine = new Gcs_xcom_engine(); |
| 193 | engine->initialize(nullptr); |
| 194 | |
| 195 | mock_gid = new Gcs_group_identifier("mock_group"); |
| 196 | static_cast<Gcs_xcom_interface *>(Gcs_xcom_interface::get_interface()) |
| 197 | ->set_xcom_group_information(mock_gid->get_group_id()); |
| 198 | mock_xcom_address = new Gcs_xcom_node_address("127.0.0.1:12345"); |
| 199 | static_cast<Gcs_xcom_interface *>(Gcs_xcom_interface::get_interface()) |
| 200 | ->set_node_address(mock_xcom_address->get_member_address()); |
| 201 | mock_stats = new mock_gcs_xcom_statistics_updater(); |
| 202 | mock_proxy = new mock_gcs_xcom_proxy(); |
| 203 | mock_vce = new mock_gcs_xcom_view_change_control_interface(); |
| 204 | net_mgr_interface = |
| 205 | std::make_unique<mock_gcs_network_provider_management_interface>(); |
| 206 | xcom_comm_if = |
| 207 | new Gcs_xcom_communication(mock_stats, mock_proxy, mock_vce, engine, |
| 208 | *mock_gid, std::move(net_mgr_interface)); |
| 209 | |
| 210 | // clang-format off |
| 211 | xcom_comm_if->get_msg_pipeline().register_stage<Gcs_message_stage_lz4>(); |
| 212 | xcom_comm_if->get_msg_pipeline().register_stage<Gcs_message_stage_lz4_v2>(); |
| 213 | xcom_comm_if->get_msg_pipeline().register_stage<Gcs_message_stage_lz4_v3>(); |
| 214 | xcom_comm_if->get_msg_pipeline().register_pipeline({ |
| 215 | { |
| 216 | Gcs_protocol_version::V1, { Stage_code::ST_LZ4_V1 } |
| 217 | }, |
| 218 | { |
| 219 | Gcs_protocol_version::V2, { Stage_code::ST_LZ4_V2 } |
| 220 | }, |
| 221 | { |
| 222 | Gcs_protocol_version::V3, { Stage_code::ST_LZ4_V3 } |
| 223 | } |
| 224 | }); |
| 225 | // clang-format on |
| 226 | } |
| 227 | |
| 228 | void TearDown() override { |
| 229 | delete mock_stats; |
| 230 | delete mock_vce; |
| 231 | delete mock_proxy; |
| 232 | delete xcom_comm_if; |
| 233 | delete mock_xcom_address; |
| 234 | delete mock_gid; |
| 235 | engine->finalize(nullptr); |
| 236 | delete engine; |
| 237 | } |
| 238 | |
| 239 | Gcs_xcom_communication *xcom_comm_if; |
| 240 | mock_gcs_xcom_statistics_updater *mock_stats; |
| 241 | mock_gcs_xcom_proxy *mock_proxy; |
| 242 | mock_gcs_xcom_view_change_control_interface *mock_vce; |
| 243 | Gcs_xcom_node_address *mock_xcom_address; |
| 244 | Gcs_xcom_engine *engine; |
| 245 | Gcs_group_identifier *mock_gid; |
| 246 | std::unique_ptr<mock_gcs_network_provider_management_interface> |
| 247 | net_mgr_interface; |
| 248 | }; |
| 249 | |
| 250 | TEST_F(XComCommunicationTest, SetEventListenerTest) { |
| 251 | mock_gcs_communication_event_listener comm_listener; |
| 252 | |
| 253 | int reference = xcom_comm_if->add_event_listener(comm_listener); |
| 254 | |
| 255 | ASSERT_NE(0, reference); |
| 256 | ASSERT_EQ((long unsigned int)1, |
| 257 | xcom_comm_if->get_event_listeners()->count(reference)); |
| 258 | ASSERT_EQ((long unsigned int)1, xcom_comm_if->get_event_listeners()->size()); |
| 259 | } |
| 260 | |
| 261 | TEST_F(XComCommunicationTest, SetEventListenersTest) { |
| 262 | mock_gcs_communication_event_listener comm_listener; |
| 263 | mock_gcs_communication_event_listener another_comm_listener; |
| 264 | |
| 265 | int reference = xcom_comm_if->add_event_listener(comm_listener); |
| 266 | int another_reference = |
| 267 | xcom_comm_if->add_event_listener(another_comm_listener); |
| 268 | |
| 269 | ASSERT_NE(0, reference); |
| 270 | ASSERT_NE(0, another_reference); |
| 271 | ASSERT_EQ((long unsigned int)1, |
| 272 | xcom_comm_if->get_event_listeners()->count(reference)); |
| 273 | ASSERT_EQ((long unsigned int)1, |
| 274 | xcom_comm_if->get_event_listeners()->count(another_reference)); |
| 275 | ASSERT_EQ((long unsigned int)2, xcom_comm_if->get_event_listeners()->size()); |
| 276 | ASSERT_NE(reference, another_reference); |
| 277 | } |
| 278 | |
| 279 | TEST_F(XComCommunicationTest, RemoveEventListenerTest) { |
| 280 | mock_gcs_communication_event_listener comm_listener; |
| 281 | mock_gcs_communication_event_listener another_comm_listener; |
| 282 | |
| 283 | int reference = xcom_comm_if->add_event_listener(comm_listener); |
| 284 | int another_reference = |
| 285 | xcom_comm_if->add_event_listener(another_comm_listener); |
| 286 | |
| 287 | xcom_comm_if->remove_event_listener(reference); |
| 288 | |
| 289 | ASSERT_NE(0, reference); |
| 290 | ASSERT_NE(0, another_reference); |
| 291 | ASSERT_EQ((long unsigned int)0, |
| 292 | xcom_comm_if->get_event_listeners()->count(reference)); |
| 293 | ASSERT_EQ((long unsigned int)1, |
| 294 | xcom_comm_if->get_event_listeners()->count(another_reference)); |
| 295 | ASSERT_EQ((long unsigned int)1, xcom_comm_if->get_event_listeners()->size()); |
| 296 | ASSERT_NE(reference, another_reference); |
| 297 | } |
| 298 | |
| 299 | bool mock_xcom_client_send_data(unsigned long long, char *data) { |
| 300 | free(data); |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | TEST_F(XComCommunicationTest, SendMessageTest) { |
| 305 | // Test Expectations |
| 306 | EXPECT_CALL(*mock_proxy, xcom_client_send_data(_, _)) |
| 307 | .Times(1) |
| 308 | .WillOnce(Invoke(&mock_xcom_client_send_data)); |
| 309 | EXPECT_CALL(*mock_stats, update_message_sent(_)).Times(1); |
| 310 | EXPECT_CALL(*mock_vce, belongs_to_group()).Times(1).WillOnce(Return(true)); |
| 311 | |
| 312 | std::string test_header("header"); |
| 313 | std::string test_payload("payload"); |
| 314 | Gcs_member_identifier member_id("member"); |
| 315 | Gcs_group_identifier group_id("group"); |
| 316 | Gcs_message_data *message_data = |
| 317 | new Gcs_message_data(test_header.length(), test_payload.length()); |
| 318 | |
| 319 | Gcs_message message(member_id, group_id, message_data); |
| 320 | |
| 321 | message.get_message_data().append_to_header( |
| 322 | pointer_cast<const uchar *>(test_header.c_str()), test_header.length()); |
| 323 | |
| 324 | message.get_message_data().append_to_payload( |
| 325 | pointer_cast<const uchar *>(test_payload.c_str()), test_payload.length()); |
| 326 | |
| 327 | enum_gcs_error message_result = xcom_comm_if->send_message(message); |
| 328 | ASSERT_EQ(GCS_OK, message_result); |
| 329 | } |
| 330 | |
| 331 | TEST_F(XComCommunicationTest, ReceiveMessageTest) { |
| 332 | mock_gcs_communication_event_listener ev_listener; |
| 333 | |
| 334 | // Test Expectations |
| 335 | EXPECT_CALL(ev_listener, on_message_received(_)).Times(1); |
| 336 | EXPECT_CALL(*mock_stats, update_message_received(_)).Times(1); |
| 337 | |
| 338 | // Test |
| 339 | std::string test_header("header"); |
| 340 | std::string test_payload("payload"); |
| 341 | Gcs_member_identifier member_id("member"); |
| 342 | Gcs_group_identifier group_id("group"); |
| 343 | Gcs_message_data message_data(test_header.length(), test_payload.length()); |
| 344 | |
| 345 | message_data.append_to_header( |
| 346 | pointer_cast<const uchar *>(test_header.c_str()), test_header.length()); |
| 347 | |
| 348 | message_data.append_to_payload( |
| 349 | pointer_cast<const uchar *>(test_payload.c_str()), test_payload.length()); |
| 350 | |
| 351 | bool error; |
| 352 | std::vector<Gcs_packet> packets_out; |
| 353 | std::tie(error, packets_out) = |
| 354 | xcom_comm_if->get_msg_pipeline().process_outgoing( |
| 355 | message_data, Cargo_type::CT_USER_DATA); |
| 356 | ASSERT_FALSE(error); |
| 357 | ASSERT_EQ(packets_out.size(), 1); |
| 358 | |
| 359 | Gcs_packet::buffer_ptr buffer; |
| 360 | unsigned long long buffer_len; |
| 361 | std::tie(buffer, buffer_len) = packets_out.at(0).serialize(); |
| 362 | |
| 363 | // Mock the sending of a dummy message instead of our test payload. |
| 364 | EXPECT_CALL(*mock_vce, belongs_to_group()).Times(1).WillOnce(Return(true)); |
| 365 | EXPECT_CALL(*mock_vce, is_view_changing()).Times(1).WillOnce(Return(false)); |
| 366 | Gcs_view mock_view( |
| 367 | {Gcs_member_identifier(mock_xcom_address->get_member_address())}, |
| 368 | Gcs_xcom_view_identifier(0, 0), {}, {}, group_id); |
| 369 | EXPECT_CALL(*mock_vce, get_unsafe_current_view()) |
| 370 | .Times(1) |
| 371 | .WillOnce(Return(&mock_view)); |
| 372 | EXPECT_CALL(*mock_proxy, xcom_client_send_data(_, _)) |
| 373 | .Times(1) |
| 374 | .WillOnce(Invoke(&mock_xcom_client_send_data)); |
| 375 | EXPECT_CALL(*mock_stats, update_message_sent(_)).Times(1); |
| 376 | enum_gcs_error message_result = xcom_comm_if->send_message( |
| 377 | Gcs_message(member_id, group_id, new Gcs_message_data(0, 0))); |
| 378 | ASSERT_EQ(GCS_OK, message_result); |
| 379 | |
| 380 | // Act as if we received our test payload |
| 381 | synode_no packet_synode; |
| 382 | packet_synode.group_id = Gcs_xcom_utils::build_xcom_group_id(*mock_gid); |
| 383 | packet_synode.msgno = 0; |
| 384 | packet_synode.node = 0; |
| 385 | auto packet = Gcs_packet::make_incoming_packet( |
| 386 | std::move(buffer), buffer_len, packet_synode, packet_synode, |
| 387 | xcom_comm_if->get_msg_pipeline()); |
| 388 | |
| 389 | int listener_ref = xcom_comm_if->add_event_listener(ev_listener); |
| 390 | |
| 391 | std::unique_ptr<Gcs_xcom_nodes> xcom_nodes(new Gcs_xcom_nodes()); |
| 392 | xcom_nodes->add_node(Gcs_xcom_node_information( |
| 393 | "127.0.0.1:12345", Gcs_xcom_uuid(), packet_synode.node, true)); |
| 394 | |
| 395 | xcom_comm_if->process_user_data_packet(std::move(packet), |
| 396 | std::move(xcom_nodes)); |
| 397 | |
| 398 | xcom_comm_if->remove_event_listener(listener_ref); |
| 399 | } |
| 400 | |
| 401 | TEST_F(XComCommunicationTest, BufferMessageTest) { |
| 402 | mock_gcs_communication_event_listener ev_listener; |
| 403 | |
| 404 | // Test Expectations |
| 405 | EXPECT_CALL(ev_listener, on_message_received(_)).Times(1); |
| 406 | |
| 407 | EXPECT_CALL(*mock_stats, update_message_received(_)).Times(1); |
| 408 | |
| 409 | // Set up the environment. |
| 410 | std::string test_header("header"); |
| 411 | std::string test_payload("payload"); |
| 412 | Gcs_member_identifier member_id("member"); |
| 413 | Gcs_group_identifier group_id("group"); |
| 414 | int listener_ref = xcom_comm_if->add_event_listener(ev_listener); |
| 415 | Gcs_message_data message_data(test_header.length(), test_payload.length()); |
| 416 | |
| 417 | message_data.append_to_header( |
| 418 | pointer_cast<const uchar *>(test_header.c_str()), test_header.length()); |
| 419 | |
| 420 | message_data.append_to_payload( |
| 421 | pointer_cast<const uchar *>(test_payload.c_str()), test_payload.length()); |
| 422 | |
| 423 | bool error; |
| 424 | std::vector<Gcs_packet> packets_out; |
| 425 | std::tie(error, packets_out) = |
| 426 | xcom_comm_if->get_msg_pipeline().process_outgoing( |
| 427 | message_data, Cargo_type::CT_USER_DATA); |
| 428 | ASSERT_FALSE(error); |
| 429 | ASSERT_EQ(packets_out.size(), 1); |
| 430 | |
| 431 | Gcs_packet::buffer_ptr buffer; |
| 432 | unsigned long long buffer_len; |
| 433 | std::tie(buffer, buffer_len) = packets_out.at(0).serialize(); |
| 434 | |
| 435 | // Mock the sending of a dummy message instead of our test payload. |
| 436 | EXPECT_CALL(*mock_vce, belongs_to_group()).Times(1).WillOnce(Return(true)); |
| 437 | EXPECT_CALL(*mock_vce, is_view_changing()).Times(1).WillOnce(Return(false)); |
| 438 | Gcs_view mock_view( |
| 439 | {Gcs_member_identifier(mock_xcom_address->get_member_address())}, |
| 440 | Gcs_xcom_view_identifier(0, 0), {}, {}, group_id); |
| 441 | EXPECT_CALL(*mock_vce, get_unsafe_current_view()) |
| 442 | .Times(1) |
| 443 | .WillOnce(Return(&mock_view)); |
| 444 | EXPECT_CALL(*mock_proxy, xcom_client_send_data(_, _)) |
| 445 | .Times(1) |
| 446 | .WillOnce(Invoke(&mock_xcom_client_send_data)); |
| 447 | EXPECT_CALL(*mock_stats, update_message_sent(_)).Times(1); |
| 448 | enum_gcs_error message_result = xcom_comm_if->send_message( |
| 449 | Gcs_message(member_id, group_id, new Gcs_message_data(0, 0))); |
| 450 | ASSERT_EQ(GCS_OK, message_result); |
| 451 | |
| 452 | // Act as if we received our test payload |
| 453 | synode_no packet_synode; |
| 454 | packet_synode.group_id = Gcs_xcom_utils::build_xcom_group_id(*mock_gid); |
| 455 | packet_synode.msgno = 0; |
| 456 | packet_synode.node = 0; |
| 457 | auto packet = Gcs_packet::make_incoming_packet( |
| 458 | std::move(buffer), buffer_len, packet_synode, packet_synode, |
| 459 | xcom_comm_if->get_msg_pipeline()); |
| 460 | |
| 461 | /* |
| 462 | Try to send a message when the view is not installed. They are |
| 463 | buffered but we flush them out. |
| 464 | */ |
| 465 | ON_CALL(*mock_vce, belongs_to_group()).WillByDefault(Return(false)); |
| 466 | ON_CALL(*mock_vce, is_view_changing()).WillByDefault(Return(true)); |
| 467 | |
| 468 | std::unique_ptr<Gcs_xcom_nodes> xcom_nodes(new Gcs_xcom_nodes()); |
| 469 | xcom_nodes->add_node(Gcs_xcom_node_information( |
| 470 | "127.0.0.1:12345", Gcs_xcom_uuid(), packet_synode.node, true)); |
| 471 | |
| 472 | xcom_comm_if->process_user_data_packet(std::move(packet), |
| 473 | std::move(xcom_nodes)); |
| 474 | |
| 475 | ON_CALL(*mock_vce, belongs_to_group()).WillByDefault(Return(true)); |
| 476 | ON_CALL(*mock_vce, is_view_changing()).WillByDefault(Return(false)); |
| 477 | |
| 478 | xcom_comm_if->deliver_buffered_packets(); |
| 479 | |
| 480 | xcom_comm_if->remove_event_listener(listener_ref); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | This function mocks a successful recovery for SuccessfulSynodRecoveryTest. |
| 485 | It receives a request for synodes 1 and 2, and sends back their application |
| 486 | payloads. |
| 487 | */ |
| 488 | static Gcs_packet::buffer_ptr synode_1_data{nullptr, |
| 489 | Gcs_packet_buffer_deleter()}; |
| 490 | static u_int synode_1_data_len = 0; |
| 491 | static Gcs_packet::buffer_ptr synode_2_data{nullptr, |
| 492 | Gcs_packet_buffer_deleter()}; |
| 493 | static u_int synode_2_data_len = 0; |
| 494 | bool mock_xcom_client_get_synode_app_data(connection_descriptor *, uint32_t, |
| 495 | synode_no_array &synodes, |
| 496 | synode_app_data_array &reply) { |
| 497 | u_int const nr_synodes = synodes.synode_no_array_len; |
| 498 | assert(nr_synodes == 2); |
| 499 | |
| 500 | reply.synode_app_data_array_val = static_cast<synode_app_data *>( |
| 501 | malloc(nr_synodes * sizeof(synode_app_data))); |
| 502 | assert(reply.synode_app_data_array_val != nullptr); |
| 503 | |
| 504 | reply.synode_app_data_array_len = nr_synodes; |
| 505 | |
| 506 | assert(synode_1_data != nullptr); |
| 507 | assert(synode_1_data_len != 0); |
| 508 | reply.synode_app_data_array_val[0].synode = synodes.synode_no_array_val[0]; |
| 509 | reply.synode_app_data_array_val[0].data.data_len = synode_1_data_len; |
| 510 | reply.synode_app_data_array_val[0].data.data_val = |
| 511 | reinterpret_cast<char *>(synode_1_data.release()); |
| 512 | |
| 513 | assert(synode_2_data != nullptr); |
| 514 | assert(synode_2_data_len != 0); |
| 515 | reply.synode_app_data_array_val[1].synode = synodes.synode_no_array_val[1]; |
| 516 | reply.synode_app_data_array_val[1].data.data_len = synode_2_data_len; |
| 517 | reply.synode_app_data_array_val[1].data.data_val = |
| 518 | reinterpret_cast<char *>(synode_2_data.release()); |
| 519 | |
| 520 | std::free(synodes.synode_no_array_val); |
| 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | int mock_xcom_client_close_connection(connection_descriptor *con) { |
| 526 | ::free(con); |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | TEST_F(XComCommunicationTest, SuccessfulSynodRecoveryTest) { |
| 531 | bool error = true; |
| 532 | synode_no base_synod; |
| 533 | base_synod.group_id = 1; |
| 534 | base_synod.msgno = 0; |
| 535 | base_synod.node = 0; |
| 536 | |
| 537 | /* Payload will be split in 4 packets of 4 bytes. */ |
| 538 | std::string const payload("Yay"); |
| 539 | std::size_t const payload_length = payload.size() + 1; // +1 for '\0' |
| 540 | Gcs_message_data message_data(0, payload_length); |
| 541 | message_data.append_to_payload( |
| 542 | reinterpret_cast<unsigned char const *>(payload.c_str()), payload_length); |
| 543 | unsigned long long constexpr split_threshold = 4; |
| 544 | std::size_t constexpr nr_fragments = 4; |
| 545 | Gcs_message_pipeline &pipeline = xcom_comm_if->get_msg_pipeline(); |
| 546 | |
| 547 | /* Reset pipeline. */ |
| 548 | pipeline.cleanup(); |
| 549 | // clang-format off |
| 550 | pipeline.register_stage<Gcs_message_stage_split_v2>(true, split_threshold); |
| 551 | error = pipeline.register_pipeline({ |
| 552 | { |
| 553 | Gcs_protocol_version::V2, { Stage_code::ST_SPLIT_V2 } |
| 554 | } |
| 555 | }); |
| 556 | // clang-format on |
| 557 | pipeline.set_version(Gcs_protocol_version::V2); |
| 558 | ASSERT_FALSE(error); |
| 559 | |
| 560 | /* Setup group members, me and other dummy guy for recovery. */ |
| 561 | Gcs_member_identifier me(mock_xcom_address->get_member_address()); |
| 562 | Gcs_xcom_node_information other("127.0.0.2:12345"); |
| 563 | Gcs_xcom_nodes xcom_nodes; |
| 564 | xcom_nodes.add_node(Gcs_xcom_node_information(me.get_member_id())); |
| 565 | xcom_nodes.add_node(other); |
| 566 | xcom_comm_if->update_members_information(me, xcom_nodes); |
| 567 | |
| 568 | /* Fragment packet. */ |
| 569 | std::vector<Gcs_packet> packets_out; |
| 570 | std::tie(error, packets_out) = |
| 571 | pipeline.process_outgoing(message_data, Cargo_type::CT_USER_DATA); |
| 572 | ASSERT_FALSE(error); |
| 573 | ASSERT_EQ(packets_out.size(), nr_fragments); |
| 574 | |
| 575 | /* |
| 576 | Now we are in imagination land, and the test begins. |
| 577 | I am a joining node that will require the first two packets via recovery. |
| 578 | The split packets will each have synod 1, 2, 3, and 4. |
| 579 | */ |
| 580 | synode_no synode_1 = base_synod; |
| 581 | synode_1.msgno = 1; |
| 582 | synode_no synode_2 = base_synod; |
| 583 | synode_2.msgno = 2; |
| 584 | synode_no synode_3 = base_synod; |
| 585 | synode_3.msgno = 3; |
| 586 | synode_no synode_4 = base_synod; |
| 587 | synode_4.msgno = 4; |
| 588 | std::unordered_set<Gcs_xcom_synode> synodes{Gcs_xcom_synode(synode_1), |
| 589 | Gcs_xcom_synode(synode_2)}; |
| 590 | |
| 591 | /* Mock the connection to the donor. */ |
| 592 | EXPECT_CALL(*mock_proxy, xcom_client_open_connection(_, _)) |
| 593 | .Times(1) |
| 594 | .WillOnce(Return(::new_connection(0, nullptr))); |
| 595 | |
| 596 | /* |
| 597 | Mock the request-reply interaction with the remote XCom. |
| 598 | We use the global variables synode_*_data{,_len} to pass the synod payloads |
| 599 | to the mocked function. |
| 600 | */ |
| 601 | std::tie(synode_1_data, synode_1_data_len) = packets_out[0].serialize(); |
| 602 | std::tie(synode_2_data, synode_2_data_len) = packets_out[1].serialize(); |
| 603 | EXPECT_CALL(*mock_proxy, xcom_client_get_synode_app_data(_, _, _, _)) |
| 604 | .Times(1) |
| 605 | .WillOnce(Invoke(&mock_xcom_client_get_synode_app_data)); |
| 606 | |
| 607 | /* Mock the disconnection from the donor. */ |
| 608 | EXPECT_CALL(*mock_proxy, xcom_client_close_connection(_)) |
| 609 | .Times(1) |
| 610 | .WillOnce(Invoke(&mock_xcom_client_close_connection)); |
| 611 | |
| 612 | /* Recover! */ |
| 613 | bool const recovered = xcom_comm_if->recover_packets(synodes); |
| 614 | ASSERT_TRUE(recovered); |
| 615 | |
| 616 | /* Receive the last two packets normally. */ |
| 617 | std::vector<Gcs_packet> packets_in; |
| 618 | std::array<synode_no, 4> synodes_in_order{synode_1, synode_2, synode_3, |
| 619 | synode_4}; |
| 620 | Gcs_packet packet_in; |
| 621 | for (std::size_t i = 2; i < 4; i++) { |
| 622 | /* Construct the packet from the "incoming" buffer. */ |
| 623 | Gcs_packet &packet_out = packets_out[i]; |
| 624 | Gcs_packet::buffer_ptr buffer; |
| 625 | unsigned long long buffer_len; |
| 626 | std::tie(buffer, buffer_len) = packet_out.serialize(); |
| 627 | synode_no const &packet_synode = synodes_in_order.at(i); |
| 628 | auto packet = Gcs_packet::make_incoming_packet( |
| 629 | std::move(buffer), buffer_len, packet_synode, packet_synode, pipeline); |
| 630 | |
| 631 | /* Reassemble the packets. */ |
| 632 | Gcs_pipeline_incoming_result error_code; |
| 633 | std::tie(error_code, packet_in) = |
| 634 | pipeline.process_incoming(std::move(packet)); |
| 635 | ASSERT_TRUE( |
| 636 | (i == 2 && error_code == Gcs_pipeline_incoming_result::OK_NO_PACKET) || |
| 637 | (i == 3 && error_code == Gcs_pipeline_incoming_result::OK_PACKET)); |
| 638 | } |
| 639 | |
| 640 | /* Compare against original payload. */ |
| 641 | Gcs_message_data received_data(packet_in.get_payload_length()); |
| 642 | ASSERT_FALSE(received_data.decode(packet_in.get_payload_pointer(), |
| 643 | packet_in.get_payload_length())); |
| 644 | std::string received_payload( |
| 645 | reinterpret_cast<char const *>(received_data.get_payload())); |
| 646 | ASSERT_EQ(payload, received_payload); |
| 647 | } |
| 648 | |
| 649 | bool mock_unsuccessful_xcom_client_get_synode_app_data( |
| 650 | connection_descriptor *, uint32_t, synode_no_array &synodes, |
| 651 | synode_app_data_array &) { |
| 652 | std::free(synodes.synode_no_array_val); |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | TEST_F(XComCommunicationTest, UnsuccessfulSynodRecoveryTest) { |
| 657 | bool error = true; |
| 658 | synode_no base_synod; |
| 659 | base_synod.group_id = 1; |
| 660 | base_synod.msgno = 0; |
| 661 | base_synod.node = 0; |
| 662 | |
| 663 | /* Payload will be split in 4 packets of 4 bytes. */ |
| 664 | std::string const payload("Yay"); |
| 665 | std::size_t const payload_length = payload.size() + 1; // +1 for '\0' |
| 666 | Gcs_message_data message_data(0, payload_length); |
| 667 | message_data.append_to_payload( |
| 668 | reinterpret_cast<unsigned char const *>(payload.c_str()), payload_length); |
| 669 | unsigned long long constexpr split_threshold = 4; |
| 670 | std::size_t constexpr nr_fragments = 4; |
| 671 | Gcs_message_pipeline &pipeline = xcom_comm_if->get_msg_pipeline(); |
| 672 | |
| 673 | /* Reset pipeline. */ |
| 674 | pipeline.cleanup(); |
| 675 | // clang-format off |
| 676 | pipeline.register_stage<Gcs_message_stage_split_v2>(true, split_threshold); |
| 677 | error = pipeline.register_pipeline({ |
| 678 | { |
| 679 | Gcs_protocol_version::V2, { Stage_code::ST_SPLIT_V2 } |
| 680 | } |
| 681 | }); |
| 682 | // clang-format on |
| 683 | pipeline.set_version(Gcs_protocol_version::V2); |
| 684 | ASSERT_FALSE(error); |
| 685 | |
| 686 | /* Setup group members, me and other dummy guy for recovery. */ |
| 687 | Gcs_member_identifier me(mock_xcom_address->get_member_address()); |
| 688 | Gcs_xcom_node_information other("127.0.0.2:12345"); |
| 689 | Gcs_xcom_nodes xcom_nodes; |
| 690 | xcom_nodes.add_node(Gcs_xcom_node_information(me.get_member_id())); |
| 691 | xcom_nodes.add_node(other); |
| 692 | xcom_comm_if->update_members_information(me, xcom_nodes); |
| 693 | |
| 694 | /* Fragment packet. */ |
| 695 | std::vector<Gcs_packet> packets_out; |
| 696 | std::tie(error, packets_out) = |
| 697 | pipeline.process_outgoing(message_data, Cargo_type::CT_USER_DATA); |
| 698 | ASSERT_FALSE(error); |
| 699 | ASSERT_EQ(packets_out.size(), nr_fragments); |
| 700 | |
| 701 | /* |
| 702 | Now we are in imagination land, and the test begins. |
| 703 | I am a joining node that will require the first two packets via recovery, |
| 704 | but the recovery will fail. |
| 705 | The split packets will each have synod 1, 2, 3, and 4. |
| 706 | */ |
| 707 | synode_no synode_1 = base_synod; |
| 708 | synode_1.msgno = 1; |
| 709 | synode_no synode_2 = base_synod; |
| 710 | synode_2.msgno = 2; |
| 711 | std::unordered_set<Gcs_xcom_synode> synodes{Gcs_xcom_synode(synode_1), |
| 712 | Gcs_xcom_synode(synode_2)}; |
| 713 | |
| 714 | /* Mock the connection to the donor. */ |
| 715 | EXPECT_CALL(*mock_proxy, xcom_client_open_connection(_, _)) |
| 716 | .Times(1) |
| 717 | .WillOnce(Return(::new_connection(0, nullptr))); |
| 718 | |
| 719 | /* |
| 720 | Mock the request-reply interaction with the remote XCom. |
| 721 | It will be unsuccessful, i.e. XCom no longer had the required synodes. |
| 722 | */ |
| 723 | EXPECT_CALL(*mock_proxy, xcom_client_get_synode_app_data(_, _, _, _)) |
| 724 | .Times(1) |
| 725 | .WillOnce(Invoke(&mock_unsuccessful_xcom_client_get_synode_app_data)); |
| 726 | |
| 727 | /* Mock the disconnection from the donor. */ |
| 728 | EXPECT_CALL(*mock_proxy, xcom_client_close_connection(_)) |
| 729 | .Times(1) |
| 730 | .WillOnce(Invoke(&mock_xcom_client_close_connection)); |
| 731 | |
| 732 | /* Fail to recover! */ |
| 733 | bool const recovered = xcom_comm_if->recover_packets(synodes); |
| 734 | ASSERT_FALSE(recovered); |
| 735 | } |
| 736 | |
| 737 | } // namespace gcs_xcom_communication_unittest |