diff options
author | Diadlo <polsha3@gmail.com> | 2017-01-07 11:28:53 +0300 |
---|---|---|
committer | Diadlo <polsha3@gmail.com> | 2017-02-26 23:16:16 +0300 |
commit | b19a9e54643eceaa52e71c0f38fa2ecae5a6809e (patch) | |
tree | 4c6fff0a086988ab21636e88dbf65c2e40a76a67 /auto_tests/TCP_test.c | |
parent | b2d04eae9d9b76523b877a9e5efd5989f83b143d (diff) |
Add part of platform-independent network API implementation
socket -> net_socket
htons -> net_htons
htonl -> net_htonl
connect -> net_connect
sendto -> net_sendto_ip4
getaddrinfo -> net_getipport
sa_family_t -> Family
Diffstat (limited to 'auto_tests/TCP_test.c')
-rw-r--r-- | auto_tests/TCP_test.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index a5a70c4b..bc684af9 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -38,13 +38,15 @@ START_TEST(test_basic) | |||
38 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 38 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
39 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 39 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
40 | 40 | ||
41 | Socket sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); | 41 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
42 | struct sockaddr_in6 addr6_loopback = {0}; | 42 | IP_Port ip_port_loopback; |
43 | addr6_loopback.sin6_family = AF_INET6; | 43 | ip_port_loopback.ip.family = AF_INET6; |
44 | addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]); | 44 | ip_port_loopback.ip.ip6.uint64[0] = 0; |
45 | addr6_loopback.sin6_addr = in6addr_loopback; | 45 | ip_port_loopback.ip.ip6.uint64[1] = 0; |
46 | 46 | ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1 | |
47 | int ret = connect(sock, (struct sockaddr *)&addr6_loopback, sizeof(addr6_loopback)); | 47 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); |
48 | |||
49 | int ret = net_connect(sock, ip_port_loopback); | ||
48 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); | 50 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); |
49 | 51 | ||
50 | uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 52 | uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
@@ -93,7 +95,7 @@ START_TEST(test_basic) | |||
93 | memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); | 95 | memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); |
94 | uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; | 96 | uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; |
95 | uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE; | 97 | uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE; |
96 | size = htons(size); | 98 | size = net_htons(size); |
97 | encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2); | 99 | encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2); |
98 | increment_nonce(f_nonce); | 100 | increment_nonce(f_nonce); |
99 | memcpy(r_req, &size, 2); | 101 | memcpy(r_req, &size, 2); |
@@ -113,7 +115,7 @@ START_TEST(test_basic) | |||
113 | ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, | 115 | ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, |
114 | "recv Failed. %u", recv_data_len); | 116 | "recv Failed. %u", recv_data_len); |
115 | memcpy(&size, packet_resp, 2); | 117 | memcpy(&size, packet_resp, 2); |
116 | ck_assert_msg(ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size."); | 118 | ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size."); |
117 | uint8_t packet_resp_plain[4096]; | 119 | uint8_t packet_resp_plain[4096]; |
118 | ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); | 120 | ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); |
119 | ck_assert_msg(ret != -1, "decryption failed"); | 121 | ck_assert_msg(ret != -1, "decryption failed"); |
@@ -136,13 +138,16 @@ struct sec_TCP_con { | |||
136 | static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | 138 | static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) |
137 | { | 139 | { |
138 | struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); | 140 | struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); |
139 | Socket sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); | 141 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
140 | struct sockaddr_in6 addr6_loopback = {0}; | 142 | |
141 | addr6_loopback.sin6_family = AF_INET6; | 143 | IP_Port ip_port_loopback; |
142 | addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]); | 144 | ip_port_loopback.ip.family = AF_INET6; |
143 | addr6_loopback.sin6_addr = in6addr_loopback; | 145 | ip_port_loopback.ip.ip6.uint64[0] = 0; |
146 | ip_port_loopback.ip.ip6.uint64[1] = 0; | ||
147 | ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1 | ||
148 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); | ||
144 | 149 | ||
145 | int ret = connect(sock, (struct sockaddr *)&addr6_loopback, sizeof(addr6_loopback)); | 150 | int ret = net_connect(sock, ip_port_loopback); |
146 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); | 151 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); |
147 | 152 | ||
148 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 153 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
@@ -190,7 +195,7 @@ static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t * | |||
190 | { | 195 | { |
191 | VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); | 196 | VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); |
192 | 197 | ||
193 | uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); | 198 | uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); |
194 | memcpy(packet, &c_length, sizeof(uint16_t)); | 199 | memcpy(packet, &c_length, sizeof(uint16_t)); |
195 | int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); | 200 | int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); |
196 | 201 | ||
@@ -405,7 +410,7 @@ START_TEST(test_client) | |||
405 | crypto_new_keypair(f_public_key, f_secret_key); | 410 | crypto_new_keypair(f_public_key, f_secret_key); |
406 | IP_Port ip_port_tcp_s; | 411 | IP_Port ip_port_tcp_s; |
407 | 412 | ||
408 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 413 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
409 | ip_port_tcp_s.ip.family = AF_INET6; | 414 | ip_port_tcp_s.ip.family = AF_INET6; |
410 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); | 415 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); |
411 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); | 416 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); |
@@ -435,7 +440,7 @@ START_TEST(test_client) | |||
435 | uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 440 | uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
436 | uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 441 | uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
437 | crypto_new_keypair(f2_public_key, f2_secret_key); | 442 | crypto_new_keypair(f2_public_key, f2_secret_key); |
438 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 443 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
439 | TCP_Client_Connection *conn2 = new_TCP_connection(ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, 0); | 444 | TCP_Client_Connection *conn2 = new_TCP_connection(ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, 0); |
440 | routing_response_handler(conn, response_callback, (char *)conn + 2); | 445 | routing_response_handler(conn, response_callback, (char *)conn + 2); |
441 | routing_status_handler(conn, status_callback, (void *)2); | 446 | routing_status_handler(conn, status_callback, (void *)2); |
@@ -503,7 +508,7 @@ START_TEST(test_client_invalid) | |||
503 | crypto_new_keypair(f_public_key, f_secret_key); | 508 | crypto_new_keypair(f_public_key, f_secret_key); |
504 | IP_Port ip_port_tcp_s; | 509 | IP_Port ip_port_tcp_s; |
505 | 510 | ||
506 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 511 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
507 | ip_port_tcp_s.ip.family = AF_INET6; | 512 | ip_port_tcp_s.ip.family = AF_INET6; |
508 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); | 513 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); |
509 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); | 514 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); |
@@ -572,7 +577,7 @@ START_TEST(test_tcp_connection) | |||
572 | 577 | ||
573 | IP_Port ip_port_tcp_s; | 578 | IP_Port ip_port_tcp_s; |
574 | 579 | ||
575 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 580 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
576 | ip_port_tcp_s.ip.family = AF_INET6; | 581 | ip_port_tcp_s.ip.family = AF_INET6; |
577 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); | 582 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); |
578 | 583 | ||
@@ -581,7 +586,7 @@ START_TEST(test_tcp_connection) | |||
581 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, | 586 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
582 | "Could not add tcp relay to connection\n"); | 587 | "Could not add tcp relay to connection\n"); |
583 | 588 | ||
584 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 589 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
585 | connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123); | 590 | connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123); |
586 | ck_assert_msg(connection == 0, "Connection id wrong"); | 591 | ck_assert_msg(connection == 0, "Connection id wrong"); |
587 | ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, | 592 | ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
@@ -681,7 +686,7 @@ START_TEST(test_tcp_connection2) | |||
681 | 686 | ||
682 | IP_Port ip_port_tcp_s; | 687 | IP_Port ip_port_tcp_s; |
683 | 688 | ||
684 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 689 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
685 | ip_port_tcp_s.ip.family = AF_INET6; | 690 | ip_port_tcp_s.ip.family = AF_INET6; |
686 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); | 691 | get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); |
687 | 692 | ||