forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHTRoutingTableSerializerTest.cc
312 lines (271 loc) · 8.23 KB
/
DHTRoutingTableSerializerTest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "DHTRoutingTableSerializer.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "util.h"
#include "DHTNode.h"
#include "array_fun.h"
#include "DHTConstants.h"
#include "bittorrent_helper.h"
#include "a2netcompat.h"
namespace aria2 {
class DHTRoutingTableSerializerTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTRoutingTableSerializerTest);
CPPUNIT_TEST(testSerialize);
CPPUNIT_TEST(testSerialize6);
CPPUNIT_TEST_SUITE_END();
private:
char zero[256];
char buf[20];
void checkToLocalnode(std::istream& ss,
const std::shared_ptr<DHTNode>& localNode);
void checkNumNodes(std::istream& ss, size_t expected);
public:
void setUp() { memset(zero, 0, sizeof(zero)); }
void tearDown() {}
void testSerialize();
void testSerialize6();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTRoutingTableSerializerTest);
void DHTRoutingTableSerializerTest::checkToLocalnode(
std::istream& ss, const std::shared_ptr<DHTNode>& localNode)
{
// header
ss.read(buf, 8);
// magic
CPPUNIT_ASSERT((char)0xa1 == buf[0]);
CPPUNIT_ASSERT((char)0xa2 == buf[1]);
// format ID
CPPUNIT_ASSERT((char)0x02 == buf[2]);
// reserved
CPPUNIT_ASSERT((char)0x00 == buf[3]);
CPPUNIT_ASSERT((char)0x00 == buf[4]);
CPPUNIT_ASSERT((char)0x00 == buf[5]);
// version
CPPUNIT_ASSERT((char)0x00 == buf[6]);
CPPUNIT_ASSERT((char)0x03 == buf[7]);
// time
ss.read(buf, 8);
time_t time;
uint64_t timebuf;
memcpy(&timebuf, buf, sizeof(timebuf));
time = ntoh64(timebuf);
std::cerr << time << std::endl;
// localnode
// 8bytes reserved
ss.read(buf, 8);
CPPUNIT_ASSERT(memcmp(zero, buf, 8) == 0);
// localnode ID
ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(localNode->getID(), buf, DHT_ID_LENGTH) == 0);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
}
void DHTRoutingTableSerializerTest::checkNumNodes(std::istream& ss,
size_t expected)
{
// number of nodes saved
ss.read(buf, 4);
uint32_t numNodes;
memcpy(&numNodes, buf, sizeof(numNodes));
numNodes = ntohl(numNodes);
CPPUNIT_ASSERT_EQUAL((uint32_t)expected, numNodes);
}
void DHTRoutingTableSerializerTest::testSerialize()
{
std::shared_ptr<DHTNode> localNode(new DHTNode());
std::vector<std::shared_ptr<DHTNode>> nodes(3);
for (size_t i = 0; i < nodes.size(); ++i) {
nodes[i].reset(new DHTNode());
nodes[i]->setIPAddress("192.168.0." + util::uitos(i + 1));
nodes[i]->setPort(6881 + i);
}
nodes[1]->setIPAddress("non-numerical-name");
DHTRoutingTableSerializer s(AF_INET);
s.setLocalNode(localNode);
s.setNodes(nodes);
std::string filename =
A2_TEST_OUT_DIR "/aria2_DHTRoutingTableSerializerTest_testSerialize";
s.serialize(filename);
std::ifstream ss(filename.c_str(), std::ios::binary);
checkToLocalnode(ss, localNode);
size_t numNodes = 3;
checkNumNodes(ss, numNodes);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[0]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)6, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 6bytes compact peer info
ss.read(buf, 6);
{
std::pair<std::string, uint16_t> peer = bittorrent::unpackcompact(
reinterpret_cast<const unsigned char*>(buf), AF_INET);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peer.second);
}
// 2bytes reserved
ss.read(buf, 2);
CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0);
// 16bytes reserved
ss.read(buf, 16);
CPPUNIT_ASSERT(memcmp(zero, buf, 16) == 0);
// localnode ID
ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[0]->getID(), buf, DHT_ID_LENGTH) == 0);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[1]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)6, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 6bytes compact peer info
ss.read(buf, 6);
// zero filled because node[1]'s hostname is not numerical form
// deserializer should skip this entry
CPPUNIT_ASSERT(memcmp(zero, buf, 6) == 0);
// 2bytes reserved
ss.read(buf, 2);
CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0);
// 16bytes reserved
ss.read(buf, 16);
CPPUNIT_ASSERT(memcmp(zero, buf, 16) == 0);
// localnode ID
ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[1]->getID(), buf, DHT_ID_LENGTH) == 0);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[2]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)6, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 6bytes compact peer info
ss.read(buf, 6);
{
std::pair<std::string, uint16_t> peer = bittorrent::unpackcompact(
reinterpret_cast<const unsigned char*>(buf), AF_INET);
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peer.second);
}
// 2bytes reserved
ss.read(buf, 2);
CPPUNIT_ASSERT(memcmp(zero, buf, 2) == 0);
// 16bytes reserved
ss.read(buf, 16);
CPPUNIT_ASSERT(memcmp(zero, buf, 16) == 0);
// localnode ID
ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[2]->getID(), buf, DHT_ID_LENGTH) == 0);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// check to see stream ends
ss.read(buf, 1);
CPPUNIT_ASSERT_EQUAL((std::streamsize)0, ss.gcount());
CPPUNIT_ASSERT(ss.eof());
}
void DHTRoutingTableSerializerTest::testSerialize6()
{
std::shared_ptr<DHTNode> localNode(new DHTNode());
std::vector<std::shared_ptr<DHTNode>> nodes(2);
for (size_t i = 0; i < nodes.size(); ++i) {
nodes[i].reset(new DHTNode());
nodes[i]->setIPAddress("2001::100" + util::uitos(i + 1));
nodes[i]->setPort(6881 + i);
}
nodes[1]->setIPAddress("non-numerical-name");
DHTRoutingTableSerializer s(AF_INET6);
s.setLocalNode(localNode);
s.setNodes(nodes);
std::string filename =
A2_TEST_OUT_DIR "/aria2_DHTRoutingTableSerializerTest_testSerialize6";
s.serialize(filename);
std::ifstream ss(filename.c_str(), std::ios::binary);
checkToLocalnode(ss, localNode);
size_t numNodes = 2;
checkNumNodes(ss, numNodes);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[0]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)18, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 18 bytes compact peer info
ss.read(buf, 18);
{
std::pair<std::string, uint16_t> peer = bittorrent::unpackcompact(
reinterpret_cast<const unsigned char*>(buf), AF_INET6);
CPPUNIT_ASSERT_EQUAL(std::string("2001::1001"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peer.second);
}
// 6bytes reserved
ss.read(buf, 6);
CPPUNIT_ASSERT(memcmp(zero, buf, 6) == 0);
// localnode ID
ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[0]->getID(), buf, DHT_ID_LENGTH) == 0);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// node[1]
// 1byte compatc peer format length
{
uint8_t len;
ss >> len;
CPPUNIT_ASSERT_EQUAL((uint8_t)18, len);
}
// 7bytes reserved
ss.read(buf, 7);
CPPUNIT_ASSERT(memcmp(zero, buf, 7) == 0);
// 18bytes compact peer info
ss.read(buf, 18);
// zero filled because node[1]'s hostname is not numerical form
// deserializer should skip this entry
CPPUNIT_ASSERT(memcmp(zero, buf, 18) == 0);
// 6bytes reserved
ss.read(buf, 6);
CPPUNIT_ASSERT(memcmp(zero, buf, 6) == 0);
// localnode ID
ss.read(buf, DHT_ID_LENGTH);
CPPUNIT_ASSERT(memcmp(nodes[1]->getID(), buf, DHT_ID_LENGTH) == 0);
// 4bytes reserved
ss.read(buf, 4);
CPPUNIT_ASSERT(memcmp(zero, buf, 4) == 0);
// check to see stream ends
ss.read(buf, 1);
CPPUNIT_ASSERT_EQUAL((std::streamsize)0, ss.gcount());
CPPUNIT_ASSERT(ss.eof());
}
} // namespace aria2