* Added first network function tests.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3179 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2010-06-20 16:04:53 +00:00
parent b8f9503c8d
commit b23b0a5d70
8 changed files with 1544 additions and 0 deletions

View File

@ -0,0 +1,49 @@
RS_TOP_DIR = ../..
##### Define any flags that are needed for this section #######
###############################################################
###############################################################
include $(RS_TOP_DIR)/tests/scripts/config.mk
###############################################################
TESTOBJ = net_test.o dht_test.o net_test1.o netiface_test.o dht_test.o
#conn_test.o
TESTS = net_test net_test1 netiface_test
MANUAL_TESTS = dht_test
#conn_test
all: tests $(MANUAL_TESTS)
gpgme_tst: gpgme_tst.o
$(CC) $(CFLAGS) -o gpgme_tst gpgme_tst.o $(LIBS)
xpgp_id: xpgp_id.o
$(CC) $(CFLAGS) -o xpgp_id xpgp_id.o $(LIBS)
dht_test: dht_test.o
$(CC) $(CFLAGS) -o dht_test dht_test.o $(LIBS)
conn_test: conn_test.o
$(CC) $(CFLAGS) -o conn_test conn_test.o $(LIBS)
net_test: net_test.o
$(CC) $(CFLAGS) -o net_test net_test.o $(LIBS)
net_test1: net_test1.o
$(CC) $(CFLAGS) -o net_test1 net_test1.o $(LIBS)
netiface_test: netiface_test.o
$(CC) $(CFLAGS) -o netiface_test netiface_test.o $(LIBS)
clobber: remove_extra_files
remove_extra_files:
-$(RM) $(MANUAL_TESTS)
###############################################################
include $(RS_TOP_DIR)/tests/scripts/rules.mk
###############################################################

View File

@ -0,0 +1,96 @@
There are a huge amount of testing to be done here.
Components and Tests.
============================================================
------------------------------------------------------------
Basic Components
------------------------------------------------------------
pqinetwork.cc: net_test, net_test1, netiface_test
pqi_base.cc: no need.
pqimonitor.cc: no need.
p3notify.cc: no need.
pqisecurity.cc: no need.
pqiservice.cc: no need.
cleanupxpgp.cc to test.
------------------------------------------------------------
Serialisation Components
------------------------------------------------------------
pqiarchive.cc
pqibin.cc
pqistreamer.cc
pqistore.cc
------------------------------------------------------------
SSL Network Components
------------------------------------------------------------
pqiloopback.cc
pqissltunnel.cc
pqissludp.cc
pqissl.cc
pqiperson.cc
pqipersongrp.cc
pqisslpersongrp.cc
pqissllistener.cc
------------------------------------------------------------
Authentication Components
------------------------------------------------------------
sslcert.cc
xpgpcert.cc
authgpg.cc
authssl.cc
------------------------------------------------------------
Manager Components
------------------------------------------------------------
p3connmgr.cc
pqihandler.cc
p3dhtmgr.cc: dht_test
p3cfgmgr.cc
============================================================
Test Specifics
============================================================
------------------------------------------------------------
net_test
------------------------------------------------------------
Tested Code: pqinetwork.cc, rsnet.cc
Description:
(1) Tests ntohll / htonll
(2) Demonstrates inet_netof / inet_network strange behaviour.
(3) Tests socket binding.
------------------------------------------------------------
net_test1
------------------------------------------------------------
Tested Code: pqinetwork.cc, rsnet.cc
Description:
(1) isExternalNet()
(2) isPrivateNet()
(3) isLoopbackNet()
(4) sameNet()
(5) isValidNet()
(6) isSameSubnet()
(7) pqi_inet_netof()
------------------------------------------------------------
netiface_test
------------------------------------------------------------
Tested Code: pqinetwork.cc
Description :
(1) getPreferredInterface()
(2) getLocalInterfaces()
This is currently a manual test - needs to be converted.
------------------------------------------------------------
dht_test
------------------------------------------------------------
Tested Code : p3dhtmgr.cc
Description :
This is a manual test of the DHT manager. needs to be
converted to an automatic test.

View File

