From d6d305feeb76735ee4b4e14c6bca737a5482bc19 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 1 Aug 2018 23:02:13 +0000 Subject: Use per-instance `Mono_Time` for Messenger and onion. --- auto_tests/TCP_test.c | 152 +++++++++++++++++++++++++++++--------------------- 1 file changed, 87 insertions(+), 65 deletions(-) (limited to 'auto_tests/TCP_test.c') 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() return ip; } -static void do_TCP_server_delay(TCP_Server *tcp_s, int delay) +static void do_TCP_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay) { c_sleep(delay); - unix_time_update(); - do_TCP_server(tcp_s); + mono_time_update(mono_time); + do_TCP_server(tcp_s, mono_time); c_sleep(delay); } static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643}; START_TEST(test_basic) { + Mono_Time *mono_time = mono_time_new(); + // Attempt to create a new TCP_Server instance. uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -99,12 +101,12 @@ START_TEST(test_basic) ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "An attempt to send the initial handshake minus last byte failed."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "The attempt to send the last byte of handshake failed."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); // Receiving server response and decrypting it uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; @@ -142,8 +144,8 @@ START_TEST(test_basic) i += msg_length; c_sleep(50); - unix_time_update(); - do_TCP_server(tcp_s); + mono_time_update(mono_time); + do_TCP_server(tcp_s, mono_time); } // Receiving the second response and verifying its validity @@ -167,6 +169,8 @@ START_TEST(test_basic) // Closing connections. kill_sock(sock); kill_TCP_server(tcp_s); + + mono_time_free(mono_time); } END_TEST @@ -178,7 +182,7 @@ struct sec_TCP_con { uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; }; -static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) +static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s, Mono_Time *mono_time) { struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); 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) ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "Failed to send the first portion of the handshake to the TCP relay server."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "Failed to send last byte of handshake."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; 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 START_TEST(test_some) { + Mono_Time *mono_time = mono_time_new(); + uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); @@ -273,9 +279,9 @@ START_TEST(test_some) ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports."); - struct sec_TCP_con *con1 = new_TCP_con(tcp_s); - struct sec_TCP_con *con2 = new_TCP_con(tcp_s); - struct sec_TCP_con *con3 = new_TCP_con(tcp_s); + struct sec_TCP_con *con1 = new_TCP_con(tcp_s, mono_time); + struct sec_TCP_con *con2 = new_TCP_con(tcp_s, mono_time); + struct sec_TCP_con *con3 = new_TCP_con(tcp_s, mono_time); uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; requ_p[0] = 0; @@ -286,7 +292,7 @@ START_TEST(test_some) memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p)); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); // Testing response from connection 1 uint8_t data[2048]; @@ -309,7 +315,7 @@ START_TEST(test_some) write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); ck_assert_msg(len == 2, "wrong len %d", len); @@ -334,7 +340,7 @@ START_TEST(test_some) write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); 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) uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); @@ -363,6 +369,8 @@ START_TEST(test_some) kill_TCP_con(con1); kill_TCP_con(con2); kill_TCP_con(con3); + + mono_time_free(mono_time); } END_TEST @@ -449,7 +457,8 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint START_TEST(test_client) { - unix_time_update(); + Mono_Time *mono_time = mono_time_new(); + uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); @@ -465,8 +474,9 @@ START_TEST(test_client) ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); - TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); - do_TCP_connection(conn, nullptr); + TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, + nullptr); + do_TCP_connection(mono_time, conn, nullptr); c_sleep(50); // The connection status should be unconfirmed here because we have finished @@ -474,13 +484,13 @@ START_TEST(test_client) ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong connection status. Expected: %d, is: %d.", TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn)); - do_TCP_server_delay(tcp_s, 50); // Now let the server handle requests... + do_TCP_server_delay(tcp_s, mono_time, 50); // Now let the server handle requests... const uint8_t LOOP_SIZE = 3; for (uint8_t i = 0; i < LOOP_SIZE; i++) { - unix_time_update(); - do_TCP_connection(conn, nullptr); // Run the connection loop. + mono_time_update(mono_time); + do_TCP_connection(mono_time, conn, nullptr); // Run the connection loop. // The status of the connection should continue to be TCP_CLIENT_CONFIRMED after multiple subsequent do_TCP_connection() calls. 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) c_sleep(i == LOOP_SIZE - 1 ? 0 : 500); // Sleep for 500ms on all except third loop. } - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); // And still after the server runs again. 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) uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(f2_public_key, f2_secret_key); ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); - TCP_Client_Connection *conn2 = new_TCP_connection( - ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); + TCP_Client_Connection *conn2 = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f2_public_key, + f2_secret_key, nullptr); // The client should call this function (defined earlier) during the routing process. routing_response_handler(conn, response_callback, (char *)conn + 2); @@ -514,13 +524,13 @@ START_TEST(test_client) // These integers will increment per successful callback. oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0; - do_TCP_connection(conn, nullptr); - do_TCP_connection(conn2, nullptr); + do_TCP_connection(mono_time, conn, nullptr); + do_TCP_connection(mono_time, conn2, nullptr); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); - do_TCP_connection(conn, nullptr); - do_TCP_connection(conn2, nullptr); + do_TCP_connection(mono_time, conn, nullptr); + do_TCP_connection(mono_time, conn2, nullptr); c_sleep(50); uint8_t data[5] = {1, 2, 3, 4, 5}; @@ -529,10 +539,10 @@ START_TEST(test_client) send_routing_request(conn, f2_public_key); send_routing_request(conn2, f_public_key); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); - do_TCP_connection(conn, nullptr); - do_TCP_connection(conn2, nullptr); + do_TCP_connection(mono_time, conn, nullptr); + do_TCP_connection(mono_time, conn2, nullptr); // All callback methods save data should have run during the above network prodding. ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called"); @@ -543,22 +553,22 @@ START_TEST(test_client) ck_assert_msg(status_callback_connection_id == response_callback_connection_id, "Status and response callback connection IDs are not equal."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); ck_assert_msg(send_data(conn2, 0, data, 5) == 1, "Failed a send_data() call."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); - do_TCP_connection(conn, nullptr); - do_TCP_connection(conn2, nullptr); + do_TCP_connection(mono_time, conn, nullptr); + do_TCP_connection(mono_time, conn2, nullptr); ck_assert_msg(data_callback_good == 1, "Data callback was not called."); status_callback_good = 0; send_disconnect_request(conn2, 0); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); - do_TCP_connection(conn, nullptr); - do_TCP_connection(conn2, nullptr); + do_TCP_connection(mono_time, conn, nullptr); + do_TCP_connection(mono_time, conn2, nullptr); ck_assert_msg(status_callback_good == 1, "Status callback not called"); ck_assert_msg(status_callback_status == 1, "Wrong status callback status."); @@ -566,13 +576,16 @@ START_TEST(test_client) kill_TCP_server(tcp_s); kill_TCP_connection(conn); kill_TCP_connection(conn2); + + mono_time_free(mono_time); } END_TEST // Test how the client handles servers that don't respond. START_TEST(test_client_invalid) { - unix_time_update(); + Mono_Time *mono_time = mono_time_new(); + uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); @@ -584,11 +597,12 @@ START_TEST(test_client_invalid) ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); - TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); + TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, + nullptr); // Run the client's main loop but not the server. - unix_time_update(); - do_TCP_connection(conn, nullptr); + mono_time_update(mono_time); + do_TCP_connection(mono_time, conn, nullptr); c_sleep(50); // After 50ms of no response... @@ -596,18 +610,20 @@ START_TEST(test_client_invalid) TCP_CLIENT_CONNECTING, tcp_con_status(conn)); // After 5s... c_sleep(5000); - unix_time_update(); - do_TCP_connection(conn, nullptr); + mono_time_update(mono_time); + do_TCP_connection(mono_time, conn, nullptr); ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.", TCP_CLIENT_CONNECTING, tcp_con_status(conn)); // 11s... (Should wait for 10 before giving up.) c_sleep(6000); - unix_time_update(); - do_TCP_connection(conn, nullptr); + mono_time_update(mono_time); + do_TCP_connection(mono_time, conn, nullptr); ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %d, is: %d.", TCP_CLIENT_DISCONNECTED, tcp_con_status(conn)); kill_TCP_connection(conn); + + mono_time_free(mono_time); } END_TEST @@ -639,8 +655,9 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t START_TEST(test_tcp_connection) { + Mono_Time *mono_time = mono_time_new(); + tcp_data_callback_called = 0; - unix_time_update(); uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); @@ -650,11 +667,11 @@ START_TEST(test_tcp_connection) TCP_Proxy_Info proxy_info; proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_1 = new_tcp_connections(self_secret_key, &proxy_info); + TCP_Connections *tc_1 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_2 = new_tcp_connections(self_secret_key, &proxy_info); + TCP_Connections *tc_2 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); IP_Port ip_port_tcp_s; @@ -676,17 +693,17 @@ START_TEST(test_tcp_connection) ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1, "Managed to readd same connection\n"); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); @@ -695,7 +712,7 @@ START_TEST(test_tcp_connection) ck_assert_msg(ret == 0, "could not send packet."); set_packet_tcp_connection_callback(tc_2, &tcp_data_callback, (void *) 120397); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); @@ -704,7 +721,7 @@ START_TEST(test_tcp_connection) ck_assert_msg(tcp_connection_to_online_tcp_relays(tc_1, 0) == 1, "Wrong number of connected relays"); ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n"); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); @@ -715,6 +732,8 @@ START_TEST(test_tcp_connection) kill_TCP_server(tcp_s); kill_tcp_connections(tc_1); kill_tcp_connections(tc_2); + + mono_time_free(mono_time); } END_TEST @@ -741,10 +760,11 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne START_TEST(test_tcp_connection2) { + Mono_Time *mono_time = mono_time_new(); + tcp_oobdata_callback_called = 0; tcp_data_callback_called = 0; - unix_time_update(); uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); @@ -754,11 +774,11 @@ START_TEST(test_tcp_connection2) TCP_Proxy_Info proxy_info; proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_1 = new_tcp_connections(self_secret_key, &proxy_info); + TCP_Connections *tc_1 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_2 = new_tcp_connections(self_secret_key, &proxy_info); + TCP_Connections *tc_2 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); IP_Port ip_port_tcp_s; @@ -774,17 +794,17 @@ START_TEST(test_tcp_connection2) ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, "Could not add global relay"); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); @@ -794,14 +814,14 @@ START_TEST(test_tcp_connection2) set_oob_packet_tcp_connection_callback(tc_2, &tcp_oobdata_callback, tc_2); set_packet_tcp_connection_callback(tc_1, &tcp_data_callback, (void *) 120397); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet."); - do_TCP_server_delay(tcp_s, 50); + do_TCP_server_delay(tcp_s, mono_time, 50); do_tcp_connections(tc_1, nullptr); do_tcp_connections(tc_2, nullptr); @@ -812,6 +832,8 @@ START_TEST(test_tcp_connection2) kill_TCP_server(tcp_s); kill_tcp_connections(tc_1); kill_tcp_connections(tc_2); + + mono_time_free(mono_time); } END_TEST -- cgit v1.2.3