Added the first top-level serialiser test for rsfileitem.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@265 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2007-11-22 04:04:13 +00:00
parent 8f63b77a57
commit 0a32122ada
6 changed files with 181 additions and 7 deletions

View File

@ -11,10 +11,16 @@ RSOBJ = rsserial.o rsbaseserial.o rstlvbase.o rstlvtypes.o rsbaseitems.o
RSOBJ += rstlvfileitem.o rstlvutil.o
TESTOBJ = tlvbase_test.o tlvbase_test2.o tlvfileitem_test.o
TESTOBJ += tlvitems_test.o tlvstack_test.o tlvconfig_test.o
TESTOBJ += tlvitems_test.o tlvstack_test.o tlvconfig_test.o
TESTOBJ += rsserial_test.o
#rsbaseitem_test.o
TESTS = tlvbase_test tlvbase_test2 tlvfileitem_test
TESTS += tlvitems_test tlvstack_test tlvconfig_test
TESTS += rsserial_test
#rsbaseitem_test
all: librs tests
@ -36,8 +42,11 @@ tlvstack_test : tlvstack_test.o
tlvconfig_test : tlvconfig_test.o
$(CC) $(CFLAGS) -o tlvconfig_test tlvconfig_test.o $(OBJ) $(LIBS)
rsserial_test : rsserial_test.o
$(CC) $(CFLAGS) -o rsserial_test rsserial_test.o $(OBJ) $(LIBS)
rsbaseitem_test : rsbaseitem_test.o
$(CC) $(CFLAGS) -o rsbaseitem_test rsbaseitem_test.o $(OBJ) $(LIBS)
###############################################################

View File

@ -27,6 +27,9 @@
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsbaseitems.h"
#define RSSERIAL_DEBUG 1
#include <iostream>
RsFileItem::~RsFileItem()
{
return;
@ -53,7 +56,7 @@ bool RsFileItemSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsiz
RsFileItem *item = (RsFileItem *) i;
uint32_t tlvsize = size(item);
uint32_t offset = 8;
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */
@ -64,12 +67,16 @@ bool RsFileItemSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsiz
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
std::cerr << "RsFileItemSerialiser::serialise() Header: " << ok << std::endl;
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, item->reqType);
std::cerr << "RsFileItemSerialiser::serialise() RawUInt32: " << ok << std::endl;
ok &= item->file.SetTlv(data, tlvsize, &offset);
std::cerr << "RsFileItemSerialiser::serialise() FileItem: " << ok << std::endl;
return ok;
}

View File

@ -59,7 +59,7 @@ class RsFileItemSerialiser: public RsSerialType
:RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
RS_PKT_TYPE_FILE_ITEM)
{ return; }
virtual ~RsFileItemSerialiser();
virtual ~RsFileItemSerialiser() { return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
@ -90,7 +90,7 @@ class RsFileDataSerialiser: public RsSerialType
:RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_BASE,
RS_PKT_TYPE_FILE_DATA)
{ return; }
virtual ~RsFileDataSerialiser();
virtual ~RsFileDataSerialiser() { return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);

View File