@ -0,0 +1,362 @@
#include "pqi/p3connmgr.h"
/***** Test for the new DHT system *****/
#include "util/rsnet.h"
#include "util/rsthreads.h"
#include "util/rsprint.h"
#include "pqi/p3dhtmgr.h"
#include "pqi/p3connmgr.h"
#include "pqi/pqisecurity.h"
#include "pqi/pqipersongrp.h"
#include <iostream>
#include <sstream>
#include "tcponudp/udpsorter.h"
/***** Test Framework *****/
const int NumOfPeers = 10;
std::string peerIds[NumOfPeers] =
{"PEER01",
"PEER02", /* Always online, no notify */
"PEER03", /* notify/online at 20sec */
"PEER04", /* Always online, notify at 30 sec */
"PEER05",
"PEER06", /* notify/online at 50sec */
"PEER07",
"PEER08",
"PEER09", /* notify/online at 80sec */
"PEER10"};
#define STUN_PORT 7777
std::string ownId = "OWNID-AAAA";
time_t ownPublishTs;
RsMutex frmMtx;
std::list<std::string> searchIds;
std::list<uint32_t> searchModes;
std::map<std::string, bool> onlineMap;
std::map<uint32_t, std::string> notifyMap;
void initTestData()
{
ownPublishTs = 0;
/* setup Peers that are online always */
bool online;
uint32_t ts;
for(int i = 0; i < NumOfPeers; i++)
{
online = false;
if ((i == 1) || (i == 3))
{
online = true;
}
onlineMap[peerIds[i]] = online;
if ((i == 2) || (i == 3) ||
(i == 5) || (i == 8))
{
ts = i * 10;
notifyMap[ts] = peerIds[i];
}
}
}
void respondPublish()
{
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
if (!ownPublishTs)
{
std::cerr << "Own ID first published!" << std::endl;
ownPublishTs = time(NULL);
}
frmMtx.unlock(); /* UNLOCK TEST FRAMEWORK MUTEX */
}
void respondSearch(p3DhtMgr *mgr, std::string id, uint32_t mode)
{
std::cerr << "Checking for Search Results" << std::endl;
time_t now = time(NULL);
bool doNotify = false;
bool doOnline = false;
std::string notifyId;
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
if ((mode == DHT_MODE_NOTIFY) && (ownPublishTs))
{
/* */
std::map<uint32_t, std::string>::iterator it;
uint32_t delta_t = now - ownPublishTs;
it = notifyMap.begin();
if (it != notifyMap.end())
{
if (it->first <= delta_t)
{
notifyId = it->second;
onlineMap[notifyId] = true;
notifyMap.erase(it);
doNotify = true;
}
}
}
else if (mode == DHT_MODE_SEARCH)
{
/* translate */
std::map<std::string, bool>::iterator mit;
for(mit = onlineMap.begin(); (mit != onlineMap.end()) &&
(RsUtil::HashId(mit->first, false) != id); mit++);
if (mit != onlineMap.end())
{
doOnline = mit->second;
}
}
frmMtx.unlock(); /* UNLOCK TEST FRAMEWORK MUTEX */
uint32_t type = 0;
struct sockaddr_in laddr;
inet_aton("10.0.0.129", &(laddr.sin_addr));
laddr.sin_port = htons(7812);
laddr.sin_family = AF_INET;
struct sockaddr_in raddr;
inet_aton("127.0.0.1", &(raddr.sin_addr));
raddr.sin_port = htons(STUN_PORT);
raddr.sin_family = AF_INET;
if (doNotify)
{
std::cerr << "Responding to Notify: id:" << notifyId << std::endl;
mgr->dhtResultNotify(RsUtil::HashId(notifyId, true));
}
if (doOnline)
{
std::cerr << "Responding to Search" << std::endl;
mgr->dhtResultSearch(id, laddr, raddr, type, "");
}
}
/***** Test Framework *****/
class DhtMgrTester: public p3DhtMgr
{
/* Implementation */
public:
DhtMgrTester(std::string id, pqiConnectCb *cb)
:p3DhtMgr(id, cb)
{
return;
}
/* Blocking calls (only from thread) */
virtual bool dhtPublish(std::string id,
struct sockaddr_in &laddr, struct sockaddr_in &raddr,
uint32_t type, std::string sign)
{
std::cerr << "DhtMgrTester::dhtPublish() id: " << RsUtil::BinToHex(id);
std::cerr << " laddr: " << inet_ntoa(laddr.sin_addr) << " lport: " << ntohs(laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr) << " rport: " << ntohs(raddr.sin_port);
std::cerr << " type: " << type << " sign: " << sign;
std::cerr << std::endl;
respondPublish();
return true;
}
virtual bool dhtNotify(std::string peerid, std::string ownid, std::string sign)
{
std::cerr << "DhtMgrTester::dhtNotify() id: " << RsUtil::BinToHex(peerid) << ", ownId: " << RsUtil::BinToHex(ownId);
std::cerr << " sign: " << sign;
std::cerr << std::endl;
return true;
}
virtual bool dhtSearch(std::string id, uint32_t mode)
{
std::cerr << "DhtMgrTester::dhtSearch(id: " << RsUtil::BinToHex(id) << ", mode: " << mode << ")" << std::endl;
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
searchIds.push_back(id);
searchModes.push_back(mode);
frmMtx.unlock(); /* LOCK TEST FRAMEWORK MUTEX */
return true;
}
};
/* OVERLOAD THE ConnMgr - to insert peers */
class p3TestConnMgr: public p3ConnectMgr
{
public:
p3TestConnMgr(int mode)
:p3ConnectMgr(new p3DummyAuthMgr()), mTestMode(mode) { return; }
protected:
/* must be virtual for testing */
virtual void loadConfiguration()
{
/* setup own address */
ownState.id = ownId;
ownState.name = "SELF NAME";
ownState.localaddr.sin_family = AF_INET;
inet_aton("127.0.0.1", &(ownState.localaddr.sin_addr));
ownState.localaddr.sin_port = htons(7812);
ownState.netMode = RS_NET_MODE_UDP;
ownState.visState = RS_VIS_STATE_STD;
/* others not important */
//ownState.state = 0;
//ownState.actions = 0;
if (mTestMode == 1) /* Add to Stun List */
{
for(int i = 0; i < NumOfPeers; i++)
{
mStunList.push_back(peerIds[i]);
}
}
else if (mTestMode == 2) /* add to peers */
{
/* add in as peers */
//addPeer();
for(int i = 0; i < NumOfPeers; i++)
{
if (i < 5)
{
mStunList.push_back(RsUtil::HashId(peerIds[i]));
}
else
{
addFriend(peerIds[i]);
}
}
}
}
protected:
uint32_t mTestMode;
};
int main()
{
time_t startTime = time(NULL);
/* setup system */
initTestData();
/* setup a Stunner to respond to ConnMgr */
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
inet_aton("127.0.0.1", &(saddr.sin_addr));
saddr.sin_port = htons(STUN_PORT);
UdpSorter stunner(saddr); /* starts a receiving thread */
p3TestConnMgr connMgr(2);
DhtMgrTester dhtTester(ownId, &connMgr);
/* now add in some peers */
connMgr.setDhtMgr(&dhtTester);
connMgr.setUpnpMgr(NULL);
/************ ADD pqipersongrp as pqimonitor *****************/
SecurityPolicy *pol = secpolicy_create();
unsigned long flags = 0;
pqipersongrp *pqipg = new pqipersongrpDummy(pol, flags);
connMgr.addMonitor(pqipg);
/************ ADD pqipersongrp as pqimonitor *****************/
/* startup dht */
std::cerr << "Starting up DhtTester()" << std::endl;
dhtTester.start();
/* wait for a little before switching on */
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
sleep(1);
#else
Sleep(1000);
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
std::cerr << "Switching on DhtTester()" << std::endl;
dhtTester.setDhtOn(true);
/* wait loop */
while(1)
{
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
sleep(1);
#else
Sleep(1000);
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
connMgr.tick();
pqipg->tick();
/* handle async search */
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
std::string id;
uint32_t mode;
bool doRespond = false;
if (searchIds.size() > 0)
{
id = searchIds.front();
mode = searchModes.front();
doRespond = true;
searchIds.pop_front();
searchModes.pop_front();
}
frmMtx.unlock(); /* UNLOCK TEST FRAMEWORK MUTEX */
if (doRespond)
{
respondSearch(&dhtTester, id, mode);
}
}
};

