| 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 | #include "gcs_xcom_utils.h" |
| 26 | #include "test_logger.h" |
| 27 | |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace gcs_xcom_utils_unittest { |
| 32 | TEST(GcsXComUtils, BuildXcomMemberIdSmokeTest) { |
| 33 | std::string peer_nodes_str("127.0.0.1:12345,127.0.0.1:12346"); |
| 34 | std::vector<std::string> processed_peers; |
| 35 | Gcs_xcom_utils::process_peer_nodes(&peer_nodes_str, processed_peers); |
| 36 | |
| 37 | ASSERT_EQ((unsigned)2, processed_peers.size()); |
| 38 | ASSERT_STREQ("127.0.0.1:12345", processed_peers.at(0).c_str()); |
| 39 | ASSERT_STREQ("127.0.0.1:12346", processed_peers.at(1).c_str()); |
| 40 | } |
| 41 | |
| 42 | TEST(GcsXComUtils, BuildXcomMemberIdSpacesAtBegin) { |
| 43 | std::string peer_nodes_str(" 127.0.0.1:12345,127.0.0.1:12346"); |
| 44 | std::vector<std::string> processed_peers; |
| 45 | Gcs_xcom_utils::process_peer_nodes(&peer_nodes_str, processed_peers); |
| 46 | |
| 47 | ASSERT_EQ((unsigned)2, processed_peers.size()); |
| 48 | ASSERT_STREQ("127.0.0.1:12345", processed_peers.at(0).c_str()); |
| 49 | ASSERT_STREQ("127.0.0.1:12346", processed_peers.at(1).c_str()); |
| 50 | } |
| 51 | |
| 52 | TEST(GcsXComUtils, BuildXcomMemberIdSpacesAtBeginAndEnd) { |
| 53 | std::string peer_nodes_str(" 127.0.0.1:12345,127.0.0.1:12346 "); |
| 54 | std::vector<std::string> processed_peers; |
| 55 | Gcs_xcom_utils::process_peer_nodes(&peer_nodes_str, processed_peers); |
| 56 | |
| 57 | ASSERT_EQ((unsigned)2, processed_peers.size()); |
| 58 | ASSERT_STREQ("127.0.0.1:12345", processed_peers.at(0).c_str()); |
| 59 | ASSERT_STREQ("127.0.0.1:12346", processed_peers.at(1).c_str()); |
| 60 | } |
| 61 | |
| 62 | TEST(GcsXComUtils, BuildXcomMemberIdSpacesSpacesEverywhere) { |
| 63 | std::string peer_nodes_str(" 127.0. 0.1:12 345, 127.0 .0.1:12346 "); |
| 64 | std::vector<std::string> processed_peers; |
| 65 | Gcs_xcom_utils::process_peer_nodes(&peer_nodes_str, processed_peers); |
| 66 | |
| 67 | ASSERT_EQ((unsigned)2, processed_peers.size()); |
| 68 | ASSERT_STREQ("127.0.0.1:12345", processed_peers.at(0).c_str()); |
| 69 | ASSERT_STREQ("127.0.0.1:12346", processed_peers.at(1).c_str()); |
| 70 | } |
| 71 | |
| 72 | TEST(GcsXcomProxyImpl, XcomClientSendDataBiggerThanUINT32) { |
| 73 | std::stringstream error_message; |
| 74 | Gcs_xcom_proxy_impl xcom_proxy; |
| 75 | |
| 76 | /* |
| 77 | xcom_client_send_data cannot send a message bigger than uint32. |
| 78 | It should log and error and return false if the message |
| 79 | is bigger than uint32. |
| 80 | */ |
| 81 | test_logger.clear_event(); |
| 82 | bool successful = xcom_proxy.xcom_client_send_data(1ULL << 32, nullptr); |
| 83 | |
| 84 | ASSERT_EQ(successful, false); |
| 85 | |
| 86 | error_message << "The data is too big. Data length should " |
| 87 | << "not exceed " << std::numeric_limits<unsigned int>::max() |
| 88 | << " bytes."; |
| 89 | test_logger.assert_error(error_message.str()); |
| 90 | } |
| 91 | |
| 92 | TEST(GcsXComUtils, GcsProtocolOutOfRange) { |
| 93 | ASSERT_FALSE(is_valid_protocol("1000000000000000000000000000000000000000")); |
| 94 | } |
| 95 | |
| 96 | TEST(GcsXComUtils, InvalidGcsProtocolToConvertToMysqlVersion) { |
| 97 | ASSERT_EQ(gcs_protocol_to_mysql_version(Gcs_protocol_version::UNKNOWN), ""); |
| 98 | } |
| 99 | |
| 100 | } // namespace gcs_xcom_utils_unittest |