| 1 |
/* |
| 2 |
Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License, version 2.0, |
| 6 |
as published by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is designed to work with certain software (including |
| 9 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 10 |
as designated in a particular file or component or in included license |
| 11 |
documentation. The authors of MySQL hereby grant you an additional |
| 12 |
permission to link the program and your derivative works with the |
| 13 |
separately licensed software that they have either included with |
| 14 |
the program or referenced in the documentation. |
| 15 |
|
| 16 |
This program is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License |
| 22 |
along with this program; if not, write to the Free Software |
| 23 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
*/ |
| 25 |
|
| 26 |
/** |
| 27 |
* BUG21873666 Correctly using configured values instead of defaults |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <fstream> |
| 32 |
#include <memory> |
| 33 |
#include <string> |
| 34 |
|
| 35 |
#include <gmock/gmock.h> |
| 36 |
|
| 37 |
#include "mysql/harness/config_parser.h" |
| 38 |
#include "mysql/harness/net_ts/io_context.h" |
| 39 |
#include "mysql_routing.h" |
| 40 |
#include "mysqlrouter/routing.h" |
| 41 |
#include "plugin_config.h" |
| 42 |
|
| 43 |
using ::testing::HasSubstr; |
| 44 |
|
| 45 |
class Bug21771595 : public ::testing::Test { |
| 46 |
protected: |
| 47 |
net::io_context io_ctx_; |
| 48 |
}; |
| 49 |
|
| 50 |
TEST_F(Bug21771595, ConstructorDefaults) { |
| 51 |
MySQLRouting r(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 52 |
Protocol::Type::kClassicProtocol, |
| 53 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 54 |
mysql_harness::Path(), "test"); |
| 55 |
ASSERT_EQ(r.get_destination_connect_timeout(), |
| 56 |
routing::kDefaultDestinationConnectionTimeout); |
| 57 |
ASSERT_EQ(r.get_max_connections(), routing::kDefaultMaxConnections); |
| 58 |
} |
| 59 |
|
| 60 |
TEST_F(Bug21771595, Constructor) { |
| 61 |
auto expect_max_connections = 20; |
| 62 |
auto expect_connect_timeout = |
| 63 |
routing::kDefaultDestinationConnectionTimeout + std::chrono::seconds(10); |
| 64 |
|
| 65 |
MySQLRouting r(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 66 |
Protocol::Type::kClassicProtocol, |
| 67 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 68 |
mysql_harness::Path(), "test", expect_max_connections, |
| 69 |
expect_connect_timeout); |
| 70 |
ASSERT_EQ(r.get_destination_connect_timeout(), expect_connect_timeout); |
| 71 |
ASSERT_EQ(r.get_max_connections(), expect_max_connections); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_F(Bug21771595, GetterSetterMaxConnections) { |
| 75 |
MySQLRouting r(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 76 |
Protocol::Type::kClassicProtocol, |
| 77 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 78 |
mysql_harness::Path(), "test"); |
| 79 |
ASSERT_EQ(r.get_max_connections(), routing::kDefaultMaxConnections); |
| 80 |
auto expected = routing::kDefaultMaxConnections + 1; |
| 81 |
ASSERT_EQ(r.set_max_connections(expected), expected); |
| 82 |
ASSERT_EQ(r.get_max_connections(), expected); |
| 83 |
} |
| 84 |
|
| 85 |
TEST_F(Bug21771595, InvalidDestinationConnectTimeout) { |
| 86 |
MySQLRouting r(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 87 |
Protocol::Type::kClassicProtocol, |
| 88 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 89 |
mysql_harness::Path(), "test"); |
| 90 |
ASSERT_THROW(r.validate_destination_connect_timeout(std::chrono::seconds(-1)), |
| 91 |
std::invalid_argument); |
| 92 |
// ASSERT_THROW(r.set_destination_connect_timeout(UINT16_MAX+1), |
| 93 |
// std::invalid_argument); |
| 94 |
try { |
| 95 |
r.validate_destination_connect_timeout(std::chrono::seconds(0)); |
| 96 |
} catch (const std::invalid_argument &exc) { |
| 97 |
ASSERT_THAT(exc.what(), |
| 98 |
HasSubstr("tried to set destination_connect_timeout using " |
| 99 |
"invalid value, was 0 ms")); |
| 100 |
} |
| 101 |
ASSERT_THROW( |
| 102 |
MySQLRouting(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 103 |
Protocol::Type::kClassicProtocol, |
| 104 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 105 |
mysql_harness::Path(), "test", 1, std::chrono::seconds(-1)), |
| 106 |
std::invalid_argument); |
| 107 |
} |
| 108 |
|
| 109 |
TEST_F(Bug21771595, InvalidMaxConnections) { |
| 110 |
MySQLRouting r(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 111 |
Protocol::Type::kClassicProtocol, |
| 112 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 113 |
mysql_harness::Path(), "test"); |
| 114 |
ASSERT_THROW(r.set_max_connections(-1), std::invalid_argument); |
| 115 |
ASSERT_THROW(r.set_max_connections(UINT16_MAX + 1), std::invalid_argument); |
| 116 |
try { |
| 117 |
r.set_max_connections(-1); |
| 118 |
} catch (const std::invalid_argument &exc) { |
| 119 |
ASSERT_THAT( |
| 120 |
exc.what(), |
| 121 |
HasSubstr( |
| 122 |
"tried to set max_connections using invalid value, was '-1'")); |
| 123 |
} |
| 124 |
ASSERT_THROW( |
| 125 |
MySQLRouting(io_ctx_, routing::RoutingStrategy::kRoundRobin, 7001, |
| 126 |
Protocol::Type::kClassicProtocol, |
| 127 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 128 |
mysql_harness::Path(), "test", -1, std::chrono::seconds(1)), |
| 129 |
std::invalid_argument); |
| 130 |
} |
| 131 |
|
| 132 |
TEST_F(Bug21771595, InvalidPort) { |
| 133 |
ASSERT_THROW(MySQLRouting(io_ctx_, routing::RoutingStrategy::kRoundRobin, 0, |
| 134 |
Protocol::Type::kClassicProtocol, |
| 135 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 136 |
mysql_harness::Path(), "test"), |
| 137 |
std::invalid_argument); |
| 138 |
try { |
| 139 |
MySQLRouting r(io_ctx_, routing::RoutingStrategy::kRoundRobin, (uint16_t)-1, |
| 140 |
Protocol::Type::kClassicProtocol, |
| 141 |
routing::AccessMode::kReadWrite, "127.0.0.1", |
| 142 |
mysql_harness::Path(), "test"); |
| 143 |
} catch (const std::invalid_argument &exc) { |
| 144 |
ASSERT_THAT(exc.what(), |
| 145 |
HasSubstr("Invalid bind address, was '127.0.0.1', port -1")); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
int main(int argc, char **argv) { |
| 150 |
::testing::InitGoogleTest(&argc, argv); |
| 151 |
return RUN_ALL_TESTS(); |
| 152 |
} |