| 1 | /* Copyright (c) 2018, 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 "gcs_xcom_communication_interface.h" |
| 27 | #include "gcs_xcom_group_management.h" |
| 28 | #include "gcs_xcom_utils.h" |
| 29 | |
| 30 | namespace gcs_xcom_groupmanagement_unittest { |
| 31 | |
| 32 | class mock_gcs_xcom_proxy : public Gcs_xcom_proxy_base { |
| 33 | public: |
| 34 | mock_gcs_xcom_proxy() { |
| 35 | ON_CALL(*this, xcom_client_boot(_, _)).WillByDefault(Return(1)); |
| 36 | ON_CALL(*this, xcom_client_add_node(_, _, _)).WillByDefault(Return(1)); |
| 37 | ON_CALL(*this, xcom_client_send_data(_, _)).WillByDefault(Return(10)); |
| 38 | ON_CALL(*this, new_node_address_uuid(_, _, _)) |
| 39 | .WillByDefault(Invoke(::new_node_address_uuid)); |
| 40 | ON_CALL(*this, delete_node_address(_, _)) |
| 41 | .WillByDefault(Invoke(::delete_node_address)); |
| 42 | } |
| 43 | |
| 44 | MOCK_METHOD3(new_node_address_uuid, |
| 45 | node_address *(unsigned int n, char const *names[], |
| 46 | blob uuids[])); |
| 47 | MOCK_METHOD2(delete_node_address, void(unsigned int n, node_address *na)); |
| 48 | MOCK_METHOD3(xcom_client_add_node, bool(connection_descriptor *fd, |
| 49 | node_list *nl, uint32_t group_id)); |
| 50 | MOCK_METHOD2(xcom_client_remove_node, bool(node_list *nl, uint32_t group_id)); |
| 51 | MOCK_METHOD3(xcom_client_remove_node, bool(connection_descriptor *fd, |
| 52 | node_list *nl, uint32_t group_id)); |
| 53 | MOCK_METHOD2(xcom_client_get_event_horizon, |
| 54 | bool(uint32_t group_id, xcom_event_horizon &event_horizon)); |
| 55 | MOCK_METHOD2(xcom_client_set_event_horizon, |
| 56 | bool(uint32_t group_id, xcom_event_horizon event_horizon)); |
| 57 | MOCK_METHOD2(xcom_client_set_max_leaders, |
| 58 | bool(uint32_t group_id, node_no max_leaders)); |
| 59 | MOCK_METHOD4(xcom_client_set_leaders, |
| 60 | bool(uint32_t group_id, u_int n, char const *names[], |
| 61 | node_no max_nr_leaders)); |
| 62 | MOCK_METHOD2(xcom_client_get_leaders, |
| 63 | bool(uint32_t gid, leader_info_data &leaders)); |
| 64 | MOCK_METHOD4(xcom_client_get_synode_app_data, |
| 65 | bool(connection_descriptor *con, uint32_t group_id_hash, |
| 66 | synode_no_array &synodes, synode_app_data_array &reply)); |
| 67 | MOCK_METHOD1(xcom_client_set_cache_size, bool(uint64_t size)); |
| 68 | MOCK_METHOD2(xcom_client_boot, bool(node_list *nl, uint32_t group_id)); |
| 69 | MOCK_METHOD2(xcom_client_open_connection, |
| 70 | connection_descriptor *(std::string, xcom_port port)); |
| 71 | MOCK_METHOD1(xcom_client_close_connection, bool(connection_descriptor *fd)); |
| 72 | MOCK_METHOD2(xcom_client_send_data, |
| 73 | bool(unsigned long long size, char *data)); |
| 74 | MOCK_METHOD1(xcom_init, void(xcom_port listen_port)); |
| 75 | MOCK_METHOD0(xcom_exit, void()); |
| 76 | MOCK_METHOD0(xcom_set_cleanup, void()); |
| 77 | MOCK_METHOD1(xcom_get_ssl_mode, int(const char *mode)); |
| 78 | MOCK_METHOD1(xcom_set_ssl_mode, int(int mode)); |
| 79 | MOCK_METHOD1(xcom_get_ssl_fips_mode, int(const char *mode)); |
| 80 | MOCK_METHOD1(xcom_set_ssl_fips_mode, int(int mode)); |
| 81 | MOCK_METHOD0(xcom_init_ssl, bool()); |
| 82 | MOCK_METHOD0(xcom_destroy_ssl, void()); |
| 83 | MOCK_METHOD0(xcom_use_ssl, bool()); |
| 84 | MOCK_METHOD2(xcom_set_ssl_parameters, |
| 85 | void(ssl_parameters ssl, tls_parameters tls)); |
| 86 | MOCK_METHOD1(find_site_def, site_def const *(synode_no synode)); |
| 87 | MOCK_METHOD0(xcom_wait_ready, enum_gcs_error()); |
| 88 | MOCK_METHOD0(xcom_is_ready, bool()); |
| 89 | MOCK_METHOD1(xcom_set_ready, void(bool value)); |
| 90 | MOCK_METHOD0(xcom_signal_ready, void()); |
| 91 | MOCK_METHOD1(xcom_wait_for_xcom_comms_status_change, void(int &status)); |
| 92 | MOCK_METHOD0(xcom_has_comms_status_changed, bool()); |
| 93 | MOCK_METHOD1(xcom_set_comms_status, void(int status)); |
| 94 | MOCK_METHOD1(xcom_signal_comms_status_changed, void(int status)); |
| 95 | MOCK_METHOD0(xcom_wait_exit, enum_gcs_error()); |
| 96 | MOCK_METHOD0(xcom_is_exit, bool()); |
| 97 | MOCK_METHOD1(xcom_set_exit, void(bool)); |
| 98 | MOCK_METHOD0(xcom_signal_exit, void()); |
| 99 | MOCK_METHOD3(xcom_client_force_config, int(connection_descriptor *fd, |
| 100 | node_list *nl, uint32_t group_id)); |
| 101 | MOCK_METHOD2(xcom_client_force_config, |
| 102 | bool(node_list *nl, uint32_t group_id)); |
| 103 | |
| 104 | MOCK_METHOD0(get_should_exit, bool()); |
| 105 | MOCK_METHOD1(set_should_exit, void(bool should_exit)); |
| 106 | |
| 107 | MOCK_METHOD2(xcom_input_connect, |
| 108 | bool(std::string const &address, xcom_port port)); |
| 109 | MOCK_METHOD0(xcom_input_disconnect, void()); |
| 110 | MOCK_METHOD1(xcom_input_try_push, bool(app_data_ptr data)); |
| 111 | /* Mocking fails compilation on Windows. It attempts to copy the std::future |
| 112 | * which is non-copyable. */ |
| 113 | Gcs_xcom_input_queue::future_reply xcom_input_try_push_and_get_reply( |
| 114 | app_data_ptr) override { |
| 115 | return std::future<std::unique_ptr<Gcs_xcom_input_queue::Reply>>(); |
| 116 | } |
| 117 | MOCK_METHOD0(xcom_input_try_pop, xcom_input_request_ptr()); |
| 118 | }; |
| 119 | |
| 120 | class mock_gcs_xcom_view_change_control_interface |
| 121 | : public Gcs_xcom_view_change_control_interface { |
| 122 | public: |
| 123 | MOCK_METHOD0(start_view_exchange, void()); |
| 124 | MOCK_METHOD0(end_view_exchange, void()); |
| 125 | MOCK_METHOD0(wait_for_view_change_end, void()); |
| 126 | MOCK_METHOD0(is_view_changing, bool()); |
| 127 | MOCK_METHOD0(start_leave, bool()); |
| 128 | MOCK_METHOD0(end_leave, void()); |
| 129 | MOCK_METHOD0(is_leaving, bool()); |
| 130 | MOCK_METHOD0(start_join, bool()); |
| 131 | MOCK_METHOD0(end_join, void()); |
| 132 | MOCK_METHOD0(is_joining, bool()); |
| 133 | |
| 134 | MOCK_METHOD1(set_current_view, void(Gcs_view *)); |
| 135 | MOCK_METHOD0(get_current_view, Gcs_view *()); |
| 136 | MOCK_METHOD0(belongs_to_group, bool()); |
| 137 | MOCK_METHOD1(set_belongs_to_group, void(bool)); |
| 138 | MOCK_METHOD1(set_unsafe_current_view, void(Gcs_view *)); |
| 139 | MOCK_METHOD0(get_unsafe_current_view, Gcs_view *()); |
| 140 | |
| 141 | MOCK_METHOD0(finalize, void()); |
| 142 | MOCK_METHOD0(is_finalized, bool()); |
| 143 | }; |
| 144 | |
| 145 | class XcomGroupManagementTest : public GcsBaseTest { |
| 146 | protected: |
| 147 | XcomGroupManagementTest() = default; |
| 148 | |
| 149 | void SetUp() override { |
| 150 | group_id = new Gcs_group_identifier("only_group"); |
| 151 | xcom_group_mgmt_if = new Gcs_xcom_group_management(&proxy, *group_id, &vce); |
| 152 | } |
| 153 | |
| 154 | void TearDown() override { |
| 155 | delete xcom_group_mgmt_if; |
| 156 | delete group_id; |
| 157 | } |
| 158 | |
| 159 | Gcs_group_identifier *group_id; |
| 160 | |
| 161 | mock_gcs_xcom_proxy proxy; |
| 162 | mock_gcs_xcom_view_change_control_interface vce; |
| 163 | Gcs_xcom_group_management *xcom_group_mgmt_if; |
| 164 | }; |
| 165 | |
| 166 | TEST_F(XcomGroupManagementTest, EmptyPeerNodes) { |
| 167 | EXPECT_CALL(proxy, xcom_client_force_config(_, _)).Times(0); |
| 168 | |
| 169 | Gcs_interface_parameters forced_group; |
| 170 | forced_group.add_parameter("peer_nodes", ""); |
| 171 | |
| 172 | enum_gcs_error result = |
| 173 | xcom_group_mgmt_if->modify_configuration(forced_group); |
| 174 | |
| 175 | ASSERT_EQ(GCS_NOK, result); |
| 176 | } |
| 177 | |
| 178 | TEST_F(XcomGroupManagementTest, UnconfiguredPeerNodes) { |
| 179 | EXPECT_CALL(proxy, xcom_client_force_config(_, _)).Times(0); |
| 180 | |
| 181 | Gcs_interface_parameters forced_group; |
| 182 | |
| 183 | enum_gcs_error result = |
| 184 | xcom_group_mgmt_if->modify_configuration(forced_group); |
| 185 | |
| 186 | ASSERT_EQ(GCS_NOK, result); |
| 187 | } |
| 188 | |
| 189 | bool operator==(const node_list &first, const node_list &second) { |
| 190 | if (first.node_list_len != second.node_list_len) return false; |
| 191 | |
| 192 | for (unsigned int i = 0; i < first.node_list_len; i++) { |
| 193 | if (strcmp(first.node_list_val[i].address, |
| 194 | second.node_list_val[i].address) != 0) |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | MATCHER_P(node_list_pointer_matcher, other_nl, "Derreference pointer") { |
| 202 | return other_nl == (*arg); |
| 203 | } |
| 204 | |
| 205 | TEST_F(XcomGroupManagementTest, TestListContent) { |
| 206 | Gcs_xcom_node_information node_1("127.0.0.1:12345"); |
| 207 | Gcs_xcom_node_information node_2("127.0.0.1:12346"); |
| 208 | Gcs_xcom_node_information node_3("127.0.0.1:12347"); |
| 209 | |
| 210 | Gcs_xcom_nodes nodes; |
| 211 | nodes.add_node(node_1); |
| 212 | nodes.add_node(node_2); |
| 213 | nodes.add_node(node_3); |
| 214 | |
| 215 | node_list nl; |
| 216 | nl.node_list_len = 2; |
| 217 | const char *node_addrs[] = {node_1.get_member_id().get_member_id().c_str(), |
| 218 | node_2.get_member_id().get_member_id().c_str()}; |
| 219 | blob blobs[] = {{{0, static_cast<char *>(malloc( |
| 220 | node_1.get_member_uuid().actual_value.size()))}}, |
| 221 | {{0, static_cast<char *>(malloc( |
| 222 | node_2.get_member_uuid().actual_value.size()))}}}; |
| 223 | node_1.get_member_uuid().encode( |
| 224 | reinterpret_cast<uchar **>(&blobs[0].data.data_val), |
| 225 | &blobs[0].data.data_len); |
| 226 | node_2.get_member_uuid().encode( |
| 227 | reinterpret_cast<uchar **>(&blobs[1].data.data_val), |
| 228 | &blobs[1].data.data_len); |
| 229 | |
| 230 | nl.node_list_val = |
| 231 | ::new_node_address_uuid(nl.node_list_len, node_addrs, blobs); |
| 232 | |
| 233 | EXPECT_CALL(proxy, xcom_client_force_config(node_list_pointer_matcher(nl), _)) |
| 234 | .Times(1) |
| 235 | .WillOnce(Return(1)); |
| 236 | |
| 237 | Gcs_interface_parameters forced_group; |
| 238 | forced_group.add_parameter("peer_nodes", "127.0.0.1:12345,127.0.0.1:12346"); |
| 239 | |
| 240 | xcom_group_mgmt_if->set_xcom_nodes(nodes); |
| 241 | enum_gcs_error result = |
| 242 | xcom_group_mgmt_if->modify_configuration(forced_group); |
| 243 | |
| 244 | ASSERT_EQ(GCS_OK, result); |
| 245 | ASSERT_EQ((unsigned)2, nl.node_list_len); |
| 246 | ASSERT_STREQ("127.0.0.1:12345", nl.node_list_val[0].address); |
| 247 | ASSERT_STREQ("127.0.0.1:12346", nl.node_list_val[1].address); |
| 248 | |
| 249 | ::delete_node_address(nl.node_list_len, nl.node_list_val); |
| 250 | |
| 251 | free(blobs[0].data.data_val); |
| 252 | free(blobs[1].data.data_val); |
| 253 | } |
| 254 | |
| 255 | TEST_F(XcomGroupManagementTest, DisallowForcingSameMembership) { |
| 256 | Gcs_xcom_node_information node_1("127.0.0.1:12345"); |
| 257 | Gcs_xcom_node_information node_2("127.0.0.1:12346"); |
| 258 | |
| 259 | Gcs_xcom_nodes nodes; |
| 260 | nodes.add_node(node_1); |
| 261 | nodes.add_node(node_2); |
| 262 | |
| 263 | node_list nl; |
| 264 | nl.node_list_len = 2; |
| 265 | const char *node_addrs[] = {node_1.get_member_id().get_member_id().c_str(), |
| 266 | node_2.get_member_id().get_member_id().c_str()}; |
| 267 | blob blobs[] = {{{0, static_cast<char *>(malloc( |
| 268 | node_1.get_member_uuid().actual_value.size()))}}, |
| 269 | {{0, static_cast<char *>(malloc( |
| 270 | node_2.get_member_uuid().actual_value.size()))}}}; |
| 271 | node_1.get_member_uuid().encode( |
| 272 | reinterpret_cast<uchar **>(&blobs[0].data.data_val), |
| 273 | &blobs[0].data.data_len); |
| 274 | node_2.get_member_uuid().encode( |
| 275 | reinterpret_cast<uchar **>(&blobs[1].data.data_val), |
| 276 | &blobs[1].data.data_len); |
| 277 | |
| 278 | nl.node_list_val = |
| 279 | ::new_node_address_uuid(nl.node_list_len, node_addrs, blobs); |
| 280 | |
| 281 | EXPECT_CALL(proxy, xcom_client_force_config(node_list_pointer_matcher(nl), _)) |
| 282 | .Times(0); |
| 283 | |
| 284 | xcom_group_mgmt_if->set_xcom_nodes(nodes); |
| 285 | |
| 286 | Gcs_interface_parameters forced_group_1; |
| 287 | forced_group_1.add_parameter("peer_nodes", "127.0.0.1:12346,127.0.0.1:12345"); |
| 288 | enum_gcs_error result_1 = |
| 289 | xcom_group_mgmt_if->modify_configuration(forced_group_1); |
| 290 | ASSERT_EQ(GCS_NOK, result_1); |
| 291 | |
| 292 | Gcs_interface_parameters forced_group_2; |
| 293 | forced_group_2.add_parameter("peer_nodes", "127.0.0.1:12345,127.0.0.1:12346"); |
| 294 | enum_gcs_error result_2 = |
| 295 | xcom_group_mgmt_if->modify_configuration(forced_group_2); |
| 296 | ASSERT_EQ(GCS_NOK, result_2); |
| 297 | |
| 298 | ASSERT_EQ((unsigned)2, nl.node_list_len); |
| 299 | ASSERT_STREQ("127.0.0.1:12345", nl.node_list_val[0].address); |
| 300 | ASSERT_STREQ("127.0.0.1:12346", nl.node_list_val[1].address); |
| 301 | |
| 302 | ::delete_node_address(nl.node_list_len, nl.node_list_val); |
| 303 | |
| 304 | free(blobs[0].data.data_val); |
| 305 | free(blobs[1].data.data_val); |
| 306 | } |
| 307 | |
| 308 | TEST_F(XcomGroupManagementTest, getWriteConcurrencyGroupLeaving) { |
| 309 | EXPECT_CALL(vce, is_leaving()).Times(1).WillOnce(Return(true)); |
| 310 | |
| 311 | uint32_t out_value; |
| 312 | enum_gcs_error result = xcom_group_mgmt_if->get_write_concurrency(out_value); |
| 313 | |
| 314 | ASSERT_EQ(GCS_NOK, result); |
| 315 | } |
| 316 | |
| 317 | TEST_F(XcomGroupManagementTest, getWriteConcurrencyNoGroup) { |
| 318 | EXPECT_CALL(vce, is_leaving()).Times(1).WillOnce(Return(false)); |
| 319 | EXPECT_CALL(proxy, xcom_is_exit()).Times(1).WillOnce(Return(true)); |
| 320 | |
| 321 | uint32_t out_value; |
| 322 | enum_gcs_error result = xcom_group_mgmt_if->get_write_concurrency(out_value); |
| 323 | |
| 324 | ASSERT_EQ(GCS_NOK, result); |
| 325 | } |
| 326 | |
| 327 | } // namespace gcs_xcom_groupmanagement_unittest |