diff --git a/libretroshare/src/tests/pqi/Makefile b/libretroshare/src/tests/pqi/Makefile index b4145a816..7164d3b2f 100644 --- a/libretroshare/src/tests/pqi/Makefile +++ b/libretroshare/src/tests/pqi/Makefile @@ -7,10 +7,11 @@ RS_TOP_DIR = ../.. include $(RS_TOP_DIR)/tests/scripts/config.mk ############################################################### -TESTOBJ = net_test.o dht_test.o net_test1.o netiface_test.o dht_test.o +TESTOBJ = net_test.o dht_test.o net_test1.o netiface_test.o dht_test.o +TESTOBJ += pkt_test.o pqiarchive_test.o #conn_test.o -TESTS = net_test net_test1 netiface_test +TESTS = net_test net_test1 netiface_test pqiarchive_test MANUAL_TESTS = dht_test #conn_test @@ -38,6 +39,10 @@ net_test1: net_test1.o netiface_test: netiface_test.o $(CC) $(CFLAGS) -o netiface_test netiface_test.o $(LIBS) +pqiarchive_test: pqiarchive_test.o pkt_test.o + $(CC) $(CFLAGS) -o pqiarchive_test pkt_test.o pqiarchive_test.o $(LIBS) + + clobber: remove_extra_files remove_extra_files: diff --git a/libretroshare/src/tests/pqi/pkt_test.cc b/libretroshare/src/tests/pqi/pkt_test.cc new file mode 100644 index 000000000..49f6edd47 --- /dev/null +++ b/libretroshare/src/tests/pqi/pkt_test.cc @@ -0,0 +1,109 @@ +/* + * libretroshare/src/tests/pqi pkt_tst.cc + * + * 3P/PQI network interface for RetroShare. + * + * Copyright 2009-2010 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". + * + */ + +/****** + * pkt_tst. + * + * This is not testing serialisation.... so we only need 1 type of packet. + * using FileData packets - as these can have arbitary size. + */ + +#include "pkt_test.h" +#include + +uint8_t testdata[TEST_DATA_LEN]; + +void generate_test_data() +{ + srand(1); + + int i; + for(i = 0; i < TEST_DATA_LEN; i++) + { + testdata[i] = rand() % 256; + } +} + +bool check_data(void *data_in, int len) +{ + int i; + uint8_t *data = (uint8_t *) data_in; + + for(i = 0; i < len; i++) + { + if (data[i] != testdata[i]) + { + std::cerr << "check_data() Different Byte: " << i; + std::cerr << std::endl; + + return false; + } + } + return true; +} + +RsFileData *gen_packet(int datasize, std::string pid) +{ + /* generate some packets */ + RsFileData *data = new RsFileData(); + data->fd.file.filesize = TEST_PKT_SIZE; + data->fd.file.hash = TEST_PKT_HASH; + data->fd.file_offset = TEST_PKT_OFFSET; + data->fd.binData.setBinData(testdata, datasize); + data->PeerId(pid); + + return data; +} + +bool check_packet(RsFileData *pkt) +{ + if (pkt->fd.file.filesize != TEST_PKT_SIZE) + { + std::cerr << "check_packet() FileSize Different"; + std::cerr << std::endl; + + return false; + } + + if (pkt->fd.file.hash != TEST_PKT_HASH) + { + std::cerr << "check_packet() FileHash Different"; + std::cerr << std::endl; + + return false; + } + + if (pkt->fd.file_offset != TEST_PKT_OFFSET) + { + std::cerr << "check_packet() FileOffset Different"; + std::cerr << std::endl; + + return false; + } + + return check_data(pkt->fd.binData.bin_data, pkt->fd.binData.bin_len); +} + + diff --git a/libretroshare/src/tests/pqi/pkt_test.h b/libretroshare/src/tests/pqi/pkt_test.h new file mode 100644 index 000000000..072274574 --- /dev/null +++ b/libretroshare/src/tests/pqi/pkt_test.h @@ -0,0 +1,50 @@ +#ifndef RS_TEST_PACKETS +#define RS_TEST_PACKETS +/* + * libretroshare/src/tests/pqi pkt_tst.cc + * + * 3P/PQI network interface for RetroShare. + * + * Copyright 2009-2010 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". + * + */ + +/****** + * pkt_tst. + * + * This is not testing serialisation.... so we only need 1 type of packet. + * using FileData packets - as these can have arbitary size. + */ + +#include "serialiser/rsbaseitems.h" + +#define TEST_PKT_SIZE 1234567 +#define TEST_PKT_HASH "HASHTESTHASH" +#define TEST_PKT_OFFSET 1234 + +#define TEST_DATA_LEN (1024 * 1024) // MB + +void generate_test_data(); +bool check_data(void *data_in, int len); + +RsFileData *gen_packet(int datasize, std::string pid); +bool check_packet(RsFileData *pkt); + + +#endif diff --git a/libretroshare/src/tests/pqi/pqiarchive_test.cc b/libretroshare/src/tests/pqi/pqiarchive_test.cc new file mode 100644 index 000000000..abe88bab7 --- /dev/null +++ b/libretroshare/src/tests/pqi/pqiarchive_test.cc @@ -0,0 +1,130 @@ +/* + * libretroshare/src/tests/pqi pqiarchive_test.cc + * + * 3P/PQI network interface for RetroShare. + * + * Copyright 2009-2010 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". + * + */ + +/****** + * pqiarchive test. + * + * This is not testing serialisation.... so we only need 1 type of packet. + * using FileData packets - as these can have arbitary size. + */ + +#include "pqi/pqiarchive.h" +#include "pqi/pqibin.h" + +#include "serialiser/rsbaseitems.h" + +#include "util/rsnet.h" +#include +#include +#include "util/utest.h" + +#include "pkt_test.h" + +INITTEST(); + +int test_pqiarchive_generate(); + +int main(int argc, char **argv) +{ + std::cerr << "pqiarchive_test" << std::endl; + + generate_test_data(); + + test_pqiarchive_generate(); + + FINALREPORT("pqiarchive_test"); + + return TESTRESULT(); +} + +const char *archive_fname = "/tmp/rs_tst_archive.tmp"; + +int test_pqiarchive_generate() +{ + + std::string pid = "123456789012345678901234567890ZZ"; + /* create a pqiarchive */ + RsSerialiser *rss = new RsSerialiser(); + rss->addSerialType(new RsFileItemSerialiser()); + + int flags = BIN_FLAGS_WRITEABLE; + BinInterface *bio_in = new BinFileInterface(archive_fname, flags); + + int bio_flagsin = flags; + pqiarchive *pqia = new pqiarchive(rss, bio_in, bio_flagsin); + CHECK(pqia != NULL); + + int i; + for(i = 10; i < TEST_DATA_LEN; i = (int) (i * 1.1)) + { + RsFileData *pkt = gen_packet(i, pid); + pqia->SendItem(pkt); + pqia->tick(); + } + + /* close it up */ + delete pqia; + + /* setup read */ + flags = BIN_FLAGS_READABLE; + + rss = new RsSerialiser(); + rss->addSerialType(new RsFileItemSerialiser()); + + bio_in = new BinFileInterface(archive_fname, flags); + bio_flagsin = flags; + pqia = new pqiarchive(rss, bio_in, bio_flagsin); + + for(i = 10; i < TEST_DATA_LEN; i = (int) (i * 1.1)) + //for(i = 10; i < TEST_DATA_LEN; i += 111) + { + RsItem *pkt = pqia->GetItem(); + pqia->tick(); + + /* check the packet */ + RsFileData *data = dynamic_cast(pkt); + CHECK(data != NULL); + if (data) + { + CHECK(data->fd.binData.bin_len == i); + CHECK(check_packet(data) == true); + delete data; + } + } + + /* if there are anymore packets -> error! */ + for(i = 0; i < 1000; i++) + { + RsItem *pkt = pqia->GetItem(); + CHECK(pkt == NULL); + } + + REPORT("pqiarchive_generate()"); + + return 1; +} + + +