]> git.lizzy.rs Git - minetest.git/blobdiff - src/test.cpp
Typo fix in networkprotocol.h
[minetest.git] / src / test.cpp
index ed92ad43a574e0ce7a560cc1abfa2bb3cda29891..d8ab6336ff75e05868bd35cf252b6e58454bd521 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"
@@ -1865,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;
@@ -1878,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);
 
@@ -1967,168 +1986,57 @@ 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);
-
-                       sleep_ms(50);
-
-                       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 << "** Server received: peer_id=" << peer_id
+                                       << ", size=" << size
+                                       << ", data=" << (const char*)pkt->getU8Ptr(0)
+                                       << std::endl;
 
-                       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);
+                       UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
                }
-#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
+
+               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);
 
@@ -2164,7 +2072,7 @@ 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);
                }