From d5d3fb7860c303a77ce93fea725bcb33221f54dd Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 20 Jun 2010 21:05:18 +0000 Subject: [PATCH] added unitary tests for serialization of turtle items git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3181 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/tests/serialiser/Makefile | 6 + .../src/tests/serialiser/rsturtleitem_test.cc | 301 ++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 libretroshare/src/tests/serialiser/rsturtleitem_test.cc diff --git a/libretroshare/src/tests/serialiser/Makefile b/libretroshare/src/tests/serialiser/Makefile index e90358f10..859e97290 100644 --- a/libretroshare/src/tests/serialiser/Makefile +++ b/libretroshare/src/tests/serialiser/Makefile @@ -10,10 +10,13 @@ include $(RS_TOP_DIR)/tests/scripts/config.mk TESTOBJ = tlvbase_test.o tlvbase_test2.o tlvfileitem_test.o TESTOBJ += tlvitems_test.o tlvstack_test.o tlvconfig_test.o TESTOBJ += rsserial_test.o rstlvwidetest.o tlvrandom_test.o +TESTOBJ += rsturtleitem_test.o TESTS = tlvbase_test tlvbase_test2 tlvfileitem_test TESTS += tlvitems_test tlvstack_test tlvconfig_test TESTS += rstlvwidetest tlvrandom_test rsserial_test +TESTS += rsturtleitem_test + #rsbaseitem_test all: tests @@ -48,6 +51,9 @@ rstlvwidetest : rstlvwidetest.o tlvrandom_test : tlvrandom_test.o $(CC) $(CFLAGS) -o tlvrandom_test tlvrandom_test.o $(OBJ) $(LIBS) +rsturtleitem_test : rsturtleitem_test.o + $(CC) $(CFLAGS) -o rsturtleitem_test rsturtleitem_test.o $(OBJ) $(LIBS) + ############################################################### include $(RS_TOP_DIR)/scripts/rules.mk diff --git a/libretroshare/src/tests/serialiser/rsturtleitem_test.cc b/libretroshare/src/tests/serialiser/rsturtleitem_test.cc new file mode 100644 index 000000000..9f99a2709 --- /dev/null +++ b/libretroshare/src/tests/serialiser/rsturtleitem_test.cc @@ -0,0 +1,301 @@ + +/* + * libretroshare/src/serialiser: rsturtleitems_test.cc + * + * RetroShare Serialiser. + * + * Copyright 2007-2008 by Cyril Soler + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +/****************************************************************** + */ + +#include +#include +#include "turtle/rsturtleitem.h" +#include +#include "util/utest.h" + +INITTEST(); + +void init_item(CompressedChunkMap& map) +{ + map._map.clear() ; + for(uint i=0;i<15;++i) + map._map.push_back(lrand48()) ; +} +bool operator==(const CompressedChunkMap& m1,const CompressedChunkMap& m2) +{ + if(m1._map.size() != m2._map.size()) return false ; + + for(uint32_t i=0;i::const_iterator i1(it1.result.begin()) ; + std::list::const_iterator i2(it2.result.begin()) ; + + for(;i1!=it1.result.end() && i2!=it2.result.end();++i1,++i2) + if( !(*i1 == *i2)) + return false ; + + return true ; +} + +template int test_RsTurtleItem() +{ + /* make a serialisable RsTurtleItem */ + + RsSerialiser srl; + + /* initialise */ + T rsfi ; + init_item(rsfi) ; + + /* attempt to serialise it before we add it to the serialiser */ + + CHECK(0 == srl.size(&rsfi)); + + static const uint32_t MAX_BUFSIZE = 16000 ; + + char *buffer = new char[MAX_BUFSIZE]; + uint32_t sersize = MAX_BUFSIZE; + + CHECK(false == srl.serialise(&rsfi, (void *) buffer, &sersize)); + + /* now add to serialiser */ + + RsTurtleSerialiser *rsfis = new RsTurtleSerialiser(); + srl.addSerialType(rsfis); + + uint32_t size = srl.size(&rsfi); + bool done = srl.serialise(&rsfi, (void *) buffer, &sersize); + + std::cerr << "test_RsFileItem() size: " << size << std::endl; + std::cerr << "test_RsFileItem() done: " << done << std::endl; + std::cerr << "test_RsFileItem() sersize: " << sersize << std::endl; + + std::cerr << "test_RsFileItem() serialised:" << std::endl; + displayRawPacket(std::cerr, (void *) buffer, sersize); + + CHECK(done == true); + + uint32_t sersize2 = sersize; + RsItem *output = srl.deserialise((void *) buffer, &sersize2); + + CHECK(output != NULL); + CHECK(sersize2 == sersize); + + T *outfi = dynamic_cast(output); + + CHECK(outfi != NULL); + + if (outfi) + CHECK(*outfi == rsfi) ; + + sersize2 = MAX_BUFSIZE; + bool done2 = srl.serialise(outfi, (void *) &(buffer[16*8]), &sersize2); + + CHECK(done2) ; + CHECK(sersize2 == sersize); + + displayRawPacket(std::cerr, (void *) buffer, 16 * 8 + sersize2); + + delete[] buffer ; + return 1; +} + +int main() +{ + std::cerr << "RsTurtleItem Tests" << std::endl; + + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleFileMapRequestItem"); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleFileMapItem"); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleFileDataItem"); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleFileRequestItem"); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleTunnelOkItem "); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleOpenTunnelItem "); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleSearchResultItem "); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleStringSearchRequestItem "); + test_RsTurtleItem(); REPORT("Serialise/Deserialise RsTurtleRegExpSearchRequestItem "); + + FINALREPORT("RsturtleItem Tests"); + + return TESTRESULT(); +} + +