Clean up log messages everywhere
[oweals/minetest.git] / src / test.cpp
index ed3df1da8b9871f8e77019ab143cfda4e78c743d..6588f113c4d622975685cf7d29f43f48c85948cd 100644 (file)
@@ -31,8 +31,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <sstream>
 #include "porting.h"
 #include "content_mapnode.h"
+#include "nodedef.h"
 #include "mapsector.h"
 #include "settings.h"
+#include "log.h"
 
 /*
        Asserts that the exception occurs
@@ -45,13 +47,71 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        assert(exception_thrown);\
 }
 
+/*
+       A few item and node definitions for those tests that need them
+*/
+
+#define CONTENT_STONE 0
+#define CONTENT_GRASS 0x800
+
+void define_some_nodes(IWritableItemDefManager *idef, IWritableNodeDefManager *ndef)
+{
+       content_t i;
+       ItemDefinition itemdef;
+       ContentFeatures f;
+
+       /*
+               Stone
+       */
+       i = CONTENT_STONE;
+       itemdef = ItemDefinition();
+       itemdef.type = ITEM_NODE;
+       itemdef.name = "default:stone";
+       itemdef.description = "Stone";
+       itemdef.groups["cracky"] = 3;
+       itemdef.inventory_image = "[inventorycube"
+               "{default_stone.png"
+               "{default_stone.png"
+               "{default_stone.png";
+       f = ContentFeatures();
+       f.name = itemdef.name;
+       for(int i = 0; i < 6; i++)
+               f.tname_tiles[i] = "default_stone.png";
+       f.is_ground_content = true;
+       idef->registerItem(itemdef);
+       ndef->set(i, f);
+
+       /*
+               Grass
+       */
+       i = CONTENT_GRASS;
+       itemdef = ItemDefinition();
+       itemdef.type = ITEM_NODE;
+       itemdef.name = "default:dirt_with_grass";
+       itemdef.description = "Dirt with grass";
+       itemdef.groups["crumbly"] = 3;
+       itemdef.inventory_image = "[inventorycube"
+               "{default_grass.png"
+               "{default_dirt.png&default_grass_side.png"
+               "{default_dirt.png&default_grass_side.png";
+       f = ContentFeatures();
+       f.name = itemdef.name;
+       f.tname_tiles[0] = "default_grass.png";
+       f.tname_tiles[1] = "default_dirt.png";
+       for(int i = 2; i < 6; i++)
+               f.tname_tiles[i] = "default_dirt.png^default_grass_side.png";
+       f.is_ground_content = true;
+       idef->registerItem(itemdef);
+       ndef->set(i, f);
+}
+
 struct TestUtilities
 {
        void Run()
        {
-               /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
-               dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
-               dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
+               /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
+               infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
+               infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
                assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
                assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
                assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
@@ -94,7 +154,120 @@ struct TestSettings
                assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
        }
 };
