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 "unity.h"
#include "proto.h"
#include "log.h"
#include <string.h>
void test_begin_pings_bootstrap_node() {
struct message outbuff[10] = {0};
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
struct message* message_cursor = outbuff;
proto_begin(&dht, time(NULL), &message_cursor, outbuff+10);
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
TEST_ASSERT_EQUAL(91, outbuff[0].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[0].payload, 43);
TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[0].payload+63, 28);
}
void test_response_from_initial_probe() {
struct message outbuff[2] = {0};
struct sockaddr_storage remote;
socklen_t remote_len;
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, time(NULL), &message_cursor, outbuff+2);
remote_len = outbuff[0].dest_len;
memcpy(&remote, &outbuff[0].dest, remote_len);
}
char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes26:bbbbbbbbbbbbbbbbbbbb\xFF\xFF\xFF\xFF\x00\x00""ee";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, 10, &message_cursor, outbuff+2);
TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Sends one packet");
TEST_ASSERT_EQUAL(91, outbuff[0].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[0].payload, 43);
TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:11:y1:qe", outbuff[0].payload+63, 28);
// The queried node gets added to the routing table
struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
struct entry* entry = routing_get(&other);
TEST_ASSERT_NOT_NULL(entry);
TEST_ASSERT_EQUAL(((struct sockaddr_in*)&remote)->sin_addr.s_addr, entry->addr.ip);
TEST_ASSERT_EQUAL(((struct sockaddr_in*)&remote)->sin_port, entry->addr.port);
TEST_ASSERT_EQUAL(PROTO_UNCTM+10, entry->expire);
}
void test_reponse_from_wrong_ip() {
struct message outbuff[2] = {0};
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, time(NULL), &message_cursor, outbuff+2);
}
// This is fragile, since the ip of the bootstrap node could change, and we
// look it up from DNS. Although it pretty unlikely that it would change to
// this ip
struct sockaddr_in remote;
remote.sin_family = AF_INET;
inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr.s_addr);
remote.sin_port = htons(6881);
char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes26:bbbbbbbbbbbbbbbbbbbb\xFF\xFF\xFF\xFF\x00\x00""ee";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), &remote, sizeof(remote), 0, &message_cursor, outbuff+2);
// We shouldn't send any packets, since the response is rejected
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff);
// Since the ip was wrong we should not have accepted the node into the
// rounting table
struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
struct entry* entry = routing_get(&other);
TEST_ASSERT_NULL(entry);
}
void test_ping() {
struct message outbuff[2] = {0};
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, time(NULL), &message_cursor, outbuff+2);
}
struct sockaddr_in remote;
remote.sin_family = AF_INET;
inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr.s_addr);
remote.sin_port = htons(6881);
char buff[] = "d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), &remote, sizeof(remote), 0, &message_cursor, outbuff+2);
// We should have sent a response
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
TEST_ASSERT_EQUAL(48, outbuff[0].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBBee", outbuff[0].payload, 47);
}
void test_unknown_method() {
struct message outbuff[2] = {0};
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, time(NULL), &message_cursor, outbuff+2);
}
struct sockaddr_in remote;
remote.sin_family = AF_INET;
inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr.s_addr);
remote.sin_port = htons(6881);
char buff[] = "d1:q4:fake1:t2:aa1:y1:qe";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), &remote, sizeof(remote), 0, &message_cursor, outbuff+2);
// We should have sent a response
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
TEST_ASSERT_EQUAL(42, outbuff[0].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:e1:eli204e14:Unknown Methodee", outbuff[0].payload, 42);
}
void test_note_times_out() {
routing_flush();
struct message outbuff[2] = {0};
struct sockaddr_storage remote;
socklen_t remote_len;
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, 0, &message_cursor, outbuff+2);
remote_len = outbuff[0].dest_len;
memcpy(&remote, &outbuff[0].dest, remote_len);
}
// The node responds to the ping at t=5
{
char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes26:bbbbbbbbbbbbbbbbbbbb\xFF\xFF\xFF\xFF\x00\x00""ee";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, 5, &message_cursor, outbuff+2);
}
// 15 minutes later a timeout is fired
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)&remote, remote_len, 905, &message_cursor, outbuff+2);
// Which should create a retry ping and a ping for the (now) uncertain node
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+2);
TEST_ASSERT_EQUAL(91, outbuff[1].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[1].payload, 43);
TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[1].payload+63, 28);
}
void test_response_after_retry() {
struct message outbuff[10] = {0};
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, 0, &message_cursor, outbuff+2);
}
// After TMOUT seconds we retry the ping
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, PROTO_TMOUT, &message_cursor, outbuff+2);
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
TEST_ASSERT_EQUAL(91, outbuff[0].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[0].payload, 43);
TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[0].payload+63, 28);
}
void test_remove_from_routing_after_3_retries() {
struct message outbuff[10] = {0};
time_t now = 0;
struct sockaddr_storage remote;
socklen_t remote_len;
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, 0, &message_cursor, outbuff+2);
remote_len = outbuff[0].dest_len;
memcpy(&remote, &outbuff[0].dest, remote_len);
}
now += 10;
{
// The node responds
// We return no new nodes to stop any new pings from going out
char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes0:""ee";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, now, &message_cursor, outbuff+2);
}
struct entry* entry = routing_get(&other);
TEST_ASSERT_NOT_NULL(entry);
now += PROTO_UNCTM;
{
// The node becomes uncertain
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Ping was not sent");
}
now += PROTO_TMOUT;
{
// 1st retry
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Ping was not sent");
}
now += PROTO_TMOUT;
{
// 2nd retry
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Ping was not sent");
}
now += PROTO_TMOUT;
// Drop the node
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff, "The timeout should send a message");
entry = routing_get(&other);
TEST_ASSERT_NULL(entry);
}
void test_ping_node_when_uncertain() {
struct message outbuff[10] = {0};
time_t now = 0;
struct sockaddr_storage remote;
socklen_t remote_len;
struct dht dht;
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
{
struct message* message_cursor = outbuff;
proto_begin(&dht, 0, &message_cursor, outbuff+2);
remote_len = outbuff[0].dest_len;
memcpy(&remote, &outbuff[0].dest, remote_len);
}
now += 10;
{
// The node responds
// We return no new nodes to stop any new pings from going out
char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes0:""ee";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, now, &message_cursor, outbuff+2);
}
struct entry* entry = routing_get(&other);
TEST_ASSERT_NOT_NULL(entry);
now += PROTO_UNCTM;
{
// The node becomes uncertain
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
// Which should create a ping
TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
TEST_ASSERT_EQUAL(91, outbuff[0].payload_len);
TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[0].payload, 43);
TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[0].payload+63, 28);
}
now += 5;
{
// The node responds
char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes0:""ee";
struct message* message_cursor = outbuff;
proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, now, &message_cursor, outbuff+2);
// The routing table entry should have its expiry time updated
struct entry* entry = routing_get(&other);
TEST_ASSERT_NOT_NULL(entry);
TEST_ASSERT_GREATER_THAN(now, entry->expire);
}
}
|