Fix some memory leaks on packet sending.
[oweals/minetest.git] / src / test.cpp
index fa18989e5dd1b5745cb59945b056f4d58fca9378..3b7c75c6e2bdac3090bdc71c6909ffccbceeebfd 100644 (file)
@@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "player.h"
 #include "main.h"
 #include "socket.h"
-#include "connection.h"
+#include "network/connection.h"
 #include "serialization.h"
 #include "voxel.h"
 #include "collision.h"
@@ -42,7 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/numeric.h"
 #include "util/serialize.h"
 #include "noise.h" // PseudoRandom used for random data for compression
-#include "clientserver.h" // LATEST_PROTOCOL_VERSION
+#include "network/networkprotocol.h" // LATEST_PROTOCOL_VERSION
 #include <algorithm>
 
 /*
@@ -59,7 +59,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define UTEST(x, fmt, ...)\
 {\
        if(!(x)){\
-               LOGLINEF(LMT_ERROR, "Test (%s) failed: " fmt, #x, ##__VA_ARGS__);\
+               dstream << "Test (" #x ") failed: " fmt << std::endl; \
                test_failed = true;\
        }\
 }
@@ -189,7 +189,7 @@ struct TestUtilities: public TestBase
                str_replace(test_str, "there", "world");
                UASSERT(test_str == "Hello world");
                test_str = "ThisAisAaAtest";
-               str_replace_char(test_str, 'A', ' ');
+               str_replace(test_str, 'A', ' ');
                UASSERT(test_str == "This is a test");
                UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
                UASSERT(string_allowed("123", "abcdefghijklmno") == false);
@@ -273,8 +273,8 @@ struct TestPath: public TestBase
                                expected fs::PathStartsWith results
                                0 = returns false
                                1 = returns true
-                               2 = returns false on windows, false elsewhere
-                               3 = returns true on windows, true elsewhere
+                               2 = returns false on windows, true elsewhere
+                               3 = returns true on windows, false elsewhere
                                4 = returns true if and only if
                                    FILESYS_CASE_INSENSITIVE is true
                        */
