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.
25 #include "util/serialize.h"
26 #include "network/connection.h"
28 class TestConnection : public TestBase {
32 if (INTERNET_SIMULATOR == false)
33 TestManager::registerTestModule(this);
36 const char *getName() { return "TestConnection"; }
38 void runTests(IGameDef *gamedef);
41 void testConnectSendReceive();
44 static TestConnection g_test_instance;
46 void TestConnection::runTests(IGameDef *gamedef)
49 TEST(testConnectSendReceive);
52 ////////////////////////////////////////////////////////////////////////////////
54 struct Handler : public con::PeerHandler
56 Handler(const char *a_name) : name(a_name) {}
58 void peerAdded(con::Peer *peer)
60 infostream << "Handler(" << name << ")::peerAdded(): "
61 "id=" << peer->id << std::endl;
66 void deletingPeer(con::Peer *peer, bool timeout)
68 infostream << "Handler(" << name << ")::deletingPeer(): "
69 "id=" << peer->id << ", timeout=" << timeout << std::endl;
79 void TestConnection::testHelpers()
81 // Some constants for testing
82 u32 proto_id = 0x12345678;
85 SharedBuffer<u8> data1(1);
87 Address a(127,0,0,1, 10);
88 const u16 seqnum = 34352;
90 con::BufferedPacket p1 = con::makePacket(a, data1,
91 proto_id, peer_id, channel);
93 We should now have a packet with this data:
96 [4] u16 sender_peer_id
101 UASSERT(readU32(&p1.data[0]) == proto_id);
102 UASSERT(readU16(&p1.data[4]) == peer_id);
103 UASSERT(readU8(&p1.data[6]) == channel);
104 UASSERT(readU8(&p1.data[7]) == data1[0]);
106 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
108 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
110 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
111 <<data1.getSize()<<std::endl;
112 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
113 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
114 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
116 UASSERT(p2.getSize() == 3 + data1.getSize());
117 UASSERT(readU8(&p2[0]) == TYPE_RELIABLE);
118 UASSERT(readU16(&p2[1]) == seqnum);
119 UASSERT(readU8(&p2[3]) == data1[0]);
123 void TestConnection::testConnectSendReceive()
125 DSTACK("TestConnection::Run");
128 Test some real connections
130 NOTE: This mostly tests the legacy interface.
133 u32 proto_id = 0xad26846a;
135 Handler hand_server("server");
136 Handler hand_client("client");
138 Address address(0, 0, 0, 0, 30001);
139 Address bind_addr(0, 0, 0, 0, 30001);
141 * Try to use the bind_address for servers with no localhost address
142 * For example: FreeBSD jails
144 std::string bind_str = g_settings->get("bind_address");
146 bind_addr.Resolve(bind_str.c_str());
148 if (!bind_addr.isIPv6()) {
151 } catch (ResolveError &e) {
154 infostream << "** Creating server Connection" << std::endl;
155 con::Connection server(proto_id, 512, 5.0, false, &hand_server);
156 server.Serve(address);
158 infostream << "** Creating client Connection" << std::endl;
159 con::Connection client(proto_id, 512, 5.0, false, &hand_client);
161 UASSERT(hand_server.count == 0);
162 UASSERT(hand_client.count == 0);
166 Address server_address(127, 0, 0, 1, 30001);
167 if (address != Address(0, 0, 0, 0, 30001)) {
168 server_address = bind_addr;
171 infostream << "** running client.Connect()" << std::endl;
172 client.Connect(server_address);
176 // Client should not have added client yet
177 UASSERT(hand_client.count == 0);
181 infostream << "** running client.Receive()" << std::endl;
182 client.Receive(&pkt);
183 infostream << "** Client received: peer_id=" << pkt.getPeerId()
184 << ", size=" << pkt.getSize() << std::endl;
185 } catch (con::NoIncomingDataException &e) {
188 // Client should have added server now
189 UASSERT(hand_client.count == 1);
190 UASSERT(hand_client.last_id == 1);
191 // Server should not have added client yet
192 UASSERT(hand_server.count == 0);
198 infostream << "** running server.Receive()" << std::endl;
199 server.Receive(&pkt);
200 infostream << "** Server received: peer_id=" << pkt.getPeerId()
201 << ", size=" << pkt.getSize()
203 } catch (con::NoIncomingDataException &e) {
204 // No actual data received, but the client has
205 // probably been connected
208 // Client should be the same
209 UASSERT(hand_client.count == 1);
210 UASSERT(hand_client.last_id == 1);
211 // Server should have the client
212 UASSERT(hand_server.count == 1);
213 UASSERT(hand_server.last_id == 2);
217 while (client.Connected() == false) {
220 infostream << "** running client.Receive()" << std::endl;
221 client.Receive(&pkt);
222 infostream << "** Client received: peer_id=" << pkt.getPeerId()
223 << ", size=" << pkt.getSize() << std::endl;
224 } catch (con::NoIncomingDataException &e) {
233 infostream << "** running server.Receive()" << std::endl;
234 server.Receive(&pkt);
235 infostream << "** Server received: peer_id=" << pkt.getPeerId()
236 << ", size=" << pkt.getSize()
238 } catch (con::NoIncomingDataException &e) {
242 Simple send-receive test
246 pkt.putRawPacket((u8*) "Hello World !", 14, 0);
248 Buffer<u8> sentdata = pkt.oldForgePacket();
250 infostream<<"** running client.Send()"<<std::endl;
251 client.Send(PEER_ID_SERVER, 0, &pkt, true);
255 NetworkPacket recvpacket;
256 infostream << "** running server.Receive()" << std::endl;
257 server.Receive(&recvpacket);
258 infostream << "** Server received: peer_id=" << pkt.getPeerId()
259 << ", size=" << pkt.getSize()
260 << ", data=" << (const char*)pkt.getU8Ptr(0)
263 Buffer<u8> recvdata = pkt.oldForgePacket();
265 UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
268 u16 peer_id_client = 2;
273 const int datasize = 30000;
274 NetworkPacket pkt(0, datasize);
275 for (u16 i=0; i<datasize; i++) {
279 infostream << "Sending data (size=" << datasize << "):";
280 for (int i = 0; i < datasize && i < 20; i++) {
284 snprintf(buf, 10, "%.2X",
285 ((int)((const char *)pkt.getU8Ptr(0))[i]) & 0xff);
290 infostream << std::endl;
292 Buffer<u8> sentdata = pkt.oldForgePacket();
294 server.Send(peer_id_client, 0, &pkt, true);
299 infostream << "** running client.Receive()" << std::endl;
302 bool received = false;
303 u64 timems0 = porting::getTimeMs();
305 if (porting::getTimeMs() - timems0 > 5000 || received)
309 client.Receive(&pkt);
310 size = pkt.getSize();
311 peer_id = pkt.getPeerId();
312 recvdata = pkt.oldForgePacket();
314 } catch (con::NoIncomingDataException &e) {
319 infostream << "** Client received: peer_id=" << peer_id
320 << ", size=" << size << std::endl;
322 infostream << "Received data (size=" << size << "): ";
323 for (int i = 0; i < size && i < 20; i++) {
327 snprintf(buf, 10, "%.2X", ((int)(recvdata[i])) & 0xff);
332 infostream << std::endl;
334 UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
335 UASSERT(peer_id == PEER_ID_SERVER);
338 // Check peer handlers
339 UASSERT(hand_client.count == 1);
340 UASSERT(hand_client.last_id == 1);
341 UASSERT(hand_server.count == 1);
342 UASSERT(hand_server.last_id == 2);