Skip to content

Commit b9ef248

Browse files
committed
Test fix.
1 parent 405854e commit b9ef248

File tree

1 file changed

+81
-111
lines changed

1 file changed

+81
-111
lines changed

auto_tests/dht_test.c

Lines changed: 81 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -409,152 +409,123 @@ void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigne
409409
}
410410
}
411411

412+
#define NUM_DHT 100
413+
412414
void test_list_main()
413415
{
414-
uint8_t keys[DHT_LIST_LENGTH][crypto_box_PUBLICKEYBYTES];
415-
uint8_t cmp_list1[LCLIENT_LIST][crypto_box_PUBLICKEYBYTES + 1];
416-
uint8_t cmp_list2[LCLIENT_LIST][crypto_box_PUBLICKEYBYTES + 1];
416+
DHT *dhts[NUM_DHT];
417417

418+
uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][crypto_box_PUBLICKEYBYTES + 1];
418419
memset(cmp_list1, 0, sizeof(cmp_list1));
419-
memset(cmp_list2, 0, sizeof(cmp_list2));
420+
420421
IP ip;
421422
ip_init(&ip, 1);
422423

423-
unsigned int i, j;
424+
unsigned int i, j, k, l;
424425

425-
for (i = 0; i < DHT_LIST_LENGTH; ++i) {
426-
for (j = 0; j < crypto_box_PUBLICKEYBYTES; ++j)
427-
keys[i][j] = rand();
428-
}
426+
for (i = 0; i < NUM_DHT; ++i) {
427+
IP ip;
428+
ip_init(&ip, 1);
429429

430-
DHT *dht1 = new_DHT(new_networking(ip, DHT_DEFAULT_PORT)), *dht2 = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + 1));
430+
dhts[i] = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + i));
431+
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
432+
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
433+
}
431434

432-
ck_assert_msg(dht1 && dht2, "Failed to create DHTs");
433-
memcpy(dht2->self_public_key, dht1->self_public_key, crypto_box_PUBLICKEYBYTES);
434-
memcpy(dht2->self_secret_key, dht1->self_secret_key, crypto_box_SECRETKEYBYTES);
435+
for (j = 0; j < NUM_DHT; ++j) {
436+
for (i = 1; i < NUM_DHT; ++i) {
437+
test_add_to_list(cmp_list1[j], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[j]->self_public_key);
438+
}
439+
}
435440

