1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Extend capabilities of Address class

This commit is contained in:
sfan5 2024-01-01 15:35:46 +01:00
parent 171f911237
commit dc7fb26921
4 changed files with 111 additions and 49 deletions

View file

@ -32,14 +32,36 @@ public:
void runTests(IGameDef *gamedef);
void testBasic();
void testIsLocalhost();
void testResolve();
};
static TestAddress g_test_instance;
void TestAddress::runTests(IGameDef *gamedef)
{
TEST(testBasic);
TEST(testIsLocalhost);
TEST(testResolve);
}
void TestAddress::testBasic()
{
Address tmp;
UASSERT(!tmp.isValid());
UASSERTEQ(int, tmp.getFamily(), 0);
tmp = Address(static_cast<u32>(0), 0);
UASSERT(tmp.isValid());
UASSERTEQ(int, tmp.getFamily(), AF_INET);
UASSERT(tmp.isAny());
tmp = Address(static_cast<IPv6AddressBytes*>(nullptr), 0);
UASSERT(tmp.isValid());
UASSERTEQ(int, tmp.getFamily(), AF_INET6);
UASSERT(tmp.isAny());
}
void TestAddress::testIsLocalhost()
@ -65,3 +87,33 @@ void TestAddress::testIsLocalhost()
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
UASSERT(!Address(ipv6Bytes.get(), 0).isLocalhost())
}
void TestAddress::testResolve()
{
// Empty test
{
Address tmp(1, 2, 3, 4, 5);
tmp.Resolve("");
UASSERT(tmp.isValid());
UASSERT(tmp.isAny());
}
// Localhost test
Address result, fallback;
result.Resolve("localhost", &fallback);
UASSERT(result.isValid());
UASSERT(result.isLocalhost());
if (fallback.isValid()) {
UASSERT(fallback.isLocalhost());
// the result should be ::1 and 127.0.0.1 so the fallback addr should be
// of a different family
UASSERTCMP(int, !=, result.getFamily(), fallback.getFamily());
} else if (g_settings->getBool("enable_ipv6")) {
warningstream << "Couldn't verify Address::Resolve fallback (no IPv6?)"
<< std::endl;
}
}