2019-02-09 19:52:38 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "network/socket.h"
|
|
|
|
|
2019-02-15 12:27:29 +01:00
|
|
|
class TestAddress : public TestBase
|
|
|
|
{
|
2019-02-09 19:52:38 +01:00
|
|
|
public:
|
2019-02-15 12:27:29 +01:00
|
|
|
TestAddress() { TestManager::registerTestModule(this); }
|
2019-02-09 19:52:38 +01:00
|
|
|
|
|
|
|
const char *getName() { return "TestAddress"; }
|
|
|
|
|
|
|
|
void runTests(IGameDef *gamedef);
|
|
|
|
|
2024-01-01 15:35:46 +01:00
|
|
|
void testBasic();
|
2019-02-09 19:52:38 +01:00
|
|
|
void testIsLocalhost();
|
2024-01-01 15:35:46 +01:00
|
|
|
void testResolve();
|
2019-02-09 19:52:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static TestAddress g_test_instance;
|
|
|
|
|
|
|
|
void TestAddress::runTests(IGameDef *gamedef)
|
|
|
|
{
|
2024-01-01 15:35:46 +01:00
|
|
|
TEST(testBasic);
|
2019-02-09 19:52:38 +01:00
|
|
|
TEST(testIsLocalhost);
|
2024-01-01 15:35:46 +01:00
|
|
|
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());
|
2019-02-09 19:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestAddress::testIsLocalhost()
|
|
|
|
{
|
|
|
|
// v4
|
|
|
|
UASSERT(Address(127, 0, 0, 1, 0).isLocalhost());
|
|
|
|
UASSERT(Address(127, 254, 12, 99, 0).isLocalhost());
|
|
|
|
UASSERT(Address(127, 188, 255, 247, 0).isLocalhost());
|
|
|
|
UASSERT(!Address(126, 255, 255, 255, 0).isLocalhost());
|
|
|
|
UASSERT(!Address(128, 0, 0, 0, 0).isLocalhost());
|
|
|
|
UASSERT(!Address(1, 0, 0, 0, 0).isLocalhost());
|
|
|
|
UASSERT(!Address(255, 255, 255, 255, 0).isLocalhost());
|
|
|
|
UASSERT(!Address(36, 45, 99, 158, 0).isLocalhost());
|
|
|
|
UASSERT(!Address(172, 45, 37, 68, 0).isLocalhost());
|
|
|
|
|
|
|
|
// v6
|
2022-02-23 20:02:58 +01:00
|
|
|
auto ipv6Bytes = std::make_unique<IPv6AddressBytes>();
|
2019-02-09 19:52:38 +01:00
|
|
|
std::vector<u8> ipv6RawAddr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
|
|
|
|
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
|
|
|
|
UASSERT(Address(ipv6Bytes.get(), 0).isLocalhost())
|
|
|
|
|
|
|
|
ipv6RawAddr = {16, 34, 0, 0, 0, 0, 29, 0, 0, 0, 188, 0, 0, 0, 0, 14};
|
|
|
|
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
|
|
|
|
UASSERT(!Address(ipv6Bytes.get(), 0).isLocalhost())
|
|
|
|
}
|
2024-01-01 15:35:46 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|