+
+struct TestSerialization
+{
+       // To be used like this:
+       //   mkstr("Some\0string\0with\0embedded\0nuls")
+       // since std::string("...") doesn't work as expected in that case.
+       template<size_t N> std::string mkstr(const char (&s)[N])
+       {
+               return std::string(s, N - 1);
+       }
+
+       void Run()
+       {
+               // Tests some serialization primitives
+
+               assert(serializeString("") == mkstr("\0\0"));
+               assert(serializeWideString(L"") == mkstr("\0\0"));
+               assert(serializeLongString("") == mkstr("\0\0\0\0"));
+               assert(serializeJsonString("") == "\"\"");
                
+               std::string teststring = "Hello world!";
+               assert(serializeString(teststring) ==
+                       mkstr("\0\14Hello world!"));
+               assert(serializeWideString(narrow_to_wide(teststring)) ==
+                       mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
+               assert(serializeLongString(teststring) ==
+                       mkstr("\0\0\0\14Hello world!"));
+               assert(serializeJsonString(teststring) ==
+                       "\"Hello world!\"");
+
+               std::string teststring2;
+               std::wstring teststring2_w;
+               std::string teststring2_w_encoded;
+               {
+                       std::ostringstream tmp_os;
+                       std::wostringstream tmp_os_w;
+                       std::ostringstream tmp_os_w_encoded;
+                       for(int i = 0; i < 256; i++)
+                       {
+                               tmp_os<<(char)i;
+                               tmp_os_w<<(wchar_t)i;
+                               tmp_os_w_encoded<<(char)0<<(char)i;
+                       }
+                       teststring2 = tmp_os.str();
+                       teststring2_w = tmp_os_w.str();
+                       teststring2_w_encoded = tmp_os_w_encoded.str();
+               }
+               assert(serializeString(teststring2) ==
+                       mkstr("\1\0") + teststring2);
+               assert(serializeWideString(teststring2_w) ==
+                       mkstr("\1\0") + teststring2_w_encoded);
+               assert(serializeLongString(teststring2) ==
+                       mkstr("\0\0\1\0") + teststring2);
+               // MSVC fails when directly using "\\\\"
+               std::string backslash = "\\";
+               assert(serializeJsonString(teststring2) ==
+                       mkstr("\"") +
+                       "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
+                       "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
+                       "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
+                       "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
+                       " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
+                       "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
+                       backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
+                       "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
+                       "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
+                       "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
+                       "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
+                       "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
+                       "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
+                       "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
+                       "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
+                       "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
+                       "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
+                       "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
+                       "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
+                       "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
+                       "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
+                       "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
+                       "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
+                       "\"");
+
+               {
+                       std::istringstream is(serializeString(teststring2), std::ios::binary);
+                       assert(deSerializeString(is) == teststring2);
+                       assert(!is.eof());
+                       is.get();
+                       assert(is.eof());
+               }
+               {
+                       std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
+                       assert(deSerializeWideString(is) == teststring2_w);
+                       assert(!is.eof());
+                       is.get();
+                       assert(is.eof());
+               }
+               {
+                       std::istringstream is(serializeLongString(teststring2), std::ios::binary);
+                       assert(deSerializeLongString(is) == teststring2);
+                       assert(!is.eof());
+                       is.get();
+                       assert(is.eof());
+               }
+               {
+                       std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
+                       //dstream<<serializeJsonString(deSerializeJsonString(is));
+                       assert(deSerializeJsonString(is) == teststring2);
+                       assert(!is.eof());
+                       is.get();
+                       assert(is.eof());
+               }
+       }
+};
+
 struct TestCompress
 {
        void Run()
@@ -112,13 +285,13 @@ struct TestCompress
 
                std::string str_out = os.str();
                
-               dstream<<"str_out.size()="<<str_out.size()<<std::endl;
-               dstream<<"TestCompress: 1,5,5,1 -> ";
+               infostream<<"str_out.size()="<<str_out.size()<<std::endl;
+               infostream<<"TestCompress: 1,5,5,1 -> ";
                for(u32 i=0; i<str_out.size(); i++)
                {
-                       dstream<<(u32)str_out[i]<<",";
+                       infostream<<(u32)str_out[i]<<",";
                }
-               dstream<<std::endl;
+               infostream<<std::endl;
 
                assert(str_out.size() == 10);
 
@@ -139,12 +312,12 @@ struct TestCompress
                decompress(is, os2, 0);
                std::string str_out2 = os2.str();
 
-               dstream<<"decompress: ";
+               infostream<<"decompress: ";
                for(u32 i=0; i<str_out2.size(); i++)
                {
-                       dstream<<(u32)str_out2[i]<<",";
+                       infostream<<(u32)str_out2[i]<<",";
                }
-               dstream<<std::endl;
+               infostream<<std::endl;
 
                assert(str_out2.size() == fromdata.getSize());
 
@@ -168,13 +341,13 @@ struct TestCompress
 
                std::string str_out = os.str();
                
-               dstream<<"str_out.size()="<<str_out.size()<<std::endl;
-               dstream<<"TestCompress: 1,5,5,1 -> ";
+               infostream<<"str_out.size()="<<str_out.size()<<std::endl;
+               infostream<<"TestCompress: 1,5,5,1 -> ";
                for(u32 i=0; i<str_out.size(); i++)
                {
-                       dstream<<(u32)str_out[i]<<",";
+                       infostream<<(u32)str_out[i]<<",";
                }
-               dstream<<std::endl;
+               infostream<<std::endl;
 
                /*assert(str_out.size() == 10);
 
@@ -195,12 +368,12 @@ struct TestCompress
                decompress(is, os2, SER_FMT_VER_HIGHEST);
                std::string str_out2 = os2.str();
 
-               dstream<<"decompress: ";
+               infostream<<"decompress: ";
                for(u32 i=0; i<str_out2.size(); i++)
                {
-                       dstream<<(u32)str_out2[i]<<",";
+                       infostream<<(u32)str_out2[i]<<",";
                }
-               dstream<<std::endl;
+               infostream<<std::endl;
 
                assert(str_out2.size() == fromdata.getSize());
 
@@ -215,26 +388,26 @@ struct TestCompress
 
 struct TestMapNode
 {
-       void Run()
+       void Run(INodeDefManager *nodedef)
        {
                MapNode n;
 
                // Default values
                assert(n.getContent() == CONTENT_AIR);
-               assert(n.getLight(LIGHTBANK_DAY) == 0);
-               assert(n.getLight(LIGHTBANK_NIGHT) == 0);
+               assert(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
+               assert(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
                
                // Transparency
                n.setContent(CONTENT_AIR);
-               assert(n.light_propagates() == true);
-               n.setContent(CONTENT_STONE);
-               assert(n.light_propagates() == false);
+               assert(nodedef->get(n).light_propagates == true);
+               n.setContent(LEGN(nodedef, "CONTENT_STONE"));
+               assert(nodedef->get(n).light_propagates == false);
        }
 };
 
 struct TestVoxelManipulator
 {
-       void Run()
+       void Run(INodeDefManager *nodedef)
        {
                /*
                        VoxelArea
@@ -258,12 +431,12 @@ struct TestVoxelManipulator
 
                assert(aa.size() == results.size());
                
-               dstream<<"Result of diff:"<<std::endl;
+               infostream<<"Result of diff:"<<std::endl;
                for(core::list<VoxelArea>::Iterator
                                i = aa.begin(); i != aa.end(); i++)
                {
-                       i->print(dstream);
-                       dstream<<std::endl;
+                       i->print(infostream);
+                       infostream<<std::endl;
                        
                        s32 j = results.linear_search(*i);
                        assert(j != -1);
@@ -277,99 +450,30 @@ struct TestVoxelManipulator
                
                VoxelManipulator v;
 
-               v.print(dstream);
+               v.print(infostream, nodedef);
 
-               dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
+               infostream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
                
-               v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
+               v.setNodeNoRef(v3s16(-1,0,-1), MapNode(CONTENT_GRASS));
 
-               v.print(dstream);
+               v.print(infostream, nodedef);
 
-               assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
+               assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
 
-               dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
+               infostream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
 
                EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
 
-               v.print(dstream);
+               v.print(infostream, nodedef);
 
-               dstream<<"*** Adding area ***"<<std::endl;
+               infostream<<"*** Adding area ***"<<std::endl;
 
                v.addArea(a);
                
-               v.print(dstream);
+               v.print(infostream, nodedef);
 
-               assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
+               assert(v.getNode(v3s16(-1,0,-1)).getContent() == CONTENT_GRASS);
                EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
-
-#if 0
-               /*
-                       Water stuff
-               */
-
-               v.clear();
-
-               const char *content =
-                       "#...######  "
-                       "#...##..##  "
-                       "#........ .."
-                       "############"
-
-                       "#...######  "
-                       "#...##..##  "
-                       "#........#  "
-                       "############"
-               ;
-
-               v3s16 size(12, 4, 2);
-               VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
-               
-               const char *p = content;
-               for(s16 z=0; z<size.Z; z++)
-               for(s16 y=size.Y-1; y>=0; y--)
-               for(s16 x=0; x<size.X; x++)
-               {
-                       MapNode n;
-                       //n.pressure = size.Y - y;
-                       if(*p == '#')
-                               n.setContent(CONTENT_STONE);
-                       else if(*p == '.')
-                               n.setContent(CONTENT_WATER);
-                       else if(*p == ' ')
-                               n.setContent(CONTENT_AIR);
-                       else
-                               assert(0);
-                       v.setNode(v3s16(x,y,z), n);
-                       p++;
-               }
-
-               v.print(dstream, VOXELPRINT_WATERPRESSURE);
-               
-               core::map<v3s16, u8> active_nodes;
-               v.updateAreaWaterPressure(area, active_nodes);
-
-               v.print(dstream, VOXELPRINT_WATERPRESSURE);
-               
-               //s16 highest_y = -32768;
-               /*
-                       NOTE: These are commented out because this behaviour is changed
-                             all the time
-               */
-               //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
-               //assert(highest_y == 3);
-               /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
-               //assert(highest_y == 3);*/
-               
-               active_nodes.clear();
-               active_nodes[v3s16(9,1,0)] = 1;
-               //v.flowWater(active_nodes, 0, true, 1000);
-               v.flowWater(active_nodes, 0, false, 1000);
-               
-               dstream<<"Final result of flowWater:"<<std::endl;
-               v.print(dstream, VOXELPRINT_WATERPRESSURE);
-#endif
-               
-               //assert(0);
        }
 };
 
