mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
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:
@@ -198,9 +198,9 @@ tr_address tr_ip_cache::bind_addr(tr_address_type type) const noexcept
|
|||||||
return {};
|
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] };
|
auto const lock = std::scoped_lock{ global_addr_mutex_[addr.type] };
|
||||||
global_addr_[addr.type] = addr;
|
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 */)
|
if (response.status == 200 /* HTTP_OK */)
|
||||||
{
|
{
|
||||||
// Update member
|
// 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;
|
success = true;
|
||||||
upkeep_timers_[type]->set_interval(UpkeepInterval);
|
upkeep_timers_[type]->set_interval(UpkeepInterval);
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ public:
|
|||||||
|
|
||||||
[[nodiscard]] tr_address bind_addr(tr_address_type type) const noexcept;
|
[[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_addr(tr_address_type type) noexcept;
|
||||||
void update_global_addr(tr_address_type type) noexcept;
|
void update_global_addr(tr_address_type type) noexcept;
|
||||||
|
|||||||
@@ -1034,7 +1034,7 @@ public:
|
|||||||
|
|
||||||
bool set_global_address(tr_address const& addr) noexcept
|
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
|
[[nodiscard]] std::optional<tr_address> global_source_address(tr_address_type type) const noexcept
|
||||||
|
|||||||
@@ -95,14 +95,18 @@ protected:
|
|||||||
TEST_F(IPCacheTest, bindAddr)
|
TEST_F(IPCacheTest, bindAddr)
|
||||||
{
|
{
|
||||||
static constexpr auto AddrTests = std::array{
|
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>{ {
|
||||||
{ "192.168.133.133"sv, "192.168.133.133"sv },
|
{ "8.8.8.8"sv, "8.8.8.8"sv },
|
||||||
{ "2001:1890:1112:1::20"sv, "0.0.0.0"sv },
|
{ "192.168.133.133"sv, "192.168.133.133"sv },
|
||||||
{ "asdasd"sv, "0.0.0.0"sv } } } /* IPv4 */,
|
{ "2001:1890:1112:1::20"sv, "0.0.0.0"sv },
|
||||||
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 },
|
||||||
{ "192.168.133.133"sv, "::"sv },
|
} } /* IPv4 */,
|
||||||
{ "2001:1890:1112:1::20"sv, "2001:1890:1112:1::20"sv },
|
std::array<std::pair<std::string_view, std::string_view>, 4>{ {
|
||||||
{ "asdasd"sv, "::"sv } } } /* IPv6 */
|
{ "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 */
|
||||||
};
|
};
|
||||||
static_assert(TR_AF_INET == 0);
|
static_assert(TR_AF_INET == 0);
|
||||||
static_assert(TR_AF_INET6 == 1);
|
static_assert(TR_AF_INET6 == 1);
|
||||||
@@ -134,34 +138,26 @@ TEST_F(IPCacheTest, bindAddr)
|
|||||||
|
|
||||||
TEST_F(IPCacheTest, setGlobalAddr)
|
TEST_F(IPCacheTest, setGlobalAddr)
|
||||||
{
|
{
|
||||||
static auto constexpr AddrStr = std::array{ "8.8.8.8"sv,
|
static auto constexpr AddrStr = std::array{
|
||||||
"192.168.133.133"sv,
|
"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,
|
||||||
"172.16.241.133"sv,
|
};
|
||||||
"2001:1890:1112:1::20"sv,
|
static auto constexpr AddrTests = std::array{ true, false, false, true, false };
|
||||||
"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_assert(TR_AF_INET == 0);
|
static_assert(TR_AF_INET == 0);
|
||||||
static_assert(TR_AF_INET6 == 1);
|
static_assert(TR_AF_INET6 == 1);
|
||||||
static_assert(NUM_TR_AF_INET_TYPES == 2);
|
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));
|
||||||
static_assert(std::size(AddrStr) == std::size(AddrTests[TR_AF_INET6]));
|
|
||||||
|
|
||||||
auto mediator = MockMediator{};
|
auto mediator = MockMediator{};
|
||||||
ip_cache_ = std::make_unique<tr_ip_cache>(mediator);
|
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 addr = tr_address::from_string(AddrStr[i]);
|
||||||
|
ASSERT_TRUE(addr.has_value());
|
||||||
|
EXPECT_EQ(ip_cache_->set_global_addr(*addr), AddrTests[i]);
|
||||||
|
if (auto const val = ip_cache_->global_addr(addr->type); val && AddrTests[i])
|
||||||
{
|
{
|
||||||
auto const type = static_cast<tr_address_type>(i);
|
EXPECT_EQ(val->display_name(), AddrStr[i]);
|
||||||
auto const addr = tr_address::from_string(AddrStr[j]);
|
|
||||||
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(val->display_name(), AddrStr[j]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user