2018-11-04 15:17:22 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* unittests/libretroshare/serialiser/support.h *
|
|
|
|
* *
|
2018-11-11 14:42:48 -05:00
|
|
|
* Copyright 2007-2008 by Christopher Evi-Parker <retroshare.project@gmail.com>*
|
|
|
|
* Copyright 2007-2008 by Cyril Soler <retroshare.project@gmail.com>*
|
2018-11-04 15:17:22 -05:00
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-04-05 03:27:18 -04:00
|
|
|
#ifndef SUPPORT_H_
|
|
|
|
#define SUPPORT_H_
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "serialiser/rsserial.h"
|
|
|
|
#include "serialiser/rstlvkeys.h"
|
|
|
|
#include "serialiser/rstlvbinary.h"
|
|
|
|
#include "serialiser/rstlvfileitem.h"
|
|
|
|
#include "serialiser/rstlvidset.h"
|
|
|
|
#include "serialiser/rstlvimage.h"
|
|
|
|
|
|
|
|
#include "rstlvutil.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This contains functions that may be useful for testing throughout the
|
|
|
|
* retroshare's serialiser
|
|
|
|
* package, if you find a function you keep using everywhere, might be a good idea to add it here
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define SHORT_STR 100
|
|
|
|
#define LARGE_STR 1000
|
|
|
|
|
|
|
|
void randString(const uint32_t, std::string&);
|
|
|
|
void randString(const uint32_t, std::wstring&);
|
|
|
|
|
|
|
|
|
|
|
|
/* for testing compound tlv items */
|
|
|
|
|
|
|
|
void init_item(RsTlvSecurityKeySet&);
|
|
|
|
void init_item(RsTlvKeySignature&);
|
|
|
|
void init_item(RsTlvKeySignatureSet&);
|
|
|
|
void init_item(RsTlvBinaryData&);
|
|
|
|
void init_item(RsTlvFileItem&);
|
|
|
|
void init_item(RsTlvFileSet&);
|
|
|
|
void init_item(RsTlvHashSet&);
|
|
|
|
void init_item(RsTlvPeerIdSet&);
|
|
|
|
void init_item(RsTlvImage&);
|
|
|
|
void init_item(RsTlvPeerIdSet&);
|
|
|
|
|
2016-06-18 17:10:12 -04:00
|
|
|
bool operator==(const RsTlvPublicRSAKey&, const RsTlvPublicRSAKey& );
|
2014-04-05 03:27:18 -04:00
|
|
|
bool operator==(const RsTlvSecurityKeySet&, const RsTlvSecurityKeySet& );
|
|
|
|
bool operator==(const RsTlvKeySignature&, const RsTlvKeySignature& );
|
|
|
|
bool operator==(const RsTlvBinaryData&, const RsTlvBinaryData&);
|
|
|
|
bool operator==(const RsTlvFileItem&, const RsTlvFileItem&);
|
|
|
|
bool operator==(const RsTlvFileSet&, const RsTlvFileSet& );
|
|
|
|
bool operator==(const RsTlvHashSet&, const RsTlvHashSet&);
|
|
|
|
bool operator==(const RsTlvImage&, const RsTlvImage& );
|
|
|
|
bool operator==(const RsTlvPeerIdSet& , const RsTlvPeerIdSet& );
|
|
|
|
bool operator==(const RsTlvKeySignatureSet& , const RsTlvKeySignatureSet& );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* This templated test function which allows you to test
|
|
|
|
* retroshare serialiser items (except compound tlv items)
|
|
|
|
* you the function must implement a function
|
|
|
|
*
|
|
|
|
* 'RsSerialType* init_item(YourRsItem& rs_item)'
|
|
|
|
* which returns valid serialiser that
|
|
|
|
* can serialiser rs_item. You also need to implement an operator
|
|
|
|
*
|
|
|
|
* Also need to implement a function
|
|
|
|
* 'bool operator =(YourRsItem& rs_itemL, YourRsItem& rs_temR)'
|
|
|
|
* which allows this function to test for equality between both parameters.
|
|
|
|
* rs_temR is the result of deserialising the left operand (1st parameter).
|
|
|
|
* not YourRsItem in specifier in about functions should be a derived type from RsItem
|
|
|
|
*
|
|
|
|
* @param T the item you want to test
|
|
|
|
*/
|
|
|
|
|
2017-07-24 06:16:06 -04:00
|
|
|
template<class ItemClass,class ItemSerialiser> int test_RsItem()
|
2014-04-05 03:27:18 -04:00
|
|
|
{
|
|
|
|
/* make a serialisable RsTurtleItem */
|
|
|
|
|
|
|
|
RsSerialiser srl;
|
|
|
|
|
|
|
|
/* initialise */
|
2017-07-24 06:16:06 -04:00
|
|
|
ItemClass rsfi ;
|
|
|
|
RsSerialType *rsfis = new ItemSerialiser;
|
|
|
|
|
|
|
|
init_item(rsfi);
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
/* attempt to serialise it before we add it to the serialiser */
|
2016-07-15 09:03:41 -04:00
|
|
|
std::cerr << "### These errors are expected." << std::endl;
|
2014-04-05 03:27:18 -04:00
|
|
|
EXPECT_TRUE(0 == srl.size(&rsfi));
|
|
|
|
|
|
|
|
static const uint32_t MAX_BUFSIZE = 22000 ;
|
|
|
|
|
|
|
|
char *buffer = new char[MAX_BUFSIZE];
|
|
|
|
uint32_t sersize = MAX_BUFSIZE;
|
|
|
|
|
2016-07-15 09:03:41 -04:00
|
|
|
std::cerr << "### These errors are expected." << std::endl;
|
2014-04-05 03:27:18 -04:00
|
|
|
EXPECT_TRUE(false == srl.serialise(&rsfi, (void *) buffer, &sersize));
|
|
|
|
|
|
|
|
/* now add to serialiser */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-07-15 09:03:41 -04:00
|
|
|
//std::cerr << "test_Item() serialised:" << std::endl;
|
2014-04-05 03:27:18 -04:00
|
|
|
//displayRawPacket(std::cerr, (void *) buffer, sersize);
|
|
|
|
|
|
|
|
EXPECT_TRUE(done == true);
|
|
|
|
|
|
|
|
uint32_t sersize2 = sersize;
|
|
|
|
RsItem *output = srl.deserialise((void *) buffer, &sersize2);
|
|
|
|
|
|
|
|
EXPECT_TRUE(output != NULL);
|
|
|
|
if (!output)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(sersize2 == sersize);
|
|
|
|
|
2017-07-24 06:16:06 -04:00
|
|
|
ItemClass *outfi = dynamic_cast<ItemClass *>(output);
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
EXPECT_TRUE(outfi != NULL);
|
|
|
|
if (!outfi)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(*outfi == rsfi))
|
|
|
|
{
|
|
|
|
std::cerr << "Items differ: "<< std::endl;
|
|
|
|
outfi->print(std::cerr,0) ;
|
|
|
|
rsfi.print(std::cerr,0) ;
|
|
|
|
}
|
|
|
|
if (outfi)
|
2018-02-15 12:44:03 -05:00
|
|
|
{
|
2014-04-05 03:27:18 -04:00
|
|
|
EXPECT_TRUE(*outfi == rsfi) ;
|
2018-02-15 12:44:03 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
sersize2 = MAX_BUFSIZE;
|
|
|
|
bool done2 = srl.serialise(outfi, (void *) &(buffer[16*8]), &sersize2);
|
|
|
|
|
|
|
|
EXPECT_TRUE(done2) ;
|
|
|
|
EXPECT_TRUE(sersize2 == sersize);
|
|
|
|
|
2017-07-24 06:16:06 -04:00
|
|
|
std::cerr << "Deleting output" <<std::endl;
|
2017-07-23 16:56:27 -04:00
|
|
|
delete output ;
|
2014-04-05 03:27:18 -04:00
|
|
|
delete[] buffer ;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-24 06:16:06 -04:00
|
|
|
template<class ItemClass,class ItemSerialiser> int test_RsItem(uint16_t servtype)
|
2014-04-05 03:27:18 -04:00
|
|
|
{
|
|
|
|
/* make a serialisable RsTurtleItem */
|
|
|
|
|
|
|
|
RsSerialiser srl;
|
|
|
|
|
|
|
|
/* initialise */
|
2017-07-24 06:16:06 -04:00
|
|
|
ItemClass rsfi(servtype) ;
|
|
|
|
RsSerialType *rsfis = new ItemSerialiser(servtype) ;
|
|
|
|
|
|
|
|
init_item(rsfi) ; // deleted on destruction of srl
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
/* attempt to serialise it before we add it to the serialiser */
|
2016-07-15 09:03:41 -04:00
|
|
|
std::cerr << "### These errors are expected." << std::endl;
|
2014-04-05 03:27:18 -04:00
|
|
|
EXPECT_TRUE(0 == srl.size(&rsfi));
|
|
|
|
|
|
|
|
static const uint32_t MAX_BUFSIZE = 22000 ;
|
|
|
|
|
|
|
|
char *buffer = new char[MAX_BUFSIZE];
|
|
|
|
uint32_t sersize = MAX_BUFSIZE;
|
|
|
|
|
|
|
|
EXPECT_TRUE(false == srl.serialise(&rsfi, (void *) buffer, &sersize));
|
|
|
|
|
|
|
|
/* now add to serialiser */
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
EXPECT_TRUE(done == true);
|
|
|
|
|
|
|
|
uint32_t sersize2 = sersize;
|
|
|
|
RsItem *output = srl.deserialise((void *) buffer, &sersize2);
|
|
|
|
|
|
|
|
EXPECT_TRUE(output != NULL);
|
|
|
|
EXPECT_TRUE(sersize2 == sersize);
|
|
|
|
|
2017-07-24 06:16:06 -04:00
|
|
|
ItemClass *outfi = dynamic_cast<ItemClass *>(output);
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
EXPECT_TRUE(outfi != NULL);
|
|
|
|
|
|
|
|
if (outfi)
|
2018-02-15 12:44:03 -05:00
|
|
|
{
|
2014-04-05 03:27:18 -04:00
|
|
|
EXPECT_TRUE(*outfi == rsfi) ;
|
2018-02-15 12:44:03 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
sersize2 = MAX_BUFSIZE;
|
|
|
|
bool done2 = srl.serialise(outfi, (void *) &(buffer[16*8]), &sersize2);
|
|
|
|
|
|
|
|
EXPECT_TRUE(done2) ;
|
|
|
|
EXPECT_TRUE(sersize2 == sersize);
|
|
|
|
|
|
|
|
// displayRawPacket(std::cerr, (void *) buffer, 16 * 8 + sersize2);
|
|
|
|
|
|
|
|
delete[] buffer ;
|
2017-07-23 16:58:01 -04:00
|
|
|
delete output ;
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* SUPPORT_H_ */
|