#include #include "GeneralUtils.h" TEST(BindAddressTests, EmptyMeansAllInterfaces) { EXPECT_EQ(GeneralUtils::ParseBindAddress(""), std::string{}); EXPECT_EQ(GeneralUtils::ParseBindAddress(" "), std::string{}); EXPECT_EQ(GeneralUtils::ParseBindAddress("0.0.0.0"), std::string{}); EXPECT_EQ(GeneralUtils::ParseBindAddress("*"), std::string{}); } TEST(BindAddressTests, AcceptsIPv4) { EXPECT_EQ(GeneralUtils::ParseBindAddress("127.0.0.1"), "127.0.0.1"); EXPECT_EQ(GeneralUtils::ParseBindAddress(" 192.168.1.20\r"), "192.168.1.20"); EXPECT_EQ(GeneralUtils::ParseBindAddress("255.255.255.255"), "255.255.255.255"); EXPECT_EQ(GeneralUtils::ParseBindAddress("localhost"), "127.0.0.1"); EXPECT_EQ(GeneralUtils::ParseBindAddress("LocalHost"), "127.0.0.1"); } TEST(BindAddressTests, RejectsEverythingElse) { for (const auto* bad : { "256.0.0.1", "1.2.3", "1.2.3.4.5", "1..2.3", "a.b.c.d", "010.0.0.1", "-1.0.0.1", "1.2.3.4:2000", "example.com", "::1", "1.2.3.4 5", "+1.2.3.4" }) { EXPECT_FALSE(GeneralUtils::ParseBindAddress(bad).has_value()) << bad; } } TEST(BindAddressTests, LoopbackHosts) { for (const auto* host : { "localhost", "LocalHost", "127.0.0.1", "127.0.0.2", "127.255.255.254", "::1", "[::1]", " 127.0.0.1\r" }) { EXPECT_TRUE(GeneralUtils::IsLoopbackHost(host)) << host; } for (const auto* host : { "", "0.0.0.0", "10.0.0.5", "192.168.1.20", "example.com", "128.0.0.1", "127.0.0", "127.0.0.1.5", "1127.0.0.1" }) { EXPECT_FALSE(GeneralUtils::IsLoopbackHost(host)) << host; } } TEST(BindAddressTests, InternalOnlyServersBindLoopbackWhenMasterIsLocal) { EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "localhost"), "127.0.0.1"); EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "127.0.0.1"), "127.0.0.1"); EXPECT_EQ(GeneralUtils::ResolveBindAddress(" ", true, "::1"), "127.0.0.1"); } TEST(BindAddressTests, InternalOnlyServersListenOnAllWhenMasterIsRemote) { EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "10.0.0.5"), std::string{}); EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, "master.example.com"), std::string{}); EXPECT_EQ(GeneralUtils::ResolveBindAddress("", true, ""), std::string{}); } TEST(BindAddressTests, ExplicitBindIpOverridesTheDefault) { EXPECT_EQ(GeneralUtils::ResolveBindAddress("127.0.0.2", true, "10.0.0.5"), "127.0.0.2"); EXPECT_EQ(GeneralUtils::ResolveBindAddress("192.168.1.20", true, "localhost"), "192.168.1.20"); // 0.0.0.0 set on purpose means all interfaces even next to a local master EXPECT_EQ(GeneralUtils::ResolveBindAddress("0.0.0.0", true, "localhost"), std::string{}); EXPECT_EQ(GeneralUtils::ResolveBindAddress("localhost", true, "10.0.0.5"), "127.0.0.1"); EXPECT_FALSE(GeneralUtils::ResolveBindAddress("example.com", true, "localhost").has_value()); } TEST(BindAddressTests, GameFacingServersAreUnchanged) { EXPECT_EQ(GeneralUtils::ResolveBindAddress("", false, "localhost"), std::string{}); EXPECT_EQ(GeneralUtils::ResolveBindAddress("", false, "127.0.0.1"), std::string{}); EXPECT_EQ(GeneralUtils::ResolveBindAddress("192.168.1.20", false, "localhost"), "192.168.1.20"); }