mirror of
https://github.com/aria2/aria2.git
synced 2025-01-09 03:21:33 +00:00
236e64cb25
Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
114 lines
3.0 KiB
C++
114 lines
3.0 KiB
C++
#include "DHTRoutingTable.h"
|
|
|
|
#include <cstring>
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include "Exception.h"
|
|
#include "util.h"
|
|
#include "DHTNode.h"
|
|
#include "DHTBucket.h"
|
|
#include "MockDHTTaskQueue.h"
|
|
#include "MockDHTTaskFactory.h"
|
|
#include "DHTTask.h"
|
|
|
|
namespace aria2 {
|
|
|
|
class DHTRoutingTableTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(DHTRoutingTableTest);
|
|
CPPUNIT_TEST(testAddNode);
|
|
CPPUNIT_TEST(testAddNode_localNode);
|
|
CPPUNIT_TEST(testGetClosestKNodes);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
public:
|
|
void setUp() {}
|
|
|
|
void tearDown() {}
|
|
|
|
void testAddNode();
|
|
void testAddNode_localNode();
|
|
void testGetClosestKNodes();
|
|
};
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DHTRoutingTableTest);
|
|
|
|
void DHTRoutingTableTest::testAddNode()
|
|
{
|
|
SharedHandle<DHTNode> localNode(new DHTNode());
|
|
DHTRoutingTable table(localNode);
|
|
SharedHandle<MockDHTTaskFactory> taskFactory(new MockDHTTaskFactory());
|
|
table.setTaskFactory(taskFactory);
|
|
SharedHandle<MockDHTTaskQueue> taskQueue(new MockDHTTaskQueue());
|
|
table.setTaskQueue(taskQueue);
|
|
uint32_t count = 0;
|
|
for(int i = 0; i < 100; ++i) {
|
|
SharedHandle<DHTNode> node(new DHTNode());
|
|
if(table.addNode(node)) {
|
|
++count;
|
|
}
|
|
}
|
|
table.showBuckets();
|
|
}
|
|
|
|
void DHTRoutingTableTest::testAddNode_localNode()
|
|
{
|
|
SharedHandle<DHTNode> localNode(new DHTNode());
|
|
DHTRoutingTable table(localNode);
|
|
SharedHandle<MockDHTTaskFactory> taskFactory(new MockDHTTaskFactory());
|
|
table.setTaskFactory(taskFactory);
|
|
SharedHandle<MockDHTTaskQueue> taskQueue(new MockDHTTaskQueue());
|
|
table.setTaskQueue(taskQueue);
|
|
|
|
SharedHandle<DHTNode> newNode(new DHTNode(localNode->getID()));
|
|
CPPUNIT_ASSERT(!table.addNode(newNode));
|
|
}
|
|
|
|
namespace {
|
|
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;
|
|
}
|
|
} // namespace
|
|
|
|
void DHTRoutingTableTest::testGetClosestKNodes()
|
|
{
|
|
unsigned char id[DHT_ID_LENGTH];
|
|
createID(id, 0x81, 0);
|
|
SharedHandle<DHTNode> localNode(new DHTNode(id));
|
|
|
|
DHTRoutingTable table(localNode);
|
|
|
|
SharedHandle<DHTNode> nodes1[8];
|
|
SharedHandle<DHTNode> nodes2[8];
|
|
SharedHandle<DHTNode> nodes3[8];
|
|
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
|
createID(id, 0xf0, i);
|
|
nodes1[i].reset(new DHTNode(id));
|
|
CPPUNIT_ASSERT(table.addNode(nodes1[i]));
|
|
}
|
|
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
|
createID(id, 0x80, i);
|
|
nodes2[i].reset(new DHTNode(id));
|
|
CPPUNIT_ASSERT(table.addNode(nodes2[i]));
|
|
}
|
|
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
|
createID(id, 0x70, i);
|
|
nodes3[i].reset(new DHTNode(id));
|
|
CPPUNIT_ASSERT(table.addNode(nodes3[i]));
|
|
}
|
|
{
|
|
createID(id, 0x80, 0x10);
|
|
std::vector<SharedHandle<DHTNode> > nodes;
|
|
table.getClosestKNodes(nodes, 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace aria2
|