Translated using Weblate (Italian)
[oweals/minetest.git] / src / unittest / test_address.cpp
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 {
28 public:
29         TestAddress() { TestManager::registerTestModule(this); }
30
31         const char *getName() { return "TestAddress"; }
32
33         void runTests(IGameDef *gamedef);
34
35         void testIsLocalhost();
36 };
37
38 static TestAddress g_test_instance;
39
40 void TestAddress::runTests(IGameDef *gamedef)
41 {
42         TEST(testIsLocalhost);
43 }
44
45 void TestAddress::testIsLocalhost()
46 {
47         // v4
48         UASSERT(Address(127, 0, 0, 1, 0).isLocalhost());
49         UASSERT(Address(127, 254, 12, 99, 0).isLocalhost());
50         UASSERT(Address(127, 188, 255, 247, 0).isLocalhost());
51         UASSERT(!Address(126, 255, 255, 255, 0).isLocalhost());
52         UASSERT(!Address(128, 0, 0, 0, 0).isLocalhost());
53         UASSERT(!Address(1, 0, 0, 0, 0).isLocalhost());
54         UASSERT(!Address(255, 255, 255, 255, 0).isLocalhost());
55         UASSERT(!Address(36, 45, 99, 158, 0).isLocalhost());
56         UASSERT(!Address(172, 45, 37, 68, 0).isLocalhost());
57
58         // v6
59         std::unique_ptr<IPv6AddressBytes> ipv6Bytes(new IPv6AddressBytes());
60         std::vector<u8> ipv6RawAddr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
61         memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
62         UASSERT(Address(ipv6Bytes.get(), 0).isLocalhost())
63
64         ipv6RawAddr = {16, 34, 0, 0, 0, 0, 29, 0, 0, 0, 188, 0, 0, 0, 0, 14};
65         memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
66         UASSERT(!Address(ipv6Bytes.get(), 0).isLocalhost())
67 }