| 1 | /* Copyright (c) 2020, 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 <gtest/gtest.h> |
| 25 | |
| 26 | #include <libmysql/dns_srv_data.h> |
| 27 | #include <unordered_set> |
| 28 | |
| 29 | namespace dns_srv_data_unittest { |
| 30 | |
| 31 | class dns_srv_data_test : public ::testing::Test { |
| 32 | protected: |
| 33 | void SetUp() override {} |
| 34 | |
| 35 | void TearDown() override { |
| 36 | data.clear(); |
| 37 | host.clear(); |
| 38 | port = 0; |
| 39 | } |
| 40 | |
| 41 | Dns_srv_data data; |
| 42 | std::string host; |
| 43 | unsigned port; |
| 44 | }; |
| 45 | |
| 46 | TEST_F(dns_srv_data_test, empty_list) { |
| 47 | host = "nothost"; |
| 48 | port = 100; |
| 49 | ASSERT_EQ(data.pop_next(host, port), true); |
| 50 | ASSERT_STREQ(host.c_str(), "nothost"); |
| 51 | ASSERT_EQ(port, 100); |
| 52 | } |
| 53 | |
| 54 | TEST_F(dns_srv_data_test, one_element) { |
| 55 | data.add("h1", 12, 0, 0); |
| 56 | |
| 57 | ASSERT_EQ(data.pop_next(host, port), false); |
| 58 | ASSERT_STREQ(host.c_str(), "h1"); |
| 59 | ASSERT_EQ(port, 12); |
| 60 | host = "h2"; |
| 61 | port = 13; |
| 62 | ASSERT_EQ(data.pop_next(host, port), true); |
| 63 | } |
| 64 | |
| 65 | TEST_F(dns_srv_data_test, different_prio) { |
| 66 | const std::string low("low"); |
| 67 | const std::string high("high"); |
| 68 | // push low prio first, high prio next |
| 69 | data.add(low, 13, 2, 0); |
| 70 | data.add(high, 12, 1, 0); |
| 71 | |
| 72 | // pop and expect high prio |
| 73 | ASSERT_EQ(data.pop_next(host, port), false); |
| 74 | ASSERT_STREQ(host.c_str(), high.c_str()); |
| 75 | ASSERT_EQ(port, 12); |
| 76 | |
| 77 | // pop and expect low prio |
| 78 | ASSERT_EQ(data.pop_next(host, port), false); |
| 79 | ASSERT_STREQ(host.c_str(), low.c_str()); |
| 80 | ASSERT_EQ(port, 13); |
| 81 | |
| 82 | // expect empty |
| 83 | ASSERT_EQ(data.pop_next(host, port), true); |
| 84 | } |
| 85 | |
| 86 | TEST_F(dns_srv_data_test, different_weight) { |
| 87 | const std::string low("low"); |
| 88 | const std::string high("high"); |
| 89 | bool got_low = false, got_high = false; |
| 90 | // push low prio first, high weight next |
| 91 | data.add(low, 13, 1, 1); |
| 92 | data.add(high, 12, 1, 2); |
| 93 | |
| 94 | while (!data.pop_next(host, port)) { |
| 95 | if (host == high && port == 12) |
| 96 | got_high = true; |
| 97 | else if (host == low && port == 13) |
| 98 | got_low = true; |
| 99 | else |
| 100 | ASSERT_TRUE(host == high || host == low); |
| 101 | } |
| 102 | ASSERT_TRUE(got_high && got_low); |
| 103 | } |
| 104 | |
| 105 | TEST_F(dns_srv_data_test, zero_weight) { |
| 106 | const std::string zero("zero"); |
| 107 | const std::string nonzero("nonzero"); |
| 108 | std::unordered_set<unsigned> s = {12, 13, 14, 15}; |
| 109 | data.add(zero, 13, 1, 0); |
| 110 | data.add(zero, 12, 1, 0); |
| 111 | data.add(nonzero, 14, 1, 1); |
| 112 | data.add(nonzero, 15, 1, 2); |
| 113 | |
| 114 | while (!data.pop_next(host, port)) { |
| 115 | std::unordered_set<unsigned>::iterator i = s.find(port); |
| 116 | ASSERT_TRUE(i != s.end()); |
| 117 | s.erase(i); |
| 118 | } |
| 119 | ASSERT_TRUE(s.empty()); |
| 120 | } |
| 121 | |
| 122 | TEST_F(dns_srv_data_test, mixed_weight) { |
| 123 | const std::string p1("p1"); |
| 124 | const std::string p2("p2"); |
| 125 | std::unordered_set<unsigned> s = {12, 13, 14, 15}; |
| 126 | data.add(p1, 13, 1, 0); |
| 127 | data.add(p1, 12, 1, 1); |
| 128 | data.add(p2, 14, 2, 0); |
| 129 | data.add(p2, 15, 2, 1); |
| 130 | |
| 131 | while (!data.pop_next(host, port)) { |
| 132 | std::unordered_set<unsigned>::iterator i = s.find(port); |
| 133 | ASSERT_TRUE(i != s.end()); |
| 134 | if (s.size() > 2) |
| 135 | ASSERT_STREQ(host.c_str(), p1.c_str()); |
| 136 | else |
| 137 | ASSERT_STREQ(host.c_str(), p2.c_str()); |
| 138 | s.erase(i); |
| 139 | } |
| 140 | ASSERT_TRUE(s.empty()); |
| 141 | } |
| 142 | |
| 143 | } // namespace dns_srv_data_unittest |