diff options
author | hugbubby <hugbubby@protonmail.com> | 2018-06-26 19:07:08 -0700 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-28 14:05:03 +0000 |
commit | 4e3bfac47dc529513195f05ab80211d8a3f845a2 (patch) | |
tree | cc321dcde1d43ece5601b9821f645aa7df01eac4 /auto_tests/TCP_test.c | |
parent | 72863b9b8052f08ca033f26afef0e01575fb1305 (diff) |
Another TCP_test.c upgrade
Mostly documentation + comments. Some cases where code was removed
in exchange for more compact/less sprawly for loops. Introduced a
function that removed like 30 lines of repeated code.
Diffstat (limited to 'auto_tests/TCP_test.c')
-rw-r--r-- | auto_tests/TCP_test.c | 227 |
1 files changed, 120 insertions, 107 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 2db46602..134bdf9b 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -45,6 +45,12 @@ static inline IP get_loopback() | |||
45 | return ip; | 45 | return ip; |
46 | } | 46 | } |
47 | 47 | ||
48 | static void do_TCP_server_delay(TCP_Server *tcp_s, int delay) | ||
49 | { | ||
50 | c_sleep(delay); | ||
51 | do_TCP_server(tcp_s); | ||
52 | c_sleep(delay); | ||
53 | } | ||
48 | static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643}; | 54 | static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643}; |
49 | 55 | ||
50 | START_TEST(test_basic) | 56 | START_TEST(test_basic) |
@@ -100,16 +106,13 @@ START_TEST(test_basic) | |||
100 | // Sending the handshake | 106 | // Sending the handshake |
101 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, | 107 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, |
102 | "An attempt to send the initial handshake minus last byte failed."); | 108 | "An attempt to send the initial handshake minus last byte failed."); |
103 | c_sleep(50); | 109 | |
104 | do_TCP_server(tcp_s); | 110 | do_TCP_server_delay(tcp_s, 50); |
105 | c_sleep(50); | ||
106 | 111 | ||
107 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, | 112 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, |
108 | "The attempt to send the last byte of handshake failed."); | 113 | "The attempt to send the last byte of handshake failed."); |
109 | 114 | ||
110 | c_sleep(50); | 115 | do_TCP_server_delay(tcp_s, 50); |
111 | do_TCP_server(tcp_s); | ||
112 | c_sleep(50); | ||
113 | 116 | ||
114 | // Receiving server response and decrypting it | 117 | // Receiving server response and decrypting it |
115 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; | 118 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; |
@@ -213,15 +216,13 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | |||
213 | 216 | ||
214 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, | 217 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, |
215 | "Failed to send the first portion of the handshake to the TCP relay server."); | 218 | "Failed to send the first portion of the handshake to the TCP relay server."); |
216 | c_sleep(50); | 219 | |
217 | do_TCP_server(tcp_s); | 220 | do_TCP_server_delay(tcp_s, 50); |
218 | c_sleep(50); | ||
219 | 221 | ||
220 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, | 222 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, |
221 | "Failed to send last byte of handshake."); | 223 | "Failed to send last byte of handshake."); |
222 | c_sleep(50); | 224 | |
223 | do_TCP_server(tcp_s); | 225 | do_TCP_server_delay(tcp_s, 50); |
224 | c_sleep(50); | ||
225 | 226 | ||
226 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; | 227 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; |
227 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; | 228 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; |
@@ -256,7 +257,7 @@ static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t * | |||
256 | 257 | ||
257 | increment_nonce(con->sent_nonce); | 258 | increment_nonce(con->sent_nonce); |
258 | 259 | ||
259 | ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "Failed to send a packet"); | 260 | ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "Failed to send a packet."); |
260 | return 0; | 261 | return 0; |
261 | } | 262 | } |
262 | 263 | ||
@@ -277,7 +278,7 @@ START_TEST(test_some) | |||
277 | crypto_new_keypair(self_public_key, self_secret_key); | 278 | crypto_new_keypair(self_public_key, self_secret_key); |
278 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); | 279 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); |
279 | ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); | 280 | ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); |
280 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 281 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports."); |
281 | 282 | ||
282 | struct sec_TCP_con *con1 = new_TCP_con(tcp_s); | 283 | struct sec_TCP_con *con1 = new_TCP_con(tcp_s); |
283 | struct sec_TCP_con *con2 = new_TCP_con(tcp_s); | 284 | struct sec_TCP_con *con2 = new_TCP_con(tcp_s); |
@@ -292,9 +293,7 @@ START_TEST(test_some) | |||
292 | memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); | 293 | memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); |
293 | write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p)); | 294 | write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p)); |
294 | 295 | ||
295 | c_sleep(50); | 296 | do_TCP_server_delay(tcp_s, 50); |
296 | do_TCP_server(tcp_s); | ||
297 | c_sleep(50); | ||
298 | 297 | ||
299 | // Testing response from connection 1 | 298 | // Testing response from connection 1 |
300 | uint8_t data[2048]; | 299 | uint8_t data[2048]; |
@@ -311,15 +310,13 @@ START_TEST(test_some) | |||
311 | ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); | 310 | ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); |
312 | ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong."); | 311 | ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong."); |
313 | 312 | ||
314 | uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; //What is this packet???? | 313 | uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? |
315 | 314 | ||
316 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 315 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
317 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 316 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
318 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 317 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
319 | 318 | ||
320 | c_sleep(50); | 319 | do_TCP_server_delay(tcp_s, 50); |
321 | do_TCP_server(tcp_s); | ||
322 | c_sleep(50); | ||
323 | 320 | ||
324 | len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); | 321 | len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); |
325 | ck_assert_msg(len == 2, "wrong len %d", len); | 322 | ck_assert_msg(len == 2, "wrong len %d", len); |
@@ -344,9 +341,7 @@ START_TEST(test_some) | |||
344 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | 341 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); |
345 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | 342 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); |
346 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | 343 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); |
347 | c_sleep(50); | 344 | do_TCP_server_delay(tcp_s, 50); |
348 | do_TCP_server(tcp_s); | ||
349 | c_sleep(50); | ||
350 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 345 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
351 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); | 346 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
352 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 347 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
@@ -362,9 +357,9 @@ START_TEST(test_some) | |||
362 | 357 | ||
363 | uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; | 358 | uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; |
364 | write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); | 359 | write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); |
365 | c_sleep(50); | 360 | |
366 | do_TCP_server(tcp_s); | 361 | do_TCP_server_delay(tcp_s, 50); |
367 | c_sleep(50); | 362 | |
368 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); | 363 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); |
369 | ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); | 364 | ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); |
370 | ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); | 365 | ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); |
@@ -466,8 +461,8 @@ START_TEST(test_client) | |||
466 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 461 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
467 | crypto_new_keypair(self_public_key, self_secret_key); | 462 | crypto_new_keypair(self_public_key, self_secret_key); |
468 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); | 463 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); |
469 | ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); | 464 | ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server."); |
470 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 465 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind the relay server to all ports."); |
471 | 466 | ||
472 | uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 467 | uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
473 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 468 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
@@ -476,28 +471,34 @@ START_TEST(test_client) | |||
476 | 471 | ||
477 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); | 472 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
478 | ip_port_tcp_s.ip = get_loopback(); | 473 | ip_port_tcp_s.ip = get_loopback(); |
474 | |||
479 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); | 475 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); |
480 | c_sleep(50); | ||
481 | do_TCP_connection(conn, nullptr); | 476 | do_TCP_connection(conn, nullptr); |
482 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong status. Expected: %u, is: %u", | ||
483 | TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn)); | ||
484 | c_sleep(50); | ||
485 | do_TCP_server(tcp_s); | ||
486 | c_sleep(50); | 477 | c_sleep(50); |
487 | do_TCP_connection(conn, nullptr); | 478 | |
488 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED, | 479 | // The connection status should be unconfirmed here because we have finished |
489 | tcp_con_status(conn)); | 480 | // sending our data and are awaiting a response. |
490 | c_sleep(500); | 481 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong connection status. Expected: %d, is: %d.", |
491 | do_TCP_connection(conn, nullptr); | 482 | TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn)); |
492 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED, | 483 | |
493 | tcp_con_status(conn)); | 484 | do_TCP_server_delay(tcp_s, 50); // Now let the server handle requests... |
494 | c_sleep(500); | 485 | |
495 | do_TCP_connection(conn, nullptr); | 486 | const uint8_t LOOP_SIZE = 3; |
496 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED, | 487 | |
497 | tcp_con_status(conn)); | 488 | for (uint8_t i = 0; i < LOOP_SIZE; i++) { |
498 | do_TCP_server(tcp_s); | 489 | do_TCP_connection(conn, nullptr); // Run the connection loop. |
499 | c_sleep(50); | 490 | |
500 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED, | 491 | // The status of the connection should continue to be TCP_CLIENT_CONFIRMED after multiple subsequent do_TCP_connection() calls. |
492 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong connection status. Expected: %d, is: %d", | ||
493 | TCP_CLIENT_CONFIRMED, tcp_con_status(conn)); | ||
494 | |||
495 | c_sleep(i == LOOP_SIZE - 1 ? 0 : 500); // Sleep for 500ms on all except third loop. | ||
496 | } | ||
497 | |||
498 | do_TCP_server_delay(tcp_s, 50); | ||
499 | |||
500 | // And still after the server runs again. | ||
501 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %d, is: %d", TCP_CLIENT_CONFIRMED, | ||
501 | tcp_con_status(conn)); | 502 | tcp_con_status(conn)); |
502 | 503 | ||
503 | uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 504 | uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
@@ -506,60 +507,75 @@ START_TEST(test_client) | |||
506 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); | 507 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
507 | TCP_Client_Connection *conn2 = new_TCP_connection( | 508 | TCP_Client_Connection *conn2 = new_TCP_connection( |
508 | ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); | 509 | ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); |
510 | |||
511 | // The client should call this function (defined earlier) during the routing process. | ||
509 | routing_response_handler(conn, response_callback, (char *)conn + 2); | 512 | routing_response_handler(conn, response_callback, (char *)conn + 2); |
513 | // The client should call this function when it receives a connection notification. | ||
510 | routing_status_handler(conn, status_callback, (void *)2); | 514 | routing_status_handler(conn, status_callback, (void *)2); |
515 | // The client should call this function when | ||
511 | routing_data_handler(conn, data_callback, (void *)3); | 516 | routing_data_handler(conn, data_callback, (void *)3); |
517 | // The client should call this function when sending out of band packets. | ||
512 | oob_data_handler(conn, oob_data_callback, (void *)4); | 518 | oob_data_handler(conn, oob_data_callback, (void *)4); |
519 | |||
520 | // These integers will increment per successful callback. | ||
513 | oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0; | 521 | oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0; |
514 | c_sleep(50); | 522 | |
515 | do_TCP_connection(conn, nullptr); | 523 | do_TCP_connection(conn, nullptr); |
516 | do_TCP_connection(conn2, nullptr); | 524 | do_TCP_connection(conn2, nullptr); |
517 | c_sleep(50); | 525 | |
518 | do_TCP_server(tcp_s); | 526 | do_TCP_server_delay(tcp_s, 50); |
519 | c_sleep(50); | 527 | |
520 | do_TCP_connection(conn, nullptr); | 528 | do_TCP_connection(conn, nullptr); |
521 | do_TCP_connection(conn2, nullptr); | 529 | do_TCP_connection(conn2, nullptr); |
522 | c_sleep(50); | 530 | c_sleep(50); |
531 | |||
523 | uint8_t data[5] = {1, 2, 3, 4, 5}; | 532 | uint8_t data[5] = {1, 2, 3, 4, 5}; |
524 | memcpy(oob_pubkey, f2_public_key, CRYPTO_PUBLIC_KEY_SIZE); | 533 | memcpy(oob_pubkey, f2_public_key, CRYPTO_PUBLIC_KEY_SIZE); |
525 | send_oob_packet(conn2, f_public_key, data, 5); | 534 | send_oob_packet(conn2, f_public_key, data, 5); |
526 | send_routing_request(conn, f2_public_key); | 535 | send_routing_request(conn, f2_public_key); |
527 | send_routing_request(conn2, f_public_key); | 536 | send_routing_request(conn2, f_public_key); |
528 | c_sleep(50); | 537 | |
529 | do_TCP_server(tcp_s); | 538 | do_TCP_server_delay(tcp_s, 50); |
530 | c_sleep(50); | 539 | |
531 | do_TCP_connection(conn, nullptr); | 540 | do_TCP_connection(conn, nullptr); |
532 | do_TCP_connection(conn2, nullptr); | 541 | do_TCP_connection(conn2, nullptr); |
533 | ck_assert_msg(oob_data_callback_good == 1, "oob callback not called"); | 542 | |
534 | ck_assert_msg(response_callback_good == 1, "response callback not called"); | 543 | // All callback methods save data should have run during the above network prodding. |
535 | ck_assert_msg(public_key_cmp(response_callback_public_key, f2_public_key) == 0, "wrong public key"); | 544 | ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called"); |
536 | ck_assert_msg(status_callback_good == 1, "status callback not called"); | 545 | ck_assert_msg(response_callback_good == 1, "Response callback not called."); |
537 | ck_assert_msg(status_callback_status == 2, "wrong status"); | 546 | ck_assert_msg(public_key_cmp(response_callback_public_key, f2_public_key) == 0, "Wrong public key."); |
538 | ck_assert_msg(status_callback_connection_id == response_callback_connection_id, "connection ids not equal"); | 547 | ck_assert_msg(status_callback_good == 1, "Status callback not called."); |
539 | c_sleep(50); | 548 | ck_assert_msg(status_callback_status == 2, "Wrong status callback status."); |
540 | do_TCP_server(tcp_s); | 549 | ck_assert_msg(status_callback_connection_id == response_callback_connection_id, |
541 | ck_assert_msg(send_data(conn2, 0, data, 5) == 1, "send data failed"); | 550 | "Status and response callback connection IDs are not equal."); |
542 | c_sleep(50); | 551 | |
543 | do_TCP_server(tcp_s); | 552 | do_TCP_server_delay(tcp_s, 50); |
544 | c_sleep(50); | 553 | |
554 | ck_assert_msg(send_data(conn2, 0, data, 5) == 1, "Failed a send_data() call."); | ||
555 | |||
556 | do_TCP_server_delay(tcp_s, 50); | ||
557 | |||
545 | do_TCP_connection(conn, nullptr); | 558 | do_TCP_connection(conn, nullptr); |
546 | do_TCP_connection(conn2, nullptr); | 559 | do_TCP_connection(conn2, nullptr); |
547 | ck_assert_msg(data_callback_good == 1, "data callback not called"); | 560 | ck_assert_msg(data_callback_good == 1, "Data callback was not called."); |
548 | status_callback_good = 0; | 561 | status_callback_good = 0; |
549 | send_disconnect_request(conn2, 0); | 562 | send_disconnect_request(conn2, 0); |
550 | c_sleep(50); | 563 | |
551 | do_TCP_server(tcp_s); | 564 | do_TCP_server_delay(tcp_s, 50); |
552 | c_sleep(50); | 565 | |
553 | do_TCP_connection(conn, nullptr); | 566 | do_TCP_connection(conn, nullptr); |
554 | do_TCP_connection(conn2, nullptr); | 567 | do_TCP_connection(conn2, nullptr); |
555 | ck_assert_msg(status_callback_good == 1, "status callback not called"); | 568 | ck_assert_msg(status_callback_good == 1, "Status callback not called"); |
556 | ck_assert_msg(status_callback_status == 1, "wrong status"); | 569 | ck_assert_msg(status_callback_status == 1, "Wrong status callback status."); |
570 | |||
571 | // Kill off all connections and servers. | ||
557 | kill_TCP_server(tcp_s); | 572 | kill_TCP_server(tcp_s); |
558 | kill_TCP_connection(conn); | 573 | kill_TCP_connection(conn); |
559 | kill_TCP_connection(conn2); | 574 | kill_TCP_connection(conn2); |
560 | } | 575 | } |
561 | END_TEST | 576 | END_TEST |
562 | 577 | ||
578 | // Test how the client handles servers that don't respond. | ||
563 | START_TEST(test_client_invalid) | 579 | START_TEST(test_client_invalid) |
564 | { | 580 | { |
565 | unix_time_update(); | 581 | unix_time_update(); |
@@ -575,17 +591,23 @@ START_TEST(test_client_invalid) | |||
575 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); | 591 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
576 | ip_port_tcp_s.ip = get_loopback(); | 592 | ip_port_tcp_s.ip = get_loopback(); |
577 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); | 593 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); |
578 | c_sleep(50); | 594 | |
595 | // Run the client's main loop but not the server. | ||
579 | do_TCP_connection(conn, nullptr); | 596 | do_TCP_connection(conn, nullptr); |
580 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %u, is: %u", | 597 | c_sleep(50); |
598 | |||
599 | // After 50ms of no response... | ||
600 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.", | ||
581 | TCP_CLIENT_CONNECTING, tcp_con_status(conn)); | 601 | TCP_CLIENT_CONNECTING, tcp_con_status(conn)); |
602 | // After 5s... | ||
582 | c_sleep(5000); | 603 | c_sleep(5000); |
583 | do_TCP_connection(conn, nullptr); | 604 | do_TCP_connection(conn, nullptr); |
584 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %u, is: %u", | 605 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.", |
585 | TCP_CLIENT_CONNECTING, tcp_con_status(conn)); | 606 | TCP_CLIENT_CONNECTING, tcp_con_status(conn)); |
607 | // 11s... (Should wait for 10 before giving up.) | ||
586 | c_sleep(6000); | 608 | c_sleep(6000); |
587 | do_TCP_connection(conn, nullptr); | 609 | do_TCP_connection(conn, nullptr); |
588 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %u, is: %u", | 610 | ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %d, is: %d.", |
589 | TCP_CLIENT_DISCONNECTED, tcp_con_status(conn)); | 611 | TCP_CLIENT_DISCONNECTED, tcp_con_status(conn)); |
590 | 612 | ||
591 | kill_TCP_connection(conn); | 613 | kill_TCP_connection(conn); |
@@ -657,19 +679,18 @@ START_TEST(test_tcp_connection) | |||
657 | ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1, | 679 | ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1, |
658 | "Managed to readd same connection\n"); | 680 | "Managed to readd same connection\n"); |
659 | 681 | ||
660 | c_sleep(50); | 682 | do_TCP_server_delay(tcp_s, 50); |
661 | do_TCP_server(tcp_s); | 683 | |
662 | c_sleep(50); | ||
663 | do_tcp_connections(tc_1, nullptr); | 684 | do_tcp_connections(tc_1, nullptr); |
664 | do_tcp_connections(tc_2, nullptr); | 685 | do_tcp_connections(tc_2, nullptr); |
665 | c_sleep(50); | 686 | |
666 | do_TCP_server(tcp_s); | 687 | do_TCP_server_delay(tcp_s, 50); |
667 | c_sleep(50); | 688 | |
668 | do_tcp_connections(tc_1, nullptr); | 689 | do_tcp_connections(tc_1, nullptr); |
669 | do_tcp_connections(tc_2, nullptr); | 690 | do_tcp_connections(tc_2, nullptr); |
670 | c_sleep(50); | 691 | |
671 | do_TCP_server(tcp_s); | 692 | do_TCP_server_delay(tcp_s, 50); |
672 | c_sleep(50); | 693 | |
673 | do_tcp_connections(tc_1, nullptr); | 694 | do_tcp_connections(tc_1, nullptr); |
674 | do_tcp_connections(tc_2, nullptr); | 695 | do_tcp_connections(tc_2, nullptr); |
675 | 696 | ||
@@ -677,9 +698,7 @@ START_TEST(test_tcp_connection) | |||
677 | ck_assert_msg(ret == 0, "could not send packet."); | 698 | ck_assert_msg(ret == 0, "could not send packet."); |
678 | set_packet_tcp_connection_callback(tc_2, &tcp_data_callback, (void *) 120397); | 699 | set_packet_tcp_connection_callback(tc_2, &tcp_data_callback, (void *) 120397); |
679 | 700 | ||
680 | c_sleep(50); | 701 | do_TCP_server_delay(tcp_s, 50); |
681 | do_TCP_server(tcp_s); | ||
682 | c_sleep(50); | ||
683 | 702 | ||
684 | do_tcp_connections(tc_1, nullptr); | 703 | do_tcp_connections(tc_1, nullptr); |
685 | do_tcp_connections(tc_2, nullptr); | 704 | do_tcp_connections(tc_2, nullptr); |
@@ -688,9 +707,8 @@ START_TEST(test_tcp_connection) | |||
688 | ck_assert_msg(tcp_connection_to_online_tcp_relays(tc_1, 0) == 1, "Wrong number of connected relays"); | 707 | ck_assert_msg(tcp_connection_to_online_tcp_relays(tc_1, 0) == 1, "Wrong number of connected relays"); |
689 | ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n"); | 708 | ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n"); |
690 | 709 | ||
691 | c_sleep(50); | 710 | do_TCP_server_delay(tcp_s, 50); |
692 | do_TCP_server(tcp_s); | 711 | |
693 | c_sleep(50); | ||
694 | do_tcp_connections(tc_1, nullptr); | 712 | do_tcp_connections(tc_1, nullptr); |
695 | do_tcp_connections(tc_2, nullptr); | 713 | do_tcp_connections(tc_2, nullptr); |
696 | 714 | ||
@@ -759,19 +777,18 @@ START_TEST(test_tcp_connection2) | |||
759 | ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, | 777 | ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
760 | "Could not add global relay"); | 778 | "Could not add global relay"); |
761 | 779 | ||
762 | c_sleep(50); | 780 | do_TCP_server_delay(tcp_s, 50); |
763 | do_TCP_server(tcp_s); | 781 | |
764 | c_sleep(50); | ||
765 | do_tcp_connections(tc_1, nullptr); | 782 | do_tcp_connections(tc_1, nullptr); |
766 | do_tcp_connections(tc_2, nullptr); | 783 | do_tcp_connections(tc_2, nullptr); |
767 | c_sleep(50); | 784 | |
768 | do_TCP_server(tcp_s); | 785 | do_TCP_server_delay(tcp_s, 50); |
769 | c_sleep(50); | 786 | |
770 | do_tcp_connections(tc_1, nullptr); | 787 | do_tcp_connections(tc_1, nullptr); |
771 | do_tcp_connections(tc_2, nullptr); | 788 | do_tcp_connections(tc_2, nullptr); |
772 | c_sleep(50); | 789 | |
773 | do_TCP_server(tcp_s); | 790 | do_TCP_server_delay(tcp_s, 50); |
774 | c_sleep(50); | 791 | |
775 | do_tcp_connections(tc_1, nullptr); | 792 | do_tcp_connections(tc_1, nullptr); |
776 | do_tcp_connections(tc_2, nullptr); | 793 | do_tcp_connections(tc_2, nullptr); |
777 | 794 | ||
@@ -780,18 +797,14 @@ START_TEST(test_tcp_connection2) | |||
780 | set_oob_packet_tcp_connection_callback(tc_2, &tcp_oobdata_callback, tc_2); | 797 | set_oob_packet_tcp_connection_callback(tc_2, &tcp_oobdata_callback, tc_2); |
781 | set_packet_tcp_connection_callback(tc_1, &tcp_data_callback, (void *) 120397); | 798 | set_packet_tcp_connection_callback(tc_1, &tcp_data_callback, (void *) 120397); |
782 | 799 | ||
783 | c_sleep(50); | 800 | do_TCP_server_delay(tcp_s, 50); |
784 | do_TCP_server(tcp_s); | ||
785 | c_sleep(50); | ||
786 | 801 | ||
787 | do_tcp_connections(tc_1, nullptr); | 802 | do_tcp_connections(tc_1, nullptr); |
788 | do_tcp_connections(tc_2, nullptr); | 803 | do_tcp_connections(tc_2, nullptr); |
789 | 804 | ||
790 | ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet."); | 805 | ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet."); |
791 | 806 | ||
792 | c_sleep(50); | 807 | do_TCP_server_delay(tcp_s, 50); |
793 | do_TCP_server(tcp_s); | ||
794 | c_sleep(50); | ||
795 | 808 | ||
796 | do_tcp_connections(tc_1, nullptr); | 809 | do_tcp_connections(tc_1, nullptr); |
797 | do_tcp_connections(tc_2, nullptr); | 810 | do_tcp_connections(tc_2, nullptr); |