@@ -460,9 +564,9 @@ struct TestMapBlock
                                MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
                
                // Changed flag should be initially set
-               assert(b.getChangedFlag() == true);
-               b.resetChangedFlag();
-               assert(b.getChangedFlag() == false);
+               assert(b.getModified() == MOD_STATE_WRITE_NEEDED);
+               b.resetModified();
+               assert(b.getModified() == MOD_STATE_CLEAN);
 
                // All nodes should have been set to
                // .d=CONTENT_IGNORE and .getLight() = 0
@@ -765,15 +869,15 @@ struct TestConnection
                assert(readU8(&p1.data[6]) == channel);
                assert(readU8(&p1.data[7]) == data1[0]);
                
-               //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
+               //infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
 
                SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
 
-               /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
+               /*infostream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
                                <<data1.getSize()<<std::endl;
-               dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
+               infostream<<"readU8(&p2[3])="<<readU8(&p2[3])
                                <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
-               dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
+               infostream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
 
                assert(p2.getSize() == 3 + data1.getSize());
                assert(readU8(&p2[0]) == TYPE_RELIABLE);
@@ -791,14 +895,14 @@ struct TestConnection
                }
                void peerAdded(con::Peer *peer)
                {
-                       dstream<<"Handler("<<name<<")::peerAdded(): "
+                       infostream<<"Handler("<<name<<")::peerAdded(): "
                                        "id="<<peer->id<<std::endl;
                        last_id = peer->id;
                        count++;
                }
                void deletingPeer(con::Peer *peer, bool timeout)
                {
-                       dstream<<"Handler("<<name<<")::deletingPeer(): "
+                       infostream<<"Handler("<<name<<")::deletingPeer(): "
                                        "id="<<peer->id
                                        <<", timeout="<<timeout<<std::endl;
                        last_id = peer->id;
@@ -818,17 +922,20 @@ struct TestConnection
 
                /*
                        Test some real connections
+
+                       NOTE: This mostly tests the legacy interface.
                */
