mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
With bind_ip empty, chat, dashboard and UGC bind 127.0.0.1 only when master is a loopback host; otherwise nothing changes. A set bind_ip always wins. Auth, master and the worlds are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
62 lines
3.0 KiB
C++
62 lines
3.0 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#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");
|
|
}
|