refactor: removed redundant type check in tr_ip_cache::set_global_addr() (#7551)

* refactor: removed redundant type check in `tr_ip_cache::set_global_addr()`

* chore: tweak formatting
This commit is contained in:
Yat Ho
2025-10-15 02:00:01 +08:00
committed by GitHub
parent fb25228f24
commit bf461a0f72
4 changed files with 29 additions and 32 deletions

View File

@@ -198,9 +198,9 @@ tr_address tr_ip_cache::bind_addr(tr_address_type type) const noexcept
return {};
}
bool tr_ip_cache::set_global_addr(tr_address_type type, tr_address const& addr) noexcept
bool tr_ip_cache::set_global_addr(tr_address const& addr) noexcept
{
if (type == addr.type && addr.is_global_unicast_address())
if (addr.is_global_unicast_address())
{
auto const lock = std::scoped_lock{ global_addr_mutex_[addr.type] };
global_addr_[addr.type] = addr;
@@ -305,7 +305,8 @@ void tr_ip_cache::on_response_ip_query(tr_address_type type, tr_web::FetchRespon
if (response.status == 200 /* HTTP_OK */)
{
// Update member
if (auto const addr = tr_address::from_string(tr_strv_strip(response.body)); addr && set_global_addr(type, *addr))
if (auto const addr = tr_address::from_string(tr_strv_strip(response.body));
addr && type == addr->type && set_global_addr(*addr))
{
success = true;
upkeep_timers_[type]->set_interval(UpkeepInterval);

View File

@@ -78,7 +78,7 @@ public:
[[nodiscard]] tr_address bind_addr(tr_address_type type) const noexcept;
bool set_global_addr(tr_address_type type, tr_address const& addr) noexcept;
bool set_global_addr(tr_address const& addr) noexcept;
void update_addr(tr_address_type type) noexcept;
void update_global_addr(tr_address_type type) noexcept;

View File

@@ -1034,7 +1034,7 @@ public:
bool set_global_address(tr_address const& addr) noexcept
{
return ip_cache_.set_global_addr(addr.type, addr);
return ip_cache_.set_global_addr(addr);
}
[[nodiscard]] std::optional<tr_address> global_source_address(tr_address_type type) const noexcept

View File

@@ -95,14 +95,18 @@ protected:
TEST_F(IPCacheTest, bindAddr)
{
static constexpr auto AddrTests = std::array{
std::array<std::pair<std::string_view, std::string_view>, 4>{ { { "8.8.8.8"sv, "8.8.8.8"sv },
std::array<std::pair<std::string_view, std::string_view>, 4>{ {
{ "8.8.8.8"sv, "8.8.8.8"sv },
{ "192.168.133.133"sv, "192.168.133.133"sv },
{ "2001:1890:1112:1::20"sv, "0.0.0.0"sv },
{ "asdasd"sv, "0.0.0.0"sv } } } /* IPv4 */,
std::array<std::pair<std::string_view, std::string_view>, 4>{ { { "fd12:3456:789a:1::1"sv, "fd12:3456:789a:1::1"sv },
{ "asdasd"sv, "0.0.0.0"sv },
} } /* IPv4 */,
std::array<std::pair<std::string_view, std::string_view>, 4>{ {
{ "fd12:3456:789a:1::1"sv, "fd12:3456:789a:1::1"sv },
{ "192.168.133.133"sv, "::"sv },
{ "2001:1890:1112:1::20"sv, "2001:1890:1112:1::20"sv },
{ "asdasd"sv, "::"sv } } } /* IPv6 */
{ "asdasd"sv, "::"sv },
} } /* IPv6 */
};
static_assert(TR_AF_INET == 0);
static_assert(TR_AF_INET6 == 1);
@@ -134,34 +138,26 @@ TEST_F(IPCacheTest, bindAddr)
TEST_F(IPCacheTest, setGlobalAddr)
{
static auto constexpr AddrStr = std::array{ "8.8.8.8"sv,
"192.168.133.133"sv,
"172.16.241.133"sv,
"2001:1890:1112:1::20"sv,
"fd12:3456:789a:1::1"sv };
static auto constexpr AddrTests = std::array{ std::array{ true, false, false, false, false /* IPv4 */ },
std::array{ false, false, false, true, false /* IPv6 */ } };
static auto constexpr AddrStr = std::array{
"8.8.8.8"sv, "192.168.133.133"sv, "172.16.241.133"sv, "2001:1890:1112:1::20"sv, "fd12:3456:789a:1::1"sv,
};
static auto constexpr AddrTests = std::array{ true, false, false, true, false };
static_assert(TR_AF_INET == 0);
static_assert(TR_AF_INET6 == 1);
static_assert(NUM_TR_AF_INET_TYPES == 2);
static_assert(std::size(AddrStr) == std::size(AddrTests[TR_AF_INET]));
static_assert(std::size(AddrStr) == std::size(AddrTests[TR_AF_INET6]));
static_assert(std::size(AddrStr) == std::size(AddrTests));
auto mediator = MockMediator{};
ip_cache_ = std::make_unique<tr_ip_cache>(mediator);
for (std::size_t i = 0; i < NUM_TR_AF_INET_TYPES; ++i)
for (std::size_t i = 0; i < std::size(AddrStr); ++i)
{
for (std::size_t j = 0; j < std::size(AddrStr); ++j)
{
auto const type = static_cast<tr_address_type>(i);
auto const addr = tr_address::from_string(AddrStr[j]);
auto const addr = tr_address::from_string(AddrStr[i]);
ASSERT_TRUE(addr.has_value());
EXPECT_EQ(ip_cache_->set_global_addr(type, *addr), AddrTests[i][j]);
if (auto const val = ip_cache_->global_addr(type); val && AddrTests[i][j])
EXPECT_EQ(ip_cache_->set_global_addr(*addr), AddrTests[i]);
if (auto const val = ip_cache_->global_addr(addr->type); val && AddrTests[i])
{
EXPECT_EQ(val->display_name(), AddrStr[j]);
}
EXPECT_EQ(val->display_name(), AddrStr[i]);
}
}
}