| 1 | /* Copyright (c) 2016, 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 <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "gcs_base_test.h" |
| 28 | |
| 29 | #include "synode_no.h" |
| 30 | |
| 31 | using std::vector; |
| 32 | |
| 33 | extern void do_cb_xcom_receive_data(synode_no message_id, synode_no origin, |
| 34 | Gcs_xcom_nodes *xcom_nodes, |
| 35 | synode_no last_removed, u_int size, |
| 36 | char *data); |
| 37 | |
| 38 | namespace gcs_interface_unittest { |
| 39 | |
| 40 | class GcsInterfaceTest : public GcsBaseTest {}; |
| 41 | |
| 42 | /** |
| 43 | This test is primarily to run valgrind to make sure |
| 44 | that the interface can be initialized and finalized |
| 45 | multiple times without any leak. |
| 46 | */ |
| 47 | TEST_F(GcsInterfaceTest, DoubleInitFinalizeTest) { |
| 48 | Gcs_interface *gcs = Gcs_xcom_interface::get_interface(); |
| 49 | |
| 50 | Gcs_interface_parameters if_params; |
| 51 | if_params.add_parameter("group_name", "ola"); |
| 52 | if_params.add_parameter("peer_nodes", "127.0.0.1:24844"); |
| 53 | if_params.add_parameter("local_node", "127.0.0.1:24844"); |
| 54 | if_params.add_parameter("bootstrap_group", "true"); |
| 55 | |
| 56 | // initialize the interface |
| 57 | gcs->initialize(if_params); |
| 58 | |
| 59 | // finalize the interface |
| 60 | gcs->finalize(); |
| 61 | |
| 62 | // initialize the interface again |
| 63 | gcs->initialize(if_params); |
| 64 | |
| 65 | // finalize it again interface |
| 66 | gcs->finalize(); |
| 67 | |
| 68 | // fake factory cleanup member function |
| 69 | static_cast<Gcs_xcom_interface *>(gcs)->cleanup(); |
| 70 | } |
| 71 | |
| 72 | TEST_F(GcsInterfaceTest, ReceiveEmptyMessageTest) { |
| 73 | Gcs_interface *gcs = Gcs_xcom_interface::get_interface(); |
| 74 | |
| 75 | Gcs_interface_parameters if_params; |
| 76 | if_params.add_parameter("group_name", "ola"); |
| 77 | if_params.add_parameter("peer_nodes", "127.0.0.1:24844"); |
| 78 | if_params.add_parameter("local_node", "127.0.0.1:24844"); |
| 79 | if_params.add_parameter("bootstrap_group", "true"); |
| 80 | |
| 81 | // initialize the interface |
| 82 | gcs->initialize(if_params); |
| 83 | |
| 84 | // invoke the callback with a message with size zero |
| 85 | do_cb_xcom_receive_data(null_synode, null_synode, nullptr, null_synode, 0, |
| 86 | nullptr); |
| 87 | |
| 88 | // finalize the interface |
| 89 | gcs->finalize(); |
| 90 | |
| 91 | // fake factory cleanup member function |
| 92 | static_cast<Gcs_xcom_interface *>(gcs)->cleanup(); |
| 93 | } |
| 94 | |
| 95 | TEST_F(GcsInterfaceTest, InvalidCacheSize) { |
| 96 | Gcs_interface *gcs = Gcs_xcom_interface::get_interface(); |
| 97 | |
| 98 | Gcs_interface_parameters if_params; |
| 99 | if_params.add_parameter("group_name", "ola"); |
| 100 | if_params.add_parameter("peer_nodes", "127.0.0.1:12345"); |
| 101 | if_params.add_parameter("local_node", "127.0.0.1:12345"); |
| 102 | if_params.add_parameter("bootstrap_group", "true"); |
| 103 | // Check for wrong value. |
| 104 | if_params.add_parameter("xcom_cache_size", "100"); |
| 105 | |
| 106 | enum_gcs_error initialized = gcs->initialize(if_params); |
| 107 | ASSERT_EQ(GCS_NOK, initialized); |
| 108 | |
| 109 | Gcs_interface_parameters cfg_params; |
| 110 | cfg_params.add_parameter("group_name", "ola"); |
| 111 | cfg_params.add_parameter("peer_nodes", "127.0.0.1:12345"); |
| 112 | cfg_params.add_parameter("local_node", "127.0.0.1:12345"); |
| 113 | cfg_params.add_parameter("bootstrap_group", "true"); |
| 114 | // Check for out of range value. |
| 115 | cfg_params.add_parameter("xcom_cache_size", "100000000000000000000"); |
| 116 | |
| 117 | initialized = gcs->initialize(cfg_params); |
| 118 | ASSERT_EQ(GCS_NOK, initialized); |
| 119 | |
| 120 | Gcs_interface_parameters cfg_params_valid; |
| 121 | cfg_params_valid.add_parameter("group_name", "ola"); |
| 122 | cfg_params_valid.add_parameter("peer_nodes", "127.0.0.1:12345"); |
| 123 | cfg_params_valid.add_parameter("local_node", "127.0.0.1:12345"); |
| 124 | cfg_params_valid.add_parameter("bootstrap_group", "true"); |
| 125 | |
| 126 | // Finally, set a valid one. |
| 127 | cfg_params_valid.add_parameter("xcom_cache_size", "4000000000"); |
| 128 | // And verify that GCS initializes correctly. |
| 129 | initialized = gcs->initialize(cfg_params_valid); |
| 130 | ASSERT_EQ(GCS_OK, initialized); |
| 131 | |
| 132 | // Finalize the interface. |
| 133 | gcs->finalize(); |
| 134 | |
| 135 | // Fake factory cleanup member function. |
| 136 | static_cast<Gcs_xcom_interface *>(gcs)->cleanup(); |
| 137 | } |
| 138 | |
| 139 | } // namespace gcs_interface_unittest |