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"
27 #include "network/networkpacket.h"
28 #include "network/socket.h"
30 class TestConnection : public TestBase {
34 if (INTERNET_SIMULATOR == false)
35 TestManager::registerTestModule(this);
38 const char *getName() { return "TestConnection"; }
40 void runTests(IGameDef *gamedef);
43 void testConnectSendReceive();
46 static TestConnection g_test_instance;
48 void TestConnection::runTests(IGameDef *gamedef)
51 TEST(testConnectSendReceive);
54 ////////////////////////////////////////////////////////////////////////////////
56 struct Handler : public con::PeerHandler
58 Handler(const char *a_name) : name(a_name) {}
60 void peerAdded(con::Peer *peer)
62 infostream << "Handler(" << name << ")::peerAdded(): "
63 "id=" << peer->id << std::endl;
68 void deletingPeer(con::Peer *peer, bool timeout)
70 infostream << "Handler(" << name << ")::deletingPeer(): "
71 "id=" << peer->id << ", timeout=" << timeout << std::endl;
81 void TestConnection::testHelpers()
83 // Some constants for testing
84 u32 proto_id = 0x12345678;
85 session_t peer_id = 123;
87 SharedBuffer<u8> data1(1);
89 Address a(127,0,0,1, 10);
90 const u16 seqnum = 34352;
92 con::BufferedPacket p1 = con::makePacket(a, data1,
93 proto_id, peer_id, channel);
95 We should now have a packet with this data:
98 [4] session_t sender_peer_id
103 UASSERT(readU32(&p1.data[0]) == proto_id);
104 UASSERT(readU16(&p1.data[4]) == peer_id);
105 UASSERT(readU8(&p1.data[6]) == channel);
106 UASSERT(readU8(&p1.data[7]) == data1[0]);
108 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
110 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
112 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
113 <<data1.getSize()<<std::endl;
114 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
115 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
116 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
118 UASSERT(p2.getSize() == 3 + data1.getSize());
119 UASSERT(readU8(&p2[0]) == con::PACKET_TYPE_RELIABLE);
120 UASSERT(readU16(&p2[1]) == seqnum);
121 UASSERT(readU8(&p2[3]) == data1[0]);
125 void TestConnection::testConnectSendReceive()
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 SharedBuffer<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 SharedBuffer<u8> recvdata = pkt.oldForgePacket();
265 UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
268 session_t 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 porting::mt_snprintf(buf, sizeof(buf), "%.2X",
285 ((int)((const char *)pkt.getU8Ptr(0))[i]) & 0xff);
290 infostream << std::endl;
292 SharedBuffer<u8> sentdata = pkt.oldForgePacket();
294 server.Send(peer_id_client, 0, &pkt, true);
298 SharedBuffer<u8> recvdata;
299 infostream << "** running client.Receive()" << std::endl;
300 session_t peer_id = 132;
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 porting::mt_snprintf(buf, sizeof(buf), "%.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);