View File

@ -0,0 +1,310 @@
/***** Test for the new DHT system *****/
#include "pqi/pqinetwork.h"
#include "util/rsnet.h"
#include "util/rsthreads.h"
#include "util/rsprint.h"
#include "pqi/p3dhtmgr.h"
#include <iostream>
#include <sstream>
/***** Test Framework *****/
const int NumOfPeers = 10;
std::string peerIds[NumOfPeers] =
{"PEER01",
"PEER02", /* Always online, no notify */
"PEER03", /* notify/online at 20sec */
"PEER04", /* Always online, notify at 30 sec */
"PEER05",
"PEER06", /* notify/online at 50sec */
"PEER07",
"PEER08",
"PEER09", /* notify/online at 80sec */
"PEER10"};
std::string ownId = "AAAA";
time_t ownPublishTs;
RsMutex frmMtx;
std::list<std::string> searchIds;
std::list<uint32_t> searchModes;
std::map<std::string, bool> onlineMap;
std::map<uint32_t, std::string> notifyMap;
void initTestData()
{
ownPublishTs = 0;
/* setup Peers that are online always */
bool online;
uint32_t ts;
for(int i = 0; i < NumOfPeers; i++)
{
online = false;
if ((i == 1) || (i == 3))
{
online = true;
}
onlineMap[peerIds[i]] = online;
if ((i == 2) || (i == 3) ||
(i == 5) || (i == 8))
{
ts = i * 10;
notifyMap[ts] = peerIds[i];
}
}
}
void respondPublish()
{
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
if (!ownPublishTs)
{
std::cerr << "Own ID first published!" << std::endl;
ownPublishTs = time(NULL);
}
frmMtx.unlock(); /* UNLOCK TEST FRAMEWORK MUTEX */
}
void respondSearch(p3DhtMgr *mgr, std::string id, uint32_t mode)
{
std::cerr << "Checking for Search Results" << std::endl;
time_t now = time(NULL);
bool doNotify = false;
bool doOnline = false;
std::string notifyId;
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
if ((mode == DHT_MODE_NOTIFY) && (ownPublishTs))
{
/* */
std::map<uint32_t, std::string>::iterator it;
uint32_t delta_t = now - ownPublishTs;
it = notifyMap.begin();
if (it != notifyMap.end())
{
if (it->first <= delta_t)
{
notifyId = it->second;
onlineMap[notifyId] = true;
notifyMap.erase(it);
doNotify = true;
}
}
}
else if (mode == DHT_MODE_SEARCH)
{
/* translate */
std::map<std::string, bool>::iterator mit;
for(mit = onlineMap.begin(); (mit != onlineMap.end()) &&
(RsUtil::HashId(mit->first, false) != id); mit++);
if (mit != onlineMap.end())
{
doOnline = mit->second;
}
}
frmMtx.unlock(); /* UNLOCK TEST FRAMEWORK MUTEX */
uint32_t type = 0;
struct sockaddr_in laddr;
inet_aton("10.0.0.129", &(laddr.sin_addr));
laddr.sin_port = htons(7812);
laddr.sin_family = AF_INET;
struct sockaddr_in raddr;
inet_aton("10.0.0.19", &(raddr.sin_addr));
raddr.sin_port = htons(7812);
raddr.sin_family = AF_INET;
if (doNotify)
{
std::cerr << "Responding to Notify" << std::endl;
mgr->dhtResultNotify(RsUtil::HashId(notifyId, true));
}
if (doOnline)
{
std::cerr << "Responding to Search" << std::endl;
mgr->dhtResultSearch(id, laddr, raddr, type, "");
}
}
/***** Test Framework *****/
class DhtMgrTester: public p3DhtMgr
{
/* Implementation */
public:
DhtMgrTester(std::string id, pqiConnectCb *cb)
:p3DhtMgr(id, cb)
{
return;
}
/* Blocking calls (only from thread) */
virtual bool dhtPublish(std::string id,
struct sockaddr_in &laddr, struct sockaddr_in &raddr,
uint32_t type, std::string sign)
{
std::cerr << "DhtMgrTester::dhtPublish() id: " << RsUtil::BinToHex(id);
std::cerr << " laddr: " << inet_ntoa(laddr.sin_addr) << " lport: " << ntohs(laddr.sin_port);
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr) << " rport: " << ntohs(raddr.sin_port);
std::cerr << " type: " << type << " sign: " << sign;
std::cerr << std::endl;
respondPublish();
return true;
}
virtual bool dhtNotify(std::string peerid, std::string ownid, std::string sign)
{
std::cerr << "DhtMgrTester::dhtNotify() id: " << RsUtil::BinToHex(peerid) << ", ownId: " << RsUtil::BinToHex(ownId);
std::cerr << " sign: " << sign;
std::cerr << std::endl;
return true;
}
virtual bool dhtSearch(std::string id, uint32_t mode)
{
std::cerr << "DhtMgrTester::dhtSearch(id: " << RsUtil::BinToHex(id) << ", mode: " << mode << ")" << std::endl;
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
searchIds.push_back(id);
searchModes.push_back(mode);
frmMtx.unlock(); /* LOCK TEST FRAMEWORK MUTEX */
return true;
}
};
int main()
{
time_t startTime = time(NULL);
bool haveOwnAddress = false;
/* setup system */
initTestData();
pqiConnectCbDummy cbTester;
DhtMgrTester dhtTester(ownId, &cbTester);
/* now add in some peers */
/* startup dht */
std::cerr << "Starting up DhtTester()" << std::endl;
dhtTester.start();
/* wait for a little before switching on */
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
sleep(1);
#else
Sleep(1000);
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
std::cerr << "Switching on DhtTester()" << std::endl;
dhtTester.enable(true);
std::cerr << "Adding a List of Peers" << std::endl;
for(int i = 0; i < NumOfPeers; i++)
{
dhtTester.findPeer(peerIds[i]);
}
/* wait loop */
while(1)
{
std::cerr << "Main waiting..." << std::endl;
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
sleep(3);
#else
Sleep(3000);
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
/* handle async search */
frmMtx.lock(); /* LOCK TEST FRAMEWORK MUTEX */
std::string id;
uint32_t mode;
bool doRespond = false;
if (searchIds.size() > 0)
{
id = searchIds.front();
mode = searchModes.front();
doRespond = true;
searchIds.pop_front();
searchModes.pop_front();
}
frmMtx.unlock(); /* UNLOCK TEST FRAMEWORK MUTEX */
if (doRespond)
{
respondSearch(&dhtTester, id, mode);
}
if (!haveOwnAddress)
{
if (time(NULL) - startTime > 20)
{
std::cerr << "Setting Own Address!" << std::endl;
haveOwnAddress = true;
uint32_t type = DHT_ADDR_UDP;
struct sockaddr_in laddr;
inet_aton("10.0.0.111", &(laddr.sin_addr));
laddr.sin_port = htons(7812);
laddr.sin_family = AF_INET;
struct sockaddr_in raddr;
inet_aton("10.0.0.11", &(raddr.sin_addr));
raddr.sin_port = htons(7812);
raddr.sin_family = AF_INET;
dhtTester.setExternalInterface(laddr, raddr, type);
}
}
}
};

