3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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.
21 #include "common_irrlicht.h"
27 #include "connection.h"
29 #include "serialization.h"
33 #include "content_mapnode.h"
35 #include "mapsector.h"
40 Asserts that the exception occurs
42 #define EXCEPTION_CHECK(EType, code)\
44 bool exception_thrown = false;\
46 catch(EType &e) { exception_thrown = true; }\
47 assert(exception_thrown);\
54 /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
55 infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
56 infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
57 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
58 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
59 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
60 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
61 assert(lowercase("Foo bAR") == "foo bar");
62 assert(is_yes("YeS") == true);
63 assert(is_yes("") == false);
64 assert(is_yes("FAlse") == false);
73 // Test reading of settings
74 s.parseConfigLine("leet = 1337");
75 s.parseConfigLine("leetleet = 13371337");
76 s.parseConfigLine("leetleet_neg = -13371337");
77 s.parseConfigLine("floaty_thing = 1.1");
78 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
79 s.parseConfigLine("coord = (1, 2, 4.5)");
80 assert(s.getS32("leet") == 1337);
81 assert(s.getS16("leetleet") == 32767);
82 assert(s.getS16("leetleet_neg") == -32768);
83 // Not sure if 1.1 is an exact value as a float, but doesn't matter
84 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
85 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
86 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
87 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
88 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
89 // Test the setting of settings too
90 s.setFloat("floaty_thing_2", 1.2);
91 s.setV3F("coord2", v3f(1, 2, 3.3));
92 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
93 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
94 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
95 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
96 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
106 SharedBuffer<u8> fromdata(4);
112 std::ostringstream os(std::ios_base::binary);
113 compress(fromdata, os, 0);
115 std::string str_out = os.str();
117 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
118 infostream<<"TestCompress: 1,5,5,1 -> ";
119 for(u32 i=0; i<str_out.size(); i++)
121 infostream<<(u32)str_out[i]<<",";
123 infostream<<std::endl;
125 assert(str_out.size() == 10);
127 assert(str_out[0] == 0);
128 assert(str_out[1] == 0);
129 assert(str_out[2] == 0);
130 assert(str_out[3] == 4);
131 assert(str_out[4] == 0);
132 assert(str_out[5] == 1);
133 assert(str_out[6] == 1);
134 assert(str_out[7] == 5);
135 assert(str_out[8] == 0);
136 assert(str_out[9] == 1);
138 std::istringstream is(str_out, std::ios_base::binary);
139 std::ostringstream os2(std::ios_base::binary);
141 decompress(is, os2, 0);
142 std::string str_out2 = os2.str();
144 infostream<<"decompress: ";
145 for(u32 i=0; i<str_out2.size(); i++)
147 infostream<<(u32)str_out2[i]<<",";
149 infostream<<std::endl;
151 assert(str_out2.size() == fromdata.getSize());
153 for(u32 i=0; i<str_out2.size(); i++)
155 assert(str_out2[i] == fromdata[i]);
162 SharedBuffer<u8> fromdata(4);
168 std::ostringstream os(std::ios_base::binary);
169 compress(fromdata, os, SER_FMT_VER_HIGHEST);
171 std::string str_out = os.str();
173 infostream<<"str_out.size()="<<str_out.size()<<std::endl;
174 infostream<<"TestCompress: 1,5,5,1 -> ";
175 for(u32 i=0; i<str_out.size(); i++)
177 infostream<<(u32)str_out[i]<<",";
179 infostream<<std::endl;
181 /*assert(str_out.size() == 10);
183 assert(str_out[0] == 0);
184 assert(str_out[1] == 0);
185 assert(str_out[2] == 0);
186 assert(str_out[3] == 4);
187 assert(str_out[4] == 0);
188 assert(str_out[5] == 1);
189 assert(str_out[6] == 1);
190 assert(str_out[7] == 5);
191 assert(str_out[8] == 0);
192 assert(str_out[9] == 1);*/
194 std::istringstream is(str_out, std::ios_base::binary);
195 std::ostringstream os2(std::ios_base::binary);
197 decompress(is, os2, SER_FMT_VER_HIGHEST);
198 std::string str_out2 = os2.str();
200 infostream<<"decompress: ";
201 for(u32 i=0; i<str_out2.size(); i++)
203 infostream<<(u32)str_out2[i]<<",";
205 infostream<<std::endl;
207 assert(str_out2.size() == fromdata.getSize());
209 for(u32 i=0; i<str_out2.size(); i++)
211 assert(str_out2[i] == fromdata[i]);
220 void Run(INodeDefManager *nodedef)
225 assert(n.getContent() == CONTENT_AIR);
226 assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
227 assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
230 n.setContent(CONTENT_AIR);
231 assert(nodedef->get(n).light_propagates == true);
232 n.setContent(CONTENT_STONE);
233 assert(nodedef->get(n).light_propagates == false);
237 struct TestVoxelManipulator
239 void Run(INodeDefManager *nodedef)
245 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
246 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
247 assert(a.index(-1,-1,-1) == 0);
249 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
250 // An area that is 1 bigger in x+ and z-
251 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
253 core::list<VoxelArea> aa;
257 core::array<VoxelArea> results;
258 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
259 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
261 assert(aa.size() == results.size());
263 infostream<<"Result of diff:"<<std::endl;
264 for(core::list<VoxelArea>::Iterator
265 i = aa.begin(); i != aa.end(); i++)
267 i->print(infostream);
268 infostream<<std::endl;
270 s32 j = results.linear_search(*i);
282 v.print(infostream, nodedef);
284 infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
286 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
288 v.print(infostream, nodedef);
290 assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
292 infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
294 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
296 v.print(infostream, nodedef);
298 infostream<<"*** Adding area ***"<<std::endl;
302 v.print(infostream, nodedef);
304 assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
305 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
310 NOTE: These tests became non-working then NodeContainer was removed.
311 These should be redone, utilizing some kind of a virtual
312 interface for Map (IMap would be fine).
317 class TC : public NodeContainer
323 core::list<v3s16> validity_exceptions;
327 position_valid = true;
330 virtual bool isValidPosition(v3s16 p)
332 //return position_valid ^ (p==position_valid_exception);
333 bool exception = false;
334 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
335 i != validity_exceptions.end(); i++)
343 return exception ? !position_valid : position_valid;
346 virtual MapNode getNode(v3s16 p)
348 if(isValidPosition(p) == false)
349 throw InvalidPositionException();
353 virtual void setNode(v3s16 p, MapNode & n)
355 if(isValidPosition(p) == false)
356 throw InvalidPositionException();
359 virtual u16 nodeContainerId() const
369 MapBlock b(&parent, v3s16(1,1,1));
370 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
372 assert(b.getPosRelative() == relpos);
374 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
375 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
376 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
377 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
378 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
379 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
381 assert(b.isValidPosition(v3s16(0,0,0)) == true);
382 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
383 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
384 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
385 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
386 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
389 TODO: this method should probably be removed
390 if the block size isn't going to be set variable
392 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
393 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
395 // Changed flag should be initially set
396 assert(b.getChangedFlag() == true);
397 b.resetChangedFlag();
398 assert(b.getChangedFlag() == false);
400 // All nodes should have been set to
401 // .d=CONTENT_IGNORE and .getLight() = 0
402 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
403 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
404 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
406 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
407 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
408 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
409 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
413 MapNode n(CONTENT_AIR);
414 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
415 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
416 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
418 b.setNode(v3s16(x,y,z), n);
423 Parent fetch functions
425 parent.position_valid = false;
426 parent.node.setContent(5);
430 // Positions in the block should still be valid
431 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
432 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
433 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
434 assert(n.getContent() == CONTENT_AIR);
436 // ...but outside the block they should be invalid
437 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
438 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
439 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
442 bool exception_thrown = false;
444 // This should throw an exception
445 MapNode n = b.getNodeParent(v3s16(0,0,-1));
447 catch(InvalidPositionException &e)
449 exception_thrown = true;
451 assert(exception_thrown);
454 parent.position_valid = true;
455 // Now the positions outside should be valid
456 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
457 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
458 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
459 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
460 assert(n.getContent() == 5);
468 assert(b.getNode(p).getContent() == 4);
469 //TODO: Update to new system
470 /*assert(b.getNodeTile(p) == 4);
471 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
476 // Set lighting of all nodes to 0
477 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
478 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
479 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
480 MapNode n = b.getNode(v3s16(x,y,z));
481 n.setLight(LIGHTBANK_DAY, 0);
482 n.setLight(LIGHTBANK_NIGHT, 0);
483 b.setNode(v3s16(x,y,z), n);
489 Check how the block handles being a lonely sky block
491 parent.position_valid = true;
492 b.setIsUnderground(false);
493 parent.node.setContent(CONTENT_AIR);
494 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
495 parent.node.setLight(LIGHTBANK_NIGHT, 0);
496 core::map<v3s16, bool> light_sources;
497 // The bottom block is invalid, because we have a shadowing node
498 assert(b.propagateSunlight(light_sources) == false);
499 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
500 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
501 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
502 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
503 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
504 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
505 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
506 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
507 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
508 // According to MapBlock::getFaceLight,
509 // The face on the z+ side should have double-diminished light
510 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
511 // The face on the z+ side should have diminished light
512 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
515 Check how the block handles being in between blocks with some non-sunlight
516 while being underground
519 // Make neighbours to exist and set some non-sunlight to them
520 parent.position_valid = true;
521 b.setIsUnderground(true);
522 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
523 core::map<v3s16, bool> light_sources;
524 // The block below should be valid because there shouldn't be
525 // sunlight in there either
526 assert(b.propagateSunlight(light_sources, true) == true);
527 // Should not touch nodes that are not affected (that is, all of them)
528 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
529 // Should set light of non-sunlighted blocks to 0.
530 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
533 Set up a situation where:
534 - There is only air in this block
535 - There is a valid non-sunlighted block at the bottom, and
536 - Invalid blocks elsewhere.
537 - the block is not underground.
539 This should result in bottom block invalidity
542 b.setIsUnderground(false);
544 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
545 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
546 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
548 n.setContent(CONTENT_AIR);
549 n.setLight(LIGHTBANK_DAY, 0);
550 b.setNode(v3s16(x,y,z), n);
554 // Make neighbours invalid
555 parent.position_valid = false;
556 // Add exceptions to the top of the bottom block
557 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
558 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
560 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
562 // Lighting value for the valid nodes
563 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
564 core::map<v3s16, bool> light_sources;
565 // Bottom block is not valid
566 assert(b.propagateSunlight(light_sources) == false);
573 class TC : public NodeContainer
582 position_valid = true;
585 virtual bool isValidPosition(v3s16 p)
587 return position_valid;
590 virtual MapNode getNode(v3s16 p)
592 if(position_valid == false)
593 throw InvalidPositionException();
597 virtual void setNode(v3s16 p, MapNode & n)
599 if(position_valid == false)
600 throw InvalidPositionException();
603 virtual u16 nodeContainerId() const
612 parent.position_valid = false;
614 // Create one with no heightmaps
615 ServerMapSector sector(&parent, v2s16(1,1));
617 assert(sector.getBlockNoCreateNoEx(0) == 0);
618 assert(sector.getBlockNoCreateNoEx(1) == 0);
620 MapBlock * bref = sector.createBlankBlock(-2);
622 assert(sector.getBlockNoCreateNoEx(0) == 0);
623 assert(sector.getBlockNoCreateNoEx(-2) == bref);
625 //TODO: Check for AlreadyExistsException
627 /*bool exception_thrown = false;
631 catch(InvalidPositionException &e){
632 exception_thrown = true;
634 assert(exception_thrown);*/
644 const int port = 30003;
648 const char sendbuffer[] = "hello world!";
649 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
654 memset(rcvbuffer, 0, sizeof(rcvbuffer));
658 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
662 //FIXME: This fails on some systems
663 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
664 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
668 struct TestConnection
673 Test helper functions
676 // Some constants for testing
677 u32 proto_id = 0x12345678;
680 SharedBuffer<u8> data1(1);
682 Address a(127,0,0,1, 10);
685 con::BufferedPacket p1 = con::makePacket(a, data1,
686 proto_id, peer_id, channel);
688 We should now have a packet with this data:
691 [4] u16 sender_peer_id
696 assert(readU32(&p1.data[0]) == proto_id);
697 assert(readU16(&p1.data[4]) == peer_id);
698 assert(readU8(&p1.data[6]) == channel);
699 assert(readU8(&p1.data[7]) == data1[0]);
701 //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
703 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
705 /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
706 <<data1.getSize()<<std::endl;
707 infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
708 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
709 infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
711 assert(p2.getSize() == 3 + data1.getSize());
712 assert(readU8(&p2[0]) == TYPE_RELIABLE);
713 assert(readU16(&p2[1]) == seqnum);
714 assert(readU8(&p2[3]) == data1[0]);
717 struct Handler : public con::PeerHandler
719 Handler(const char *a_name)
725 void peerAdded(con::Peer *peer)
727 infostream<<"Handler("<<name<<")::peerAdded(): "
728 "id="<<peer->id<<std::endl;
732 void deletingPeer(con::Peer *peer, bool timeout)
734 infostream<<"Handler("<<name<<")::deletingPeer(): "
736 <<", timeout="<<timeout<<std::endl;
748 DSTACK("TestConnection::Run");
753 Test some real connections
755 NOTE: This mostly tests the legacy interface.
758 u32 proto_id = 0xad26846a;
760 Handler hand_server("server");
761 Handler hand_client("client");
763 infostream<<"** Creating server Connection"<<std::endl;
764 con::Connection server(proto_id, 512, 5.0, &hand_server);
767 infostream<<"** Creating client Connection"<<std::endl;
768 con::Connection client(proto_id, 512, 5.0, &hand_client);
770 assert(hand_server.count == 0);
771 assert(hand_client.count == 0);
775 Address server_address(127,0,0,1, 30001);
776 infostream<<"** running client.Connect()"<<std::endl;
777 client.Connect(server_address);
781 // Client should not have added client yet
782 assert(hand_client.count == 0);
787 SharedBuffer<u8> data;
788 infostream<<"** running client.Receive()"<<std::endl;
789 u32 size = client.Receive(peer_id, data);
790 infostream<<"** Client received: peer_id="<<peer_id
794 catch(con::NoIncomingDataException &e)
798 // Client should have added server now
799 assert(hand_client.count == 1);
800 assert(hand_client.last_id == 1);
801 // Server should not have added client yet
802 assert(hand_server.count == 0);
809 SharedBuffer<u8> data;
810 infostream<<"** running server.Receive()"<<std::endl;
811 u32 size = server.Receive(peer_id, data);
812 infostream<<"** Server received: peer_id="<<peer_id
816 catch(con::NoIncomingDataException &e)
818 // No actual data received, but the client has
819 // probably been connected
822 // Client should be the same
823 assert(hand_client.count == 1);
824 assert(hand_client.last_id == 1);
825 // Server should have the client
826 assert(hand_server.count == 1);
827 assert(hand_server.last_id == 2);
831 while(client.Connected() == false)
836 SharedBuffer<u8> data;
837 infostream<<"** running client.Receive()"<<std::endl;
838 u32 size = client.Receive(peer_id, data);
839 infostream<<"** Client received: peer_id="<<peer_id
843 catch(con::NoIncomingDataException &e)
854 SharedBuffer<u8> data;
855 infostream<<"** running server.Receive()"<<std::endl;
856 u32 size = server.Receive(peer_id, data);
857 infostream<<"** Server received: peer_id="<<peer_id
861 catch(con::NoIncomingDataException &e)
866 Simple send-receive test
869 /*u8 data[] = "Hello World!";
870 u32 datasize = sizeof(data);*/
871 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
873 infostream<<"** running client.Send()"<<std::endl;
874 client.Send(PEER_ID_SERVER, 0, data, true);
879 SharedBuffer<u8> recvdata;
880 infostream<<"** running server.Receive()"<<std::endl;
881 u32 size = server.Receive(peer_id, recvdata);
882 infostream<<"** Server received: peer_id="<<peer_id
886 assert(memcmp(*data, *recvdata, data.getSize()) == 0);
889 u16 peer_id_client = 2;
892 Send consequent packets in different order
893 Not compatible with new Connection, thus commented out.
896 //u8 data1[] = "hello1";
897 //u8 data2[] = "hello2";
898 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
899 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
901 Address client_address =
902 server.GetPeerAddress(peer_id_client);
904 infostream<<"*** Sending packets in wrong order (2,1,2)"
908 con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
909 u16 sn = ch->next_outgoing_seqnum;
910 ch->next_outgoing_seqnum = sn+1;
911 server.Send(peer_id_client, chn, data2, true);
912 ch->next_outgoing_seqnum = sn;
913 server.Send(peer_id_client, chn, data1, true);
914 ch->next_outgoing_seqnum = sn+1;
915 server.Send(peer_id_client, chn, data2, true);
919 infostream<<"*** Receiving the packets"<<std::endl;
922 SharedBuffer<u8> recvdata;
925 infostream<<"** running client.Receive()"<<std::endl;
927 size = client.Receive(peer_id, recvdata);
928 infostream<<"** Client received: peer_id="<<peer_id
930 <<", data="<<*recvdata
932 assert(size == data1.getSize());
933 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
934 assert(peer_id == PEER_ID_SERVER);
936 infostream<<"** running client.Receive()"<<std::endl;
938 size = client.Receive(peer_id, recvdata);
939 infostream<<"** Client received: peer_id="<<peer_id
941 <<", data="<<*recvdata
943 assert(size == data2.getSize());
944 assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
945 assert(peer_id == PEER_ID_SERVER);
947 bool got_exception = false;
950 infostream<<"** running client.Receive()"<<std::endl;
952 size = client.Receive(peer_id, recvdata);
953 infostream<<"** Client received: peer_id="<<peer_id
955 <<", data="<<*recvdata
958 catch(con::NoIncomingDataException &e)
960 infostream<<"** No incoming data for client"<<std::endl;
961 got_exception = true;
963 assert(got_exception);
968 Send large amounts of packets (infinite test)
969 Commented out because of infinity.
972 infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
975 int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
976 infostream<<"datasize="<<datasize<<std::endl;
977 SharedBuffer<u8> data1(datasize);
978 for(u16 i=0; i<datasize; i++)
981 int sendtimes = myrand_range(1,10);
982 for(int i=0; i<sendtimes; i++){
983 server.Send(peer_id_client, 0, data1, true);
986 infostream<<"sendcount="<<sendcount<<std::endl;
988 //int receivetimes = myrand_range(1,20);
989 int receivetimes = 20;
990 for(int i=0; i<receivetimes; i++){
991 SharedBuffer<u8> recvdata;
994 bool received = false;
996 size = client.Receive(peer_id, recvdata);
998 }catch(con::NoIncomingDataException &e){
1008 const int datasize = 30000;
1009 SharedBuffer<u8> data1(datasize);
1010 for(u16 i=0; i<datasize; i++){
1014 infostream<<"Sending data (size="<<datasize<<"):";
1015 for(int i=0; i<datasize && i<20; i++){
1016 if(i%2==0) DEBUGPRINT(" ");
1017 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1021 infostream<<std::endl;
1023 server.Send(peer_id_client, 0, data1, true);
1027 SharedBuffer<u8> recvdata;
1028 infostream<<"** running client.Receive()"<<std::endl;
1031 bool received = false;
1032 u32 timems0 = porting::getTimeMs();
1034 if(porting::getTimeMs() - timems0 > 5000)
1037 size = client.Receive(peer_id, recvdata);
1039 }catch(con::NoIncomingDataException &e){
1044 infostream<<"** Client received: peer_id="<<peer_id
1048 infostream<<"Received data (size="<<size<<"):";
1049 for(int i=0; i<size && i<20; i++){
1050 if(i%2==0) DEBUGPRINT(" ");
1051 DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
1055 infostream<<std::endl;
1057 assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
1058 assert(peer_id == PEER_ID_SERVER);
1061 // Check peer handlers
1062 assert(hand_client.count == 1);
1063 assert(hand_client.last_id == 1);
1064 assert(hand_server.count == 1);
1065 assert(hand_server.last_id == 2);
1074 infostream<<"Running " #X <<std::endl;\
1078 #define TESTPARAMS(X, ...)\
1081 infostream<<"Running " #X <<std::endl;\
1082 x.Run(__VA_ARGS__);\
1087 DSTACK(__FUNCTION_NAME);
1089 // Create node definitions
1090 IWritableNodeDefManager *nodedef = createNodeDefManager();
1091 content_mapnode_init(nodedef);
1093 infostream<<"run_tests() started"<<std::endl;
1094 TEST(TestUtilities);
1097 TESTPARAMS(TestMapNode, nodedef);
1098 TESTPARAMS(TestVoxelManipulator, nodedef);
1099 //TEST(TestMapBlock);
1100 //TEST(TestMapSector);
1101 if(INTERNET_SIMULATOR == false){
1103 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1104 TEST(TestConnection);
1105 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1107 infostream<<"run_tests() passed"<<std::endl;