|
| 1 | +/* |
| 2 | +Minetest |
| 3 | +Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> |
| 4 | +
|
| 5 | +This program is free software; you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU Lesser General Public License as published by |
| 7 | +the Free Software Foundation; either version 2.1 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU Lesser General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU Lesser General Public License along |
| 16 | +with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "test.h" |
| 21 | + |
| 22 | +#include "log.h" |
| 23 | +#include "settings.h" |
| 24 | +#include "network/socket.h" |
| 25 | + |
| 26 | +class TestAddress : public TestBase { |
| 27 | +public: |
| 28 | + TestAddress() |
| 29 | + { |
| 30 | + TestManager::registerTestModule(this); |
| 31 | + } |
| 32 | + |
| 33 | + const char *getName() { return "TestAddress"; } |
| 34 | + |
| 35 | + void runTests(IGameDef *gamedef); |
| 36 | + |
| 37 | + void testIsLocalhost(); |
| 38 | +}; |
| 39 | + |
| 40 | +static TestAddress g_test_instance; |
| 41 | + |
| 42 | +void TestAddress::runTests(IGameDef *gamedef) |
| 43 | +{ |
| 44 | + TEST(testIsLocalhost); |
| 45 | +} |
| 46 | + |
| 47 | +void TestAddress::testIsLocalhost() |
| 48 | +{ |
| 49 | + // v4 |
| 50 | + UASSERT(Address(127, 0, 0, 1, 0).isLocalhost()); |
| 51 | + UASSERT(Address(127, 254, 12, 99, 0).isLocalhost()); |
| 52 | + UASSERT(Address(127, 188, 255, 247, 0).isLocalhost()); |
| 53 | + UASSERT(!Address(126, 255, 255, 255, 0).isLocalhost()); |
| 54 | + UASSERT(!Address(128, 0, 0, 0, 0).isLocalhost()); |
| 55 | + UASSERT(!Address(1, 0, 0, 0, 0).isLocalhost()); |
| 56 | + UASSERT(!Address(255, 255, 255, 255, 0).isLocalhost()); |
| 57 | + UASSERT(!Address(36, 45, 99, 158, 0).isLocalhost()); |
| 58 | + UASSERT(!Address(172, 45, 37, 68, 0).isLocalhost()); |
| 59 | + |
| 60 | + // v6 |
| 61 | + std::unique_ptr<IPv6AddressBytes> ipv6Bytes(new IPv6AddressBytes()); |
| 62 | + std::vector<u8> ipv6RawAddr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
| 63 | + memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16); |
| 64 | + UASSERT(Address(ipv6Bytes.get(), 0).isLocalhost()) |
| 65 | + |
| 66 | + ipv6RawAddr = {16, 34, 0, 0, 0, 0, 29, 0, 0, 0, 188, 0, 0, 0, 0, 14}; |
| 67 | + memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16); |
| 68 | + UASSERT(!Address(ipv6Bytes.get(), 0).isLocalhost()) |
| 69 | +} |
0 commit comments