View File

@ -0,0 +1,76 @@
#include "pqi/authgpg.h"
const std::string key_path("./tmp/privkey.pem");
const std::string passwd("8764");
const std::string gpg_passwd("aaaa");
const std::string name("Test X509");
const std::string email("test@email.com");
const std::string org("Org");
const std::string loc("Loc");
const std::string state("State");
const std::string country("GB");
int main()
{
/* Init the auth manager */
GPGAuthMgr mgr;
/* Select which GPG Keys we use */
/* print all keys */
mgr.printKeys();
std::list<std::string> idList;
mgr.availablePGPCertificates(idList);
if (idList.size() < 1)
{
fprintf(stderr, "No GPG Certificate to use!\n");
exit(1);
}
std::string id = idList.front();
fprintf(stderr, "Using GPG Certificate:%s \n", id.c_str());
std::string noname;
mgr.GPGInit(id);
mgr.LoadGPGPassword(gpg_passwd);
/* Init SSL library */
mgr.InitAuth(NULL, NULL, NULL);
/* then try to generate and sign a X509 certificate */
int nbits_in = 2048;
std::string errString;
/* Generate a Certificate Request */
X509_REQ *req = GenerateX509Req(key_path, passwd, name, email, org,
loc, state, country, nbits_in, errString);
// setup output.
BIO *bio_out = NULL;
bio_out = BIO_new(BIO_s_file());
BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
/* Print it out */
int nmflag = 0;
int reqflag = 0;
X509_REQ_print_ex(bio_out, req, nmflag, reqflag);
X509 *x509 = mgr.SignX509Req(req, 100, gpg_passwd);
X509_print_ex(bio_out, x509, nmflag, reqflag);
BIO_flush(bio_out);
BIO_free(bio_out);
/* now try to validate it */
mgr.AuthX509(x509);
//sleep(10);
}

