summaryrefslogtreecommitdiff
path: root/auto_tests/TCP_test.c
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/TCP_test.c
parent54066f338f185f2fbd6694d9a4877f42cbfa21c8 (diff)
Use per-instance `Mono_Time` for Messenger and onion.
Diffstat (limited to 'auto_tests/TCP_test.c')
-rw-r--r--auto_tests/TCP_test.c152
1 files changed, 87 insertions, 65 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