+
                u32 proto_id = 0xad26846a;
 
                Handler hand_server("server");
                Handler hand_client("client");
                
-               dstream<<"** Creating server Connection"<<std::endl;
+               infostream<<"** Creating server Connection"<<std::endl;
                con::Connection server(proto_id, 512, 5.0, &hand_server);
                server.Serve(30001);
                
-               dstream<<"** Creating client Connection"<<std::endl;
+               infostream<<"** Creating client Connection"<<std::endl;
                con::Connection client(proto_id, 512, 5.0, &hand_client);
 
                assert(hand_server.count == 0);
@@ -837,24 +944,43 @@ struct TestConnection
                sleep_ms(50);
                
                Address server_address(127,0,0,1, 30001);
-               dstream<<"** running client.Connect()"<<std::endl;
+               infostream<<"** running client.Connect()"<<std::endl;
                client.Connect(server_address);
 
                sleep_ms(50);
                
+               // Client should not have added client yet
+               assert(hand_client.count == 0);
+               
+               try
+               {
+                       u16 peer_id;
+                       SharedBuffer<u8> data;
+                       infostream<<"** running client.Receive()"<<std::endl;
+                       u32 size = client.Receive(peer_id, data);
+                       infostream<<"** Client received: peer_id="<<peer_id
+                                       <<", size="<<size
+                                       <<std::endl;
+               }
+               catch(con::NoIncomingDataException &e)
+               {
+               }
+
                // Client should have added server now
                assert(hand_client.count == 1);
                assert(hand_client.last_id == 1);
-               // But server should not have added client
+               // Server should not have added client yet
                assert(hand_server.count == 0);
+               
+               sleep_ms(50);
 
                try
                {
                        u16 peer_id;
-                       u8 data[100];
-                       dstream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, data, 100);
-                       dstream<<"** Server received: peer_id="<<peer_id
+                       SharedBuffer<u8> data;
+                       infostream<<"** running server.Receive()"<<std::endl;
+                       u32 size = server.Receive(peer_id, data);
+                       infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
                }
@@ -878,10 +1004,10 @@ struct TestConnection
                        try
                        {
                                u16 peer_id;
-                               u8 data[100];
-                               dstream<<"** running client.Receive()"<<std::endl;
-                               u32 size = client.Receive(peer_id, data, 100);
-                               dstream<<"** Client received: peer_id="<<peer_id
+                               SharedBuffer<u8> data;
+                               infostream<<"** running client.Receive()"<<std::endl;
+                               u32 size = client.Receive(peer_id, data);
+                               infostream<<"** Client received: peer_id="<<peer_id
                                                <<", size="<<size
                                                <<std::endl;
                        }
@@ -896,57 +1022,61 @@ struct TestConnection
                try
                {
                        u16 peer_id;
-                       u8 data[100];
-                       dstream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, data, 100);
-                       dstream<<"** Server received: peer_id="<<peer_id
+                       SharedBuffer<u8> data;
+                       infostream<<"** running server.Receive()"<<std::endl;
+                       u32 size = server.Receive(peer_id, data);
+                       infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
                }
                catch(con::NoIncomingDataException &e)
                {
                }
-
+#if 1
+               /*
+                       Simple send-receive test
+               */
                {
                        /*u8 data[] = "Hello World!";
                        u32 datasize = sizeof(data);*/
                        SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
 
-                       dstream<<"** running client.Send()"<<std::endl;
+                       infostream<<"** running client.Send()"<<std::endl;
                        client.Send(PEER_ID_SERVER, 0, data, true);
 
                        sleep_ms(50);
 
                        u16 peer_id;
-                       u8 recvdata[100];
-                       dstream<<"** running server.Receive()"<<std::endl;
-                       u32 size = server.Receive(peer_id, recvdata, 100);
-                       dstream<<"** Server received: peer_id="<<peer_id
+                       SharedBuffer<u8> recvdata;
+                       infostream<<"** running server.Receive()"<<std::endl;
+                       u32 size = server.Receive(peer_id, recvdata);
+                       infostream<<"** Server received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<", data="<<*data
                                        <<std::endl;
-                       assert(memcmp(*data, recvdata, data.getSize()) == 0);
+                       assert(memcmp(*data, *recvdata, data.getSize()) == 0);
                }
-               
+#endif
                u16 peer_id_client = 2;
