diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-08-01 23:02:13 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-08-16 21:01:43 +0000 |
commit | d6d305feeb76735ee4b4e14c6bca737a5482bc19 (patch) | |
tree | 99005c635a452245006b3b5de44f1dd80da9f77f /auto_tests/dht_test.c | |
parent | 54066f338f185f2fbd6694d9a4877f42cbfa21c8 (diff) |
Use per-instance `Mono_Time` for Messenger and onion.
Diffstat (limited to 'auto_tests/dht_test.c')
-rw-r--r-- | auto_tests/dht_test.c | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 2f273727..009c0bbd 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c | |||
@@ -32,39 +32,39 @@ static inline IP get_loopback() | |||
32 | return ip; | 32 | return ip; |
33 | } | 33 | } |
34 | 34 | ||
35 | static void mark_bad(IPPTsPng *ipptp) | 35 | static void mark_bad(const Mono_Time *mono_time, IPPTsPng *ipptp) |
36 | { | 36 | { |
37 | ipptp->timestamp = unix_time() - 2 * BAD_NODE_TIMEOUT; | 37 | ipptp->timestamp = mono_time_get(mono_time) - 2 * BAD_NODE_TIMEOUT; |
38 | ipptp->hardening.routes_requests_ok = 0; | 38 | ipptp->hardening.routes_requests_ok = 0; |
39 | ipptp->hardening.send_nodes_ok = 0; | 39 | ipptp->hardening.send_nodes_ok = 0; |
40 | ipptp->hardening.testing_requests = 0; | 40 | ipptp->hardening.testing_requests = 0; |
41 | } | 41 | } |
42 | 42 | ||
43 | static void mark_possible_bad(IPPTsPng *ipptp) | 43 | static void mark_possible_bad(const Mono_Time *mono_time, IPPTsPng *ipptp) |
44 | { | 44 | { |
45 | ipptp->timestamp = unix_time(); | 45 | ipptp->timestamp = mono_time_get(mono_time); |
46 | ipptp->hardening.routes_requests_ok = 0; | 46 | ipptp->hardening.routes_requests_ok = 0; |
47 | ipptp->hardening.send_nodes_ok = 0; | 47 | ipptp->hardening.send_nodes_ok = 0; |
48 | ipptp->hardening.testing_requests = 0; | 48 | ipptp->hardening.testing_requests = 0; |
49 | } | 49 | } |
50 | 50 | ||
51 | static void mark_good(IPPTsPng *ipptp) | 51 | static void mark_good(const Mono_Time *mono_time, IPPTsPng *ipptp) |
52 | { | 52 | { |
53 | ipptp->timestamp = unix_time(); | 53 | ipptp->timestamp = mono_time_get(mono_time); |
54 | ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1; | 54 | ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1; |
55 | ipptp->hardening.send_nodes_ok = (HARDENING_ALL_OK >> 1) & 1; | 55 | ipptp->hardening.send_nodes_ok = (HARDENING_ALL_OK >> 1) & 1; |
56 | ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1; | 56 | ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1; |
57 | } | 57 | } |
58 | 58 | ||
59 | static void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6) | 59 | static void mark_all_good(const Mono_Time *mono_time, Client_data *list, uint32_t length, uint8_t ipv6) |
60 | { | 60 | { |
61 | uint32_t i; | 61 | uint32_t i; |
62 | 62 | ||
63 | for (i = 0; i < length; ++i) { | 63 | for (i = 0; i < length; ++i) { |
64 | if (ipv6) { | 64 | if (ipv6) { |
65 | mark_good(&list[i].assoc6); | 65 | mark_good(mono_time, &list[i].assoc6); |
66 | } else { | 66 | } else { |
67 | mark_good(&list[i].assoc4); | 67 | mark_good(mono_time, &list[i].assoc4); |
68 | } | 68 | } |
69 | } | 69 | } |
70 | } | 70 | } |
@@ -183,7 +183,7 @@ static void test_addto_lists_bad(DHT *dht, | |||
183 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; | 183 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
184 | 184 | ||
185 | random_bytes(public_key, sizeof(public_key)); | 185 | random_bytes(public_key, sizeof(public_key)); |
186 | mark_all_good(list, length, ipv6); | 186 | mark_all_good(dht->mono_time, list, length, ipv6); |
187 | 187 | ||
188 | test1 = random_u32() % (length / 3); | 188 | test1 = random_u32() % (length / 3); |
189 | test2 = random_u32() % (length / 3) + length / 3; | 189 | test2 = random_u32() % (length / 3) + length / 3; |
@@ -196,13 +196,13 @@ static void test_addto_lists_bad(DHT *dht, | |||
196 | 196 | ||
197 | // mark nodes as "bad" | 197 | // mark nodes as "bad" |
198 | if (ipv6) { | 198 | if (ipv6) { |
199 | mark_bad(&list[test1].assoc6); | 199 | mark_bad(dht->mono_time, &list[test1].assoc6); |
200 | mark_bad(&list[test2].assoc6); | 200 | mark_bad(dht->mono_time, &list[test2].assoc6); |
201 | mark_bad(&list[test3].assoc6); | 201 | mark_bad(dht->mono_time, &list[test3].assoc6); |
202 | } else { | 202 | } else { |
203 | mark_bad(&list[test1].assoc4); | 203 | mark_bad(dht->mono_time, &list[test1].assoc4); |
204 | mark_bad(&list[test2].assoc4); | 204 | mark_bad(dht->mono_time, &list[test2].assoc4); |
205 | mark_bad(&list[test3].assoc4); | 205 | mark_bad(dht->mono_time, &list[test3].assoc4); |
206 | } | 206 | } |
207 | 207 | ||
208 | ip_port->port += 1; | 208 | ip_port->port += 1; |
@@ -227,7 +227,7 @@ static void test_addto_lists_possible_bad(DHT *dht, | |||
227 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; | 227 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
228 | 228 | ||
229 | random_bytes(public_key, sizeof(public_key)); | 229 | random_bytes(public_key, sizeof(public_key)); |
230 | mark_all_good(list, length, ipv6); | 230 | mark_all_good(dht->mono_time, list, length, ipv6); |
231 | 231 | ||
232 | test1 = random_u32() % (length / 3); | 232 | test1 = random_u32() % (length / 3); |
233 | test2 = random_u32() % (length / 3) + length / 3; | 233 | test2 = random_u32() % (length / 3) + length / 3; |
@@ -240,13 +240,13 @@ static void test_addto_lists_possible_bad(DHT *dht, | |||
240 | 240 | ||
241 | // mark nodes as "possibly bad" | 241 | // mark nodes as "possibly bad" |
242 | if (ipv6) { | 242 | if (ipv6) { |
243 | mark_possible_bad(&list[test1].assoc6); | 243 | mark_possible_bad(dht->mono_time, &list[test1].assoc6); |
244 | mark_possible_bad(&list[test2].assoc6); | 244 | mark_possible_bad(dht->mono_time, &list[test2].assoc6); |
245 | mark_possible_bad(&list[test3].assoc6); | 245 | mark_possible_bad(dht->mono_time, &list[test3].assoc6); |
246 | } else { | 246 | } else { |
247 | mark_possible_bad(&list[test1].assoc4); | 247 | mark_possible_bad(dht->mono_time, &list[test1].assoc4); |
248 | mark_possible_bad(&list[test2].assoc4); | 248 | mark_possible_bad(dht->mono_time, &list[test2].assoc4); |
249 | mark_possible_bad(&list[test3].assoc4); | 249 | mark_possible_bad(dht->mono_time, &list[test3].assoc4); |
250 | } | 250 | } |
251 | 251 | ||
252 | ip_port->port += 1; | 252 | ip_port->port += 1; |
@@ -288,7 +288,7 @@ static void test_addto_lists_good(DHT *dht, | |||
288 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 288 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
289 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; | 289 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
290 | 290 | ||
291 | mark_all_good(list, length, ipv6); | 291 | mark_all_good(dht->mono_time, list, length, ipv6); |
292 | 292 | ||
293 | // check "good" client id replacement | 293 | // check "good" client id replacement |
294 | do { | 294 | do { |
@@ -319,10 +319,13 @@ static void test_addto_lists(IP ip) | |||
319 | uint32_t index = 1; | 319 | uint32_t index = 1; |
320 | logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index); | 320 | logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index); |
321 | 321 | ||
322 | Mono_Time *mono_time = mono_time_new(); | ||
323 | ck_assert_msg(mono_time != nullptr, "Failed to create Mono_Time"); | ||
324 | |||
322 | Networking_Core *net = new_networking(log, ip, TOX_PORT_DEFAULT); | 325 | Networking_Core *net = new_networking(log, ip, TOX_PORT_DEFAULT); |
323 | ck_assert_msg(net != nullptr, "Failed to create Networking_Core"); | 326 | ck_assert_msg(net != nullptr, "Failed to create Networking_Core"); |
324 | 327 | ||
325 | DHT *dht = new_dht(log, net, true); | 328 | DHT *dht = new_dht(log, mono_time, net, true); |
326 | ck_assert_msg(dht != nullptr, "Failed to create DHT"); | 329 | ck_assert_msg(dht != nullptr, "Failed to create DHT"); |
327 | 330 | ||
328 | IP_Port ip_port; | 331 | IP_Port ip_port; |
@@ -451,6 +454,7 @@ static void test_list_main(void) | |||
451 | { | 454 | { |
452 | DHT *dhts[NUM_DHT]; | 455 | DHT *dhts[NUM_DHT]; |
453 | Logger *logs[NUM_DHT]; | 456 | Logger *logs[NUM_DHT]; |
457 | Mono_Time *mono_times[NUM_DHT]; | ||
454 | uint32_t index[NUM_DHT]; | 458 | uint32_t index[NUM_DHT]; |
455 | 459 | ||
456 | uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1]; | 460 | uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1]; |
@@ -466,7 +470,9 @@ static void test_list_main(void) | |||
466 | index[i] = i + 1; | 470 | index[i] = i + 1; |
467 | logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); | 471 | logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); |
468 | 472 | ||
469 | dhts[i] = new_dht(logs[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true); | 473 | mono_times[i] = mono_time_new(); |
474 | |||
475 | dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true); | ||
470 | ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); | 476 | ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); |
471 | ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, | 477 | ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, |
472 | "Bound to wrong port: %d", net_port(dhts[i]->net)); | 478 | "Bound to wrong port: %d", net_port(dhts[i]->net)); |
@@ -573,6 +579,7 @@ static void test_list_main(void) | |||
573 | Networking_Core *n = dhts[i]->net; | 579 | Networking_Core *n = dhts[i]->net; |
574 | kill_dht(dhts[i]); | 580 | kill_dht(dhts[i]); |
575 | kill_networking(n); | 581 | kill_networking(n); |
582 | mono_time_free(mono_times[i]); | ||
576 | logger_kill(logs[i]); | 583 | logger_kill(logs[i]); |
577 | } | 584 | } |
578 | } | 585 | } |
@@ -598,10 +605,9 @@ static void test_DHT_test(void) | |||
598 | uint32_t to_comp = 8394782; | 605 | uint32_t to_comp = 8394782; |
599 | DHT *dhts[NUM_DHT]; | 606 | DHT *dhts[NUM_DHT]; |
600 | Logger *logs[NUM_DHT]; | 607 | Logger *logs[NUM_DHT]; |
608 | Mono_Time *mono_times[NUM_DHT]; | ||
601 | uint32_t index[NUM_DHT]; | 609 | uint32_t index[NUM_DHT]; |
602 | 610 | ||
603 | unix_time_update(); | ||
604 | |||
605 | uint32_t i, j; | 611 | uint32_t i, j; |
606 | 612 | ||
607 | for (i = 0; i < NUM_DHT; ++i) { | 613 | for (i = 0; i < NUM_DHT; ++i) { |
@@ -612,7 +618,9 @@ static void test_DHT_test(void) | |||
612 | index[i] = i + 1; | 618 | index[i] = i + 1; |
613 | logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); | 619 | logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); |
614 | 620 | ||
615 | dhts[i] = new_dht(logs[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true); | 621 | mono_times[i] = mono_time_new(); |
622 | |||
623 | dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true); | ||
616 | ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); | 624 | ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); |
617 | ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port"); | 625 | ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port"); |
618 | } | 626 | } |
@@ -663,7 +671,7 @@ loop_top: | |||
663 | } | 671 | } |
664 | 672 | ||
665 | for (i = 0; i < NUM_DHT; ++i) { | 673 | for (i = 0; i < NUM_DHT; ++i) { |
666 | unix_time_update(); | 674 | mono_time_update(mono_times[i]); |
667 | networking_poll(dhts[i]->net, nullptr); | 675 | networking_poll(dhts[i]->net, nullptr); |
668 | do_dht(dhts[i]); | 676 | do_dht(dhts[i]); |
669 | } | 677 | } |
@@ -675,6 +683,7 @@ loop_top: | |||
675 | Networking_Core *n = dhts[i]->net; | 683 | Networking_Core *n = dhts[i]->net; |
676 | kill_dht(dhts[i]); | 684 | kill_dht(dhts[i]); |
677 | kill_networking(n); | 685 | kill_networking(n); |
686 | mono_time_free(mono_times[i]); | ||
678 | logger_kill(logs[i]); | 687 | logger_kill(logs[i]); |
679 | } | 688 | } |
680 | } | 689 | } |
@@ -792,8 +801,6 @@ int main(void) | |||
792 | { | 801 | { |
793 | setvbuf(stdout, nullptr, _IONBF, 0); | 802 | setvbuf(stdout, nullptr, _IONBF, 0); |
794 | 803 | ||
795 | unix_time_update(); | ||
796 | |||
797 | test_dht_create_packet(); | 804 | test_dht_create_packet(); |
798 | test_dht_node_packing(); | 805 | test_dht_node_packing(); |
799 | 806 | ||