diff options
Diffstat (limited to 'auto_tests/dht_test.c')
-rw-r--r-- | auto_tests/dht_test.c | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 11406554..7734e64a 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c | |||
@@ -12,7 +12,8 @@ | |||
12 | 12 | ||
13 | 13 | ||
14 | // These tests currently fail. | 14 | // These tests currently fail. |
15 | #if 0 | 15 | static bool enable_broken_tests = false; |
16 | |||
16 | #define swap(x,y) do \ | 17 | #define swap(x,y) do \ |
17 | { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \ | 18 | { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \ |
18 | memcpy(swap_temp,&y,sizeof(x)); \ | 19 | memcpy(swap_temp,&y,sizeof(x)); \ |
@@ -93,14 +94,14 @@ static void test_addto_lists_update(DHT *dht, | |||
93 | { | 94 | { |
94 | int used, test, test1, test2, found; | 95 | int used, test, test1, test2, found; |
95 | IP_Port test_ipp; | 96 | IP_Port test_ipp; |
96 | uint8_t test_id[crypto_box_PUBLICKEYBYTES]; | 97 | uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE]; |
97 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 98 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
98 | 99 | ||
99 | // check id update for existing ip_port | 100 | // check id update for existing ip_port |
100 | test = rand() % length; | 101 | test = rand() % length; |
101 | ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); | 102 | ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); |
102 | 103 | ||
103 | randombytes(test_id, sizeof(test_id)); | 104 | random_bytes(test_id, sizeof(test_id)); |
104 | used = addto_lists(dht, test_ipp, test_id); | 105 | used = addto_lists(dht, test_ipp, test_id); |
105 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 106 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
106 | // it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test | 107 | // it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test |
@@ -167,11 +168,11 @@ static void test_addto_lists_bad(DHT *dht, | |||
167 | { | 168 | { |
168 | // check "bad" clients replacement | 169 | // check "bad" clients replacement |
169 | int used, test1, test2, test3; | 170 | int used, test1, test2, test3; |
170 | uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES], | 171 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], |
171 | test_id3[crypto_box_PUBLICKEYBYTES]; | 172 | test_id3[CRYPTO_PUBLIC_KEY_SIZE]; |
172 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 173 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
173 | 174 | ||
174 | randombytes(public_key, sizeof(public_key)); | 175 | random_bytes(public_key, sizeof(public_key)); |
175 | mark_all_good(list, length, ipv6); | 176 | mark_all_good(list, length, ipv6); |
176 | 177 | ||
177 | test1 = rand() % (length / 3); | 178 | test1 = rand() % (length / 3); |
@@ -211,11 +212,11 @@ static void test_addto_lists_possible_bad(DHT *dht, | |||
211 | { | 212 | { |
212 | // check "possibly bad" clients replacement | 213 | // check "possibly bad" clients replacement |
213 | int used, test1, test2, test3; | 214 | int used, test1, test2, test3; |
214 | uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES], | 215 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], |
215 | test_id3[crypto_box_PUBLICKEYBYTES]; | 216 | test_id3[CRYPTO_PUBLIC_KEY_SIZE]; |
216 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 217 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
217 | 218 | ||
218 | randombytes(public_key, sizeof(public_key)); | 219 | random_bytes(public_key, sizeof(public_key)); |
219 | mark_all_good(list, length, ipv6); | 220 | mark_all_good(list, length, ipv6); |
220 | 221 | ||
221 | test1 = rand() % (length / 3); | 222 | test1 = rand() % (length / 3); |
@@ -274,14 +275,14 @@ static void test_addto_lists_good(DHT *dht, | |||
274 | IP_Port *ip_port, | 275 | IP_Port *ip_port, |
275 | const uint8_t *comp_client_id) | 276 | const uint8_t *comp_client_id) |
276 | { | 277 | { |
277 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 278 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
278 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 279 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
279 | 280 | ||
280 | mark_all_good(list, length, ipv6); | 281 | mark_all_good(list, length, ipv6); |
281 | 282 | ||
282 | // check "good" client id replacement | 283 | // check "good" client id replacement |
283 | do { | 284 | do { |
284 | randombytes(public_key, sizeof(public_key)); | 285 | random_bytes(public_key, sizeof(public_key)); |
285 | } while (is_furthest(comp_client_id, list, length, public_key)); | 286 | } while (is_furthest(comp_client_id, list, length, public_key)); |
286 | 287 | ||
287 | ip_port->port += 1; | 288 | ip_port->port += 1; |
@@ -290,7 +291,7 @@ static void test_addto_lists_good(DHT *dht, | |||
290 | 291 | ||
291 | // check "good" client id skip | 292 | // check "good" client id skip |
292 | do { | 293 | do { |
293 | randombytes(public_key, sizeof(public_key)); | 294 | random_bytes(public_key, sizeof(public_key)); |
294 | } while (!is_furthest(comp_client_id, list, length, public_key)); | 295 | } while (!is_furthest(comp_client_id, list, length, public_key)); |
295 | 296 | ||
296 | ip_port->port += 1; | 297 | ip_port->port += 1; |
@@ -307,12 +308,12 @@ static void test_addto_lists(IP ip) | |||
307 | ck_assert_msg(dht != 0, "Failed to create DHT"); | 308 | ck_assert_msg(dht != 0, "Failed to create DHT"); |
308 | 309 | ||
309 | IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; | 310 | IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; |
310 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 311 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
311 | int i, used; | 312 | int i, used; |
312 | 313 | ||
313 | // check lists filling | 314 | // check lists filling |
314 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { | 315 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { |
315 | randombytes(public_key, sizeof(public_key)); | 316 | random_bytes(public_key, sizeof(public_key)); |
316 | used = addto_lists(dht, ip_port, public_key); | 317 | used = addto_lists(dht, ip_port, public_key); |
317 | ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port"); | 318 | ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port"); |
318 | } | 319 | } |
@@ -325,7 +326,7 @@ static void test_addto_lists(IP ip) | |||
325 | 326 | ||
326 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { | 327 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { |
327 | ip_port.port += 1; | 328 | ip_port.port += 1; |
328 | randombytes(public_key, sizeof(public_key)); | 329 | random_bytes(public_key, sizeof(public_key)); |
329 | used = addto_lists(dht, ip_port, public_key); | 330 | used = addto_lists(dht, ip_port, public_key); |
330 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 331 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
331 | } | 332 | } |
@@ -346,13 +347,15 @@ static void test_addto_lists(IP ip) | |||
346 | } | 347 | } |
347 | 348 | ||
348 | // check "possibly bad" entries | 349 | // check "possibly bad" entries |
349 | /* | 350 | if (enable_broken_tests) { |
350 | test_addto_lists_possible_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); | 351 | test_addto_lists_possible_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); |
352 | |||
353 | for (i = 0; i < dht->num_friends; ++i) { | ||
354 | test_addto_lists_possible_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port, | ||
355 | dht->friends_list[i].public_key); | ||
356 | } | ||
357 | } | ||
351 | 358 | ||
352 | for (i = 0; i < dht->num_friends; ++i) | ||
353 | test_addto_lists_possible_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port, | ||
354 | dht->friends_list[i].public_key); | ||
355 | */ | ||
356 | // check "good" entries | 359 | // check "good" entries |
357 | test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); | 360 | test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); |
358 | 361 | ||
@@ -379,7 +382,6 @@ START_TEST(test_addto_lists_ipv6) | |||
379 | test_addto_lists(ip); | 382 | test_addto_lists(ip); |
380 | } | 383 | } |
381 | END_TEST | 384 | END_TEST |
382 | #endif | ||
383 | 385 | ||
384 | #define DHT_DEFAULT_PORT (TOX_PORT_DEFAULT + 20) | 386 | #define DHT_DEFAULT_PORT (TOX_PORT_DEFAULT + 20) |
385 | 387 | ||
@@ -389,36 +391,36 @@ static void print_pk(uint8_t *public_key) | |||
389 | { | 391 | { |
390 | uint32_t j; | 392 | uint32_t j; |
391 | 393 | ||
392 | for (j = 0; j < crypto_box_PUBLICKEYBYTES; j++) { | 394 | for (j = 0; j < CRYPTO_PUBLIC_KEY_SIZE; j++) { |
393 | printf("%02hhX", public_key[j]); | 395 | printf("%02hhX", public_key[j]); |
394 | } | 396 | } |
395 | 397 | ||
396 | printf("\n"); | 398 | printf("\n"); |
397 | } | 399 | } |
398 | 400 | ||
399 | static void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], | 401 | static void test_add_to_list(uint8_t cmp_list[][CRYPTO_PUBLIC_KEY_SIZE + 1], |
400 | unsigned int length, const uint8_t *pk, | 402 | unsigned int length, const uint8_t *pk, |
401 | const uint8_t *cmp_pk) | 403 | const uint8_t *cmp_pk) |
402 | { | 404 | { |
403 | uint8_t p_b[crypto_box_PUBLICKEYBYTES]; | 405 | uint8_t p_b[CRYPTO_PUBLIC_KEY_SIZE]; |
404 | unsigned int i; | 406 | unsigned int i; |
405 | 407 | ||
406 | for (i = 0; i < length; ++i) { | 408 | for (i = 0; i < length; ++i) { |
407 | if (!cmp_list[i][crypto_box_PUBLICKEYBYTES]) { | 409 | if (!cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE]) { |
408 | memcpy(cmp_list[i], pk, crypto_box_PUBLICKEYBYTES); | 410 | memcpy(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE); |
409 | cmp_list[i][crypto_box_PUBLICKEYBYTES] = 1; | 411 | cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE] = 1; |
410 | return; | 412 | return; |
411 | } | 413 | } |
412 | 414 | ||
413 | if (memcmp(cmp_list[i], pk, crypto_box_PUBLICKEYBYTES) == 0) { | 415 | if (memcmp(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE) == 0) { |
414 | return; | 416 | return; |
415 | } | 417 | } |
416 | } | 418 | } |
417 | 419 | ||
418 | for (i = 0; i < length; ++i) { | 420 | for (i = 0; i < length; ++i) { |
419 | if (id_closest(cmp_pk, cmp_list[i], pk) == 2) { | 421 | if (id_closest(cmp_pk, cmp_list[i], pk) == 2) { |
420 | memcpy(p_b, cmp_list[i], crypto_box_PUBLICKEYBYTES); | 422 | memcpy(p_b, cmp_list[i], CRYPTO_PUBLIC_KEY_SIZE); |
421 | memcpy(cmp_list[i], pk, crypto_box_PUBLICKEYBYTES); | 423 | memcpy(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE); |
422 | test_add_to_list(cmp_list, length, p_b, cmp_pk); | 424 | test_add_to_list(cmp_list, length, p_b, cmp_pk); |
423 | break; | 425 | break; |
424 | } | 426 | } |
@@ -431,7 +433,7 @@ static void test_list_main(void) | |||
431 | { | 433 | { |
432 | DHT *dhts[NUM_DHT]; | 434 | DHT *dhts[NUM_DHT]; |
433 | 435 | ||
434 | uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][crypto_box_PUBLICKEYBYTES + 1]; | 436 | uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1]; |
435 | memset(cmp_list1, 0, sizeof(cmp_list1)); | 437 | memset(cmp_list1, 0, sizeof(cmp_list1)); |
436 | 438 | ||
437 | unsigned int i, j, k, l; | 439 | unsigned int i, j, k, l; |
@@ -480,7 +482,7 @@ static void test_list_main(void) | |||
480 | for (l = 0; l < NUM_DHT; ++l) { | 482 | for (l = 0; l < NUM_DHT; ++l) { |
481 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 483 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
482 | for (j = 1; j < NUM_DHT; ++j) { | 484 | for (j = 1; j < NUM_DHT; ++j) { |
483 | if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, crypto_box_PUBLICKEYBYTES) != 0) { | 485 | if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, CRYPTO_PUBLIC_KEY_SIZE) != 0) { |
484 | continue; | 486 | continue; |
485 | } | 487 | } |
486 | 488 | ||
@@ -488,7 +490,7 @@ static void test_list_main(void) | |||
488 | 490 | ||
489 | for (k = 0; k < LCLIENT_LIST; ++k) { | 491 | for (k = 0; k < LCLIENT_LIST; ++k) { |
490 | if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key, | 492 | if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key, |
491 | crypto_box_PUBLICKEYBYTES) == 0) { | 493 | CRYPTO_PUBLIC_KEY_SIZE) == 0) { |
492 | ++count; | 494 | ++count; |
493 | } | 495 | } |
494 | } | 496 | } |
@@ -519,7 +521,7 @@ static void test_list_main(void) | |||
519 | count = 0; | 521 | count = 0; |
520 | 522 | ||
521 | for (k = 0; k < MAX_SENT_NODES; ++k) { | 523 | for (k = 0; k < MAX_SENT_NODES; ++k) { |
522 | if (memcmp(dhts[l]->self_public_key, ln[k].public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 524 | if (memcmp(dhts[l]->self_public_key, ln[k].public_key, CRYPTO_PUBLIC_KEY_SIZE) == 0) { |
523 | ++count; | 525 | ++count; |
524 | } | 526 | } |
525 | } | 527 | } |
@@ -653,16 +655,16 @@ END_TEST | |||
653 | START_TEST(test_dht_create_packet) | 655 | START_TEST(test_dht_create_packet) |
654 | { | 656 | { |
655 | uint8_t plain[100] = {0}; | 657 | uint8_t plain[100] = {0}; |
656 | uint8_t pkt[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(plain) + crypto_box_MACBYTES]; | 658 | uint8_t pkt[1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE]; |
657 | 659 | ||
658 | uint8_t key[crypto_box_KEYBYTES]; | 660 | uint8_t key[CRYPTO_SYMMETRIC_KEY_SIZE]; |
659 | new_symmetric_key(key); | 661 | new_symmetric_key(key); |
660 | 662 | ||
661 | int length = DHT_create_packet(key, key, NET_PACKET_GET_NODES, plain, sizeof(plain), pkt); | 663 | int length = DHT_create_packet(key, key, NET_PACKET_GET_NODES, plain, sizeof(plain), pkt); |
662 | 664 | ||
663 | ck_assert_msg(pkt[0] == NET_PACKET_GET_NODES, "Malformed packet."); | 665 | ck_assert_msg(pkt[0] == NET_PACKET_GET_NODES, "Malformed packet."); |
664 | ck_assert_msg(memcmp(pkt + 1, key, crypto_box_KEYBYTES) == 0, "Malformed packet."); | 666 | ck_assert_msg(memcmp(pkt + 1, key, CRYPTO_SYMMETRIC_KEY_SIZE) == 0, "Malformed packet."); |
665 | ck_assert_msg(length == 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(plain) + crypto_box_MACBYTES, | 667 | ck_assert_msg(length == 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE, |
666 | "Invalid size. Should be %d got %d", sizeof(pkt), length); | 668 | "Invalid size. Should be %d got %d", sizeof(pkt), length); |
667 | 669 | ||
668 | printf("Create Packet Successful!\n"); | 670 | printf("Create Packet Successful!\n"); |
@@ -676,10 +678,12 @@ static Suite *dht_suite(void) | |||
676 | 678 | ||
677 | DEFTESTCASE_SLOW(list, 20); | 679 | DEFTESTCASE_SLOW(list, 20); |
678 | DEFTESTCASE_SLOW(DHT_test, 50); | 680 | DEFTESTCASE_SLOW(DHT_test, 50); |
679 | #if 0 | 681 | |
680 | DEFTESTCASE(addto_lists_ipv4); | 682 | if (enable_broken_tests) { |
681 | DEFTESTCASE(addto_lists_ipv6); | 683 | DEFTESTCASE(addto_lists_ipv4); |
682 | #endif | 684 | DEFTESTCASE(addto_lists_ipv6); |
685 | } | ||
686 | |||
683 | return s; | 687 | return s; |
684 | } | 688 | } |
685 | 689 | ||