mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
added serialisation methods and tests for grouter
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6959 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
2c19810e37
commit
03bd9dbef4
@ -35,8 +35,10 @@ bool RsGRouterItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvs
|
||||
uint32_t RsGRouterPublishKeyItem::serial_size() const
|
||||
{
|
||||
uint32_t s = 8 ; // header
|
||||
s += 4 ; // randomized distance
|
||||
s += 4 ; // diffusion_id
|
||||
s += 20 ; // sha1 for published_key
|
||||
s += 4 ; // service id
|
||||
s += 4 ; // randomized distance
|
||||
s += GetTlvStringSize(description_string) ; // description
|
||||
|
||||
return s ;
|
||||
@ -50,8 +52,10 @@ bool RsGRouterPublishKeyItem::serialise(void *data, uint32_t& pktsize) const
|
||||
return false ;
|
||||
|
||||
/* add mandatory parts first */
|
||||
ok &= setRawUFloat32(data, tlvsize, &offset, randomized_distance);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, diffusion_id);
|
||||
ok &= setRawSha1(data, tlvsize, &offset, published_key);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, service_id);
|
||||
ok &= setRawUFloat32(data, tlvsize, &offset, randomized_distance);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, description_string);
|
||||
|
||||
if (offset != tlvsize)
|
||||
@ -80,7 +84,7 @@ RsItem *RsGRouterSerialiser::deserialise(void *data, uint32_t *pktsize)
|
||||
switch(getRsItemSubType(rstype))
|
||||
{
|
||||
case RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY: return deserialise_RsGRouterPublishKeyItem(data, *pktsize);
|
||||
case RS_PKT_SUBTYPE_GROUTER_DATA: return deserialise_RsGRouterDataItem(data, *pktsize);
|
||||
case RS_PKT_SUBTYPE_GROUTER_DATA: return deserialise_RsGRouterGenericDataItem(data, *pktsize);
|
||||
case RS_PKT_SUBTYPE_GROUTER_ACK: return deserialise_RsGRouterACKItem(data, *pktsize);
|
||||
default:
|
||||
std::cerr << "RsGRouterSerialiser::deserialise(): Could not de-serialise item. SubPacket id = " << std::hex << getRsItemSubType(rstype) << " id = " << rstype << std::dec << std::endl;
|
||||
@ -97,8 +101,10 @@ RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterPublishKeyItem(void *da
|
||||
|
||||
RsGRouterPublishKeyItem *item = new RsGRouterPublishKeyItem() ;
|
||||
|
||||
ok &= getRawUFloat32(data, pktsize, &offset, item->randomized_distance); // file hash
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->diffusion_id); // file hash
|
||||
ok &= getRawSha1(data, pktsize, &offset, item->published_key);
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->service_id); // file hash
|
||||
ok &= getRawUFloat32(data, pktsize, &offset, item->randomized_distance); // file hash
|
||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE,item->description_string);
|
||||
|
||||
if (offset != rssize || !ok)
|
||||
@ -110,20 +116,56 @@ RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterPublishKeyItem(void *da
|
||||
return item;
|
||||
}
|
||||
|
||||
RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterDataItem(void *data, uint32_t pktsize) const
|
||||
RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterGenericDataItem(void *data, uint32_t pktsize) const
|
||||
{
|
||||
std::cerr << "(II) " << __PRETTY_FUNCTION__ << " not yet implemented!" << std::endl;
|
||||
uint32_t offset = 8; // skip the header
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
bool ok = true ;
|
||||
|
||||
RsGRouterGenericDataItem *item = new RsGRouterGenericDataItem() ;
|
||||
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->routing_id); // file hash
|
||||
ok &= getRawSha1(data, pktsize, &offset, item->destination_key);
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->data_size); // file hash
|
||||
|
||||
if( NULL == (item->data_bytes = (uint8_t*)malloc(item->data_size)))
|
||||
{
|
||||
std::cerr << __PRETTY_FUNCTION__ << ": Cannot allocate memory for chunk " << item->data_size << std::endl;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
memcpy(item->data_bytes,&((uint8_t*)data)[offset],item->data_size) ;
|
||||
offset += item->data_size ;
|
||||
|
||||
if (offset != rssize || !ok)
|
||||
{
|
||||
std::cerr << __PRETTY_FUNCTION__ << ": error while deserialising! Item will be dropped." << std::endl;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterACKItem(void *data, uint32_t pktsize) const
|
||||
{
|
||||
std::cerr << "(II) " << __PRETTY_FUNCTION__ << " not yet implemented!" << std::endl;
|
||||
uint32_t offset = 8; // skip the header
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
bool ok = true ;
|
||||
|
||||
RsGRouterACKItem *item = new RsGRouterACKItem() ;
|
||||
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->mid); // file hash
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &item->state); // file hash
|
||||
|
||||
if (offset != rssize || !ok)
|
||||
{
|
||||
std::cerr << __PRETTY_FUNCTION__ << ": error while deserialising! Item will be dropped." << std::endl;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
RsGRouterGenericDataItem *RsGRouterGenericDataItem::duplicate() const
|
||||
{
|
||||
RsGRouterGenericDataItem *item = new RsGRouterGenericDataItem ;
|
||||
@ -139,25 +181,65 @@ RsGRouterGenericDataItem *RsGRouterGenericDataItem::duplicate() const
|
||||
|
||||
uint32_t RsGRouterGenericDataItem::serial_size() const
|
||||
{
|
||||
std::cerr << "(II) " << __PRETTY_FUNCTION__ << " not yet implemented!" << std::endl;
|
||||
return 0 ;
|
||||
uint32_t s = 8 ; // header
|
||||
s += sizeof(GRouterMsgPropagationId) ; // routing id
|
||||
s += 20 ; // sha1 for published_key
|
||||
s += 4 ; // data_size
|
||||
s += data_size ; // data_size
|
||||
|
||||
return s ;
|
||||
}
|
||||
uint32_t RsGRouterACKItem::serial_size() const
|
||||
{
|
||||
std::cerr << "(II) " << __PRETTY_FUNCTION__ << " not yet implemented!" << std::endl;
|
||||
return 0 ;
|
||||
uint32_t s = 8 ; // header
|
||||
s += sizeof(GRouterMsgPropagationId) ; // routing id
|
||||
s += 4 ; // state
|
||||
|
||||
return s ;
|
||||
}
|
||||
bool RsGRouterGenericDataItem::serialise(void *data,uint32_t& size) const
|
||||
{
|
||||
std::cerr << "(II) " << __PRETTY_FUNCTION__ << " not yet implemented!" << std::endl;
|
||||
uint32_t tlvsize,offset=0;
|
||||
bool ok = true;
|
||||
|
||||
if(!serialise_header(data,size,tlvsize,offset))
|
||||
return false ;
|
||||
|
||||
/* add mandatory parts first */
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, routing_id);
|
||||
ok &= setRawSha1(data, tlvsize, &offset, destination_key);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, data_size);
|
||||
|
||||
memcpy(&((uint8_t*)data)[offset],data_bytes,data_size) ;
|
||||
offset += data_size ;
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
std::cerr << "rsfileitemserialiser::serialisedata() size error! " << std::endl;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
bool RsGRouterACKItem::serialise(void *data,uint32_t& size) const
|
||||
{
|
||||
std::cerr << "(II) " << __PRETTY_FUNCTION__ << " not yet implemented!" << std::endl;
|
||||
uint32_t tlvsize,offset=0;
|
||||
bool ok = true;
|
||||
|
||||
if(!serialise_header(data,size,tlvsize,offset))
|
||||
return false ;
|
||||
|
||||
/* add mandatory parts first */
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, mid);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, state);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
std::cerr << "rsfileitemserialiser::serialisedata() size error! " << std::endl;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------//
|
||||
|
@ -92,6 +92,7 @@ class RsGRouterGenericDataItem: public RsGRouterItem
|
||||
{
|
||||
public:
|
||||
RsGRouterGenericDataItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_DATA) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER_DATA) ; }
|
||||
virtual ~RsGRouterGenericDataItem() { free(data_bytes); data_bytes=NULL;}
|
||||
|
||||
virtual bool serialise(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
@ -148,7 +149,7 @@ class RsGRouterSerialiser: public RsSerialType
|
||||
|
||||
private:
|
||||
RsGRouterItem *deserialise_RsGRouterPublishKeyItem(void *data,uint32_t size) const ;
|
||||
RsGRouterItem *deserialise_RsGRouterDataItem(void *data,uint32_t size) const ;
|
||||
RsGRouterItem *deserialise_RsGRouterGenericDataItem(void *data,uint32_t size) const ;
|
||||
RsGRouterItem *deserialise_RsGRouterACKItem(void *data,uint32_t size) const ;
|
||||
};
|
||||
|
||||
|
@ -15,12 +15,14 @@ TESTOBJ += tlvitems_test.o tlvstack_test.o rsserial_test.o
|
||||
TESTOBJ += rstlvwidetest.o tlvrandom_test.o rsturtleitem_test.o
|
||||
TESTOBJ += tlvtypes_test.o support.o distribitem_test.o rsmsgitem_test.o
|
||||
TESTOBJ += rsstatusitem_test.o rsconfigitem_test.o
|
||||
TESTOBJ += rsgrouteritem_test.o
|
||||
|
||||
TESTS = tlvbase_test tlvbase_test2 rstlvwidetest
|
||||
TESTS += tlvitems_test tlvstack_test rstunnelitem_test
|
||||
TESTS += tlvrandom_test rsserial_test rsturtleitem_test
|
||||
TESTS += tlvtypes_test distribitem_test rsmsgitem_test
|
||||
TESTS += rsstatusitem_test rsconfigitem_test
|
||||
TESTOBJ += rsgrouteritem_test
|
||||
|
||||
#rsbaseitem_test
|
||||
|
||||
@ -41,6 +43,9 @@ tlvstack_test : tlvstack_test.o
|
||||
rsserial_test : rsserial_test.o
|
||||
$(CC) $(CFLAGS) -o rsserial_test rsserial_test.o $(OBJ) $(LIBS)
|
||||
|
||||
rsserial_test : rsgrouteritem_test.o
|
||||
$(CC) $(CFLAGS) -o rsgrouteritem_test rsgrouteritem_test.o $(OBJ) $(LIBS)
|
||||
|
||||
rsbaseitem_test : rsbaseitem_test.o
|
||||
$(CC) $(CFLAGS) -o rsbaseitem_test rsbaseitem_test.o $(OBJ) $(LIBS)
|
||||
|
||||
|
113
libretroshare/src/tests/serialiser/rsgrouteritem_test.cc
Normal file
113
libretroshare/src/tests/serialiser/rsgrouteritem_test.cc
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* libretroshare/src/tests/serialiser: msgitem_test.cc
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2010 by Christopher Evi-Parker.
|
||||
*
|
||||
* 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 "util/rsrandom.h"
|
||||
#include "grouter/grouteritems.h"
|
||||
#include "serialiser/rstlvutil.h"
|
||||
#include "util/utest.h"
|
||||
#include "support.h"
|
||||
#include "rsmsgitem_test.h"
|
||||
|
||||
INITTEST();
|
||||
|
||||
RsSerialType* init_item(RsGRouterGenericDataItem& cmi)
|
||||
{
|
||||
cmi.data_size = lrand48()%1000 + 1000 ;
|
||||
cmi.data_bytes = (uint8_t*)malloc(cmi.data_size) ;
|
||||
RSRandom::random_bytes(cmi.data_bytes,cmi.data_size) ;
|
||||
cmi.routing_id = RSRandom::random_u32() ;
|
||||
|
||||
Sha1CheckSum cs ;
|
||||
for(int i=0;i<5;++i) cs.fourbytes[i] = RSRandom::random_u32() ;
|
||||
cmi.destination_key = cs ;
|
||||
|
||||
return new RsGRouterSerialiser();
|
||||
}
|
||||
RsSerialType* init_item(RsGRouterACKItem& cmi)
|
||||
{
|
||||
cmi.mid = RSRandom::random_u32() ;
|
||||
cmi.state = RSRandom::random_u32() ;
|
||||
|
||||
return new RsGRouterSerialiser();
|
||||
}
|
||||
RsSerialType* init_item(RsGRouterPublishKeyItem& cmi)
|
||||
{
|
||||
cmi.diffusion_id = RSRandom::random_u32() ;
|
||||
cmi.service_id = RSRandom::random_u32() ;
|
||||
cmi.randomized_distance = RSRandom::random_f32() ;
|
||||
|
||||
Sha1CheckSum cs ;
|
||||
for(int i=0;i<5;++i) cs.fourbytes[i] = RSRandom::random_u32() ;
|
||||
cmi.published_key = cs ;
|
||||
cmi.description_string = "test key" ;
|
||||
|
||||
return new RsGRouterSerialiser();
|
||||
}
|
||||
bool operator ==(const RsGRouterGenericDataItem& cmiLeft,const RsGRouterGenericDataItem& cmiRight)
|
||||
{
|
||||
if(cmiLeft.routing_id != cmiRight.routing_id) return false;
|
||||
if(cmiLeft.data_size != cmiRight.data_size) return false;
|
||||
if(!(cmiLeft.destination_key == cmiRight.destination_key)) return false;
|
||||
if(memcmp(cmiLeft.data_bytes,cmiRight.data_bytes,cmiLeft.data_size)) return false;
|
||||
|
||||
return true ;
|
||||
}
|
||||
bool operator ==(const RsGRouterPublishKeyItem& cmiLeft,const RsGRouterPublishKeyItem& cmiRight)
|
||||
{
|
||||
if(cmiLeft.diffusion_id != cmiRight.diffusion_id) return false;
|
||||
if(!(cmiLeft.published_key == cmiRight.published_key)) return false;
|
||||
if(cmiLeft.service_id != cmiRight.service_id) return false;
|
||||
// if(cmiLeft.randomized_distance != cmiRight.randomized_distance) return false;
|
||||
if(cmiLeft.description_string != cmiRight.description_string) return false;
|
||||
|
||||
return true ;
|
||||
}
|
||||
bool operator ==(const RsGRouterACKItem& cmiLeft,const RsGRouterACKItem& cmiRight)
|
||||
{
|
||||
if(cmiLeft.mid != cmiRight.mid) return false;
|
||||
if(cmiLeft.state != cmiRight.state) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_RsItem<RsGRouterGenericDataItem >(); REPORT("Serialise/Deserialise RsGRouterGenericDataItem");
|
||||
test_RsItem<RsGRouterACKItem >(); REPORT("Serialise/Deserialise RsGRouterACKItem");
|
||||
test_RsItem<RsGRouterPublishKeyItem >(); REPORT("Serialise/Deserialise RsGRouterPublishKeyItem");
|
||||
|
||||
std::cerr << std::endl;
|
||||
|
||||
FINALREPORT("RsGRouter Tests");
|
||||
|
||||
return TESTRESULT();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
50
libretroshare/src/tests/serialiser/rsgrouteritem_test.h
Normal file
50
libretroshare/src/tests/serialiser/rsgrouteritem_test.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef MSGITEM_TEST_H_
|
||||
#define MSGITEM_TEST_H_
|
||||
|
||||
/*
|
||||
* libretroshare/src/tests/serialiser: msgitem_test.cc
|
||||
*
|
||||
* RetroShare Serialiser.
|
||||
*
|
||||
* Copyright 2010 by Christopher Evi-Parker.
|
||||
*
|
||||
* 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 "serialiser/rsmsgitems.h"
|
||||
|
||||
|
||||
RsSerialType* init_item(RsChatMsgItem& );
|
||||
RsSerialType* init_item(RsChatStatusItem& );
|
||||
RsSerialType* init_item(RsChatAvatarItem& );
|
||||
RsSerialType* init_item(RsMsgItem& );
|
||||
RsSerialType* init_item(RsMsgTagType& );
|
||||
RsSerialType* init_item(RsMsgTags& );
|
||||
RsSerialType* init_item(RsMsgSrcId& );
|
||||
|
||||
bool operator ==(const RsChatMsgItem& ,const RsChatMsgItem& );
|
||||
bool operator ==(const RsChatStatusItem& , const RsChatStatusItem& );
|
||||
bool operator ==(const RsChatAvatarItem&, const RsChatAvatarItem& );
|
||||
bool operator ==(const RsMsgTagType&, const RsMsgTagType& );
|
||||
bool operator ==(const RsMsgTags&, const RsMsgTags& );
|
||||
bool operator ==(const RsMsgItem&, const RsMsgItem& );
|
||||
bool operator ==(const RsMsgSrcId&, const RsMsgSrcId& );
|
||||
|
||||
#endif /* MSGITEM_TEST_H_ */
|
Loading…
Reference in New Issue
Block a user