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
This commit is contained in:
csoler 2010-06-20 21:05:18 +00:00
parent f9e9bf58a5
commit d5d3fb7860
2 changed files with 307 additions and 0 deletions

View File

@ -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

View File

@ -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 <iostream>
#include <sstream>
#include "turtle/rsturtleitem.h"
#include <serialiser/rstlvutil.h>
#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<m1._map.size();++i)
if(m1._map[i] != m2._map[i])
return false ;
return true ;
}
bool operator==(const RsTurtleFileMapRequestItem& it1,const RsTurtleFileMapRequestItem& it2)
{
if(it1.direction != it2.direction) return false ;
if(it1.tunnel_id != it2.tunnel_id) return false ;
return true ;
}
void init_item(RsTurtleFileMapRequestItem& item)
{
item.direction = 1 ;
item.tunnel_id = 0x4ff823e2 ;
}
bool operator==(const RsTurtleFileMapItem& it1,const RsTurtleFileMapItem& it2)
{
if(it1.direction != it2.direction) return false ;
if(it1.tunnel_id != it2.tunnel_id) return false ;
if(!(it1.compressed_map == it2.compressed_map)) return false ;
return true ;
}
void init_item(RsTurtleFileMapItem& item)
{
item.direction = 1 ;
item.tunnel_id = 0xf48fe232 ;
init_item(item.compressed_map) ;
}
void init_item(RsTurtleFileDataItem& item)
{
static const uint32_t S = 3456 ;
item.tunnel_id = 0x33eef982 ;
item.chunk_offset = 0x25ea228437894379ull ;
item.chunk_size = S ;
item.chunk_data = new unsigned char[S] ;
for(uint32_t i=0;i<S;++i)
((unsigned char *)item.chunk_data)[i] = lrand48()%256 ;
}
bool operator==(const RsTurtleFileDataItem& i1,const RsTurtleFileDataItem& i2)
{
if(i1.tunnel_id != i2.tunnel_id) return false ;
if(i1.chunk_offset != i2.chunk_offset) return false ;
if(i1.chunk_size != i2.chunk_size) return false ;
for(uint32_t i=0;i<i1.chunk_size;++i)
if( ((unsigned char *)i1.chunk_data)[i] != ((unsigned char *)i2.chunk_data)[i])
return false ;
return true ;
}
void init_item(RsTurtleFileRequestItem& item)
{
item.tunnel_id = lrand48() ;
item.chunk_offset = 0x25ea228437894379ull ;
item.chunk_size = lrand48() ;
}
bool operator==(const RsTurtleFileRequestItem& it1,const RsTurtleFileRequestItem& it2)
{
if(it1.tunnel_id != it2.tunnel_id) return false ;
if(it1.chunk_offset != it2.chunk_offset) return false ;
if(it1.chunk_size != it2.chunk_size) return false ;
return true ;
}
void init_item(RsTurtleTunnelOkItem& item)
{
item.tunnel_id = lrand48() ;
item.request_id = lrand48() ;
}
bool operator==(const RsTurtleTunnelOkItem& it1,const RsTurtleTunnelOkItem& it2)
{
if(it1.tunnel_id != it2.tunnel_id) return false ;
if(it1.request_id != it2.request_id) return false ;
return true ;
}
void init_item(RsTurtleOpenTunnelItem& item)
{
item.depth = lrand48() ;
item.request_id = lrand48() ;
item.partial_tunnel_id = lrand48() ;
item.file_hash = std::string("c0edcfecc0844ef175d61dd589ab288d262b6bc8") ;
}
bool operator==(const RsTurtleOpenTunnelItem& it1,const RsTurtleOpenTunnelItem& it2)
{
if(it1.depth != it2.depth) return false ;
if(it1.request_id != it2.request_id) return false ;
if(it1.partial_tunnel_id != it2.partial_tunnel_id) return false ;
if(it1.file_hash != it2.file_hash) return false ;
return true ;
}
void init_item(RsTurtleRegExpSearchRequestItem& item)
{
item.request_id = lrand48() ;
item.depth = lrand48() ;
item.expr._tokens.clear() ;
item.expr._ints.clear() ;
item.expr._strings.clear() ;
for(uint32_t i=0;i<10u;++i) item.expr._tokens.push_back(lrand48()%8) ;
for(uint32_t i=0;i<6u;++i) item.expr._ints.push_back(lrand48()) ;
for(uint32_t i=0;i<8u;++i) item.expr._strings.push_back("test string") ;
}
bool operator==(const RsTurtleRegExpSearchRequestItem& it1,const RsTurtleRegExpSearchRequestItem& it2)
{
if(it1.request_id != it2.request_id) return false ;
if(it1.depth != it2.depth) return false ;
if(it1.expr._tokens.size() != it2.expr._tokens.size()) return false ;
if(it1.expr._ints.size() != it2.expr._ints.size()) return false ;
if(it1.expr._strings.size() != it2.expr._strings.size()) return false ;
for(uint32_t i=0;i<it1.expr._tokens.size();++i) if(it1.expr._tokens[i] != it2.expr._tokens[i]) return false ;
for(uint32_t i=0;i<it1.expr._ints.size();++i) if(it1.expr._ints[i] != it2.expr._ints[i]) return false ;
for(uint32_t i=0;i<it1.expr._strings.size();++i) if(it1.expr._strings[i] != it2.expr._strings[i]) return false ;
return true ;
}
void init_item(RsTurtleStringSearchRequestItem& item)
{
item.request_id = lrand48() ;
item.depth = lrand48() ;
item.match_string = std::string("432hkjfdsjkhjk43r3fw") ;
}
bool operator==(const RsTurtleStringSearchRequestItem& it1,const RsTurtleStringSearchRequestItem& it2)
{
if(it1.request_id != it2.request_id) return false ;
if(it1.depth != it2.depth) return false ;
if(it1.match_string != it2.match_string)
return false ;
return true ;
}
void init_item(TurtleFileInfo& info)
{
info.hash = "3f753e8ac3b94ab9fddfad94480f747bf4418370";
info.name = "toto.png";
info.size = 0x3392085443897ull ;
}
bool operator==(const TurtleFileInfo& it1,const TurtleFileInfo& it2)
{
if(it1.hash != it2.hash) return false ;
if(it1.name != it2.name) return false ;
if(it1.size != it2.size) return false ;
return true ;
}
void init_item(RsTurtleSearchResultItem& item)
{
item.depth = lrand48() ;
item.request_id = lrand48() ;
item.result.clear() ;
static const uint32_t S = 10 ;
for(uint32_t i=0;i<S;++i)
{
TurtleFileInfo f;
init_item(f) ;
item.result.push_back(f) ;
}
}
bool operator==(const RsTurtleSearchResultItem& it1,const RsTurtleSearchResultItem& it2)
{
if(it1.request_id != it2.request_id) return false ;
if(it1.depth != it2.depth) return false ;
std::list<TurtleFileInfo>::const_iterator i1(it1.result.begin()) ;
std::list<TurtleFileInfo>::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<class T> 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<T *>(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<RsTurtleFileMapRequestItem>(); REPORT("Serialise/Deserialise RsTurtleFileMapRequestItem");
test_RsTurtleItem<RsTurtleFileMapItem >(); REPORT("Serialise/Deserialise RsTurtleFileMapItem");
test_RsTurtleItem<RsTurtleFileDataItem >(); REPORT("Serialise/Deserialise RsTurtleFileDataItem");
test_RsTurtleItem<RsTurtleFileRequestItem >(); REPORT("Serialise/Deserialise RsTurtleFileRequestItem");
test_RsTurtleItem<RsTurtleTunnelOkItem >(); REPORT("Serialise/Deserialise RsTurtleTunnelOkItem ");
test_RsTurtleItem<RsTurtleOpenTunnelItem >(); REPORT("Serialise/Deserialise RsTurtleOpenTunnelItem ");
test_RsTurtleItem<RsTurtleSearchResultItem >(); REPORT("Serialise/Deserialise RsTurtleSearchResultItem ");
test_RsTurtleItem<RsTurtleStringSearchRequestItem >(); REPORT("Serialise/Deserialise RsTurtleStringSearchRequestItem ");
test_RsTurtleItem<RsTurtleRegExpSearchRequestItem >(); REPORT("Serialise/Deserialise RsTurtleRegExpSearchRequestItem ");
FINALREPORT("RsturtleItem Tests");
return TESTRESULT();
}