forked from lukablurr/n2n_v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sn_multiple_test.c
572 lines (462 loc) · 12.6 KB
/
sn_multiple_test.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* sn_multiple_test.c
*
* Created on: Mar 25, 2012
* Author: Costin Lupu
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "n2n.h"
#include "n2n_list.h"
#include "sn_multiple.h"
#include "sn_multiple_wire.h"
typedef int (*enc_func) (uint8_t *base,
size_t *idx,
const snm_hdr_t *hdr,
const void *msg);
typedef int (*dec_func) (void *msg,
const snm_hdr_t *hdr,
const uint8_t *base,
size_t *rem,
size_t *idx);
typedef void (*rand_func) (void *item);
typedef void (*add_func) (struct n2n_list *list, struct n2n_list *new);
typedef size_t (*size_func) (const struct n2n_list *list);
typedef size_t (*clear_func) (struct n2n_list *list);
struct list_ops {
size_t item_size;
rand_func rand;
read_entry_func read_entry;
write_entry_func write_entry;
};
/*
* SUPERNODE
*/
static void rand_n2n_sock(n2n_sock_t *sn)
{
sn->family = AF_INET;
unsigned int ipv4 = random() % 0xFFFFFFFF;
memcpy(sn->addr.v4, &ipv4, IPV4_SIZE);
sn->port = random() % ((1 << 16) - 1);
}
static void rand_sn(void *item)
{
struct sn_info *si = (struct sn_info *) item;
list_init(&si->list);
rand_n2n_sock(&si->sn);
}
struct list_ops sn_list_ops = {
.item_size = sizeof(struct sn_info),
.rand = rand_sn,
.read_entry = read_sn,
.write_entry = write_sn
};
/*
* COMMUNITY
*/
static void rand_comm(void *item)
{
struct comm_info *ci = (struct comm_info *) item;
int i;
list_init(&ci->list);
ci->sn_num = random() % N2N_MAX_SN_PER_COMM;
for (i = 0; i < ci->sn_num; i++)
{
rand_n2n_sock(&ci->sn_sock[i]);
}
for (; i < N2N_MAX_SN_PER_COMM; i++)
{
memset(&ci->sn_sock[i], 0, sizeof(n2n_sock_t));
}
int name_size = random() % (N2N_COMMUNITY_SIZE - 1) + 1;
for (i = 0; i < name_size; i++)
{
ci->name[i] = random() % ('z' - 'a') + 'a';
}
ci->name[i] = 0;
}
struct list_ops comm_list_ops = {
.item_size = sizeof(struct comm_info),
.rand = rand_comm,
.read_entry = read_community,
.write_entry = write_community
};
/*
* RANDOMIZE
*/
static size_t generate_random_list(struct n2n_list *list,
struct list_ops *ops)
{
int i = 0, size = random() % 100;
traceNormal("Generating %d list items\n", size);
for (i = 0; i < size; i++)
{
void *item = calloc(1, ops->item_size);
ops->rand(item);
list_add(list, item);
}
return size;
}
/*
* COMPARE functions
*/
static int cmp_SNM_REQ(const void *a, const void *b)
{
const n2n_SNM_REQ_t *reqA = (const n2n_SNM_REQ_t *) a;
const n2n_SNM_REQ_t *reqB = (const n2n_SNM_REQ_t *) b;
int i, diff = reqA->comm_num - reqB->comm_num;
if (diff)
{
return diff;
}
for(i = 0; i < reqA->comm_num; i++)
{
diff = memcmp(reqA->comm_ptr[i].name,
reqB->comm_ptr[i].name,
reqA->comm_ptr[i].size);
if (diff)
{
return diff;
}
}
return 0;
}
static int cmp_SNM_INFO(const void *a, const void *b)
{
const n2n_SNM_INFO_t *infoA = (const n2n_SNM_INFO_t *) a;
const n2n_SNM_INFO_t *infoB = (const n2n_SNM_INFO_t *) b;
int i, diff = infoA->sn_num - infoB->sn_num;
if (diff)
{
return diff;
}
diff = infoA->comm_num - infoB->comm_num;
if (diff)
{
return diff;
}
for(i = 0; i < infoA->sn_num; i++)
{
diff = sock_equal(infoA->sn_ptr + i, infoB->sn_ptr + i);
if (diff)
{
return diff;
}
}
for(i = 0; i < infoA->comm_num; i++)
{
diff = memcmp(infoA->comm_ptr[i].name,
infoB->comm_ptr[i].name,
infoA->comm_ptr[i].size);
if (diff)
{
return diff;
}
}
return 0;
}
static int cmp_SNM_ADV(const void *a, const void *b)
{
const n2n_SNM_ADV_t *advA = (const n2n_SNM_ADV_t *) a;
const n2n_SNM_ADV_t *advB = (const n2n_SNM_ADV_t *) b;
int i, diff = memcmp(&advA->sn, &advB->sn, sizeof(n2n_sock_t));
if (diff)
{
return diff;
}
diff = advA->comm_num - advB->comm_num;
if (diff)
{
return diff;
}
for(i = 0; i < advA->comm_num; i++)
{
diff = memcmp(advA->comm_ptr[i].name,
advB->comm_ptr[i].name,
advA->comm_ptr[i].size);
if (diff)
{
return diff;
}
}
return 0;
}
/*
* TESTING functions for MESSAGES
*/
struct SNM_msg_ops
{
size_t struct_size;
enc_func enc;
dec_func dec;
cmp_func cmp;
};
struct SNM_msg_ops SNM_REQ_ops = {
.struct_size = sizeof(n2n_SNM_REQ_t),
.enc = (enc_func) encode_SNM_REQ,
.dec = (dec_func) decode_SNM_REQ,
.cmp = cmp_SNM_REQ
};
struct SNM_msg_ops SNM_INFO_ops = {
.struct_size = sizeof(n2n_SNM_INFO_t),
.enc = (enc_func) encode_SNM_INFO,
.dec = (dec_func) decode_SNM_INFO,
.cmp = cmp_SNM_INFO
};
struct SNM_msg_ops SNM_ADV_ops = {
.struct_size = sizeof(n2n_SNM_ADV_t),
.enc = (enc_func) encode_SNM_ADV,
.dec = (dec_func) decode_SNM_ADV,
.cmp = cmp_SNM_ADV
};
static int test_SNM_MSG(struct SNM_msg_ops *ops,
const snm_hdr_t *hdr,
const void *msg,
size_t size)
{
uint8_t buf[1024 * 1024];
snm_hdr_t new_hdr;
void *new_msg = NULL;
size_t idx = 0, rem = size;
if (ops->enc(buf, &idx, hdr, msg) != idx)
{
traceError("Error encoding message");
return -1;
}
new_msg = calloc(1, ops->struct_size);
rem = idx;
idx = 0;
if (decode_SNM_hdr(&new_hdr, buf, &rem, &idx) < 0)
{
traceError("Failed to decode header");
goto test_SNM_MSG_err;
}
log_SNM_hdr(&new_hdr);
if (ops->dec(new_msg, &new_hdr, buf, &rem, &idx) < 0)
{
traceError("Error decoding message");
goto test_SNM_MSG_err;
}
if (ops->cmp(msg, new_msg))
{
traceError("Mismatched messages");
goto test_SNM_MSG_err;
}
if (hdr->type == SNM_TYPE_REQ_LIST_MSG)
{
log_SNM_REQ(new_msg);
free_communities(&((n2n_SNM_REQ_t *) new_msg)->comm_ptr);
}
else if (hdr->type == SNM_TYPE_RSP_LIST_MSG)
{
log_SNM_INFO(new_msg);
free_supernodes(&((n2n_SNM_INFO_t *) new_msg)->sn_ptr);
free_communities(&((n2n_SNM_INFO_t *) new_msg)->comm_ptr);
}
else if(hdr->type == SNM_TYPE_ADV_MSG)
{
log_SNM_ADV(new_msg);
free_communities(&((n2n_SNM_ADV_t *) new_msg)->comm_ptr);
}
free(new_msg);
return 0;
test_SNM_MSG_err:
free(new_msg);
return -1;
}
static void test_REQ_LIST()
{
snm_hdr_t hdr = {SNM_TYPE_REQ_LIST_MSG, 0, 3134};
n2n_SNM_REQ_t req;
size_t size = 0;
size_t lst_size = 0;
struct n2n_list communities = { NULL };
traceNormal("---- Testing SNM REQUEST message");
SET_N(hdr.flags);
lst_size = generate_random_list(&communities, &comm_list_ops);
req.comm_num = list_size(&communities);
alloc_communities(&req.comm_ptr, req.comm_num);
struct comm_info *ci = NULL;
int i = 0;
N2N_LIST_FOR_EACH_ENTRY(ci, &communities)
{
req.comm_ptr[i].size = strlen((char *) ci->name);
memcpy(&req.comm_ptr[i].name, ci->name, sizeof(n2n_community_t));
i++;
}
log_SNM_hdr(&hdr);
log_SNM_REQ(&req);
if (test_SNM_MSG(&SNM_REQ_ops, &hdr, &req, size))
{
traceError("Error testing n2n_SNM_REQ_t");
}
free_communities(&req.comm_ptr);
list_clear(&communities);
traceNormal("---- End testing SNM REQUEST message");
}
static int sn_cmp_timestamp_asc(const void *l, const void *r)
{
return (((const struct sn_info *)l)->timestamp -
((const struct sn_info *)r)->timestamp);
}
static void test_sn_sort(struct n2n_list *list)
{
struct sn_info *sni = NULL;
N2N_LIST_FOR_EACH_ENTRY(sni, list)
{
/* set random timestamps */
sni->timestamp = random();
}
list_sort(list, sn_cmp_timestamp_asc);
int prev_timestamp = 0;
N2N_LIST_FOR_EACH_ENTRY(sni, list)
{
if (sni->timestamp < prev_timestamp)
{
traceError("Sort testing failed");
return;
}
//traceNormal("--- %d", sni->last_seen);
prev_timestamp = sni->timestamp;
}
traceNormal("--- Sort testing succeeded");
}
static void test_INFO()
{
snm_hdr_t req_hdr = {SNM_TYPE_REQ_LIST_MSG, 0, 3134};
n2n_SNM_REQ_t req;
snm_hdr_t rsp_hdr;
n2n_SNM_INFO_t rsp;
size_t size = 0;
traceNormal("---- Testing SNM INFO message");
SET_S(req_hdr.flags);
SET_C(req_hdr.flags);
sn_list_t supernodes = { {NULL}, {0} };
generate_random_list(&supernodes.head, &sn_list_ops);
comm_list_t communities = { {NULL}, {NULL}, {0} };
generate_random_list(&communities.head, &comm_list_ops);
test_sn_sort(&supernodes.head);
build_snm_info(0, &supernodes, &communities, &req_hdr, &req, &rsp_hdr, &rsp);
list_clear(&supernodes.head);
list_clear(&communities.head);
req_hdr.type = SNM_TYPE_RSP_LIST_MSG;
log_SNM_hdr(&req_hdr);
log_SNM_INFO(&rsp);
if (test_SNM_MSG(&SNM_INFO_ops, &req_hdr, &rsp, size))
{
traceError("Error testing n2n_SNM_INFO_t");
}
clear_snm_info(&rsp);
traceNormal("---- End testing SNM INFO message");
}
static void test_ADV()
{
snm_hdr_t hdr = {SNM_TYPE_ADV_MSG, 0, 5463};
n2n_SNM_ADV_t adv;
size_t size = 0;
comm_list_t communities = { {NULL}, {NULL}, {0} };
traceNormal("---- Testing SNM ADV message");
SET_N(hdr.flags);
generate_random_list(&communities.head, &comm_list_ops);
int sock = open_socket(45555, 1);
build_snm_adv(sock, &communities.head, &hdr, &adv);
closesocket(sock);
if (test_SNM_MSG(&SNM_ADV_ops, &hdr, &adv, size))
{
traceError("Error testing n2n_SNM_ADV_t");
}
traceNormal("---- End testing SNM ADV message");
}
/*
* LISTS TESTS
*/
static int test_write_read_list(struct n2n_list *list,
struct list_ops *ops)
{
const char *filename = "tmp_list";
struct n2n_list new_list = { NULL };
if (write_list_to_file(filename, list, ops->write_entry))
{
traceError("Error writing list");
goto out_err;
}
if (read_list_from_file(filename, &new_list, ops->read_entry))
{
traceError("Error reading list");
goto out_err;
}
size_t size = list_size(list);
size_t new_size = list_size(&new_list);
if (size != new_size)
{
traceError("Mismatched list sizes: %d instead of %d", new_size, size);
goto out_err;
}
struct n2n_list *old_entry = list->next;
struct n2n_list *new_entry = NULL;
int i = 0;
LIST_FOR_EACH(new_entry, &new_list)
{
#define HDR_SIZE sizeof(struct n2n_list)
#define ITEM_DATA(item) (void *) ((unsigned int) (item) + HDR_SIZE)
if (memcmp(ITEM_DATA(old_entry), ITEM_DATA(new_entry), ops->item_size - HDR_SIZE))
{
traceError("Mismatched item %d", i);
goto out_err;
}
old_entry = old_entry->next;
i++;
}
if (list_clear(&new_list) != size)
{
traceError("Error clearing list");
goto out_err;
}
unlink(filename);
return 0;
out_err:
unlink(filename);
return -1;
}
static void test_list(struct list_ops *ops)
{
struct n2n_list list = { NULL };
size_t size = generate_random_list(&list, ops);
if (list_size(&list) != size)
{
traceError("Mismatched list size");
return;
}
if (test_write_read_list(&list, ops))
{
traceError("Error write/read test");
}
if (list_clear(&list) != size)
{
traceError("Error clearing list");
}
}
static void test_sn()
{
traceNormal("--- Testing supernodes lists IO");
test_list(&sn_list_ops);
traceNormal("--- End testing supernodes lists IO");
}
static void test_comm()
{
traceNormal("--- Testing community lists IO");
test_list(&comm_list_ops);
traceNormal("--- End testing community lists IO");
}
int main()
{
srandom(time(NULL));
test_REQ_LIST();
test_INFO();
test_ADV();
test_sn();
test_comm();
return 0;
}