Files
Sunshine/tests/unit/test_network.cpp
T
ReenigneArcher 15b06f1707 refactor(config): inject owned settings into runtime services
Replace namespace configuration state with an application-owned store. Pass immutable startup settings into services, platform capture and encoder contexts, network helpers, CLI commands, and tray callbacks. Keep runtime credentials in synchronized snapshots and gamepad policy in its input owner.

Validation: UCRT64 sunshine, test_sunshine, audio-info, and sunshinesvc builds with tray support; 566 regression tests passed and 5 device/workflow tests skipped; strict production Doxygen, required clang-format, and CLI smoke checks passed. Full Linux/macOS builds, hardware coverage, and hosted Sonar reanalysis remain CI validation.
2026-10-11 14:37:32 -04:00

251 lines
9.1 KiB
C++

/**
* @file tests/unit/test_network.cpp
* @brief Test src/network.*
*/
// test includes
#include "../tests_common.h"
// local includes
#include <src/network.h>
/**
* @brief Address classifications supplied by the immutable IPv4 and IPv6 range tables.
*/
struct NetworkAddressTest: BaseTest, testing::WithParamInterface<std::tuple<std::string, net::net_e>> {};
/**
* @brief Preserve loopback, LAN, and WAN classification across each configured range.
*/
TEST_P(NetworkAddressTest, ClassifiesAddress) {
const auto &[address, expected] = GetParam();
EXPECT_EQ(net::from_address(address), expected);
}
/**
* @brief Cover the first and last usable addresses and adjacent addresses for each range.
*/
INSTANTIATE_TEST_SUITE_P(
NetworkAddressTests,
NetworkAddressTest,
testing::Values(
std::make_tuple("127.0.0.1", net::PC),
std::make_tuple("127.255.255.254", net::PC),
std::make_tuple("126.255.255.254", net::WAN),
std::make_tuple("128.0.0.1", net::WAN),
std::make_tuple("192.168.0.1", net::LAN),
std::make_tuple("192.168.255.254", net::LAN),
std::make_tuple("192.167.255.254", net::WAN),
std::make_tuple("192.169.0.1", net::WAN),
std::make_tuple("172.16.0.1", net::LAN),
std::make_tuple("172.31.255.254", net::LAN),
std::make_tuple("172.15.255.254", net::WAN),
std::make_tuple("172.32.0.1", net::WAN),
std::make_tuple("10.0.0.1", net::LAN),
std::make_tuple("10.255.255.254", net::LAN),
std::make_tuple("9.255.255.254", net::WAN),
std::make_tuple("11.0.0.1", net::WAN),
std::make_tuple("100.64.0.1", net::LAN),
std::make_tuple("100.127.255.254", net::LAN),
std::make_tuple("100.63.255.254", net::WAN),
std::make_tuple("100.128.0.1", net::WAN),
std::make_tuple("169.254.0.1", net::LAN),
std::make_tuple("169.254.255.254", net::LAN),
std::make_tuple("169.253.255.254", net::WAN),
std::make_tuple("169.255.0.1", net::WAN),
std::make_tuple("::1", net::PC),
std::make_tuple("::2", net::WAN),
std::make_tuple("fc00::1", net::LAN),
std::make_tuple("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", net::LAN),
std::make_tuple("fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", net::WAN),
std::make_tuple("fe00::1", net::WAN),
std::make_tuple("fe80::1", net::LAN),
std::make_tuple("fe80::ffff:ffff:ffff:ffff", net::LAN),
std::make_tuple("fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff", net::WAN),
std::make_tuple("fe81::1", net::WAN),
std::make_tuple("::ffff:127.0.0.1", net::PC),
std::make_tuple("::ffff:192.168.1.1", net::LAN),
std::make_tuple("::ffff:198.51.100.1", net::WAN)
)
);
struct MdnsInstanceNameTest: BaseTest, testing::WithParamInterface<std::tuple<std::string, std::string>> {};
TEST_P(MdnsInstanceNameTest, Run) {
auto [input, expected] = GetParam();
ASSERT_EQ(net::mdns_instance_name(input), expected);
}
INSTANTIATE_TEST_SUITE_P(
MdnsInstanceNameTests,
MdnsInstanceNameTest,
testing::Values(
std::make_tuple("shortname-123", "shortname-123"),
std::make_tuple("space 123", "space-123"),
std::make_tuple("hostname.domain.test", "hostname"),
std::make_tuple("&", "Sunshine"),
std::make_tuple("", "Sunshine"),
std::make_tuple("😁", "Sunshine"),
std::make_tuple(std::string(128, 'a'), std::string(63, 'a'))
)
);
/**
* @brief Test fixture for bind_address tests with setup/teardown
*/
class BindAddressTest: public BaseTest {
protected:
config::store_t configuration; ///< Configuration isolated to this fixture.
};
/**
* @brief Test that get_bind_address returns wildcard when bind_address is not configured
*/
TEST_F(BindAddressTest, DefaultBehaviorIPv4) {
// Clear bind_address to test the default behavior
configuration.edit_startup().sunshine.bind_address = "";
const auto bind_addr = net::get_bind_address(configuration.values(), net::af_e::IPV4);
ASSERT_EQ(bind_addr, "0.0.0.0");
}
/**
* @brief Test that get_bind_address returns wildcard when bind_address is not configured (IPv6)
*/
TEST_F(BindAddressTest, DefaultBehaviorIPv6) {
// Clear bind_address to test the default behavior
configuration.edit_startup().sunshine.bind_address = "";
const auto bind_addr = net::get_bind_address(configuration.values(), net::af_e::BOTH);
ASSERT_EQ(bind_addr, "::");
}
/**
* @brief Test that an empty bind address preserves the configured address family.
*/
TEST_F(BindAddressTest, EmptyAddressPreservesConfiguredFamily) {
configuration.edit_startup().sunshine.bind_address = "";
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::IPV4), net::af_e::IPV4);
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::BOTH), net::af_e::BOTH);
}
/**
* @brief Test that an explicit IPv4 bind address selects an IPv4 socket.
*/
TEST_F(BindAddressTest, IPv4AddressSelectsIPv4Family) {
configuration.edit_startup().sunshine.bind_address = "192.0.2.101";
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::IPV4), net::af_e::IPV4);
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::BOTH), net::af_e::IPV4);
}
/**
* @brief Test that an explicit IPv6 bind address does not override the configured family.
*/
TEST_F(BindAddressTest, IPv6AddressPreservesConfiguredFamily) {
configuration.edit_startup().sunshine.bind_address = "2001:db8::1";
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::IPV4), net::af_e::IPV4);
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::BOTH), net::af_e::BOTH);
}
/**
* @brief Test that invalid bind addresses preserve the configured family for downstream error reporting.
*/
TEST_F(BindAddressTest, InvalidAddressPreservesConfiguredFamily) {
configuration.edit_startup().sunshine.bind_address = "not-an-address";
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::IPV4), net::af_e::IPV4);
EXPECT_EQ(net::get_effective_address_family(configuration.values(), net::af_e::BOTH), net::af_e::BOTH);
}
/**
* @brief Test that bind addresses are formatted for use as URL hosts.
*/
TEST_F(BindAddressTest, AddressFormatsAsUrlHost) {
configuration.edit_startup().sunshine.bind_address = "";
EXPECT_EQ(net::get_bind_address_url_host(configuration.values()), "localhost");
configuration.edit_startup().sunshine.bind_address = "198.51.100.56";
EXPECT_EQ(net::get_bind_address_url_host(configuration.values()), "198.51.100.56");
configuration.edit_startup().sunshine.bind_address = "2001:db8::1";
EXPECT_EQ(net::get_bind_address_url_host(configuration.values()), "[2001:db8::1]");
configuration.edit_startup().sunshine.bind_address = "not-an-address";
EXPECT_EQ(net::get_bind_address_url_host(configuration.values()), "not-an-address");
}
/**
* @brief Test that get_bind_address returns configured IPv4 address
*/
TEST_F(BindAddressTest, ConfiguredIPv4Address) {
// Set a specific IPv4 address
configuration.edit_startup().sunshine.bind_address = "192.168.1.100";
const auto bind_addr = net::get_bind_address(configuration.values(), net::af_e::IPV4);
ASSERT_EQ(bind_addr, "192.168.1.100");
}
/**
* @brief Test that get_bind_address returns configured IPv6 address
*/
TEST_F(BindAddressTest, ConfiguredIPv6Address) {
// Set a specific IPv6 address
configuration.edit_startup().sunshine.bind_address = "::1";
const auto bind_addr = net::get_bind_address(configuration.values(), net::af_e::BOTH);
ASSERT_EQ(bind_addr, "::1");
}
/**
* @brief Test that get_bind_address returns configured address regardless of address family
*/
TEST_F(BindAddressTest, ConfiguredAddressOverridesFamily) {
// Set a specific IPv6 address but request IPv4 family
// The configured address should still be returned
configuration.edit_startup().sunshine.bind_address = "2001:db8::1";
const auto bind_addr = net::get_bind_address(configuration.values(), net::af_e::IPV4);
ASSERT_EQ(bind_addr, "2001:db8::1");
}
/**
* @brief Test with loopback addresses
*/
TEST_F(BindAddressTest, LoopbackAddresses) {
// Test IPv4 loopback
configuration.edit_startup().sunshine.bind_address = "127.0.0.1";
const auto bind_addr_v4 = net::get_bind_address(configuration.values(), net::af_e::IPV4);
ASSERT_EQ(bind_addr_v4, "127.0.0.1");
// Test IPv6 loopback
configuration.edit_startup().sunshine.bind_address = "::1";
const auto bind_addr_v6 = net::get_bind_address(configuration.values(), net::af_e::BOTH);
ASSERT_EQ(bind_addr_v6, "::1");
}
/**
* @brief Test with link-local addresses
*/
TEST_F(BindAddressTest, LinkLocalAddresses) {
// Test IPv4 link-local
configuration.edit_startup().sunshine.bind_address = "169.254.1.1";
const auto bind_addr_v4 = net::get_bind_address(configuration.values(), net::af_e::IPV4);
ASSERT_EQ(bind_addr_v4, "169.254.1.1");
// Test IPv6 link-local
configuration.edit_startup().sunshine.bind_address = "fe80::1";
const auto bind_addr_v6 = net::get_bind_address(configuration.values(), net::af_e::BOTH);
ASSERT_EQ(bind_addr_v6, "fe80::1");
}
/**
* @brief Test that af_to_any_address_string still works correctly
*/
TEST_F(BindAddressTest, WildcardAddressFunction) {
ASSERT_EQ(net::af_to_any_address_string(net::af_e::IPV4), "0.0.0.0");
ASSERT_EQ(net::af_to_any_address_string(net::af_e::BOTH), "::");
}