-
+#if 0
+               /*
+                       Send consequent packets in different order
+                       Not compatible with new Connection, thus commented out.
+               */
                {
-                       /*
-                               Send consequent packets in different order
-                       */
                        //u8 data1[] = "hello1";
                        //u8 data2[] = "hello2";
                        SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
                        SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
 
                        Address client_address =
-                                       server.GetPeer(peer_id_client)->address;
+                                       server.GetPeerAddress(peer_id_client);
                        
-                       dstream<<"*** Sending packets in wrong order (2,1,2)"
+                       infostream<<"*** Sending packets in wrong order (2,1,2)"
                                        <<std::endl;
                        
                        u8 chn = 0;
-                       con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
+                       con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
                        u16 sn = ch->next_outgoing_seqnum;
                        ch->next_outgoing_seqnum = sn+1;
                        server.Send(peer_id_client, chn, data2, true);
@@ -957,52 +1087,94 @@ struct TestConnection
 
                        sleep_ms(50);
 
-                       dstream<<"*** Receiving the packets"<<std::endl;
+                       infostream<<"*** Receiving the packets"<<std::endl;
 
                        u16 peer_id;
-                       u8 recvdata[20];
+                       SharedBuffer<u8> recvdata;
                        u32 size;
 
-                       dstream<<"** running client.Receive()"<<std::endl;
+                       infostream<<"** running client.Receive()"<<std::endl;
                        peer_id = 132;
-                       size = client.Receive(peer_id, recvdata, 20);
-                       dstream<<"** Client received: peer_id="<<peer_id
+                       size = client.Receive(peer_id, recvdata);
+                       infostream<<"** Client received: peer_id="<<peer_id
                                        <<", size="<<size
-                                       <<", data="<<recvdata
+                                       <<", data="<<*recvdata
                                        <<std::endl;
                        assert(size == data1.getSize());
-                       assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
+                       assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                        
-                       dstream<<"** running client.Receive()"<<std::endl;
+                       infostream<<"** running client.Receive()"<<std::endl;
                        peer_id = 132;
-                       size = client.Receive(peer_id, recvdata, 20);
-                       dstream<<"** Client received: peer_id="<<peer_id
+                       size = client.Receive(peer_id, recvdata);
+                       infostream<<"** Client received: peer_id="<<peer_id
                                        <<", size="<<size
-                                       <<", data="<<recvdata
+                                       <<", data="<<*recvdata
                                        <<std::endl;
                        assert(size == data2.getSize());
-                       assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
+                       assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                        
                        bool got_exception = false;
                        try
                        {
-                               dstream<<"** running client.Receive()"<<std::endl;
+                               infostream<<"** running client.Receive()"<<std::endl;
                                peer_id = 132;
-                               size = client.Receive(peer_id, recvdata, 20);
-                               dstream<<"** Client received: peer_id="<<peer_id
+                               size = client.Receive(peer_id, recvdata);
+                               infostream<<"** Client received: peer_id="<<peer_id
                                                <<", size="<<size
-                                               <<", data="<<recvdata
+                                               <<", data="<<*recvdata
                                                <<std::endl;
                        }
                        catch(con::NoIncomingDataException &e)
                        {
-                               dstream<<"** No incoming data for client"<<std::endl;
+                               infostream<<"** No incoming data for client"<<std::endl;
                                got_exception = true;
                        }
                        assert(got_exception);
                }
+#endif
+#if 0
+               /*
+                       Send large amounts of packets (infinite test)
+                       Commented out because of infinity.
+               */
+               {
+                       infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
+                       int sendcount = 0;
+                       for(;;){
+                               int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
+                               infostream<<"datasize="<<datasize<<std::endl;
+                               SharedBuffer<u8> data1(datasize);
+                               for(u16 i=0; i<datasize; i++)
+                                       data1[i] = i/4;
+                               
+                               int sendtimes = myrand_range(1,10);
+                               for(int i=0; i<sendtimes; i++){
+                                       server.Send(peer_id_client, 0, data1, true);
+                                       sendcount++;
+                               }
+                               infostream<<"sendcount="<<sendcount<<std::endl;
+                               
+                               //int receivetimes = myrand_range(1,20);
+                               int receivetimes = 20;
+                               for(int i=0; i<receivetimes; i++){
+                                       SharedBuffer<u8> recvdata;
+                                       u16 peer_id = 132;
+                                       u16 size = 0;
+                                       bool received = false;
+                                       try{
+                                               size = client.Receive(peer_id, recvdata);
+                                               received = true;
+                                       }catch(con::NoIncomingDataException &e){
+                                       }
+                               }
+                       }
+               }
+#endif
+               /*
+                       Send a large packet
+               */
                {
                        const int datasize = 30000;
                        SharedBuffer<u8> data1(datasize);
@@ -1010,37 +1182,54 @@ struct TestConnection
                                data1[i] = i/4;
                        }
 
