3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 class TestSocket : public TestBase {
30 if (INTERNET_SIMULATOR == false)
31 TestManager::registerTestModule(this);
34 const char *getName() { return "TestSocket"; }
36 void runTests(IGameDef *gamedef);
38 void testIPv4Socket();
39 void testIPv6Socket();
41 static const int port = 30003;
44 static TestSocket g_test_instance;
46 void TestSocket::runTests(IGameDef *gamedef)
50 if (g_settings->getBool("enable_ipv6"))
54 ////////////////////////////////////////////////////////////////////////////////
56 void TestSocket::testIPv4Socket()
58 Address address(0, 0, 0, 0, port);
59 Address bind_addr(0, 0, 0, 0, port);
62 * Try to use the bind_address for servers with no localhost address
63 * For example: FreeBSD jails
65 std::string bind_str = g_settings->get("bind_address");
67 bind_addr.Resolve(bind_str.c_str());
69 if (!bind_addr.isIPv6()) {
72 } catch (ResolveError &e) {
75 UDPSocket socket(false);
78 const char sendbuffer[] = "hello world!";
80 * If there is a bind address, use it.
81 * It's useful in container environments
83 if (address != Address(0, 0, 0, 0, port))
84 socket.Send(address, sendbuffer, sizeof(sendbuffer));
86 socket.Send(Address(127, 0, 0, 1, port), sendbuffer, sizeof(sendbuffer));
90 char rcvbuffer[256] = { 0 };
93 if (socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
96 //FIXME: This fails on some systems
97 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
99 if (address != Address(0, 0, 0, 0, port)) {
100 UASSERT(sender.getAddress().sin_addr.s_addr ==
101 address.getAddress().sin_addr.s_addr);
103 UASSERT(sender.getAddress().sin_addr.s_addr ==
104 Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
108 void TestSocket::testIPv6Socket()
110 Address address6((IPv6AddressBytes *)NULL, port);
113 if (!socket6.init(true, true)) {
114 /* Note: Failing to create an IPv6 socket is not technically an
115 error because the OS may not support IPv6 or it may
116 have been disabled. IPv6 is not /required/ by
117 minetest and therefore this should not cause the unit
120 dstream << "WARNING: IPv6 socket creation failed (unit test)"
125 const char sendbuffer[] = "hello world!";
126 IPv6AddressBytes bytes;
129 socket6.Bind(address6);
132 socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));
136 char rcvbuffer[256] = { 0 };
140 if (socket6.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
143 //FIXME: This fails on some systems
144 UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
145 UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
146 Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
147 } catch (SendFailedException &e) {
148 errorstream << "IPv6 support enabled but not available!"