View File

@ -0,0 +1,320 @@
/*
* libretroshare/src/test/pqi net_test.cc
*
* 3P/PQI network interface for RetroShare.
*
* Copyright 2007-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".
*
*/
/******
* NETWORKING Test to check Big/Little Endian behaviour
* as well as socket behaviour
*
*/
#include "util/utest.h"
#include "pqi/pqinetwork.h"
#include "util/rsnet.h"
#include <iostream>
#include <sstream>
bool test_byte_manipulation();
bool test_local_address_manipulation();
bool test_address_listen();
INITTEST();
int main(int argc, char **argv)
{
std::cerr << "libretroshare:pqi " << argv[0] << std::endl;
test_byte_manipulation();
test_local_address_manipulation();
test_address_listen();
FINALREPORT("libretroshare Net Basics Tests");
return TESTRESULT();
}
/* test 1: byte manipulation */
bool test_byte_manipulation()
{
uint64_t num1 = 0x0000000000000000ffULL; /* 255 */
uint64_t num2 = 0x00000000000000ff00ULL; /* */
uint64_t n_num1 = htonll(num1);
uint64_t n_num2 = htonll(num2);
uint64_t h_num1 = ntohll(n_num1);
uint64_t h_num2 = ntohll(n_num2);
std::ostringstream out;
out << std::hex;
out << "num1: " << num1 << " netOrder: " << n_num1 << " hostOrder: " << h_num1 << std::endl;
out << "num2: " << num2 << " netOrder: " << n_num2 << " hostOrder: " << h_num2 << std::endl;
std::cerr << out.str();
CHECK( num1 == h_num1 );
CHECK( num2 == h_num2 );
REPORT("Test Byte Manipulation");
return true;
}
const char * loopback_addrstr = "127.0.0.1";
const char * localnet1_addrstr = "192.168.0.1";
const char * localnet2_addrstr = "10.0.0.1";
const char * localnet3_addrstr = "10.5.63.78";
const char * localnet4_addrstr = "192.168.74.91";
/* test 2: address manipulation */
bool test_local_address_manipulation()
{
struct sockaddr_in loopback_addr;
struct sockaddr_in localnet1_addr;
struct sockaddr_in localnet2_addr;
struct sockaddr_in localnet3_addr;
struct sockaddr_in localnet4_addr;
std::cerr << "test_local_address_manipulation() NB: This function demonstrates ";
std::cerr << "the strange behaviour of inet_netof() and inet_network()";
std::cerr << std::endl;
std::cerr << "FAILures are not counted in the test!";
std::cerr << std::endl;
/* setup some addresses */
inet_aton(loopback_addrstr, &(loopback_addr.sin_addr));
inet_aton(localnet1_addrstr, &(localnet1_addr.sin_addr));
inet_aton(localnet2_addrstr, &(localnet2_addr.sin_addr));
inet_aton(localnet3_addrstr, &(localnet3_addr.sin_addr));
inet_aton(localnet4_addrstr, &(localnet4_addr.sin_addr));
std::cerr << "Loopback Addr " << inet_ntoa(loopback_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet1 Addr " << inet_ntoa(localnet1_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet2 Addr " << inet_ntoa(localnet2_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet3 Addr " << inet_ntoa(localnet3_addr.sin_addr);
std::cerr << std::endl;
std::cerr << "Localnet4 Addr " << inet_ntoa(localnet4_addr.sin_addr);
std::cerr << std::endl;
std::cerr << std::endl;
std::cerr << "Test 1a - networks";
std::cerr << std::endl;
struct sockaddr_in addr_ans, addr1, addr2;
std::string addr_ans_str;
addr_ans_str = "127.0.0.0";
inet_aton(addr_ans_str.c_str(), &(addr_ans.sin_addr));
addr1.sin_addr.s_addr = htonl(inet_netof(loopback_addr.sin_addr));
addr2.sin_addr.s_addr = htonl(inet_network(loopback_addrstr));
std::cerr << "Loopback Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
addr_ans_str = "192.168.0.0";
inet_aton(addr_ans_str.c_str(), &(addr_ans.sin_addr));
addr1.sin_addr.s_addr = htonl(inet_netof(localnet1_addr.sin_addr));
addr2.sin_addr.s_addr = htonl(inet_network(localnet1_addrstr));
std::cerr << "Localnet1 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
addr_ans_str = "10.0.0.0";
inet_aton(addr_ans_str.c_str(), &(addr_ans.sin_addr));
addr1.sin_addr.s_addr = htonl(inet_netof(localnet2_addr.sin_addr));
addr2.sin_addr.s_addr = htonl(inet_network(localnet2_addrstr));
std::cerr << "Localnet2 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
addr_ans_str = "10.0.0.0";
inet_aton(addr_ans_str.c_str(), &(addr_ans.sin_addr));
addr1.sin_addr.s_addr = htonl(inet_netof(localnet3_addr.sin_addr));
addr2.sin_addr.s_addr = htonl(inet_network(localnet3_addrstr));
std::cerr << "Localnet3 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
addr_ans_str = "192.168.0.0";
inet_aton(addr_ans_str.c_str(), &(addr_ans.sin_addr));
addr1.sin_addr.s_addr = htonl(inet_netof(localnet4_addr.sin_addr));
addr2.sin_addr.s_addr = htonl(inet_network(localnet4_addrstr));
std::cerr << "Localnet4 Net(expected): " << addr_ans_str;
std::cerr << " -> " << inet_ntoa(addr_ans.sin_addr);
std::cerr << " Net(1):" << inet_ntoa(addr1.sin_addr);
std::cerr << " Net(2):" << inet_ntoa(addr2.sin_addr);
std::cerr << std::endl;
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr1.sin_addr)));
//CHECK( 0 == strcmp(addr_ans_str.c_str(), inet_ntoa(addr2.sin_addr)));
REPORT("Test Local Address Manipulation");
return true;
}
#if 0
std::ostream &showSocketError(std::ostream &out);
std::string socket_errorType(int err);
int sockaddr_cmp(struct sockaddr_in &addr1, struct sockaddr_in &addr2 );
int inaddr_cmp(struct sockaddr_in addr1, struct sockaddr_in addr1 );
int inaddr_cmp(struct sockaddr_in addr1, unsigned long);
std::list<std::string> getLocalInterfaces(); // returns all possible addrs.
bool isExternalNet(struct in_addr *addr); // if Valid & is not Private or Loopback.
bool isPrivateNet(struct in_addr *addr); // if inside 10.0.0.0 or
// other then firewalled.
bool isLoopbackNet(struct in_addr *addr);
bool sameNet(struct in_addr *addr, struct in_addr *addr2);
bool isValidNet(struct in_addr *addr);
// checks (addr1 & 255.255.255.0) == (addr2 & 255.255.255.0)
bool isSameSubnet(struct in_addr *addr1, struct in_addr *addr2);
struct in_addr getPreferredInterface(); // returns best addr.
in_addr_t pqi_inet_netof(struct in_addr addr); // our implementation.
bool LookupDNSAddr(std::string name, struct sockaddr_in &addr);
/* universal socket interface */
int unix_close(int sockfd);
int unix_socket(int domain, int type, int protocol);
int unix_fcntl_nonblock(int sockfd);
int unix_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
int unix_getsockopt_error(int sockfd, int *err);
#endif
bool test_bind_addr(struct sockaddr_in addr);
bool test_address_listen()
{
struct sockaddr_in addr1, addr2, addr3;
sockaddr_clear(&addr1);
addr1.sin_family = AF_INET;
inet_aton(loopback_addrstr, &(addr1.sin_addr));
addr1.sin_port = htons(12345);
sockaddr_clear(&addr2);
addr2.sin_family = AF_INET;
addr2.sin_addr = getPreferredInterface(); // returns best addr.
addr2.sin_port = htons(13245);
sockaddr_clear(&addr3);
addr3.sin_family = AF_INET;
addr3.sin_addr = getPreferredInterface(); // returns best addr.
addr3.sin_port = htons(23451);
/* test bind to loopback, and preferred interfaces */
test_bind_addr(addr1);
test_bind_addr(addr2);
test_bind_addr(addr3);
REPORT("Test Address Listen");
return true;
}
bool test_bind_addr(struct sockaddr_in addr)
{
int err;
std::cerr << "test_bind_addr()";
std::cerr << std::endl;
std::cerr << "\tAddress Family: " << (int) addr.sin_family;
std::cerr << std::endl;
std::cerr << "\tAddress: " << inet_ntoa(addr.sin_addr);
std::cerr << std::endl;
std::cerr << "\tPort: " << ntohs(addr.sin_port);
std::cerr << std::endl;
int sockfd = unix_socket(PF_INET, SOCK_STREAM, 0);
CHECK( 0 < sockfd );
if (0 != (err = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr))))
{
std::cerr << " Failed to Bind to Local Address!" << std::endl;
showSocketError(std::cerr);
FAILED("Couldn't Bind to socket");
return false;
}
std::cerr << " Successfully Bound Socket to Address" << std::endl;
unix_close(sockfd);
return true;
}