-                       dstream<<"Sending data (size="<<datasize<<"):";
+                       infostream<<"Sending data (size="<<datasize<<"):";
                        for(int i=0; i<datasize && i<20; i++){
-                               if(i%2==0) DEBUGPRINT(" ");
-                               DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
+                               if(i%2==0) infostream<<" ";
+                               char buf[10];
+                               snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
+                               infostream<<buf;
                        }
                        if(datasize>20)
-                               dstream<<"...";
-                       dstream<<std::endl;
+                               infostream<<"...";
+                       infostream<<std::endl;
                        
                        server.Send(peer_id_client, 0, data1, true);
 
-                       sleep_ms(50);
+                       //sleep_ms(3000);
                        
-                       u8 recvdata[datasize + 1000];
-                       dstream<<"** running client.Receive()"<<std::endl;
+                       SharedBuffer<u8> recvdata;
+                       infostream<<"** running client.Receive()"<<std::endl;
                        u16 peer_id = 132;
-                       u16 size = client.Receive(peer_id, recvdata, datasize + 1000);
-                       dstream<<"** Client received: peer_id="<<peer_id
+                       u16 size = 0;
+                       bool received = false;
+                       u32 timems0 = porting::getTimeMs();
+                       for(;;){
+                               if(porting::getTimeMs() - timems0 > 5000 || received)
+                                       break;
+                               try{
+                                       size = client.Receive(peer_id, recvdata);
+                                       received = true;
+                               }catch(con::NoIncomingDataException &e){
+                               }
+                               sleep_ms(10);
+                       }
+                       assert(received);
+                       infostream<<"** Client received: peer_id="<<peer_id
                                        <<", size="<<size
                                        <<std::endl;
 
-                       dstream<<"Received data (size="<<size<<"):";
+                       infostream<<"Received data (size="<<size<<"): ";
                        for(int i=0; i<size && i<20; i++){
-                               if(i%2==0) DEBUGPRINT(" ");
-                               DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
+                               if(i%2==0) infostream<<" ";
+                               char buf[10];
+                               snprintf(buf, 10, "%.2X", ((int)(recvdata[i]))&0xff);
+                               infostream<<buf;
                        }
                        if(size>20)
-                               dstream<<"...";
-                       dstream<<std::endl;
+                               infostream<<"...";
+                       infostream<<std::endl;
 
-                       assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
+                       assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
                        assert(peer_id == PEER_ID_SERVER);
                }
                
@@ -1057,19 +1246,33 @@ struct TestConnection
 #define TEST(X)\
 {\
        X x;\
-       dstream<<"Running " #X <<std::endl;\
+       infostream<<"Running " #X <<std::endl;\
        x.Run();\
 }
 
+#define TESTPARAMS(X, ...)\
+{\
+       X x;\
+       infostream<<"Running " #X <<std::endl;\
+       x.Run(__VA_ARGS__);\
+}
+
 void run_tests()
 {
        DSTACK(__FUNCTION_NAME);
-       dstream<<"run_tests() started"<<std::endl;
+       
+       // Create item and node definitions
+       IWritableItemDefManager *idef = createItemDefManager();
+       IWritableNodeDefManager *ndef = createNodeDefManager();
+       define_some_nodes(idef, ndef);
+
+       infostream<<"run_tests() started"<<std::endl;
        TEST(TestUtilities);
        TEST(TestSettings);
        TEST(TestCompress);
-       TEST(TestMapNode);
-       TEST(TestVoxelManipulator);
+       TEST(TestSerialization);
+       TESTPARAMS(TestMapNode, ndef);
+       TESTPARAMS(TestVoxelManipulator, ndef);
        //TEST(TestMapBlock);
        //TEST(TestMapSector);
        if(INTERNET_SIMULATOR == false){
@@ -1078,6 +1281,6 @@ void run_tests()
                TEST(TestConnection);
                dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
        }
-       dstream<<"run_tests() passed"<<std::endl;
+       infostream<<"run_tests() passed"<<std::endl;
 }