summaryrefslogtreecommitdiff
path: root/auto_tests
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-01 23:02:13 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-16 21:01:43 +0000
commitd6d305feeb76735ee4b4e14c6bca737a5482bc19 (patch)
tree99005c635a452245006b3b5de44f1dd80da9f77f /auto_tests
parent54066f338f185f2fbd6694d9a4877f42cbfa21c8 (diff)
Use per-instance `Mono_Time` for Messenger and onion.
Diffstat (limited to 'auto_tests')
-rw-r--r--auto_tests/TCP_test.c152
-rw-r--r--auto_tests/dht_test.c71
-rw-r--r--auto_tests/messenger_test.c5
-rw-r--r--auto_tests/onion_test.c76
4 files changed, 170 insertions, 134 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c
index 52ecad77..3c5d56b9 100644
--- a/auto_tests/TCP_test.c
+++ b/auto_tests/TCP_test.c
@@ -36,17 +36,19 @@ static inline IP get_loopback()
36 return ip; 36 return ip;
37} 37}
38 38
39static void do_TCP_server_delay(TCP_Server *tcp_s, int delay) 39static void do_TCP_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay)
40{ 40{
41 c_sleep(delay); 41 c_sleep(delay);
42 unix_time_update(); 42 mono_time_update(mono_time);
43 do_TCP_server(tcp_s); 43 do_TCP_server(tcp_s, mono_time);
44 c_sleep(delay); 44 c_sleep(delay);
45} 45}
46static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643}; 46static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
47 47
48START_TEST(test_basic) 48START_TEST(test_basic)
49{ 49{
50 Mono_Time *mono_time = mono_time_new();
51
50 // Attempt to create a new TCP_Server instance. 52 // Attempt to create a new TCP_Server instance.
51 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 53 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
52 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; 54 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -99,12 +101,12 @@ START_TEST(test_basic)
99 ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, 101 ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
100 "An attempt to send the initial handshake minus last byte failed."); 102 "An attempt to send the initial handshake minus last byte failed.");
101 103
102 do_TCP_server_delay(tcp_s, 50); 104 do_TCP_server_delay(tcp_s, mono_time, 50);
103 105
104 ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, 106 ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1,
105 "The attempt to send the last byte of handshake failed."); 107 "The attempt to send the last byte of handshake failed.");
106 108
107 do_TCP_server_delay(tcp_s, 50); 109 do_TCP_server_delay(tcp_s, mono_time, 50);
108 110
109 // Receiving server response and decrypting it 111 // Receiving server response and decrypting it
110 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; 112 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
@@ -142,8 +144,8 @@ START_TEST(test_basic)
142 i += msg_length; 144 i += msg_length;
143 145
144 c_sleep(50); 146 c_sleep(50);
145 unix_time_update(); 147 mono_time_update(mono_time);
146 do_TCP_server(tcp_s); 148 do_TCP_server(tcp_s, mono_time);
147 } 149 }
148 150
149 // Receiving the second response and verifying its validity 151 // Receiving the second response and verifying its validity
@@ -167,6 +169,8 @@ START_TEST(test_basic)
167 // Closing connections. 169 // Closing connections.
168 kill_sock(sock); 170 kill_sock(sock);
169 kill_TCP_server(tcp_s); 171 kill_TCP_server(tcp_s);
172
173 mono_time_free(mono_time);
170} 174}
171END_TEST 175END_TEST
172 176
@@ -178,7 +182,7 @@ struct sec_TCP_con {
178 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; 182 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
179}; 183};
180 184
181static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) 185static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s, Mono_Time *mono_time)
182{ 186{
183 struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); 187 struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con));
184 Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); 188 Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
@@ -210,12 +214,12 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
210 ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, 214 ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
211 "Failed to send the first portion of the handshake to the TCP relay server."); 215 "Failed to send the first portion of the handshake to the TCP relay server.");
212 216
213 do_TCP_server_delay(tcp_s, 50); 217 do_TCP_server_delay(tcp_s, mono_time, 50);
214 218
215 ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, 219 ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1,
216 "Failed to send last byte of handshake."); 220 "Failed to send last byte of handshake.");
217 221
218 do_TCP_server_delay(tcp_s, 50); 222 do_TCP_server_delay(tcp_s, mono_time, 50);
219 223
220 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; 224 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
221 uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; 225 uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
@@ -266,6 +270,8 @@ static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t
266 270
267START_TEST(test_some) 271START_TEST(test_some)
268{ 272{
273 Mono_Time *mono_time = mono_time_new();
274
269 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 275 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
270 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; 276 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
271 crypto_new_keypair(self_public_key, self_secret_key); 277 crypto_new_keypair(self_public_key, self_secret_key);
@@ -273,9 +279,9 @@ START_TEST(test_some)
273 ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); 279 ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server");
274 ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports."); 280 ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports.");
275 281
276 struct sec_TCP_con *con1 = new_TCP_con(tcp_s); 282 struct sec_TCP_con *con1 = new_TCP_con(tcp_s, mono_time);
277 struct sec_TCP_con *con2 = new_TCP_con(tcp_s); 283 struct sec_TCP_con *con2 = new_TCP_con(tcp_s, mono_time);
278 struct sec_TCP_con *con3 = new_TCP_con(tcp_s); 284 struct sec_TCP_con *con3 = new_TCP_con(tcp_s, mono_time);
279 285
280 uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; 286 uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE];
281 requ_p[0] = 0; 287 requ_p[0] = 0;
@@ -286,7 +292,7 @@ START_TEST(test_some)
286 memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); 292 memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE);
287 write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p)); 293 write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p));
288 294
289 do_TCP_server_delay(tcp_s, 50); 295 do_TCP_server_delay(tcp_s, mono_time, 50);
290 296
291 // Testing response from connection 1 297 // Testing response from connection 1
292 uint8_t data[2048]; 298 uint8_t data[2048];
@@ -309,7 +315,7 @@ START_TEST(test_some)
309 write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); 315 write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet));
310 write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); 316 write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet));
311 317
312 do_TCP_server_delay(tcp_s, 50); 318 do_TCP_server_delay(tcp_s, mono_time, 50);
313 319
314 len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); 320 len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE);
315 ck_assert_msg(len == 2, "wrong len %d", len); 321 ck_assert_msg(len == 2, "wrong len %d", len);
@@ -334,7 +340,7 @@ START_TEST(test_some)
334 write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); 340 write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet));
335 write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); 341 write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet));
336 write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); 342 write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet));
337 do_TCP_server_delay(tcp_s, 50); 343 do_TCP_server_delay(tcp_s, mono_time, 50);
338 len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); 344 len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
339 ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); 345 ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
340 ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], 346 ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
@@ -351,7 +357,7 @@ START_TEST(test_some)
351 uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; 357 uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67};
352 write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); 358 write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet));
353 359
354 do_TCP_server_delay(tcp_s, 50); 360 do_TCP_server_delay(tcp_s, mono_time, 50);
355 361
356 len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); 362 len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE);
357 ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); 363 ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len);
@@ -363,6 +369,8 @@ START_TEST(test_some)
363 kill_TCP_con(con1); 369 kill_TCP_con(con1);
364 kill_TCP_con(con2); 370 kill_TCP_con(con2);
365 kill_TCP_con(con3); 371 kill_TCP_con(con3);
372
373 mono_time_free(mono_time);
366} 374}
367END_TEST 375END_TEST
368 376
@@ -449,7 +457,8 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
449 457
450START_TEST(test_client) 458START_TEST(test_client)
451{ 459{
452 unix_time_update(); 460 Mono_Time *mono_time = mono_time_new();
461
453 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 462 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
454 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; 463 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
455 crypto_new_keypair(self_public_key, self_secret_key); 464 crypto_new_keypair(self_public_key, self_secret_key);
@@ -465,8 +474,9 @@ START_TEST(test_client)
465 ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); 474 ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
466 ip_port_tcp_s.ip = get_loopback(); 475 ip_port_tcp_s.ip = get_loopback();
467 476
468 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); 477 TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key,
469 do_TCP_connection(conn, nullptr); 478 nullptr);
479 do_TCP_connection(mono_time, conn, nullptr);
470 c_sleep(50); 480 c_sleep(50);
471 481
472 // The connection status should be unconfirmed here because we have finished 482 // The connection status should be unconfirmed here because we have finished
@@ -474,13 +484,13 @@ START_TEST(test_client)
474 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong connection status. Expected: %d, is: %d.", 484 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong connection status. Expected: %d, is: %d.",
475 TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn)); 485 TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn));
476 486
477 do_TCP_server_delay(tcp_s, 50); // Now let the server handle requests... 487 do_TCP_server_delay(tcp_s, mono_time, 50); // Now let the server handle requests...
478 488
479 const uint8_t LOOP_SIZE = 3; 489 const uint8_t LOOP_SIZE = 3;
480 490
481 for (uint8_t i = 0; i < LOOP_SIZE; i++) { 491 for (uint8_t i = 0; i < LOOP_SIZE; i++) {
482 unix_time_update(); 492 mono_time_update(mono_time);
483 do_TCP_connection(conn, nullptr); // Run the connection loop. 493 do_TCP_connection(mono_time, conn, nullptr); // Run the connection loop.
484 494
485 // The status of the connection should continue to be TCP_CLIENT_CONFIRMED after multiple subsequent do_TCP_connection() calls. 495 // The status of the connection should continue to be TCP_CLIENT_CONFIRMED after multiple subsequent do_TCP_connection() calls.
486 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong connection status. Expected: %d, is: %d", 496 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong connection status. Expected: %d, is: %d",
@@ -489,7 +499,7 @@ START_TEST(test_client)
489 c_sleep(i == LOOP_SIZE - 1 ? 0 : 500); // Sleep for 500ms on all except third loop. 499 c_sleep(i == LOOP_SIZE - 1 ? 0 : 500); // Sleep for 500ms on all except third loop.
490 } 500 }
491 501
492 do_TCP_server_delay(tcp_s, 50); 502 do_TCP_server_delay(tcp_s, mono_time, 50);
493 503
494 // And still after the server runs again. 504 // And still after the server runs again.
495 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %d, is: %d", TCP_CLIENT_CONFIRMED, 505 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %d, is: %d", TCP_CLIENT_CONFIRMED,
@@ -499,8 +509,8 @@ START_TEST(test_client)
499 uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; 509 uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE];
500 crypto_new_keypair(f2_public_key, f2_secret_key); 510 crypto_new_keypair(f2_public_key, f2_secret_key);
501 ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); 511 ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
502 TCP_Client_Connection *conn2 = new_TCP_connection( 512 TCP_Client_Connection *conn2 = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f2_public_key,
503 ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); 513 f2_secret_key, nullptr);
504 514
505 // The client should call this function (defined earlier) during the routing process. 515 // The client should call this function (defined earlier) during the routing process.
506 routing_response_handler(conn, response_callback, (char *)conn + 2); 516 routing_response_handler(conn, response_callback, (char *)conn + 2);
@@ -514,13 +524,13 @@ START_TEST(test_client)
514 // These integers will increment per successful callback. 524 // These integers will increment per successful callback.
515 oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0; 525 oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0;
516 526
517 do_TCP_connection(conn, nullptr); 527 do_TCP_connection(mono_time, conn, nullptr);
518 do_TCP_connection(conn2, nullptr); 528 do_TCP_connection(mono_time, conn2, nullptr);
519 529
520 do_TCP_server_delay(tcp_s, 50); 530 do_TCP_server_delay(tcp_s, mono_time, 50);
521 531
522 do_TCP_connection(conn, nullptr); 532 do_TCP_connection(mono_time, conn, nullptr);
523 do_TCP_connection(conn2, nullptr); 533 do_TCP_connection(mono_time, conn2, nullptr);
524 c_sleep(50); 534 c_sleep(50);
525 535
526 uint8_t data[5] = {1, 2, 3, 4, 5}; 536 uint8_t data[5] = {1, 2, 3, 4, 5};
@@ -529,10 +539,10 @@ START_TEST(test_client)
529 send_routing_request(conn, f2_public_key); 539 send_routing_request(conn, f2_public_key);
530 send_routing_request(conn2, f_public_key); 540 send_routing_request(conn2, f_public_key);
531 541
532 do_TCP_server_delay(tcp_s, 50); 542 do_TCP_server_delay(tcp_s, mono_time, 50);
533 543
534 do_TCP_connection(conn, nullptr); 544 do_TCP_connection(mono_time, conn, nullptr);
535 do_TCP_connection(conn2, nullptr); 545 do_TCP_connection(mono_time, conn2, nullptr);
536 546
537 // All callback methods save data should have run during the above network prodding. 547 // All callback methods save data should have run during the above network prodding.
538 ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called"); 548 ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called");
@@ -543,22 +553,22 @@ START_TEST(test_client)
543 ck_assert_msg(status_callback_connection_id == response_callback_connection_id, 553 ck_assert_msg(status_callback_connection_id == response_callback_connection_id,
544 "Status and response callback connection IDs are not equal."); 554 "Status and response callback connection IDs are not equal.");
545 555
546 do_TCP_server_delay(tcp_s, 50); 556 do_TCP_server_delay(tcp_s, mono_time, 50);
547 557
548 ck_assert_msg(send_data(conn2, 0, data, 5) == 1, "Failed a send_data() call."); 558 ck_assert_msg(send_data(conn2, 0, data, 5) == 1, "Failed a send_data() call.");
549 559
550 do_TCP_server_delay(tcp_s, 50); 560 do_TCP_server_delay(tcp_s, mono_time, 50);
551 561
552 do_TCP_connection(conn, nullptr); 562 do_TCP_connection(mono_time, conn, nullptr);
553 do_TCP_connection(conn2, nullptr); 563 do_TCP_connection(mono_time, conn2, nullptr);
554 ck_assert_msg(data_callback_good == 1, "Data callback was not called."); 564 ck_assert_msg(data_callback_good == 1, "Data callback was not called.");
555 status_callback_good = 0; 565 status_callback_good = 0;
556 send_disconnect_request(conn2, 0); 566 send_disconnect_request(conn2, 0);
557 567
558 do_TCP_server_delay(tcp_s, 50); 568 do_TCP_server_delay(tcp_s, mono_time, 50);
559 569
560 do_TCP_connection(conn, nullptr); 570 do_TCP_connection(mono_time, conn, nullptr);
561 do_TCP_connection(conn2, nullptr); 571 do_TCP_connection(mono_time, conn2, nullptr);
562 ck_assert_msg(status_callback_good == 1, "Status callback not called"); 572 ck_assert_msg(status_callback_good == 1, "Status callback not called");
563 ck_assert_msg(status_callback_status == 1, "Wrong status callback status."); 573 ck_assert_msg(status_callback_status == 1, "Wrong status callback status.");
564 574
@@ -566,13 +576,16 @@ START_TEST(test_client)
566 kill_TCP_server(tcp_s); 576 kill_TCP_server(tcp_s);
567 kill_TCP_connection(conn); 577 kill_TCP_connection(conn);
568 kill_TCP_connection(conn2); 578 kill_TCP_connection(conn2);
579
580 mono_time_free(mono_time);
569} 581}
570END_TEST 582END_TEST
571 583
572// Test how the client handles servers that don't respond. 584// Test how the client handles servers that don't respond.
573START_TEST(test_client_invalid) 585START_TEST(test_client_invalid)
574{ 586{
575 unix_time_update(); 587 Mono_Time *mono_time = mono_time_new();
588
576 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 589 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
577 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; 590 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
578 crypto_new_keypair(self_public_key, self_secret_key); 591 crypto_new_keypair(self_public_key, self_secret_key);
@@ -584,11 +597,12 @@ START_TEST(test_client_invalid)
584 597
585 ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); 598 ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
586 ip_port_tcp_s.ip = get_loopback(); 599 ip_port_tcp_s.ip = get_loopback();
587 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); 600 TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key,
601 nullptr);
588 602
589 // Run the client's main loop but not the server. 603 // Run the client's main loop but not the server.
590 unix_time_update(); 604 mono_time_update(mono_time);
591 do_TCP_connection(conn, nullptr); 605 do_TCP_connection(mono_time, conn, nullptr);
592 c_sleep(50); 606 c_sleep(50);
593 607
594 // After 50ms of no response... 608 // After 50ms of no response...
@@ -596,18 +610,20 @@ START_TEST(test_client_invalid)
596 TCP_CLIENT_CONNECTING, tcp_con_status(conn)); 610 TCP_CLIENT_CONNECTING, tcp_con_status(conn));
597 // After 5s... 611 // After 5s...
598 c_sleep(5000); 612 c_sleep(5000);
599 unix_time_update(); 613 mono_time_update(mono_time);
600 do_TCP_connection(conn, nullptr); 614 do_TCP_connection(mono_time, conn, nullptr);
601 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.", 615 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.",
602 TCP_CLIENT_CONNECTING, tcp_con_status(conn)); 616 TCP_CLIENT_CONNECTING, tcp_con_status(conn));
603 // 11s... (Should wait for 10 before giving up.) 617 // 11s... (Should wait for 10 before giving up.)
604 c_sleep(6000); 618 c_sleep(6000);
605 unix_time_update(); 619 mono_time_update(mono_time);
606 do_TCP_connection(conn, nullptr); 620 do_TCP_connection(mono_time, conn, nullptr);
607 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %d, is: %d.", 621 ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %d, is: %d.",
608 TCP_CLIENT_DISCONNECTED, tcp_con_status(conn)); 622 TCP_CLIENT_DISCONNECTED, tcp_con_status(conn));
609 623
610 kill_TCP_connection(conn); 624 kill_TCP_connection(conn);
625
626 mono_time_free(mono_time);
611} 627}
612END_TEST 628END_TEST
613 629
@@ -639,8 +655,9 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
639 655
640START_TEST(test_tcp_connection) 656START_TEST(test_tcp_connection)
641{ 657{
658 Mono_Time *mono_time = mono_time_new();
659
642 tcp_data_callback_called = 0; 660 tcp_data_callback_called = 0;
643 unix_time_update();
644 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 661 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
645 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; 662 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
646 crypto_new_keypair(self_public_key, self_secret_key); 663 crypto_new_keypair(self_public_key, self_secret_key);
@@ -650,11 +667,11 @@ START_TEST(test_tcp_connection)
650 TCP_Proxy_Info proxy_info; 667 TCP_Proxy_Info proxy_info;
651 proxy_info.proxy_type = TCP_PROXY_NONE; 668 proxy_info.proxy_type = TCP_PROXY_NONE;
652 crypto_new_keypair(self_public_key, self_secret_key); 669 crypto_new_keypair(self_public_key, self_secret_key);
653 TCP_Connections *tc_1 = new_tcp_connections(self_secret_key, &proxy_info); 670 TCP_Connections *tc_1 = new_tcp_connections(mono_time, self_secret_key, &proxy_info);
654 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); 671 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
655 672
656 crypto_new_keypair(self_public_key, self_secret_key); 673 crypto_new_keypair(self_public_key, self_secret_key);
657 TCP_Connections *tc_2 = new_tcp_connections(self_secret_key, &proxy_info); 674 TCP_Connections *tc_2 = new_tcp_connections(mono_time, self_secret_key, &proxy_info);
658 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); 675 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
659 676
660 IP_Port ip_port_tcp_s; 677 IP_Port ip_port_tcp_s;
@@ -676,17 +693,17 @@ START_TEST(test_tcp_connection)
676 ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1, 693 ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1,
677 "Managed to readd same connection\n"); 694 "Managed to readd same connection\n");
678 695
679 do_TCP_server_delay(tcp_s, 50); 696 do_TCP_server_delay(tcp_s, mono_time, 50);
680 697
681 do_tcp_connections(tc_1, nullptr); 698 do_tcp_connections(tc_1, nullptr);
682 do_tcp_connections(tc_2, nullptr); 699 do_tcp_connections(tc_2, nullptr);
683 700
684 do_TCP_server_delay(tcp_s, 50); 701 do_TCP_server_delay(tcp_s, mono_time, 50);
685 702
686 do_tcp_connections(tc_1, nullptr); 703 do_tcp_connections(tc_1, nullptr);
687 do_tcp_connections(tc_2, nullptr); 704 do_tcp_connections(tc_2, nullptr);
688 705
689 do_TCP_server_delay(tcp_s, 50); 706 do_TCP_server_delay(tcp_s, mono_time, 50);
690 707
691 do_tcp_connections(tc_1, nullptr); 708 do_tcp_connections(tc_1, nullptr);
692 do_tcp_connections(tc_2, nullptr); 709 do_tcp_connections(tc_2, nullptr);
@@ -695,7 +712,7 @@ START_TEST(test_tcp_connection)
695 ck_assert_msg(ret == 0, "could not send packet."); 712 ck_assert_msg(ret == 0, "could not send packet.");
696 set_packet_tcp_connection_callback(tc_2, &tcp_data_callback, (void *) 120397); 713 set_packet_tcp_connection_callback(tc_2, &tcp_data_callback, (void *) 120397);
697 714
698 do_TCP_server_delay(tcp_s, 50); 715 do_TCP_server_delay(tcp_s, mono_time, 50);
699 716
700 do_tcp_connections(tc_1, nullptr); 717 do_tcp_connections(tc_1, nullptr);
701 do_tcp_connections(tc_2, nullptr); 718 do_tcp_connections(tc_2, nullptr);
@@ -704,7 +721,7 @@ START_TEST(test_tcp_connection)
704 ck_assert_msg(tcp_connection_to_online_tcp_relays(tc_1, 0) == 1, "Wrong number of connected relays"); 721 ck_assert_msg(tcp_connection_to_online_tcp_relays(tc_1, 0) == 1, "Wrong number of connected relays");
705 ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n"); 722 ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
706 723
707 do_TCP_server_delay(tcp_s, 50); 724 do_TCP_server_delay(tcp_s, mono_time, 50);
708 725
709 do_tcp_connections(tc_1, nullptr); 726 do_tcp_connections(tc_1, nullptr);
710 do_tcp_connections(tc_2, nullptr); 727 do_tcp_connections(tc_2, nullptr);
@@ -715,6 +732,8 @@ START_TEST(test_tcp_connection)
715 kill_TCP_server(tcp_s); 732 kill_TCP_server(tcp_s);
716 kill_tcp_connections(tc_1); 733 kill_tcp_connections(tc_1);
717 kill_tcp_connections(tc_2); 734 kill_tcp_connections(tc_2);
735
736 mono_time_free(mono_time);
718} 737}
719END_TEST 738END_TEST
720 739
@@ -741,10 +760,11 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne
741 760
742START_TEST(test_tcp_connection2) 761START_TEST(test_tcp_connection2)
743{ 762{
763 Mono_Time *mono_time = mono_time_new();
764
744 tcp_oobdata_callback_called = 0; 765 tcp_oobdata_callback_called = 0;
745 tcp_data_callback_called = 0; 766 tcp_data_callback_called = 0;
746 767
747 unix_time_update();
748 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 768 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
749 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; 769 uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
750 crypto_new_keypair(self_public_key, self_secret_key); 770 crypto_new_keypair(self_public_key, self_secret_key);
@@ -754,11 +774,11 @@ START_TEST(test_tcp_connection2)
754 TCP_Proxy_Info proxy_info; 774 TCP_Proxy_Info proxy_info;
755 proxy_info.proxy_type = TCP_PROXY_NONE; 775 proxy_info.proxy_type = TCP_PROXY_NONE;
756 crypto_new_keypair(self_public_key, self_secret_key); 776 crypto_new_keypair(self_public_key, self_secret_key);
757 TCP_Connections *tc_1 = new_tcp_connections(self_secret_key, &proxy_info); 777 TCP_Connections *tc_1 = new_tcp_connections(mono_time, self_secret_key, &proxy_info);
758 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); 778 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
759 779
760 crypto_new_keypair(self_public_key, self_secret_key); 780 crypto_new_keypair(self_public_key, self_secret_key);
761 TCP_Connections *tc_2 = new_tcp_connections(self_secret_key, &proxy_info); 781 TCP_Connections *tc_2 = new_tcp_connections(mono_time, self_secret_key, &proxy_info);
762 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); 782 ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
763 783
764 IP_Port ip_port_tcp_s; 784 IP_Port ip_port_tcp_s;
@@ -774,17 +794,17 @@ START_TEST(test_tcp_connection2)
774 ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, 794 ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
775 "Could not add global relay"); 795 "Could not add global relay");
776 796
777 do_TCP_server_delay(tcp_s, 50); 797 do_TCP_server_delay(tcp_s, mono_time, 50);
778 798
779 do_tcp_connections(tc_1, nullptr); 799 do_tcp_connections(tc_1, nullptr);
780 do_tcp_connections(tc_2, nullptr); 800 do_tcp_connections(tc_2, nullptr);
781 801
782 do_TCP_server_delay(tcp_s, 50); 802 do_TCP_server_delay(tcp_s, mono_time, 50);
783 803
784 do_tcp_connections(tc_1, nullptr); 804 do_tcp_connections(tc_1, nullptr);
785 do_tcp_connections(tc_2, nullptr); 805 do_tcp_connections(tc_2, nullptr);
786 806
787 do_TCP_server_delay(tcp_s, 50); 807 do_TCP_server_delay(tcp_s, mono_time, 50);
788 808
789 do_tcp_connections(tc_1, nullptr); 809 do_tcp_connections(tc_1, nullptr);
790 do_tcp_connections(tc_2, nullptr); 810 do_tcp_connections(tc_2, nullptr);
@@ -794,14 +814,14 @@ START_TEST(test_tcp_connection2)
794 set_oob_packet_tcp_connection_callback(tc_2, &tcp_oobdata_callback, tc_2); 814 set_oob_packet_tcp_connection_callback(tc_2, &tcp_oobdata_callback, tc_2);
795 set_packet_tcp_connection_callback(tc_1, &tcp_data_callback, (void *) 120397); 815 set_packet_tcp_connection_callback(tc_1, &tcp_data_callback, (void *) 120397);
796 816
797 do_TCP_server_delay(tcp_s, 50); 817 do_TCP_server_delay(tcp_s, mono_time, 50);
798 818
799 do_tcp_connections(tc_1, nullptr); 819 do_tcp_connections(tc_1, nullptr);
800 do_tcp_connections(tc_2, nullptr); 820 do_tcp_connections(tc_2, nullptr);
801 821
802 ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet."); 822 ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet.");
803 823
804 do_TCP_server_delay(tcp_s, 50); 824 do_TCP_server_delay(tcp_s, mono_time, 50);
805 825
806 do_tcp_connections(tc_1, nullptr); 826 do_tcp_connections(tc_1, nullptr);
807 do_tcp_connections(tc_2, nullptr); 827 do_tcp_connections(tc_2, nullptr);
@@ -812,6 +832,8 @@ START_TEST(test_tcp_connection2)
812 kill_TCP_server(tcp_s); 832 kill_TCP_server(tcp_s);
813 kill_tcp_connections(tc_1); 833 kill_tcp_connections(tc_1);
814 kill_tcp_connections(tc_2); 834 kill_tcp_connections(tc_2);
835
836 mono_time_free(mono_time);
815} 837}
816END_TEST 838END_TEST
817 839
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
35static void mark_bad(IPPTsPng *ipptp) 35static 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
43static void mark_possible_bad(IPPTsPng *ipptp) 43static 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
51static void mark_good(IPPTsPng *ipptp) 51static 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
59static void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6) 59static 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
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c
index b8fbbc1f..dd644e50 100644
--- a/auto_tests/messenger_test.c
+++ b/auto_tests/messenger_test.c
@@ -341,13 +341,15 @@ int main(void)
341 good_id = hex_string_to_bin(good_id_str); 341 good_id = hex_string_to_bin(good_id_str);
342 bad_id = hex_string_to_bin(bad_id_str); 342 bad_id = hex_string_to_bin(bad_id_str);
343 343
344 Mono_Time *mono_time = mono_time_new();
345
344 /* IPv6 status from global define */ 346 /* IPv6 status from global define */
345 Messenger_Options options = {0}; 347 Messenger_Options options = {0};
346 options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; 348 options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
347 options.port_range[0] = 41234; 349 options.port_range[0] = 41234;
348 options.port_range[1] = 44234; 350 options.port_range[1] = 44234;
349 options.log_callback = (logger_cb *)print_debug_log; 351 options.log_callback = (logger_cb *)print_debug_log;
350 m = new_messenger(&options, nullptr); 352 m = new_messenger(mono_time, &options, nullptr);
351 353
352 /* setup a default friend and friendnum */ 354 /* setup a default friend and friendnum */
353 if (m_addfriend_norequest(m, friend_id) < 0) { 355 if (m_addfriend_norequest(m, friend_id) < 0) {
@@ -375,6 +377,7 @@ int main(void)
375 free(bad_id); 377 free(bad_id);
376 378
377 kill_messenger(m); 379 kill_messenger(m);
380 mono_time_free(mono_time);
378 381
379 return number_failed; 382 return number_failed;
380} 383}
diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c
index 0a9a0577..a11fb807 100644
--- a/auto_tests/onion_test.c
+++ b/auto_tests/onion_test.c
@@ -31,7 +31,7 @@ static inline IP get_loopback()
31} 31}
32static void do_onion(Onion *onion) 32static void do_onion(Onion *onion)
33{ 33{
34 unix_time_update(); 34 mono_time_update(onion->mono_time);
35 35
36 networking_poll(onion->net, nullptr); 36 networking_poll(onion->net, nullptr);
37 do_dht(onion->dht); 37 do_dht(onion->dht);
@@ -152,7 +152,7 @@ static int handle_test_4(void *object, IP_Port source, const uint8_t *packet, ui
152 return 0; 152 return 0;
153} 153}
154 154
155START_TEST(test_basic) 155static void test_basic(void)
156{ 156{
157 uint32_t index[] = { 1, 2, 3 }; 157 uint32_t index[] = { 1, 2, 3 };
158 Logger *log1 = logger_new(); 158 Logger *log1 = logger_new();
@@ -160,9 +160,12 @@ START_TEST(test_basic)
160 Logger *log2 = logger_new(); 160 Logger *log2 = logger_new();
161 logger_callback_log(log2, (logger_cb *)print_debug_log, nullptr, &index[1]); 161 logger_callback_log(log2, (logger_cb *)print_debug_log, nullptr, &index[1]);
162 162
163 Mono_Time *mono_time1 = mono_time_new();
164 Mono_Time *mono_time2 = mono_time_new();
165
163 IP ip = get_loopback(); 166 IP ip = get_loopback();
164 Onion *onion1 = new_onion(new_dht(log1, new_networking(log1, ip, 36567), true)); 167 Onion *onion1 = new_onion(mono_time1, new_dht(log1, mono_time1, new_networking(log1, ip, 36567), true));
165 Onion *onion2 = new_onion(new_dht(log2, new_networking(log2, ip, 36568), true)); 168 Onion *onion2 = new_onion(mono_time2, new_dht(log2, mono_time2, new_networking(log2, ip, 36568), true));
166 ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing."); 169 ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
167 networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2); 170 networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);
168 171
@@ -202,8 +205,8 @@ START_TEST(test_basic)
202 do_onion(onion2); 205 do_onion(onion2);
203 } 206 }
204 207
205 Onion_Announce *onion1_a = new_onion_announce(onion1->dht); 208 Onion_Announce *onion1_a = new_onion_announce(mono_time1, onion1->dht);
206 Onion_Announce *onion2_a = new_onion_announce(onion2->dht); 209 Onion_Announce *onion2_a = new_onion_announce(mono_time2, onion2->dht);
207 networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1); 210 networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1);
208 ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing."); 211 ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing.");
209 uint8_t zeroes[64] = {0}; 212 uint8_t zeroes[64] = {0};
@@ -229,7 +232,7 @@ START_TEST(test_basic)
229 random_bytes(sb_data, sizeof(sb_data)); 232 random_bytes(sb_data, sizeof(sb_data));
230 memcpy(&s, sb_data, sizeof(uint64_t)); 233 memcpy(&s, sb_data, sizeof(uint64_t));
231 memcpy(onion_announce_entry_public_key(onion2_a, 1), dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE); 234 memcpy(onion_announce_entry_public_key(onion2_a, 1), dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
232 onion_announce_entry_set_time(onion2_a, 1, unix_time()); 235 onion_announce_entry_set_time(onion2_a, 1, mono_time_get(mono_time2));
233 networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1); 236 networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1);
234 send_announce_request(onion1->net, &path, nodes[3], 237 send_announce_request(onion1->net, &path, nodes[3],
235 dht_get_self_public_key(onion1->dht), 238 dht_get_self_public_key(onion1->dht),
@@ -250,7 +253,9 @@ START_TEST(test_basic)
250 Logger *log3 = logger_new(); 253 Logger *log3 = logger_new();
251 logger_callback_log(log3, (logger_cb *)print_debug_log, nullptr, &index[2]); 254 logger_callback_log(log3, (logger_cb *)print_debug_log, nullptr, &index[2]);
252 255
253 Onion *onion3 = new_onion(new_dht(log3, new_networking(log3, ip, 36569), true)); 256 Mono_Time *mono_time3 = mono_time_new();
257
258 Onion *onion3 = new_onion(mono_time3, new_dht(log3, mono_time3, new_networking(log3, ip, 36569), true));
254 ck_assert_msg((onion3 != nullptr), "Onion failed initializing."); 259 ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
255 260
256 random_nonce(nonce); 261 random_nonce(nonce);
@@ -278,6 +283,7 @@ START_TEST(test_basic)
278 kill_onion(onion); 283 kill_onion(onion);
279 kill_dht(dht); 284 kill_dht(dht);
280 kill_networking(net); 285 kill_networking(net);
286 mono_time_free(mono_time3);
281 logger_kill(log3); 287 logger_kill(log3);
282 } 288 }
283 289
@@ -289,6 +295,7 @@ START_TEST(test_basic)
289 kill_onion(onion); 295 kill_onion(onion);
290 kill_dht(dht); 296 kill_dht(dht);
291 kill_networking(net); 297 kill_networking(net);
298 mono_time_free(mono_time2);
292 logger_kill(log2); 299 logger_kill(log2);
293 } 300 }
294 301
@@ -300,13 +307,14 @@ START_TEST(test_basic)
300 kill_onion(onion); 307 kill_onion(onion);
301 kill_dht(dht); 308 kill_dht(dht);
302 kill_networking(net); 309 kill_networking(net);
310 mono_time_free(mono_time1);
303 logger_kill(log1); 311 logger_kill(log1);
304 } 312 }
305} 313}
306END_TEST
307 314
308typedef struct { 315typedef struct {
309 Logger *log; 316 Logger *log;
317 Mono_Time *mono_time;
310 Onion *onion; 318 Onion *onion;
311 Onion_Announce *onion_a; 319 Onion_Announce *onion_a;
312 Onion_Client *onion_c; 320 Onion_Client *onion_c;
@@ -329,56 +337,67 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
329 return nullptr; 337 return nullptr;
330 } 338 }
331 339
332 unix_time_update();
333
334 logger_callback_log(on->log, (logger_cb *)print_debug_log, nullptr, index); 340 logger_callback_log(on->log, (logger_cb *)print_debug_log, nullptr, index);
335 341
342 on->mono_time = mono_time_new();
343
344 if (!on->mono_time) {
345 logger_kill(on->log);
346 free(on);
347 return nullptr;
348 }
349
336 Networking_Core *net = new_networking(on->log, ip, port); 350 Networking_Core *net = new_networking(on->log, ip, port);
337 351
338 if (!net) { 352 if (!net) {
353 mono_time_free(on->mono_time);
339 logger_kill(on->log); 354 logger_kill(on->log);
340 free(on); 355 free(on);
341 return nullptr; 356 return nullptr;
342 } 357 }
343 358
344 DHT *dht = new_dht(on->log, net, true); 359 DHT *dht = new_dht(on->log, on->mono_time, net, true);
345 360
346 if (!dht) { 361 if (!dht) {
347 kill_networking(net); 362 kill_networking(net);
363 mono_time_free(on->mono_time);
348 logger_kill(on->log); 364 logger_kill(on->log);
349 free(on); 365 free(on);
350 return nullptr; 366 return nullptr;
351 } 367 }
352 368
353 on->onion = new_onion(dht); 369 on->onion = new_onion(on->mono_time, dht);
354 370
355 if (!on->onion) { 371 if (!on->onion) {
356 kill_dht(dht); 372 kill_dht(dht);
357 kill_networking(net); 373 kill_networking(net);
374 mono_time_free(on->mono_time);
358 logger_kill(on->log); 375 logger_kill(on->log);
359 free(on); 376 free(on);
360 return nullptr; 377 return nullptr;
361 } 378 }
362 379
363 on->onion_a = new_onion_announce(dht); 380 on->onion_a = new_onion_announce(on->mono_time, dht);
364 381
365 if (!on->onion_a) { 382 if (!on->onion_a) {
366 kill_onion(on->onion); 383 kill_onion(on->onion);
367 kill_dht(dht); 384 kill_dht(dht);
368 kill_networking(net); 385 kill_networking(net);
386 mono_time_free(on->mono_time);
369 logger_kill(on->log); 387 logger_kill(on->log);
370 free(on); 388 free(on);
371 return nullptr; 389 return nullptr;
372 } 390 }
373 391
374 TCP_Proxy_Info inf = {{{{0}}}}; 392 TCP_Proxy_Info inf = {{{{0}}}};
375 on->onion_c = new_onion_client(new_net_crypto(on->log, dht, &inf)); 393 on->onion_c = new_onion_client(on->mono_time, new_net_crypto(on->log, on->mono_time, dht, &inf));
376 394
377 if (!on->onion_c) { 395 if (!on->onion_c) {
378 kill_onion_announce(on->onion_a); 396 kill_onion_announce(on->onion_a);
379 kill_onion(on->onion); 397 kill_onion(on->onion);
380 kill_dht(dht); 398 kill_dht(dht);
381 kill_networking(net); 399 kill_networking(net);
400 mono_time_free(on->mono_time);
382 logger_kill(on->log); 401 logger_kill(on->log);
383 free(on); 402 free(on);
384 return nullptr; 403 return nullptr;
@@ -389,7 +408,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
389 408
390static void do_onions(Onions *on) 409static void do_onions(Onions *on)
391{ 410{
392 unix_time_update(); 411 mono_time_update(on->mono_time);
393 412
394 networking_poll(on->onion->net, nullptr); 413 networking_poll(on->onion->net, nullptr);
395 do_dht(on->onion->dht); 414 do_dht(on->onion->dht);
@@ -407,6 +426,7 @@ static void kill_onions(Onions *on)
407 kill_net_crypto(c); 426 kill_net_crypto(c);
408 kill_dht(dht); 427 kill_dht(dht);
409 kill_networking(net); 428 kill_networking(net);
429 mono_time_free(on->mono_time);
410 logger_kill(on->log); 430 logger_kill(on->log);
411 free(on); 431 free(on);
412} 432}
@@ -468,7 +488,7 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub
468 } 488 }
469} 489}
470 490
471START_TEST(test_announce) 491static void test_announce(void)
472{ 492{
473 uint32_t i, j; 493 uint32_t i, j;
474 uint32_t index[NUM_ONIONS]; 494 uint32_t index[NUM_ONIONS];
@@ -553,29 +573,13 @@ START_TEST(test_announce)
553 kill_onions(onions[i]); 573 kill_onions(onions[i]);
554 } 574 }
555} 575}
556END_TEST
557
558static Suite *onion_suite(void)
559{
560 Suite *s = suite_create("Onion");
561
562 DEFTESTCASE_SLOW(basic, 5);
563 DEFTESTCASE_SLOW(announce, 70);
564 return s;
565}
566 576
567int main(void) 577int main(void)
568{ 578{
569 setvbuf(stdout, nullptr, _IONBF, 0); 579 setvbuf(stdout, nullptr, _IONBF, 0);
570 580
571 Suite *onion = onion_suite(); 581 test_basic();
572 SRunner *test_runner = srunner_create(onion); 582 test_announce();
573
574 int number_failed = 0;
575 srunner_run_all(test_runner, CK_NORMAL);
576 number_failed = srunner_ntests_failed(test_runner);
577 583
578 srunner_free(test_runner); 584 return 0;
579
580 return number_failed;
581} 585}