View File

@ -0,0 +1,268 @@
/*
* libretroshare/src/pqi net_test.cc
*
* 3P/PQI network interface for RetroShare.
*
* 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".
*
*/
/******
* NETWORKING Test to check Big/Little Endian behaviour
* as well as socket behaviour
*
*/
#include "pqi/pqinetwork.h"
#include "util/rsnet.h"
#include <iostream>
#include <sstream>
#include "util/utest.h"
const char * loopback_addrstr = "127.0.0.1";
const char * localnet1_addrstr = "10.0.0.1";
const char * localnet2_addrstr = "169.254.0.1";
const char * localnet3_addrstr = "172.16.0.1";
const char * localnet4_addrstr = "192.168.1.1";
const char * localnet5_addrstr = "10.4.28.34";
const char * localnet6_addrstr = "169.254.1.81";
const char * localnet7_addrstr = "172.20.9.201";
const char * localnet8_addrstr = "192.168.1.254";
const char * external_addrstr = "74.125.19.99"; /* google */
const char * invalid_addrstr = "AAA.BBB.256.256";
int test_isExternalNet();
int test_isPrivateNet();
int test_isLoopbackNet();
int test_sameNet();
int test_isValidNet();
int test_isSameSubnet();
int test_pqi_inet_netof();
INITTEST();
int main(int argc, char **argv)
{
std::cerr << "net_test1" << std::endl;
test_isExternalNet();
test_isPrivateNet();
test_isLoopbackNet();
test_sameNet();
test_isValidNet();
test_isSameSubnet();
test_pqi_inet_netof();
FINALREPORT("net_test1");
return TESTRESULT();
}
int test_isExternalNet()
{
struct in_addr loopback_addr;
struct in_addr localnet1_addr;
struct in_addr localnet2_addr;
struct in_addr localnet3_addr;
struct in_addr localnet4_addr;
struct in_addr external_addr;
struct in_addr invalid_addr;
struct in_addr invalid_addr2;
inet_aton(loopback_addrstr, &loopback_addr);
inet_aton(localnet1_addrstr, &localnet1_addr);
inet_aton(localnet2_addrstr, &localnet2_addr);
inet_aton(localnet3_addrstr, &localnet3_addr);
inet_aton(localnet4_addrstr, &localnet4_addr);
inet_aton(external_addrstr, &external_addr);
invalid_addr.s_addr = 0;
invalid_addr2.s_addr = -1;
CHECK(isExternalNet(&loopback_addr)==false);
CHECK(isExternalNet(&localnet1_addr)==false);
CHECK(isExternalNet(&localnet2_addr)==false);
CHECK(isExternalNet(&localnet3_addr)==false);
CHECK(isExternalNet(&localnet4_addr)==false);
CHECK(isExternalNet(&external_addr)==true);
CHECK(isExternalNet(&invalid_addr)==false);
CHECK(isExternalNet(&invalid_addr2)==false);
REPORT("isExternalNet()");
return 1;
}
int test_isPrivateNet()
{
struct in_addr loopback_addr;
struct in_addr localnet1_addr;
struct in_addr localnet2_addr;
struct in_addr localnet3_addr;
struct in_addr localnet4_addr;
struct in_addr external_addr;
inet_aton(loopback_addrstr, &loopback_addr);
inet_aton(localnet1_addrstr, &localnet1_addr);
inet_aton(localnet2_addrstr, &localnet2_addr);
inet_aton(localnet3_addrstr, &localnet3_addr);
inet_aton(localnet4_addrstr, &localnet4_addr);
inet_aton(external_addrstr, &external_addr);
CHECK(isPrivateNet(&loopback_addr)==false); //loopback not considered a "private network"
CHECK(isPrivateNet(&localnet1_addr)==true);
CHECK(isPrivateNet(&localnet2_addr)==true);
CHECK(isPrivateNet(&localnet3_addr)==true);
CHECK(isPrivateNet(&localnet4_addr)==true);
CHECK(isPrivateNet(&external_addr)==false);
REPORT("isPrivateNet()");
return 1;
}
int test_isLoopbackNet()
{
struct in_addr loopback_addr;
struct in_addr localnet1_addr;
struct in_addr external_addr;
inet_aton(loopback_addrstr, &loopback_addr);
inet_aton(localnet1_addrstr, &localnet1_addr);
inet_aton(external_addrstr, &external_addr);
CHECK(isLoopbackNet(&loopback_addr)==true);
CHECK(isLoopbackNet(&localnet1_addr)==false);
CHECK(isLoopbackNet(&external_addr)==false);
REPORT("isLoopbackNet()");
return 1;
}
int test_sameNet()
{
struct in_addr localnet1_addr;
struct in_addr localnet2_addr;
struct in_addr localnet3_addr;
struct in_addr localnet4_addr;
struct in_addr localnet5_addr;
struct in_addr localnet6_addr;
struct in_addr localnet7_addr;
struct in_addr localnet8_addr;
struct in_addr external_addr;
inet_aton(localnet1_addrstr, &localnet1_addr);
inet_aton(localnet2_addrstr, &localnet2_addr);
inet_aton(localnet3_addrstr, &localnet3_addr);
inet_aton(localnet4_addrstr, &localnet4_addr);
inet_aton(localnet5_addrstr, &localnet5_addr);
inet_aton(localnet6_addrstr, &localnet6_addr);
inet_aton(localnet7_addrstr, &localnet7_addr);
inet_aton(localnet8_addrstr, &localnet8_addr);
inet_aton(external_addrstr, &external_addr);
CHECK(sameNet(&localnet1_addr, &localnet5_addr)==true);
CHECK(sameNet(&localnet2_addr, &localnet6_addr)==true);
CHECK(sameNet(&localnet3_addr, &localnet7_addr)==true);
CHECK(sameNet(&localnet4_addr, &localnet8_addr)==true);
CHECK(sameNet(&localnet1_addr, &external_addr)==false);
CHECK(sameNet(&localnet2_addr, &external_addr)==false);
CHECK(sameNet(&localnet3_addr, &external_addr)==false);
CHECK(sameNet(&localnet4_addr, &external_addr)==false);
REPORT("sameNet()");
return 1;
}
int test_isValidNet()
{
struct in_addr localnet1_addr;
struct in_addr invalid_addr;
inet_aton(localnet1_addrstr, &localnet1_addr);
CHECK(isValidNet(&localnet1_addr)==true);
CHECK(inet_aton(invalid_addrstr, &invalid_addr)==0);
std::cerr << inet_ntoa(invalid_addr) << std::endl;
//CHECK(isValidNet(&invalid_addr)==false);
REPORT("isValidNet()");
return 1;
}
int test_isSameSubnet()
{
struct in_addr localnet1_addr;
struct in_addr classc1_addr;
struct in_addr classc2_addr;
inet_aton(localnet1_addrstr, &localnet1_addr);
//random class C addresses
inet_aton("197.67.28.93", &classc1_addr);
inet_aton("197.67.28.3", &classc2_addr);
CHECK(isSameSubnet(&localnet1_addr, &classc1_addr)==false);
CHECK(isSameSubnet(&classc1_addr, &classc2_addr)==true);
REPORT("isSameSubnet()");
return 1;
}
int test_pqi_inet_netof()
{
struct in_addr localnet1_addr;
struct in_addr localnet2_addr;
struct in_addr localnet3_addr;
struct in_addr localnet4_addr;
struct in_addr localnet5_addr;
struct in_addr localnet6_addr;
struct in_addr localnet7_addr;
struct in_addr localnet8_addr;
struct in_addr external_addr;
inet_aton(localnet1_addrstr, &localnet1_addr);
inet_aton(localnet2_addrstr, &localnet2_addr);
inet_aton(localnet3_addrstr, &localnet3_addr);
inet_aton(localnet4_addrstr, &localnet4_addr);
inet_aton(localnet5_addrstr, &localnet5_addr);
inet_aton(localnet6_addrstr, &localnet6_addr);
inet_aton(localnet7_addrstr, &localnet7_addr);
inet_aton(localnet8_addrstr, &localnet8_addr);
inet_aton(external_addrstr, &external_addr);
CHECK(pqi_inet_netof(localnet1_addr)==htonl(10<<24));
CHECK(pqi_inet_netof(localnet2_addr)==htonl(169<<24 | 254<<16));
CHECK(pqi_inet_netof(localnet3_addr)==htonl(172<<24 | 16<<16));
CHECK(pqi_inet_netof(localnet4_addr)==htonl(192<<24 | 168<<16 | 1<<8));
CHECK(pqi_inet_netof(localnet5_addr)==htonl(10<<24));
CHECK(pqi_inet_netof(localnet6_addr)==htonl(169<<24 | 254<<16));
CHECK(pqi_inet_netof(localnet7_addr)==htonl(172<<24 | 20<<16));
CHECK(pqi_inet_netof(localnet8_addr)==htonl(192<<24 | 168<<16 | 1<<8));
CHECK(pqi_inet_netof(external_addr)==htonl(74<<24));
REPORT("pqi_inet_netof()");
return 1;
}

View File

@ -0,0 +1,63 @@
/*
* libretroshare/src/pqi net_test.cc
*
* 3P/PQI network interface for RetroShare.
*
* 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".
*
*/
/******
* NETWORKING Test to check Big/Little Endian behaviour
* as well as socket behaviour
*
*/
#include "pqi/pqinetwork.h"
#include "util/rsnet.h"
#include <iostream>
#include <sstream>
bool test_iface();
int main(int argc, char **argv)
{
test_iface();
return 1;
}
/* test 1: byte manipulation */
bool test_iface()
{
struct in_addr pref_iface = getPreferredInterface();
std::list<std::string> ifaces = getLocalInterfaces();
std::list<std::string>::iterator it;
std::cerr << "test_iface()" << std::endl;
for(it = ifaces.begin(); it != ifaces.end(); it++)
{
std::cerr << "available iface: " << *it << std::endl;
}
std::cerr << "preferred " << inet_ntoa(pref_iface) << std::endl;
return true;
}