@ -29,6 +29,11 @@
#include <map>
#define RSSERIAL_DEBUG 1
#ifdef RSSERIAL_DEBUG
#include <iostream>
#endif
RsItem::RsItem(uint32_t t)
:type(t)
@ -152,7 +157,7 @@ RsSerialiser::~RsSerialiser()
bool RsSerialiser::addSerialType(RsSerialType *serialiser)
{
uint32_t type = serialiser->PacketId();
uint32_t type = (serialiser->PacketId() & 0xFFFFFF00);
std::map<uint32_t, RsSerialType *>::iterator it;
if (serialisers.end() != (it = serialisers.find(type)))
{
@ -172,7 +177,7 @@ bool RsSerialiser::addSerialType(RsSerialType *serialiser)
uint32_t RsSerialiser::size(RsItem *item)
{
/* find the type */
uint32_t type = item->PacketId();
uint32_t type = (item->PacketId() & 0xFFFFFF00);
std::map<uint32_t, RsSerialType *>::iterator it;
if (serialisers.end() == (it = serialisers.find(type)))
@ -184,6 +189,12 @@ uint32_t RsSerialiser::size(RsItem *item)
return 0;
}
#ifdef RSSERIAL_DEBUG
std::cerr << "RsSerialiser::serialise() Found type: " << type;
std::cerr << std::endl;
#endif
return (it->second)->size(item);
}
@ -202,6 +213,11 @@ bool RsSerialiser::serialise (RsItem *item, void *data, uint32_t *size)
return false;
}
#ifdef RSSERIAL_DEBUG
std::cerr << "RsSerialiser::serialise() Found type: " << type;
std::cerr << std::endl;
#endif
return (it->second)->serialise(item, data, size);
}

View File

@ -0,0 +1,136 @@
/*
* libretroshare/src/serialiser: rsserial_test.cc
*
* RetroShare Serialiser.
*
* Copyright 2007-2008 by Robert Fernie.
*
* 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".
*
*/
/******************************************************************
* tlvfileitem test.
*
*
*/
#include <iostream>
#include <sstream>
#include "serialiser/rsserial.h"
#include "serialiser/rsbaseitems.h"
#include "serialiser/rstlvutil.h"
#include "util/utest.h"
INITTEST();
static int test_RsFileItem();
static int test_RsFileData();
int main()
{
std::cerr << "RsFile[Item/Data/...] Tests" << std::endl;
test_RsFileItem();
//test_RsFileData();
FINALREPORT("RsTlvFile[Item/Data/...] Tests");
return TESTRESULT();
}
int test_RsFileItem()
{
/* make a serialisable FileItem */
RsSerialiser srl;
RsFileItem rsfi;
/* initialise */
rsfi.file.filesize = 101010;
rsfi.file.hash = "ABCDEFEGHE";
rsfi.file.name = "TestFile.txt";
rsfi.file.pop = 12;
rsfi.file.age = 456;
/* attempt to serialise it before we add it to the serialiser */
CHECK(0 == srl.size(&rsfi));
#define MAX_BUFSIZE 16000
char buffer[MAX_BUFSIZE];
uint32_t sersize = MAX_BUFSIZE;
CHECK(false == srl.serialise(&rsfi, (void *) buffer, &sersize));
/* now add to serialiser */
RsFileItemSerialiser *rsfis = new RsFileItemSerialiser();
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);
RsFileItem *outfi = dynamic_cast<RsFileItem *>(output);
CHECK(outfi != NULL);
if (outfi)
{
/* check the data is the same */
CHECK(rsfi.file.filesize == outfi->file.filesize);
CHECK(rsfi.file.hash == outfi->file.hash);
CHECK(rsfi.file.name == outfi->file.name);
CHECK(rsfi.file.path == outfi->file.path);
CHECK(rsfi.file.pop == outfi->file.pop);
CHECK(rsfi.file.age == outfi->file.age);
}
sersize2 = MAX_BUFSIZE;
bool done2 = srl.serialise(outfi, (void *) &(buffer[16*8]), &sersize2);
CHECK(sersize2 == sersize);
displayRawPacket(std::cerr, (void *) buffer, 16 * 8 + sersize2);
REPORT("Serialise/Deserialise RsFileItem");
return 1;
}

View File

@ -101,7 +101,13 @@ bool RsTlvFileItem::SetTlv(void *data, uint32_t size, uint32_t *offset)
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
{
#ifdef TLV_FI_DEBUG
std::cerr << "RsTlvFileItem::SetTlv() Failed size (" << size;
std::cerr << ") < tlvend (" << tlvend << ")" << std::endl;
#endif
return false; /* not enough space */
}
bool ok = true;