436-
for (i = 0; i < DHT_LIST_LENGTH; ++i) {
437-
IP_Port ip_port;
438-
ip_init(&ip_port.ip, 0);
439-
ip_port.ip.ip4.uint32 = rand();
440-
ip_port.port = rand() % (UINT16_MAX - 1);
441-
++ip_port.port;
442-
addto_lists(dht1, ip_port, keys[i]);
443-
444-
ip_init(&ip_port.ip, 0);
445-
ip_port.ip.ip4.uint32 = rand();
446-
ip_port.port = rand() % (UINT16_MAX - 1);
447-
++ip_port.port;
448-
addto_lists(dht2, ip_port, keys[(DHT_LIST_LENGTH - (i + 1))]);
449-
450-
test_add_to_list(cmp_list1, LCLIENT_LIST, keys[i], dht1->self_public_key);
451-
test_add_to_list(cmp_list2, LCLIENT_LIST, keys[(DHT_LIST_LENGTH - (i + 1))], dht2->self_public_key);
441+
for (j = 0; j < NUM_DHT; ++j) {
442+
for (i = 0; i < NUM_DHT; ++i) {
443+
if (i == j)
444+
continue;
445+
446+
IP_Port ip_port;
447+
ip_init(&ip_port.ip, 0);
448+
ip_port.ip.ip4.uint32 = rand();
449+
ip_port.port = rand() % (UINT16_MAX - 1);
450+
++ip_port.port;
451+
addto_lists(dhts[j], ip_port, dhts[i]->self_public_key);
452+
}
452453
}
453454

454455
/*
455-
for (i = 0; i < LCLIENT_LIST; ++i) {
456+
print_pk(dhts[0]->self_public_key);
457+
458+
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
456459
printf("----Entry %u----\n", i);
457460
458461
print_pk(cmp_list1[i]);
459-
print_pk(cmp_list2[i]);
460-
print_pk(dht1->close_clientlist[i].public_key);
461-
print_pk(dht2->close_clientlist[i].public_key);
462462
}
463463
*/
464-
for (i = 0; i < LCLIENT_LIST; ++i) {
465-
uint8_t *pk = cmp_list1[i];
464+
unsigned int m_count = 0;
466465

467-
unsigned int count = 0;
466+
for (l = 0; l < NUM_DHT; ++l) {
467+
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
468+
for (j = 1; j < NUM_DHT; ++j) {
469+
if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, crypto_box_PUBLICKEYBYTES) != 0)
470+
continue;
468471

469-
for (j = 0; j < LCLIENT_LIST; ++j) {
470-
if (memcmp(pk, cmp_list2[j], crypto_box_PUBLICKEYBYTES) == 0)
471-
++count;
472-
}
472+
unsigned int count = 0;
473473

474-
ck_assert_msg(count == 1, "Bad distance formula");
475-
}
474+
for (k = 0; k < LCLIENT_LIST; ++k) {
475+
if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key,
476+
crypto_box_PUBLICKEYBYTES) == 0)
477+
++count;
478+
}
476479

477-
for (i = 0; i < LCLIENT_LIST; ++i) {
478-
uint8_t *pk = cmp_list1[i];
480+
if (count != 1) {
481+
print_pk(dhts[l]->self_public_key);
479482

480-
unsigned int count = 0;
483+
for (k = 0; k < MAX_FRIEND_CLIENTS; ++k) {
484+
printf("----Entry %u----\n", k);
481485

482-
for (j = 0; j < LCLIENT_LIST; ++j) {
483-
if (memcmp(pk, dht1->close_clientlist[j].public_key, crypto_box_PUBLICKEYBYTES) == 0)
484-
++count;
485-
}
486+
print_pk(cmp_list1[l][k]);
487+
}
486488

487-
ck_assert_msg(count == 1, "Keys not in first close list, %u, %X %X", i, cmp_list1[i][0], dht1->self_public_key[0]);
489+
for (k = 0; k < LCLIENT_LIST; ++k) {
490+
printf("----Closel %u----\n", k);
491+
print_pk(dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key);
492+
}
488493

489-
for (j = 0; j < LCLIENT_LIST; ++j) {
490-
if (memcmp(pk, dht2->close_clientlist[j].public_key, crypto_box_PUBLICKEYBYTES) == 0)
491-
++count;
492-
}
494+
print_pk(dhts[(l + j) % NUM_DHT]->self_public_key);
495+
}
493496

494-
ck_assert_msg(count == 2, "Keys not in second close list, %u, %X %X", i, cmp_list1[i][0], dht2->self_public_key[0]);
495-
}
496-
497-
uint8_t cmp_list3[MAX_SENT_NODES][crypto_box_PUBLICKEYBYTES + 1];
498-
memset(cmp_list3, 0, sizeof(cmp_list3));
497+
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
499498

500-
for (i = 0; i < DHT_LIST_LENGTH; ++i) {
501-
test_add_to_list(cmp_list3, MAX_SENT_NODES, keys[i], dht1->self_public_key);
502-
}
499+
Node_format ln[MAX_SENT_NODES];
500+
int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0);
501+
ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j);
503502

