forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHTRoutingTableTest.cc
111 lines (94 loc) · 2.93 KB
/
DHTRoutingTableTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#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));
}
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);
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::deque<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