Network proto handlers/container fixes (#6334)
[oweals/minetest.git] / src / unittest / test_connection.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 "util/serialize.h"
25 #include "network/connection.h"
26 #include "network/networkpacket.h"
27 #include "network/socket.h"
28
29 class TestConnection : public TestBase {
30 public:
31         TestConnection()
32         {
33                 if (INTERNET_SIMULATOR == false)
34                         TestManager::registerTestModule(this);
35         }
36
37         const char *getName() { return "TestConnection"; }
38
39         void runTests(IGameDef *gamedef);
40
41         void testHelpers();
42         void testConnectSendReceive();
43 };
44
45 static TestConnection g_test_instance;
46
47 void TestConnection::runTests(IGameDef *gamedef)
48 {
49         TEST(testHelpers);
50         TEST(testConnectSendReceive);
51 }
52
53 ////////////////////////////////////////////////////////////////////////////////
54
55 struct Handler : public con::PeerHandler
56 {
57         Handler(const char *a_name) : name(a_name) {}
58
59         void peerAdded(con::Peer *peer)
60         {
61                 infostream << "Handler(" << name << ")::peerAdded(): "
62                         "id=" << peer->id << std::endl;
63                 last_id = peer->id;
64                 count++;
65         }
66
67         void deletingPeer(con::Peer *peer, bool timeout)
68         {
69                 infostream << "Handler(" << name << ")::deletingPeer(): "
70                         "id=" << peer->id << ", timeout=" << timeout << std::endl;
71                 last_id = peer->id;
72                 count--;
73         }
74
75         s32 count = 0;
76         u16 last_id = 0;
77         const char *name;
78 };
79
80 void TestConnection::testHelpers()
81 {
82         // Some constants for testing
83         u32 proto_id = 0x12345678;
84         u16 peer_id = 123;
85         u8 channel = 2;
86         SharedBuffer<u8> data1(1);
87         data1[0] = 100;
88         Address a(127,0,0,1, 10);
89         const u16 seqnum = 34352;
90
91         con::BufferedPacket p1 = con::makePacket(a, data1,
92                         proto_id, peer_id, channel);
93         /*
94                 We should now have a packet with this data:
95                 Header:
96                         [0] u32 protocol_id
97                         [4] u16 sender_peer_id
98                         [6] u8 channel
99                 Data:
100                         [7] u8 data1[0]
101         */
102         UASSERT(readU32(&p1.data[0]) == proto_id);
103         UASSERT(readU16(&p1.data[4]) == peer_id);
104         UASSERT(readU8(&p1.data[6]) == channel);
105         UASSERT(readU8(&p1.data[7]) == data1[0]);
106
107         //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
108
109         SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
110
111         /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
112                         <<data1.getSize()<<std::endl;
113         infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
114                         <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
115         infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
116
117         UASSERT(p2.getSize() == 3 + data1.getSize());
118         UASSERT(readU8(&p2[0]) == con::PACKET_TYPE_RELIABLE);
119         UASSERT(readU16(&p2[1]) == seqnum);
120         UASSERT(readU8(&p2[3]) == data1[0]);
121 }
122
123
124 void TestConnection::testConnectSendReceive()
125 {
126         DSTACK("TestConnection::Run");
127
128         /*
129                 Test some real connections
130
131                 NOTE: This mostly tests the legacy interface.
132         */
133
134         u32 proto_id = 0xad26846a;
135
136         Handler hand_server("server");
137         Handler hand_client("client");
138
139         Address address(0, 0, 0, 0, 30001);
140         Address bind_addr(0, 0, 0, 0, 30001);
141         /*
142          * Try to use the bind_address for servers with no localhost address
143          * For example: FreeBSD jails
144          */
145         std::string bind_str = g_settings->get("bind_address");
146         try {
147                 bind_addr.Resolve(bind_str.c_str());
148
149                 if (!bind_addr.isIPv6()) {
150                         address = bind_addr;
151                 }
152         } catch (ResolveError &e) {
153         }
154
155         infostream << "** Creating server Connection" << std::endl;
156         con::Connection server(proto_id, 512, 5.0, false, &hand_server);
157         server.Serve(address);
158
159         infostream << "** Creating client Connection" << std::endl;
160         con::Connection client(proto_id, 512, 5.0, false, &hand_client);
161
162         UASSERT(hand_server.count == 0);
163         UASSERT(hand_client.count == 0);
164
165         sleep_ms(50);
166
167         Address server_address(127, 0, 0, 1, 30001);
168         if (address != Address(0, 0, 0, 0, 30001)) {
169                 server_address = bind_addr;
170         }
171
172         infostream << "** running client.Connect()" << std::endl;
173         client.Connect(server_address);
174
175         sleep_ms(50);
176
177         // Client should not have added client yet
178         UASSERT(hand_client.count == 0);
179
180         try {
181                 NetworkPacket pkt;
182                 infostream << "** running client.Receive()" << std::endl;
183                 client.Receive(&pkt);
184                 infostream << "** Client received: peer_id=" << pkt.getPeerId()
185                         << ", size=" << pkt.getSize() << std::endl;
186         } catch (con::NoIncomingDataException &e) {
187         }
188
189         // Client should have added server now
190         UASSERT(hand_client.count == 1);
191         UASSERT(hand_client.last_id == 1);
192         // Server should not have added client yet
193         UASSERT(hand_server.count == 0);
194
195         sleep_ms(100);
196
197         try {
198                 NetworkPacket pkt;
199                 infostream << "** running server.Receive()" << std::endl;
200                 server.Receive(&pkt);
201                 infostream << "** Server received: peer_id=" << pkt.getPeerId()
202                                 << ", size=" << pkt.getSize()
203                                 << std::endl;
204         } catch (con::NoIncomingDataException &e) {
205                 // No actual data received, but the client has
206                 // probably been connected
207         }
208
209         // Client should be the same
210         UASSERT(hand_client.count == 1);
211         UASSERT(hand_client.last_id == 1);
212         // Server should have the client
213         UASSERT(hand_server.count == 1);
214         UASSERT(hand_server.last_id == 2);
215
216         //sleep_ms(50);
217
218         while (client.Connected() == false) {
219                 try {
220                         NetworkPacket pkt;
221                         infostream << "** running client.Receive()" << std::endl;
222                         client.Receive(&pkt);
223                         infostream << "** Client received: peer_id=" << pkt.getPeerId()
224                                 << ", size=" << pkt.getSize() << std::endl;
225                 } catch (con::NoIncomingDataException &e) {
226                 }
227                 sleep_ms(50);
228         }
229
230         sleep_ms(50);
231
232         try {
233                 NetworkPacket pkt;
234                 infostream << "** running server.Receive()" << std::endl;
235                 server.Receive(&pkt);
236                 infostream << "** Server received: peer_id=" << pkt.getPeerId()
237                                 << ", size=" << pkt.getSize()
238                                 << std::endl;
239         } catch (con::NoIncomingDataException &e) {
240         }
241
242         /*
243                 Simple send-receive test
244         */
245         {
246                 NetworkPacket pkt;
247                 pkt.putRawPacket((u8*) "Hello World !", 14, 0);
248
249                 SharedBuffer<u8> sentdata = pkt.oldForgePacket();
250
251                 infostream<<"** running client.Send()"<<std::endl;
252                 client.Send(PEER_ID_SERVER, 0, &pkt, true);
253
254                 sleep_ms(50);
255
256                 NetworkPacket recvpacket;
257                 infostream << "** running server.Receive()" << std::endl;
258                 server.Receive(&recvpacket);
259                 infostream << "** Server received: peer_id=" << pkt.getPeerId()
260                                 << ", size=" << pkt.getSize()
261                                 << ", data=" << (const char*)pkt.getU8Ptr(0)
262                                 << std::endl;
263
264                 SharedBuffer<u8> recvdata = pkt.oldForgePacket();
265
266                 UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
267         }
268
269         u16 peer_id_client = 2;
270         /*
271                 Send a large packet
272         */
273         {
274                 const int datasize = 30000;
275                 NetworkPacket pkt(0, datasize);
276                 for (u16 i=0; i<datasize; i++) {
277                         pkt << (u8) i/4;
278                 }
279
280                 infostream << "Sending data (size=" << datasize << "):";
281                 for (int i = 0; i < datasize && i < 20; i++) {
282                         if (i % 2 == 0)
283                                 infostream << " ";
284                         char buf[10];
285                         snprintf(buf, 10, "%.2X",
286                                 ((int)((const char *)pkt.getU8Ptr(0))[i]) & 0xff);
287                         infostream<<buf;
288                 }
289                 if (datasize > 20)
290                         infostream << "...";
291                 infostream << std::endl;
292
293                 SharedBuffer<u8> sentdata = pkt.oldForgePacket();
294
295                 server.Send(peer_id_client, 0, &pkt, true);
296
297                 //sleep_ms(3000);
298
299                 SharedBuffer<u8> recvdata;
300                 infostream << "** running client.Receive()" << std::endl;
301                 u16 peer_id = 132;
302                 u16 size = 0;
303                 bool received = false;
304                 u64 timems0 = porting::getTimeMs();
305                 for (;;) {
306                         if (porting::getTimeMs() - timems0 > 5000 || received)
307                                 break;
308                         try {
309                                 NetworkPacket pkt;
310                                 client.Receive(&pkt);
311                                 size = pkt.getSize();
312                                 peer_id = pkt.getPeerId();
313                                 recvdata = pkt.oldForgePacket();
314                                 received = true;
315                         } catch (con::NoIncomingDataException &e) {
316                         }
317                         sleep_ms(10);
318                 }
319                 UASSERT(received);
320                 infostream << "** Client received: peer_id=" << peer_id
321                         << ", size=" << size << std::endl;
322
323                 infostream << "Received data (size=" << size << "): ";
324                 for (int i = 0; i < size && i < 20; i++) {
325                         if (i % 2 == 0)
326                                 infostream << " ";
327                         char buf[10];
328                         snprintf(buf, 10, "%.2X", ((int)(recvdata[i])) & 0xff);
329                         infostream << buf;
330                 }
331                 if (size > 20)
332                         infostream << "...";
333                 infostream << std::endl;
334
335                 UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
336                 UASSERT(peer_id == PEER_ID_SERVER);
337         }
338
339         // Check peer handlers
340         UASSERT(hand_client.count == 1);
341         UASSERT(hand_client.last_id == 1);
342         UASSERT(hand_server.count == 1);
343         UASSERT(hand_server.last_id == 2);
344 }