From ff1f521a2bb7f80ecca121d0a62d4575106e534f Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 20 Jun 2010 21:36:53 +0000 Subject: [PATCH] added tests for rstunnel item serialization git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3182 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/tests/serialiser/Makefile | 7 +- .../src/tests/serialiser/rstunnelitem_test.cc | 163 ++++++++++++++++++ .../src/tests/serialiser/rsturtleitem_test.cc | 29 ++-- 3 files changed, 184 insertions(+), 15 deletions(-) create mode 100644 libretroshare/src/tests/serialiser/rstunnelitem_test.cc diff --git a/libretroshare/src/tests/serialiser/Makefile b/libretroshare/src/tests/serialiser/Makefile index 859e97290..f870cace4 100644 --- a/libretroshare/src/tests/serialiser/Makefile +++ b/libretroshare/src/tests/serialiser/Makefile @@ -10,12 +10,12 @@ 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 +TESTOBJ += rsturtleitem_test.o rstunnelitem_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 +TESTS += rsturtleitem_test rstunnelitem_test #rsbaseitem_test @@ -54,6 +54,9 @@ tlvrandom_test : tlvrandom_test.o rsturtleitem_test : rsturtleitem_test.o $(CC) $(CFLAGS) -o rsturtleitem_test rsturtleitem_test.o $(OBJ) $(LIBS) +rstunnelitem_test : rstunnelitem_test.o + $(CC) $(CFLAGS) -o rstunnelitem_test rstunnelitem_test.o $(OBJ) $(LIBS) + ############################################################### include $(RS_TOP_DIR)/scripts/rules.mk diff --git a/libretroshare/src/tests/serialiser/rstunnelitem_test.cc b/libretroshare/src/tests/serialiser/rstunnelitem_test.cc new file mode 100644 index 000000000..7030d208d --- /dev/null +++ b/libretroshare/src/tests/serialiser/rstunnelitem_test.cc @@ -0,0 +1,163 @@ + +/* + * libretroshare/src/serialiser: rstunnelitems_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 +#include +#include "util/utest.h" + +INITTEST(); + +void init_item(RsTunnelDataItem& item) +{ + uint32_t S = lrand48()%20000 ; + item.encoded_data = malloc(S) ; + item.encoded_data_len = S ; + for(uint32_t i=0;i int test_RsItem() +{ + /* 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 = 22000 ; + + char *buffer = new char[MAX_BUFSIZE]; + uint32_t sersize = MAX_BUFSIZE; + + CHECK(false == srl.serialise(&rsfi, (void *) buffer, &sersize)); + + /* now add to serialiser */ + + RsTunnelSerialiser *rsfis = new RsTunnelSerialiser(); + srl.addSerialType(rsfis); + + uint32_t size = srl.size(&rsfi); + bool done = srl.serialise(&rsfi, (void *) buffer, &sersize); + + std::cerr << "test_Item() size: " << size << std::endl; + std::cerr << "test_Item() done: " << done << std::endl; + std::cerr << "test_Item() sersize: " << sersize << std::endl; + + std::cerr << "test_Item() 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() +{ +// srand48(1) ; // always use the same random numbers + std::cerr << "RsTurtleItem Tests" << std::endl; + + for(uint32_t i=0;i<20;++i) + { + test_RsItem(); REPORT("Serialise/Deserialise RsTunnelDataItem"); + test_RsItem(); REPORT("Serialise/Deserialise RsTunnelHandshakeItem"); + } + + FINALREPORT("RstunnelItem Tests"); + + return TESTRESULT(); +} + + diff --git a/libretroshare/src/tests/serialiser/rsturtleitem_test.cc b/libretroshare/src/tests/serialiser/rsturtleitem_test.cc index 9f99a2709..96074d93b 100644 --- a/libretroshare/src/tests/serialiser/rsturtleitem_test.cc +++ b/libretroshare/src/tests/serialiser/rsturtleitem_test.cc @@ -245,11 +245,11 @@ template int test_RsTurtleItem() 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_Item() size: " << size << std::endl; + std::cerr << "test_Item() done: " << done << std::endl; + std::cerr << "test_Item() sersize: " << sersize << std::endl; - std::cerr << "test_RsFileItem() serialised:" << std::endl; + std::cerr << "test_Item() serialised:" << std::endl; displayRawPacket(std::cerr, (void *) buffer, sersize); CHECK(done == true); @@ -283,15 +283,18 @@ 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 "); + for(uint32_t i=0;i<20;++i) + { + 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");