| 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 | using std::vector; |
| 30 | |
| 31 | namespace gcs_xcom_networking_unittest { |
| 32 | |
| 33 | void clean_sock_probe(sock_probe *s) { free(s); } |
| 34 | |
| 35 | class mock_gcs_sock_probe_interface : public Gcs_sock_probe_interface { |
| 36 | public: |
| 37 | MOCK_METHOD1(init_sock_probe, int(sock_probe *s)); |
| 38 | MOCK_METHOD1(number_of_interfaces, int(sock_probe *s)); |
| 39 | MOCK_METHOD3(get_sockaddr_address, |
| 40 | void(sock_probe *s, int count, struct sockaddr **out)); |
| 41 | MOCK_METHOD3(get_sockaddr_netmask, |
| 42 | void(sock_probe *s, int count, struct sockaddr **out)); |
| 43 | MOCK_METHOD2(get_if_name, char *(sock_probe *s, int count)); |
| 44 | MOCK_METHOD2(is_if_running, bool_t(sock_probe *s, int count)); |
| 45 | MOCK_METHOD1(close_sock_probe, void(sock_probe *s)); |
| 46 | |
| 47 | void mock_gcs_sock_probe_interface_default() { |
| 48 | ON_CALL(*this, close_sock_probe(_)).WillByDefault(Invoke(clean_sock_probe)); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | class GcsXComNetworking : public GcsBaseTest { |
| 53 | protected: |
| 54 | GcsXComNetworking() : m_sock_probe_mock() {} |
| 55 | ~GcsXComNetworking() override = default; |
| 56 | |
| 57 | void SetUp() override { |
| 58 | m_sock_probe_mock.mock_gcs_sock_probe_interface_default(); |
| 59 | } |
| 60 | |
| 61 | mock_gcs_sock_probe_interface m_sock_probe_mock; |
| 62 | }; |
| 63 | |
| 64 | TEST_F(GcsXComNetworking, SockProbeInvalid) { |
| 65 | EXPECT_CALL(m_sock_probe_mock, init_sock_probe(_)) |
| 66 | .Times(1) |
| 67 | .WillOnce(Return(-1)); |
| 68 | |
| 69 | std::map<std::string, int> out_value; |
| 70 | bool result = get_local_addresses(m_sock_probe_mock, out_value); |
| 71 | |
| 72 | ASSERT_TRUE(result); |
| 73 | } |
| 74 | |
| 75 | TEST_F(GcsXComNetworking, NoInterfaces) { |
| 76 | EXPECT_CALL(m_sock_probe_mock, init_sock_probe(_)) |
| 77 | .Times(1) |
| 78 | .WillOnce(Return(0)); |
| 79 | |
| 80 | EXPECT_CALL(m_sock_probe_mock, close_sock_probe(_)).Times(1); |
| 81 | |
| 82 | EXPECT_CALL(m_sock_probe_mock, number_of_interfaces(_)) |
| 83 | .Times(1) |
| 84 | .WillOnce(Return(0)); |
| 85 | |
| 86 | std::map<std::string, int> out_value; |
| 87 | bool result = get_local_addresses(m_sock_probe_mock, out_value); |
| 88 | |
| 89 | ASSERT_TRUE(result); |
| 90 | } |
| 91 | |
| 92 | TEST_F(GcsXComNetworking, ErrorRetrievingSockaddr) { |
| 93 | std::string if_name("interface"); |
| 94 | |
| 95 | EXPECT_CALL(m_sock_probe_mock, init_sock_probe(_)) |
| 96 | .Times(1) |
| 97 | .WillOnce(Return(0)); |
| 98 | |
| 99 | EXPECT_CALL(m_sock_probe_mock, close_sock_probe(_)).Times(1); |
| 100 | |
| 101 | EXPECT_CALL(m_sock_probe_mock, number_of_interfaces(_)) |
| 102 | .Times(3) |
| 103 | .WillRepeatedly(Return(1)); |
| 104 | |
| 105 | struct sockaddr *null_sockaddr = nullptr; |
| 106 | EXPECT_CALL(m_sock_probe_mock, get_sockaddr_netmask(_, _, _)) |
| 107 | .Times(1) |
| 108 | .WillOnce(testing::SetArgPointee<2>(null_sockaddr)); |
| 109 | |
| 110 | EXPECT_CALL(m_sock_probe_mock, get_sockaddr_address(_, _, _)) |
| 111 | .Times(1) |
| 112 | .WillOnce(testing::SetArgPointee<2>(null_sockaddr)); |
| 113 | |
| 114 | EXPECT_CALL(m_sock_probe_mock, get_if_name(_, _)) |
| 115 | .Times(1) |
| 116 | .WillOnce(Return(const_cast<char *>(if_name.c_str()))); |
| 117 | |
| 118 | std::map<std::string, int> out_value; |
| 119 | bool result = get_local_addresses(m_sock_probe_mock, out_value); |
| 120 | |
| 121 | ASSERT_TRUE(result); |
| 122 | } |
| 123 | |
| 124 | TEST_F(GcsXComNetworking, ResolveAllIPV6) { |
| 125 | std::vector<std::pair<sa_family_t, std::string>> out_value; |
| 126 | bool retval = resolve_all_ip_addr_from_hostname("::1", out_value); |
| 127 | |
| 128 | ASSERT_FALSE(retval); |
| 129 | } |
| 130 | |
| 131 | } // namespace gcs_xcom_networking_unittest |