504-
Node_format n_list[MAX_SENT_NODES];
505-
ck_assert_msg(get_close_nodes(dht1, dht1->self_public_key, n_list, 0, 1, 0) == MAX_SENT_NODES,
506-
"Wrong number of nodes returned");
507-
/*
508-
for (i = 0; i < MAX_SENT_NODES; ++i) {
509-
printf("----Close %u----\n", i);
503+
count = 0;
510504

511-
print_pk(cmp_list3[i]);
512-
print_pk(n_list[i].public_key);
513-
}
514-
*/
505+
for (k = 0; k < MAX_SENT_NODES; ++k) {
506+
if (memcmp(dhts[l]->self_public_key, ln[k].public_key, crypto_box_PUBLICKEYBYTES) == 0)
507+
++count;
508+
}
515509

516-
for (i = 0; i < MAX_SENT_NODES; ++i) {
517-
uint8_t *pk = cmp_list3[i];
518-
unsigned int count = 0;
519-
520-
for (j = 0; j < MAX_SENT_NODES; ++j) {
521-
if (memcmp(pk, n_list[j].public_key, crypto_box_PUBLICKEYBYTES) == 0)
522-
++count;
510+
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
511+
/*
512+
for (k = 0; k < MAX_SENT_NODES; ++k) {
513+
printf("----gn %u----\n", k);
514+
print_pk(ln[k].public_key);
515+
}*/
516+
++m_count;
517+
}
523518
}
524-
525-
ck_assert_msg(count == 1, "Bad get_close_nodes");
526-
}
527-
528-
uint8_t cmp_new[crypto_box_PUBLICKEYBYTES];
529-
memcpy(cmp_new, cmp_list3[0], crypto_box_PUBLICKEYBYTES);
530-
ck_assert_msg(get_close_nodes(dht1, cmp_new, n_list, 0, 1, 0) == MAX_SENT_NODES,
531-
"Wrong number of nodes returned");
532-
533-
memset(cmp_list3, 0, sizeof(cmp_list3));
534-
535-
for (i = 0; i < DHT_LIST_LENGTH; ++i) {
536-
test_add_to_list(cmp_list3, MAX_SENT_NODES, keys[i], cmp_new);
537519
}
538520

539-
for (i = 0; i < MAX_SENT_NODES; ++i) {
540-
uint8_t *pk = cmp_list3[i];
541-
unsigned int count = 0;
542-
543-
for (j = 0; j < MAX_SENT_NODES; ++j) {
544-
if (memcmp(pk, n_list[j].public_key, crypto_box_PUBLICKEYBYTES) == 0)
545-
++count;
546-
}
521+
ck_assert_msg(m_count == (NUM_DHT) * (MAX_FRIEND_CLIENTS), "Bad count. %u != %u", m_count,
522+
(NUM_DHT) * (MAX_FRIEND_CLIENTS));
547523

548-
ck_assert_msg(count == 1, "Bad get_close_nodes");
524+
for (i = 0; i < NUM_DHT; ++i) {
525+
void *n = dhts[i]->net;
526+
kill_DHT(dhts[i]);
527+
kill_networking(n);
549528
}
550-
551-
void *n = dht1->net;
552-
kill_DHT(dht1);
553-
kill_networking(n);
554-
555-
n = dht2->net;
556-
kill_DHT(dht2);
557-
kill_networking(n);
558529
}
559530

560531

@@ -581,7 +552,6 @@ void ip_callback(void *data, int32_t number, IP_Port ip_port)
581552
#define c_sleep(x) usleep(1000*x)
582553
#endif
583554

584-
#define NUM_DHT 100
585555
#define NUM_DHT_FRIENDS 20
586556

587557
START_TEST(test_DHT_test)
@@ -669,7 +639,7 @@ Suite *dht_suite(void)
669639

670640
//DEFTESTCASE(addto_lists_ipv4);
671641
//DEFTESTCASE(addto_lists_ipv6);
672-
DEFTESTCASE(list);
642+
DEFTESTCASE_SLOW(list, 20);
673643
DEFTESTCASE_SLOW(DHT_test, 50);
674644
return s;
675645
}

0 commit comments

Comments
 (0)