aria2/test/DHTRoutingTableTest.cc
Tatsuhiro Tsujikawa 0f13363229 2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
	DHT is disabled by default. To enable it, give --enable-dht to 
aria2c.
	You may need to specify entry point to DHT network using
	--dht-entry-point. DHT uses UDP port to listen incoming message.
	Use --dht-listen-port to specify port number. Make sure that 
your
	firewall configuration can pass through UDP traffic to the port.
	The routing table is saved in $HOME/.aria2/dht.dat.
	* src/DHT*
	* src/BNode.{h, cc}
	* src/PeerInteractionCommand.cc: enable DHT functionality for a
	particular torrent.
	* src/Data.cc: Rewritten ctor.
	* src/OptionHandlerFactory.cc: Added --enable-dht, 
--dht-listen-port,
	--dht-entry-point.
	* src/DefaultBtInteractive.cc: Send port message if dht is 
enabled.
	* src/RequestGroup.cc: Initialize DHT functionality. When 
download
	ends, remove BtContext from DHTPeerAnnounceStorage.
	* src/BtPortMessage.{h, cc}: Rewritten.
	* src/message.h
	* src/OptionHandlerImpl.cc
	* src/option_processing.cc: Added --enable-dht, 
--dht-listen-port,
	--dht-entry-point.
	* src/Dictionary.{h, cc} (remove): New function.
	* src/prefs.h
	* src/DefaultBtMessageFactory.h
	* src/BtHandshakeMessage.cc
	* src/ActivePeerConnectionCommand.cc
	* src/SocketCore.{h, cc}: Added datagram socket support.
	* src/DefaultBtMessageFactory.cc
	* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
	Create DHT commands.
	* src/BtMessageFactory.h
	* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00

85 lines
2.2 KiB
C++

#include "DHTRoutingTable.h"
#include "Exception.h"
#include "Util.h"
#include "DHTNode.h"
#include "DHTBucket.h"
#include "MockDHTTaskQueue.h"
#include "MockDHTTaskFactory.h"
#include "DHTTask.h"
#include <cppunit/extensions/HelperMacros.h>
class DHTRoutingTableTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTRoutingTableTest);
CPPUNIT_TEST(testAddNode);
CPPUNIT_TEST(testGetClosestKNodes);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testAddNode();
void testGetClosestKNodes();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTRoutingTableTest);
void DHTRoutingTableTest::testAddNode()
{
DHTRoutingTable table(new DHTNode());
table.setTaskFactory(new MockDHTTaskFactory());
table.setTaskQueue(new MockDHTTaskQueue());
uint32_t count = 0;
for(int i = 0; i < 100; ++i) {
if(table.addNode(new DHTNode())) {
++count;
}
}
table.showBuckets();
}
static void createID(unsigned char* id, unsigned char firstChar, unsigned char lastChar)
{
memset(id, 0, DHT_ID_LENGTH);
id[0] = firstChar;
id[DHT_ID_LENGTH-1] = lastChar;
}
void DHTRoutingTableTest::testGetClosestKNodes()
{
unsigned char id[DHT_ID_LENGTH];
createID(id, 0x81, 0);
DHTNodeHandle localNode = new DHTNode(id);
DHTRoutingTable table(localNode);
DHTNodeHandle nodes1[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
DHTNodeHandle nodes2[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
DHTNodeHandle nodes3[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
for(size_t i = 0; i < DHTBucket::K; ++i) {
createID(id, 0xf0, i);
nodes1[i] = new DHTNode(id);
CPPUNIT_ASSERT(table.addNode(nodes1[i]));
}
for(size_t i = 0; i < DHTBucket::K; ++i) {
createID(id, 0x80, i);
nodes2[i] = new DHTNode(id);
CPPUNIT_ASSERT(table.addNode(nodes2[i]));
}
for(size_t i = 0; i < DHTBucket::K; ++i) {
createID(id, 0x70, i);
nodes3[i] = new DHTNode(id);
CPPUNIT_ASSERT(table.addNode(nodes3[i]));
}
{
createID(id, 0x80, 0x10);
DHTNodes nodes = table.getClosestKNodes(id);
CPPUNIT_ASSERT_EQUAL((size_t)8, nodes.size());
for(size_t i = 0; i < nodes.size(); ++i) {
CPPUNIT_ASSERT(memcmp(nodes2[0]->getID(), nodes[0]->getID(), DHT_ID_LENGTH) == 0);
}
}
}