From eb21343685c9ee7a5d2df6ce142a7d93cf3227a8 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 14 Oct 2010 21:45:53 +0000 Subject: [PATCH] added a torture test for file creators that also are used as providers git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3672 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/tests/ft/Makefile | 8 +- .../tests/ft/ftcrossprovidercreatortest.cc | 164 ++++++++++++++++++ 2 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 libretroshare/src/tests/ft/ftcrossprovidercreatortest.cc diff --git a/libretroshare/src/tests/ft/Makefile b/libretroshare/src/tests/ft/Makefile index 735b1da9f..d2ef7f29e 100644 --- a/libretroshare/src/tests/ft/Makefile +++ b/libretroshare/src/tests/ft/Makefile @@ -7,10 +7,9 @@ RS_TOP_DIR = ../.. include $(RS_TOP_DIR)/tests/scripts/config.mk ############################################################### -TESTOBJ = ftfileprovidertest.o ftfilecreatortest.o ftextralisttest.o ftdataplextest.o fttransfermoduletest.o ftcrc32test.o ftcontrollertest.o ftserver1test.o ftserver2test.o ftserver3test.o +TESTOBJ = ftfileprovidertest.o ftfilecreatortest.o ftextralisttest.o ftdataplextest.o fttransfermoduletest.o ftcrc32test.o ftcrossprovidercreatortest.o ftcontrollertest.o ftserver1test.o ftserver2test.o ftserver3test.o - -TESTS = ftfileprovidertest ftfilecreatortest ftextralisttest ftdataplextest fttransfermoduletest ftcrc32test ftcontrollertest ftserver1test ftserver2test fttransfermoduletest ftserver3test +TESTS = ftfileprovidertest ftfilecreatortest ftextralisttest ftdataplextest fttransfermoduletest ftcrc32test ftcrossprovidercreatortest ftcontrollertest ftserver1test ftserver2test fttransfermoduletest ftserver3test all: tests @@ -20,6 +19,9 @@ ftcontrollertest : ftcontrollertest.o ftfilecreatortest : ftfilecreatortest.o $(CC) $(CFLAGS) -o ftfilecreatortest ftfilecreatortest.o $(LIBS) +ftcrossprovidercreatortest : ftcrossprovidercreatortest.o + $(CC) $(CFLAGS) -o ftcrossprovidercreatortest ftcrossprovidercreatortest.o $(LIBS) + ftfileprovidertest : ftfileprovidertest.o $(CC) $(CFLAGS) -o ftfileprovidertest ftfileprovidertest.o $(LIBS) diff --git a/libretroshare/src/tests/ft/ftcrossprovidercreatortest.cc b/libretroshare/src/tests/ft/ftcrossprovidercreatortest.cc new file mode 100644 index 000000000..fcc4f3cb6 --- /dev/null +++ b/libretroshare/src/tests/ft/ftcrossprovidercreatortest.cc @@ -0,0 +1,164 @@ +#include +#include "ft/ftfilecreator.h" + +#include "util/utest.h" +#include "util/rsdir.h" +#include + +#include "util/rswin.h" +#include "ft/ftserver.h" + +INITTEST(); + +static void createTmpFile(const std::string& name,uint64_t size,std::string& hash) ; +static void transfer(ftFileProvider *server, const std::string& server_peer_id,ftFileCreator *client,const std::string& client_peer_id) ; + +int main() +{ + // We create two file creators and feed them with a file both from elsewhere and from each others, as file providers. + // This test should check that no data race occurs while reading/writting data and exchanging chunks. + // + + // 1 - Create a temporary file, compute its hash + // + std::string fname = "source_tmp.bin" ; + std::string fname_copy_1 = "copy_1_tmp.bin" ; + std::string fname_copy_2 = "copy_2_tmp.bin" ; + uint64_t size = 55766278 ; + std::string hash = "" ; + + createTmpFile(fname,size,hash) ; + + // 2 - Create one file provider for this file, and 2 file creators + // + + ftFileProvider *server = new ftFileProvider(fname,size,hash) ; + ftFileCreator *client1 = new ftFileCreator(fname_copy_1,size,hash) ; + ftFileCreator *client2 = new ftFileCreator(fname_copy_2,size,hash) ; + + // 3 - Exchange chunks, and build two copies of the file. + // + std::string peer_id_1("client peer id 1") ; + std::string peer_id_2("client peer id 2") ; + std::string server_id("server peer id") ; + + while( !(client1->finished() && client2->finished())) + { + // 1 - select a random client, get the data from him. + + ftFileProvider *tmpserver ; + ftFileCreator *tmpclient ; + std::string tmpserver_pid ; + std::string tmpclient_pid ; + + // choose client and server, randomly + // + if(lrand48()&1) + { + tmpclient = client1 ; + tmpclient_pid = peer_id_1 ; + + if(lrand48()&1) + { + tmpserver = client2 ; + tmpserver_pid = peer_id_2 ; + } + else + { + tmpserver = server ; + tmpserver_pid = server_id ; + } + } + else + { + tmpclient = client2 ; + tmpclient_pid = peer_id_2 ; + + if(lrand48()&1) + { + tmpserver = client1 ; + tmpserver_pid = peer_id_1 ; + } + else + { + tmpserver = server ; + tmpserver_pid = server_id ; + } + + } + + // 2 - transfer from the server to the client. + + transfer(tmpserver,tmpserver_pid,tmpclient,tmpclient_pid) ; + } + + // hash the received data + + std::string hash1 ; client1->hashReceivedData(hash1) ; + std::string hash2 ; client2->hashReceivedData(hash2) ; + + std::cout << "Hash = " << hash << std::endl ; + std::cout << "Hash1 = " << hash1 << std::endl ; + std::cout << "Hash2 = " << hash2 << std::endl ; + + if(hash != hash1) FAILED("hashs 0/1 defer after transfer !") ; + if(hash != hash2) FAILED("hashs 0/2 defer after transfer !") ; + if(hash1 != hash2) FAILED("hashs 1/2 defer after transfer !") ; + + FINALREPORT("File transfer test"); + + return TESTRESULT(); +} + +void transfer(ftFileProvider *server, const std::string& server_peer_id,ftFileCreator *client,const std::string& client_peer_id) +{ + uint32_t size_hint = 128 + (lrand48()%8000) ; + uint64_t offset = 0; + uint32_t chunk_size = 0; + bool toOld = false ; + + if(! client->getMissingChunk(server_peer_id, size_hint, offset, chunk_size, toOld)) + return ; + + void *data = malloc(chunk_size) ; + + if( server->getFileData(client_peer_id,offset,chunk_size,data) ) + client->addFileData(offset,chunk_size,data) ; + + free(data) ; +} + +void createTmpFile(const std::string& name,uint64_t S,std::string& hash) +{ + FILE *tmpf = fopen("source_tmp.bin","w") ; + + // write 1MB chunks + uint64_t C = 1024*1024 ; + uint8_t *data = new uint8_t[C] ; + + for(uint i=0;i<=S/C;++i) + { + int Cp = C ; + if(i==S/C) + { + if(S%C>0) + Cp = S%C ; + else + break ; + } + + for(int k=0;k