@@ -855,9 +855,8 @@ struct TestMapNode: public TestBase
 {
        void Run(INodeDefManager *nodedef)
        {
-               MapNode n;
+               MapNode n(CONTENT_AIR);
 
-               // Default values
                UASSERT(n.getContent() == CONTENT_AIR);
                UASSERT(n.getLight(LIGHTBANK_DAY, nodedef) == 0);
                UASSERT(n.getLight(LIGHTBANK_NIGHT, nodedef) == 0);
@@ -1670,11 +1669,26 @@ struct TestSocket: public TestBase
        void Run()
        {
                const int port = 30003;
-               Address address(0,0,0,0, port);
+               Address address(0, 0, 0, 0, port);
+               Address bind_addr(0, 0, 0, 0, port);
                Address address6((IPv6AddressBytes*) NULL, port);
 
+               /*
+                * Try to use the bind_address for servers with no localhost address
+                * For example: FreeBSD jails
+                */
+               std::string bind_str = g_settings->get("bind_address");
+               try {
+                       bind_addr.Resolve(bind_str.c_str());
+
+                       if (!bind_addr.isIPv6()) {
+                               address = bind_addr;
+                       }
+               } catch (ResolveError &e) {
+               }
+
                // IPv6 socket test
-               {
+               if (g_settings->getBool("enable_ipv6")) {
                        UDPSocket socket6;
 
                        if (!socket6.init(true, true)) {
@@ -1710,7 +1724,7 @@ struct TestSocket: public TestBase
                                        UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
                                                        Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
                                }
-                               catch (SendFailedException e) {
+                               catch (SendFailedException &e) {
                                        errorstream << "IPv6 support enabled but not available!"
                                                    << std::endl;
                                }
@@ -1723,7 +1737,15 @@ struct TestSocket: public TestBase
                        socket.Bind(address);
 
                        const char sendbuffer[] = "hello world!";
-                       socket.Send(Address(127, 0, 0 ,1, port), sendbuffer, sizeof(sendbuffer));
+                       /*
+                        * If there is a bind address, use it.
+                        * It's useful in container environments
+                        */
+                       if (address != Address(0, 0, 0, 0, port)) {
+                               socket.Send(address, sendbuffer, sizeof(sendbuffer));
+                       }
+                       else
+                               socket.Send(Address(127, 0, 0 ,1, port), sendbuffer, sizeof(sendbuffer));
 
                        sleep_ms(50);
 
@@ -1735,8 +1757,15 @@ struct TestSocket: public TestBase
                        }
                        //FIXME: This fails on some systems
                        UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
-                       UASSERT(sender.getAddress().sin_addr.s_addr ==
-                                       Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
+
+                       if (address != Address(0, 0, 0, 0, port)) {
+                               UASSERT(sender.getAddress().sin_addr.s_addr ==
+                                               address.getAddress().sin_addr.s_addr);
+                       }
+                       else {
+                               UASSERT(sender.getAddress().sin_addr.s_addr ==
+                                               Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
+                       }
                }
        }
 };
@@ -1836,9 +1865,24 @@ struct TestConnection: public TestBase
                Handler hand_server("server");
                Handler hand_client("client");
 
+               Address address(0, 0, 0, 0, 30001);
+               Address bind_addr(0, 0, 0, 0, 30001);
+               /*
+                * Try to use the bind_address for servers with no localhost address
+                * For example: FreeBSD jails
+                */
+               std::string bind_str = g_settings->get("bind_address");
+               try {
+                       bind_addr.Resolve(bind_str.c_str());
+
+                       if (!bind_addr.isIPv6()) {
+                               address = bind_addr;
+                       }
+               } catch (ResolveError &e) {
+               }
+
                infostream<<"** Creating server Connection"<<std::endl;
                con::Connection server(proto_id, 512, 5.0, false, &hand_server);
-               Address address(0,0,0,0, 30001);
                server.Serve(address);
 
                infostream<<"** Creating client Connection"<<std::endl;
@@ -1849,7 +1893,11 @@ struct TestConnection: public TestBase
 
                sleep_ms(50);
 
-               Address server_address(127,0,0,1, 30001);
+               Address server_address(127, 0, 0, 1, 30001);
+               if (address != Address(0, 0, 0, 0, 30001)) {
+                       server_address = bind_addr;
+               }
+
                infostream<<"** running client.Connect()"<<std::endl;
                client.Connect(server_address);
 
@@ -1938,168 +1986,59 @@ struct TestConnection: public TestBase
                catch(con::NoIncomingDataException &e)
                {
                }
-#if 1
+
                /*
                        Simple send-receive test
                */
                {
-                       /*u8 data[] = "Hello World!";
-                       u32 datasize = sizeof(data);*/
-                       SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
+                       NetworkPacket* pkt = new NetworkPacket((u8*) "Hello World !", 14, 0);
+
+                       SharedBuffer<u8> sentdata = pkt->oldForgePacket();
 
                        infostream<<"** running client.Send()"<<std::endl;
-                       client.Send(PEER_ID_SERVER, 0, data, true);
+                       client.Send(PEER_ID_SERVER, 0, pkt, true);
 
                        sleep_ms(50);
 
                        u16 peer_id;
                        SharedBuffer<u8> recvdata;
-                       infostream<<"** running server.Receive()"<<std::endl;
+                       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;
-                       UASSERT(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.
-               */
-               {
-                       //u8 data1[] = "hello1";
-                       //u8 data2[] = "hello2";
-                       SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
-                       SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
-
-                       Address client_address =
-                                       server.GetPeerAddress(peer_id_client);
-
-                       infostream<<"*** Sending packets in wrong order (2,1,2)"
-                                       <<std::endl;
-
-                       u8 chn = 0;
-                       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);
-                       ch->next_outgoing_seqnum = sn;
-                       server.Send(peer_id_client, chn, data1, true);
-                       ch->next_outgoing_seqnum = sn+1;
-                       server.Send(peer_id_client, chn, data2, true);
+                       infostream << "** Server received: peer_id=" << peer_id
+                                       << ", size=" << size
+                                       << ", data=" << (const char*)pkt->getU8Ptr(0)
+                                       << std::endl;
 
-                       sleep_ms(50);
+                       UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
 
-                       infostream<<"*** Receiving the packets"<<std::endl;
-
-                       u16 peer_id;
-                       SharedBuffer<u8> recvdata;
-                       u32 size;
-
-                       infostream<<"** running client.Receive()"<<std::endl;
-                       peer_id = 132;
-                       size = client.Receive(peer_id, recvdata);
-                       infostream<<"** Client received: peer_id="<<peer_id
-                                       <<", size="<<size
-                                       <<", data="<<*recvdata
-                                       <<std::endl;
-                       UASSERT(size == data1.getSize());
-                       UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
-                       UASSERT(peer_id == PEER_ID_SERVER);
-
-                       infostream<<"** running client.Receive()"<<std::endl;
-                       peer_id = 132;
-                       size = client.Receive(peer_id, recvdata);
-                       infostream<<"** Client received: peer_id="<<peer_id
-                                       <<", size="<<size
-                                       <<", data="<<*recvdata
-                                       <<std::endl;
-                       UASSERT(size == data2.getSize());
-                       UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
-                       UASSERT(peer_id == PEER_ID_SERVER);
-
-                       bool got_exception = false;
-                       try
-                       {
-                               infostream<<"** running client.Receive()"<<std::endl;
-                               peer_id = 132;
-                               size = client.Receive(peer_id, recvdata);
-                               infostream<<"** Client received: peer_id="<<peer_id
-                                               <<", size="<<size
-                                               <<", data="<<*recvdata
-                                               <<std::endl;
-                       }
-                       catch(con::NoIncomingDataException &e)
-                       {
-                               infostream<<"** No incoming data for client"<<std::endl;
-                               got_exception = true;
-                       }
-                       UASSERT(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){
-                                       }
-                               }
-                       }
+                       delete pkt;
                }
-#endif
+
+               u16 peer_id_client = 2;
                /*
                        Send a large packet
                */
                {
                        const int datasize = 30000;
-                       SharedBuffer<u8> data1(datasize);
+                       NetworkPacket* pkt = new NetworkPacket(0, datasize);
                        for(u16 i=0; i<datasize; i++){
-                               data1[i] = i/4;
+                               *pkt << (u8) i/4;
                        }
 
                        infostream<<"Sending data (size="<<datasize<<"):";
                        for(int i=0; i<datasize && i<20; i++){
                                if(i%2==0) infostream<<" ";
                                char buf[10];
-                               snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
+                               snprintf(buf, 10, "%.2X", ((int)((const char*)pkt->getU8Ptr(0))[i])&0xff);
                                infostream<<buf;
                        }
                        if(datasize>20)
                                infostream<<"...";
                        infostream<<std::endl;
 
-                       server.Send(peer_id_client, 0, data1, true);
+                       SharedBuffer<u8> sentdata = pkt->oldForgePacket();
+
+                       server.Send(peer_id_client, 0, pkt, true);
 
                        //sleep_ms(3000);
 
@@ -2135,8 +2074,10 @@ struct TestConnection: public TestBase
                                infostream<<"...";
                        infostream<<std::endl;
 
-                       UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
+                       UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
                        UASSERT(peer_id == PEER_ID_SERVER);
+
+                       delete pkt;
                }
 
                // Check peer handlers
@@ -2195,9 +2136,9 @@ void run_tests()
        TEST(TestCollision);
        if(INTERNET_SIMULATOR == false){
                TEST(TestSocket);
-               dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
+               dout_con << "=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ===" << std::endl;
                TEST(TestConnection);
-               dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
+               dout_con << "=== END RUNNING UNIT TESTS FOR CONNECTION ===" << std::endl;
        }
 
        log_set_lev_silence(LMT_ERROR, false);
@@ -2205,13 +2146,13 @@ void run_tests()
        delete idef;
        delete ndef;
 
-       if(tests_failed == 0){
-               infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
-               infostream<<"run_tests() passed."<<std::endl;
+       if(tests_failed == 0) {
+               actionstream << "run_tests(): " << tests_failed << " / " << tests_run << " tests failed." << std::endl;
+               actionstream << "run_tests() passed." << std::endl;
                return;
        } else {
-               errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
-               errorstream<<"run_tests() aborting."<<std::endl;
+               errorstream << "run_tests(): " << tests_failed << " / " << tests_run << " tests failed." << std::endl;
+               errorstream << "run_tests() aborting." << std::endl;
                abort();
        }
 }