mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-07 06:02:41 -04:00
Major improvements to the serialiser to make it work with retroshare.
This is still a first draft - the message types will surely change. -corrected ids and added service classes. -Added disc/msg/chat/cache/file messages -Extended serialiser to handle service extensions. -corrected IpAddrPort code. -More debugging code. -Added some tests. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@270 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
9f76b1a313
commit
e8ccb0b427
22 changed files with 3101 additions and 333 deletions
|
@ -36,11 +36,13 @@
|
|||
#include "serialiser/rstlvbase.h"
|
||||
#include "serialiser/rstlvutil.h"
|
||||
#include "util/utest.h"
|
||||
#include "util/rsnet.h"
|
||||
|
||||
INITTEST();
|
||||
|
||||
static int test_RsTlvString();
|
||||
static int test_RsTlvUInt32();
|
||||
static int test_RsTlvIPAddr();
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -48,6 +50,7 @@ int main()
|
|||
|
||||
test_RsTlvString();
|
||||
test_RsTlvUInt32();
|
||||
test_RsTlvIPAddr();
|
||||
|
||||
FINALREPORT("RsTlvBase Tests");
|
||||
|
||||
|
@ -87,6 +90,7 @@ int test_OneString(std::string input, uint16_t type)
|
|||
std::string OutString;
|
||||
|
||||
std::cerr << "test_OneString() Testing ... Print/Serialise/Deserialise";
|
||||
std::cerr << std::endl;
|
||||
/* start with SetTlvString() */
|
||||
|
||||
uint16_t initsize = GetTlvStringSize(input);
|
||||
|
@ -117,5 +121,71 @@ int test_OneString(std::string input, uint16_t type)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int test_IpAddr(struct sockaddr_in *addr, uint16_t type);
|
||||
|
||||
static int test_RsTlvIPAddr()
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
|
||||
inet_aton("10.0.0.111", &(addr.sin_addr));
|
||||
addr.sin_port = htons(1111);
|
||||
|
||||
test_IpAddr(&addr, 1234);
|
||||
|
||||
inet_aton("255.255.255.1", &(addr.sin_addr));
|
||||
addr.sin_port = htons(9999);
|
||||
|
||||
test_IpAddr(&addr, 1234);
|
||||
|
||||
inet_aton("128.255.255.1", &(addr.sin_addr));
|
||||
addr.sin_port = htons(0);
|
||||
|
||||
test_IpAddr(&addr, 1234);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_IpAddr(struct sockaddr_in *addr, uint16_t type)
|
||||
{
|
||||
/* an array to work from */
|
||||
char tlvdata[2048];
|
||||
struct sockaddr_in outaddr;
|
||||
|
||||
std::cerr << "test_IpAddr() Testing ... Print/Serialise/Deserialise";
|
||||
std::cerr << std::endl;
|
||||
/* start with SetTlvString() */
|
||||
|
||||
uint16_t initsize = GetTlvIpAddrPortV4Size();
|
||||
uint32_t outOffset = 0;
|
||||
uint32_t inOffset = 0;
|
||||
|
||||
std::cerr << "Serialising IPAddr: " << inet_ntoa(addr->sin_addr) << std::endl;
|
||||
std::cerr << " Port : " << ntohs(addr->sin_port) << std::endl;
|
||||
|
||||
CHECK(SetTlvIpAddrPortV4((void*)tlvdata, 2048, &outOffset, type, addr));
|
||||
std::cerr << "Init Size: " << initsize << std::endl;
|
||||
std::cerr << "Serialised Size: " << outOffset << std::endl;
|
||||
displayRawPacket(std::cerr, tlvdata, outOffset);
|
||||
|
||||
CHECK(outOffset == initsize); /* check that the offset matches the size */
|
||||
|
||||
std::cerr << "DeSerialising" << std::endl;
|
||||
|
||||
/* fails if type is wrong! */
|
||||
CHECK(0 == GetTlvIpAddrPortV4((void*)tlvdata, outOffset, &inOffset, type-1, &outaddr));
|
||||
CHECK(GetTlvIpAddrPortV4((void*)tlvdata, outOffset, &inOffset, type, &outaddr));
|
||||
|
||||
CHECK(initsize == inOffset); /* check that the offset matches the size */
|
||||
CHECK(addr->sin_addr.s_addr == outaddr.sin_addr.s_addr); /* check that IP match */
|
||||
CHECK(addr->sin_port == outaddr.sin_port); /* check that Port match */
|
||||
std::cerr << "Deserialised: Size: " << inOffset << std::endl;
|
||||
std::cerr << "Deserialised IPAddr: " << inet_ntoa(outaddr.sin_addr) << std::endl;
|
||||
std::cerr << " Port : " << ntohs(outaddr.sin_port) << std::endl;
|
||||
|
||||
REPORT("Serialise OneIP/Port");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue