2014-03-15 02:12:35 +08:00
|
|
|
#include <stdlib.h>
|
2016-09-01 07:33:20 +08:00
|
|
|
#include <string.h>
|
2014-03-15 02:12:35 +08:00
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2014-04-07 08:51:03 +08:00
|
|
|
#include "../toxcore/TCP_client.h"
|
2022-01-16 06:10:23 +08:00
|
|
|
#include "../toxcore/TCP_common.h"
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "../toxcore/TCP_server.h"
|
2018-03-28 21:36:14 +08:00
|
|
|
#include "../toxcore/crypto_core.h"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2014-03-15 02:12:35 +08:00
|
|
|
#include "../toxcore/util.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "check_compat.h"
|
2014-08-08 04:59:47 +08:00
|
|
|
|
2014-03-15 02:12:35 +08:00
|
|
|
#define NUM_PORTS 3
|
|
|
|
|
2017-11-15 16:13:31 +08:00
|
|
|
#ifndef USE_IPV6
|
|
|
|
#define USE_IPV6 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !USE_IPV6
|
2018-02-27 09:07:46 +08:00
|
|
|
#define net_family_ipv6 net_family_ipv4
|
2017-11-15 16:13:31 +08:00
|
|
|
#endif
|
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
static IP get_loopback(void)
|
2017-11-15 16:13:31 +08:00
|
|
|
{
|
|
|
|
IP ip;
|
|
|
|
#if USE_IPV6
|
2018-02-27 09:07:46 +08:00
|
|
|
ip.family = net_family_ipv6;
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v6 = get_ip6_loopback();
|
2017-11-15 16:13:31 +08:00
|
|
|
#else
|
2018-02-27 09:07:46 +08:00
|
|
|
ip.family = net_family_ipv4;
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v4 = get_ip4_loopback();
|
2017-11-15 16:13:31 +08:00
|
|
|
#endif
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
static void do_TCP_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay)
|
2018-06-27 10:07:08 +08:00
|
|
|
{
|
|
|
|
c_sleep(delay);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
|
|
|
do_TCP_server(tcp_s, mono_time);
|
2018-06-27 10:07:08 +08:00
|
|
|
c_sleep(delay);
|
|
|
|
}
|
2018-06-24 20:41:26 +08:00
|
|
|
static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2022-01-16 09:06:33 +08:00
|
|
|
static void test_basic(void)
|
2014-03-15 02:12:35 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2020-05-04 07:40:59 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2018-06-20 15:33:19 +08:00
|
|
|
// Attempt to create a new TCP_Server instance.
|
2016-12-19 10:47:42 +08:00
|
|
|
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);
|
2020-05-04 07:40:59 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
|
2018-06-20 15:33:19 +08:00
|
|
|
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS,
|
2018-06-21 23:00:25 +08:00
|
|
|
"Failed to bind a TCP relay server to all %d attempted ports.", NUM_PORTS);
|
2018-06-20 15:33:19 +08:00
|
|
|
|
2018-06-23 22:38:07 +08:00
|
|
|
Socket sock = {0};
|
2022-02-02 04:49:42 +08:00
|
|
|
IP_Port localhost;
|
|
|
|
localhost.ip = get_loopback();
|
|
|
|
localhost.port = 0;
|
2018-06-20 15:33:19 +08:00
|
|
|
|
|
|
|
// Check all opened ports for connectivity.
|
|
|
|
for (uint8_t i = 0; i < NUM_PORTS; i++) {
|
|
|
|
sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
|
2022-02-02 04:49:42 +08:00
|
|
|
localhost.port = net_htons(ports[i]);
|
2022-02-03 04:02:23 +08:00
|
|
|
int ret = net_connect(logger, sock, &localhost);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(ret == 0, "Failed to connect to created TCP relay server on port %d.", ports[i]);
|
2018-06-20 15:33:19 +08:00
|
|
|
|
|
|
|
// Leave open one connection for the next test.
|
|
|
|
if (i + 1 < NUM_PORTS) {
|
|
|
|
kill_sock(sock);
|
|
|
|
}
|
|
|
|
}
|
2014-03-15 02:12:35 +08:00
|
|
|
|
2018-06-21 23:00:25 +08:00
|
|
|
// Key creation.
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
|
|
|
uint8_t f_nonce[CRYPTO_NONCE_SIZE];
|
|
|
|
crypto_new_keypair(f_public_key, f_secret_key);
|
2014-03-15 02:12:35 +08:00
|
|
|
random_nonce(f_nonce);
|
|
|
|
|
2018-06-21 23:00:25 +08:00
|
|
|
// Generation of the initial handshake.
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t t_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
2014-03-15 02:12:35 +08:00
|
|
|
uint8_t handshake_plain[TCP_HANDSHAKE_PLAIN_SIZE];
|
2016-12-19 10:47:42 +08:00
|
|
|
crypto_new_keypair(handshake_plain, t_secret_key);
|
|
|
|
memcpy(handshake_plain + CRYPTO_PUBLIC_KEY_SIZE, f_nonce, CRYPTO_NONCE_SIZE);
|
2014-03-15 02:12:35 +08:00
|
|
|
uint8_t handshake[TCP_CLIENT_HANDSHAKE_SIZE];
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(handshake, f_public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
|
|
|
random_nonce(handshake + CRYPTO_PUBLIC_KEY_SIZE);
|
2014-03-15 02:12:35 +08:00
|
|
|
|
2018-06-21 23:00:25 +08:00
|
|
|
// Encrypting handshake
|
2018-06-20 15:33:19 +08:00
|
|
|
int ret = encrypt_data(self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
|
|
|
|
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
|
2016-12-19 10:47:42 +08:00
|
|
|
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
|
2018-06-21 23:00:25 +08:00
|
|
|
"encrypt_data() call failed.");
|
|
|
|
|
|
|
|
// Sending the handshake
|
2022-02-02 04:49:42 +08:00
|
|
|
ck_assert_msg(net_send(logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1,
|
2022-02-03 04:02:23 +08:00
|
|
|
&localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
|
2018-06-21 23:00:25 +08:00
|
|
|
"An attempt to send the initial handshake minus last byte failed.");
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1,
|
2018-06-21 23:00:25 +08:00
|
|
|
"The attempt to send the last byte of handshake failed.");
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
|
|
|
// Receiving server response and decrypting it
|
2014-03-15 02:12:35 +08:00
|
|
|
uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
|
|
|
|
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
|
2018-06-21 23:00:25 +08:00
|
|
|
"Could/did not receive a server response to the initial handshake.");
|
2016-12-19 10:47:42 +08:00
|
|
|
ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE,
|
|
|
|
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt handshake response.");
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t f_nonce_r[CRYPTO_NONCE_SIZE];
|
|
|
|
uint8_t f_shared_key[CRYPTO_SHARED_KEY_SIZE];
|
2014-03-15 02:12:35 +08:00
|
|
|
encrypt_precompute(response_plain, t_secret_key, f_shared_key);
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(f_nonce_r, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE);
|
2014-03-15 02:12:35 +08:00
|
|
|
|
2018-06-21 23:00:25 +08:00
|
|
|
// Building a request
|
2020-05-04 09:12:57 +08:00
|
|
|
uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
r_req_p[0] = TCP_PACKET_ROUTING_REQUEST;
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
|
|
|
uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE];
|
|
|
|
uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE;
|
2017-01-07 16:28:53 +08:00
|
|
|
size = net_htons(size);
|
2016-12-19 10:47:42 +08:00
|
|
|
encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2);
|
2014-03-27 08:55:14 +08:00
|
|
|
increment_nonce(f_nonce);
|
|
|
|
memcpy(r_req, &size, 2);
|
|
|
|
|
2018-06-21 23:00:25 +08:00
|
|
|
// Sending the request at random intervals in random pieces.
|
|
|
|
for (uint32_t i = 0; i < sizeof(r_req);) {
|
|
|
|
uint8_t msg_length = rand() % 5 + 1; // msg_length = 1 to 5
|
|
|
|
|
|
|
|
if (i + msg_length >= sizeof(r_req)) {
|
|
|
|
msg_length = sizeof(r_req) - i;
|
|
|
|
}
|
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(net_send(logger, sock, r_req + i, msg_length, &localhost) == msg_length,
|
2018-06-21 23:00:25 +08:00
|
|
|
"Failed to send request after completing the handshake.");
|
|
|
|
i += msg_length;
|
|
|
|
|
2014-03-27 08:55:14 +08:00
|
|
|
c_sleep(50);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
|
|
|
do_TCP_server(tcp_s, mono_time);
|
2014-03-27 08:55:14 +08:00
|
|
|
}
|
2014-03-15 02:12:35 +08:00
|
|
|
|
2018-06-21 23:00:25 +08:00
|
|
|
// Receiving the second response and verifying its validity
|
2014-03-27 08:55:14 +08:00
|
|
|
uint8_t packet_resp[4096];
|
2022-02-03 04:02:23 +08:00
|
|
|
int recv_data_len = net_recv(logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, &localhost);
|
2016-12-19 10:47:42 +08:00
|
|
|
ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
|
2021-12-09 19:07:45 +08:00
|
|
|
"Failed to receive server response to request. %d", recv_data_len);
|
2014-03-27 08:55:14 +08:00
|
|
|
memcpy(&size, packet_resp, 2);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
|
|
|
|
"Wrong packet size for request response.");
|
|
|
|
|
2014-03-27 08:55:14 +08:00
|
|
|
uint8_t packet_resp_plain[4096];
|
2014-04-22 04:51:36 +08:00
|
|
|
ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response.");
|
2014-03-27 08:55:14 +08:00
|
|
|
increment_nonce(f_nonce_r);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2020-05-04 09:12:57 +08:00
|
|
|
ck_assert_msg(packet_resp_plain[0] == TCP_PACKET_ROUTING_RESPONSE, "Server sent the wrong packet id: %u",
|
|
|
|
packet_resp_plain[0]);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection.");
|
|
|
|
ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key.");
|
|
|
|
|
|
|
|
// Closing connections.
|
2018-01-14 19:48:35 +08:00
|
|
|
kill_sock(sock);
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_TCP_server(tcp_s);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
logger_kill(logger);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time);
|
2014-03-27 08:55:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sec_TCP_con {
|
2017-01-04 00:26:15 +08:00
|
|
|
Socket sock;
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
uint8_t recv_nonce[CRYPTO_NONCE_SIZE];
|
|
|
|
uint8_t sent_nonce[CRYPTO_NONCE_SIZE];
|
|
|
|
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
|
2014-03-27 08:55:14 +08:00
|
|
|
};
|
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
static struct sec_TCP_con *new_TCP_con(const Logger *logger, TCP_Server *tcp_s, Mono_Time *mono_time)
|
2014-03-27 08:55:14 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con));
|
2020-05-03 08:09:06 +08:00
|
|
|
ck_assert(sec_c != nullptr);
|
2018-02-27 09:07:46 +08:00
|
|
|
Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
|
2017-01-07 16:28:53 +08:00
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
IP_Port localhost;
|
|
|
|
localhost.ip = get_loopback();
|
|
|
|
localhost.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
int ret = net_connect(logger, sock, &localhost);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(ret == 0, "Failed to connect to the test TCP relay server.");
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
|
|
|
crypto_new_keypair(sec_c->public_key, f_secret_key);
|
2014-03-27 08:55:14 +08:00
|
|
|
random_nonce(sec_c->sent_nonce);
|
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t t_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
2014-03-27 08:55:14 +08:00
|
|
|
uint8_t handshake_plain[TCP_HANDSHAKE_PLAIN_SIZE];
|
2016-12-19 10:47:42 +08:00
|
|
|
crypto_new_keypair(handshake_plain, t_secret_key);
|
|
|
|
memcpy(handshake_plain + CRYPTO_PUBLIC_KEY_SIZE, sec_c->sent_nonce, CRYPTO_NONCE_SIZE);
|
2014-03-27 08:55:14 +08:00
|
|
|
uint8_t handshake[TCP_CLIENT_HANDSHAKE_SIZE];
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(handshake, sec_c->public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
|
|
|
random_nonce(handshake + CRYPTO_PUBLIC_KEY_SIZE);
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
|
|
|
|
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
|
|
|
|
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
|
2018-06-21 23:00:25 +08:00
|
|
|
"Failed to encrypt the outgoing handshake.");
|
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
ck_assert_msg(net_send(logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1,
|
2022-02-03 04:02:23 +08:00
|
|
|
&localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
|
2018-06-21 23:00:25 +08:00
|
|
|
"Failed to send the first portion of the handshake to the TCP relay server.");
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1,
|
2018-06-21 23:00:25 +08:00
|
|
|
"Failed to send last byte of handshake.");
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2014-03-27 08:55:14 +08:00
|
|
|
uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
|
|
|
|
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
|
2018-06-21 23:00:25 +08:00
|
|
|
"Failed to receive server handshake response.");
|
2016-12-19 10:47:42 +08:00
|
|
|
ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE,
|
|
|
|
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt server handshake response.");
|
2014-03-27 08:55:14 +08:00
|
|
|
encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key);
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(sec_c->recv_nonce, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE);
|
2014-03-27 08:55:14 +08:00
|
|
|
sec_c->sock = sock;
|
|
|
|
return sec_c;
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void kill_TCP_con(struct sec_TCP_con *con)
|
2015-02-08 11:57:04 +08:00
|
|
|
{
|
|
|
|
kill_sock(con->sock);
|
|
|
|
free(con);
|
|
|
|
}
|
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
static int write_packet_TCP_test_connection(const Logger *logger, struct sec_TCP_con *con, const uint8_t *data,
|
|
|
|
uint16_t length)
|
2014-03-27 08:55:14 +08:00
|
|
|
{
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2017-01-07 16:28:53 +08:00
|
|
|
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
|
2014-03-27 08:55:14 +08:00
|
|
|
memcpy(packet, &c_length, sizeof(uint16_t));
|
2014-04-22 04:51:36 +08:00
|
|
|
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2017-01-21 05:16:55 +08:00
|
|
|
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
|
2014-03-27 08:55:14 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-03-27 08:55:14 +08:00
|
|
|
|
|
|
|
increment_nonce(con->sent_nonce);
|
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
IP_Port localhost;
|
|
|
|
localhost.ip = get_loopback();
|
|
|
|
localhost.port = 0;
|
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(net_send(logger, con->sock, packet, SIZEOF_VLA(packet), &localhost) == SIZEOF_VLA(packet),
|
2022-02-02 04:49:42 +08:00
|
|
|
"Failed to send a packet.");
|
2014-03-27 08:55:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, uint8_t *data, uint16_t length)
|
2014-03-27 08:55:14 +08:00
|
|
|
{
|
2022-02-02 04:49:42 +08:00
|
|
|
IP_Port localhost;
|
|
|
|
localhost.ip = get_loopback();
|
|
|
|
localhost.port = 0;
|
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
int rlen = net_recv(logger, con->sock, data, length, &localhost);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen);
|
|
|
|
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data);
|
|
|
|
ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server.");
|
2014-03-27 08:55:14 +08:00
|
|
|
increment_nonce(con->recv_nonce);
|
2018-06-21 23:00:25 +08:00
|
|
|
return rlen;
|
2014-03-27 08:55:14 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 09:06:33 +08:00
|
|
|
static void test_some(void)
|
2014-03-27 08:55:14 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2020-05-04 07:40:59 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
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);
|
2020-05-04 07:40:59 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server");
|
2018-06-27 10:07:08 +08:00
|
|
|
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports.");
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
struct sec_TCP_con *con1 = new_TCP_con(logger, tcp_s, mono_time);
|
|
|
|
struct sec_TCP_con *con2 = new_TCP_con(logger, tcp_s, mono_time);
|
|
|
|
struct sec_TCP_con *con3 = new_TCP_con(logger, tcp_s, mono_time);
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE];
|
2020-05-04 09:12:57 +08:00
|
|
|
requ_p[0] = TCP_PACKET_ROUTING_REQUEST;
|
2018-06-21 23:00:25 +08:00
|
|
|
|
|
|
|
// Sending wrong public keys to test server response.
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
2022-02-02 04:49:42 +08:00
|
|
|
write_packet_TCP_test_connection(logger, con1, requ_p, sizeof(requ_p));
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
2022-02-02 04:49:42 +08:00
|
|
|
write_packet_TCP_test_connection(logger, con3, requ_p, sizeof(requ_p));
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
|
|
|
// Testing response from connection 1
|
2014-03-27 08:55:14 +08:00
|
|
|
uint8_t data[2048];
|
2022-02-02 04:49:42 +08:00
|
|
|
int len = read_packet_sec_TCP(logger, con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
|
2020-05-04 09:12:57 +08:00
|
|
|
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
|
|
|
|
ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong.");
|
|
|
|
|
|
|
|
// Connection 3
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
|
2020-05-04 09:12:57 +08:00
|
|
|
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
|
|
|
|
ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong.");
|
|
|
|
|
2018-06-27 10:07:08 +08:00
|
|
|
uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????
|
2014-03-27 08:55:14 +08:00
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
write_packet_TCP_test_connection(logger, con3, test_packet, sizeof(test_packet));
|
|
|
|
write_packet_TCP_test_connection(logger, con3, test_packet, sizeof(test_packet));
|
|
|
|
write_packet_TCP_test_connection(logger, con3, test_packet, sizeof(test_packet));
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-21 23:00:25 +08:00
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con1, data, 2 + 2 + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == 2, "wrong len %d", len);
|
2020-05-04 09:12:57 +08:00
|
|
|
ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]);
|
2014-03-27 08:55:14 +08:00
|
|
|
ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con3, data, 2 + 2 + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == 2, "wrong len %d", len);
|
2020-05-04 09:12:57 +08:00
|
|
|
ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
|
|
|
|
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
|
|
|
|
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
|
|
|
|
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
write_packet_TCP_test_connection(logger, con1, test_packet, sizeof(test_packet));
|
|
|
|
write_packet_TCP_test_connection(logger, con1, test_packet, sizeof(test_packet));
|
|
|
|
write_packet_TCP_test_connection(logger, con1, test_packet, sizeof(test_packet));
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
|
|
|
|
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
|
|
|
|
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
|
2014-03-29 10:37:42 +08:00
|
|
|
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
|
|
|
|
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
|
2014-04-01 08:37:03 +08:00
|
|
|
|
2020-05-04 09:12:57 +08:00
|
|
|
uint8_t ping_packet[1 + sizeof(uint64_t)] = {TCP_PACKET_PING, 8, 6, 9, 67};
|
2022-02-02 04:49:42 +08:00
|
|
|
write_packet_TCP_test_connection(logger, con1, ping_packet, sizeof(ping_packet));
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE);
|
2018-06-21 23:00:25 +08:00
|
|
|
ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len);
|
2020-05-04 09:12:57 +08:00
|
|
|
ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]);
|
2014-04-01 08:37:03 +08:00
|
|
|
ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data");
|
2018-06-21 23:00:25 +08:00
|
|
|
|
|
|
|
// Kill off the connections
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_TCP_server(tcp_s);
|
|
|
|
kill_TCP_con(con1);
|
|
|
|
kill_TCP_con(con2);
|
|
|
|
kill_TCP_con(con3);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
logger_kill(logger);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time);
|
2014-03-15 02:12:35 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 04:59:48 +08:00
|
|
|
static int response_callback_good;
|
|
|
|
static uint8_t response_callback_connection_id;
|
2016-12-19 10:47:42 +08:00
|
|
|
static uint8_t response_callback_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
2014-07-03 04:04:41 +08:00
|
|
|
static int response_callback(void *object, uint8_t connection_id, const uint8_t *public_key)
|
2014-04-15 04:59:48 +08:00
|
|
|
{
|
2021-12-07 22:51:31 +08:00
|
|
|
if (set_tcp_connection_number((TCP_Client_Connection *)(void *)((char *)object - 2), connection_id, 7) != 0) {
|
2014-05-14 18:30:05 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-14 18:30:05 +08:00
|
|
|
|
2014-04-15 04:59:48 +08:00
|
|
|
response_callback_connection_id = connection_id;
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(response_callback_public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
2014-04-15 04:59:48 +08:00
|
|
|
response_callback_good++;
|
2014-05-14 08:52:34 +08:00
|
|
|
return 0;
|
2014-04-15 04:59:48 +08:00
|
|
|
}
|
|
|
|
static int status_callback_good;
|
|
|
|
static uint8_t status_callback_connection_id;
|
|
|
|
static uint8_t status_callback_status;
|
2014-05-14 08:52:34 +08:00
|
|
|
static int status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status)
|
2014-04-15 04:59:48 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (object != (void *)2) {
|
2014-04-15 04:59:48 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-04-15 04:59:48 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (number != 7) {
|
2014-05-14 08:52:34 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-14 08:52:34 +08:00
|
|
|
|
2014-04-15 04:59:48 +08:00
|
|
|
status_callback_connection_id = connection_id;
|
|
|
|
status_callback_status = status;
|
|
|
|
status_callback_good++;
|
2014-05-14 08:52:34 +08:00
|
|
|
return 0;
|
2014-04-15 04:59:48 +08:00
|
|
|
}
|
|
|
|
static int data_callback_good;
|
2016-08-17 17:52:04 +08:00
|
|
|
static int data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length,
|
|
|
|
void *userdata)
|
2014-04-15 04:59:48 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (object != (void *)3) {
|
2014-04-15 04:59:48 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-04-15 04:59:48 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (number != 7) {
|
2014-05-14 08:52:34 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-14 08:52:34 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length != 5) {
|
2014-04-15 04:59:48 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-04-15 04:59:48 +08:00
|
|
|
|
|
|
|
if (data[0] == 1 && data[1] == 2 && data[2] == 3 && data[3] == 4 && data[4] == 5) {
|
|
|
|
data_callback_good++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-05-15 05:15:43 +08:00
|
|
|
static int oob_data_callback_good;
|
2016-12-19 10:47:42 +08:00
|
|
|
static uint8_t oob_pubkey[CRYPTO_PUBLIC_KEY_SIZE];
|
2016-08-17 17:52:04 +08:00
|
|
|
static int oob_data_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length,
|
|
|
|
void *userdata)
|
2014-05-15 05:15:43 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (object != (void *)4) {
|
2014-05-15 05:15:43 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-15 05:15:43 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length != 5) {
|
2014-05-15 05:15:43 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-15 05:15:43 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (public_key_cmp(public_key, oob_pubkey) != 0) {
|
2014-05-15 05:15:43 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-15 05:15:43 +08:00
|
|
|
|
|
|
|
if (data[0] == 1 && data[1] == 2 && data[2] == 3 && data[3] == 4 && data[4] == 5) {
|
|
|
|
oob_data_callback_good++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-16 09:06:33 +08:00
|
|
|
static void test_client(void)
|
2014-04-07 08:51:03 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2020-05-04 07:40:59 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
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);
|
2020-05-04 07:40:59 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
|
|
|
|
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind the relay server to all ports.");
|
2014-04-07 08:51:03 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
|
|
|
crypto_new_keypair(f_public_key, f_secret_key);
|
2014-04-07 08:51:03 +08:00
|
|
|
IP_Port ip_port_tcp_s;
|
|
|
|
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2017-11-15 16:13:31 +08:00
|
|
|
ip_port_tcp_s.ip = get_loopback();
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, &ip_port_tcp_s, self_public_key, f_public_key,
|
2022-02-02 04:49:42 +08:00
|
|
|
f_secret_key, nullptr);
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
2014-04-07 08:51:03 +08:00
|
|
|
c_sleep(50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// The connection status should be unconfirmed here because we have finished
|
|
|
|
// sending our data and are awaiting a response.
|
|
|
|
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong connection status. Expected: %d, is: %d.",
|
|
|
|
TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn));
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50); // Now let the server handle requests...
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
const uint8_t LOOP_SIZE = 3;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < LOOP_SIZE; i++) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr); // Run the connection loop.
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// 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",
|
|
|
|
TCP_CLIENT_CONFIRMED, tcp_con_status(conn));
|
|
|
|
|
|
|
|
c_sleep(i == LOOP_SIZE - 1 ? 0 : 500); // Sleep for 500ms on all except third loop.
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// 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,
|
2018-01-14 03:19:20 +08:00
|
|
|
tcp_con_status(conn));
|
2014-04-15 04:59:48 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
|
|
|
crypto_new_keypair(f2_public_key, f2_secret_key);
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2022-02-03 04:02:23 +08:00
|
|
|
TCP_Client_Connection *conn2 = new_TCP_connection(logger, mono_time, &ip_port_tcp_s, self_public_key, f2_public_key,
|
2018-08-02 07:02:13 +08:00
|
|
|
f2_secret_key, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// The client should call this function (defined earlier) during the routing process.
|
2016-09-05 21:43:58 +08:00
|
|
|
routing_response_handler(conn, response_callback, (char *)conn + 2);
|
2018-06-27 10:07:08 +08:00
|
|
|
// The client should call this function when it receives a connection notification.
|
2014-04-15 04:59:48 +08:00
|
|
|
routing_status_handler(conn, status_callback, (void *)2);
|
2018-06-27 10:07:08 +08:00
|
|
|
// The client should call this function when
|
2014-04-15 04:59:48 +08:00
|
|
|
routing_data_handler(conn, data_callback, (void *)3);
|
2018-06-27 10:07:08 +08:00
|
|
|
// The client should call this function when sending out of band packets.
|
2014-05-15 05:15:43 +08:00
|
|
|
oob_data_handler(conn, oob_data_callback, (void *)4);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// These integers will increment per successful callback.
|
2014-05-15 05:15:43 +08:00
|
|
|
oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0;
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
|
|
|
do_TCP_connection(logger, mono_time, conn2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
|
|
|
do_TCP_connection(logger, mono_time, conn2, nullptr);
|
2014-04-15 04:59:48 +08:00
|
|
|
c_sleep(50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2014-05-15 05:15:43 +08:00
|
|
|
uint8_t data[5] = {1, 2, 3, 4, 5};
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(oob_pubkey, f2_public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
2022-02-02 04:49:42 +08:00
|
|
|
send_oob_packet(logger, conn2, f_public_key, data, 5);
|
|
|
|
send_routing_request(logger, conn, f2_public_key);
|
|
|
|
send_routing_request(logger, conn2, f_public_key);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
|
|
|
do_TCP_connection(logger, mono_time, conn2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// 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");
|
|
|
|
ck_assert_msg(response_callback_good == 1, "Response callback not called.");
|
|
|
|
ck_assert_msg(public_key_cmp(response_callback_public_key, f2_public_key) == 0, "Wrong public key.");
|
|
|
|
ck_assert_msg(status_callback_good == 1, "Status callback not called.");
|
|
|
|
ck_assert_msg(status_callback_status == 2, "Wrong status callback status.");
|
|
|
|
ck_assert_msg(status_callback_connection_id == response_callback_connection_id,
|
|
|
|
"Status and response callback connection IDs are not equal.");
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2022-02-02 04:49:42 +08:00
|
|
|
ck_assert_msg(send_data(logger, conn2, 0, data, 5) == 1, "Failed a send_data() call.");
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
|
|
|
do_TCP_connection(logger, mono_time, conn2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
ck_assert_msg(data_callback_good == 1, "Data callback was not called.");
|
2014-05-16 08:57:55 +08:00
|
|
|
status_callback_good = 0;
|
2022-02-02 04:49:42 +08:00
|
|
|
send_disconnect_request(logger, conn2, 0);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
|
|
|
do_TCP_connection(logger, mono_time, conn2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
ck_assert_msg(status_callback_good == 1, "Status callback not called");
|
|
|
|
ck_assert_msg(status_callback_status == 1, "Wrong status callback status.");
|
|
|
|
|
|
|
|
// Kill off all connections and servers.
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_TCP_server(tcp_s);
|
|
|
|
kill_TCP_connection(conn);
|
|
|
|
kill_TCP_connection(conn2);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
logger_kill(logger);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time);
|
2014-04-07 08:51:03 +08:00
|
|
|
}
|
|
|
|
|
2018-06-27 10:07:08 +08:00
|
|
|
// Test how the client handles servers that don't respond.
|
2022-01-16 09:06:33 +08:00
|
|
|
static void test_client_invalid(void)
|
2014-04-07 08:51:03 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2020-05-04 07:40:59 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
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);
|
2014-04-07 08:51:03 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
|
|
|
crypto_new_keypair(f_public_key, f_secret_key);
|
2014-04-07 08:51:03 +08:00
|
|
|
IP_Port ip_port_tcp_s;
|
|
|
|
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2017-11-15 16:13:31 +08:00
|
|
|
ip_port_tcp_s.ip = get_loopback();
|
2022-02-03 04:02:23 +08:00
|
|
|
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, &ip_port_tcp_s, self_public_key, f_public_key,
|
2022-02-02 04:49:42 +08:00
|
|
|
f_secret_key, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
|
|
|
// Run the client's main loop but not the server.
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
c_sleep(50);
|
|
|
|
|
|
|
|
// After 50ms of no response...
|
|
|
|
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.",
|
2018-01-14 03:19:20 +08:00
|
|
|
TCP_CLIENT_CONNECTING, tcp_con_status(conn));
|
2018-06-27 10:07:08 +08:00
|
|
|
// After 5s...
|
2014-04-07 08:51:03 +08:00
|
|
|
c_sleep(5000);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.",
|
2018-01-14 03:19:20 +08:00
|
|
|
TCP_CLIENT_CONNECTING, tcp_con_status(conn));
|
2018-06-27 10:07:08 +08:00
|
|
|
// 11s... (Should wait for 10 before giving up.)
|
2014-04-07 08:51:03 +08:00
|
|
|
c_sleep(6000);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
2020-05-04 07:40:59 +08:00
|
|
|
do_TCP_connection(logger, mono_time, conn, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %d, is: %d.",
|
2018-01-14 03:19:20 +08:00
|
|
|
TCP_CLIENT_DISCONNECTED, tcp_con_status(conn));
|
2015-02-08 11:57:04 +08:00
|
|
|
|
|
|
|
kill_TCP_connection(conn);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
logger_kill(logger);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time);
|
2014-04-07 08:51:03 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 10:27:47 +08:00
|
|
|
#include "../toxcore/TCP_connection.h"
|
|
|
|
|
2016-09-22 20:13:29 +08:00
|
|
|
static bool tcp_data_callback_called;
|
2016-08-17 17:52:04 +08:00
|
|
|
static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t length, void *userdata)
|
2015-07-02 10:13:27 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (object != (void *)120397) {
|
2015-07-02 10:13:27 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-02 10:13:27 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (id != 123) {
|
2015-07-02 10:13:27 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-02 10:13:27 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length != 6) {
|
2015-07-02 10:13:27 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-02 10:13:27 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (memcmp(data, "Gentoo", length) != 0) {
|
2015-07-02 10:13:27 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-02 10:13:27 +08:00
|
|
|
|
|
|
|
tcp_data_callback_called = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-16 09:06:33 +08:00
|
|
|
static void test_tcp_connection(void)
|
2015-06-30 10:27:47 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2020-05-04 07:40:59 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2015-07-06 11:08:14 +08:00
|
|
|
tcp_data_callback_called = 0;
|
2016-12-19 10:47:42 +08:00
|
|
|
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);
|
2020-05-04 07:40:59 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
|
2016-09-22 01:57:17 +08:00
|
|
|
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
|
2015-06-30 10:27:47 +08:00
|
|
|
|
|
|
|
TCP_Proxy_Info proxy_info;
|
|
|
|
proxy_info.proxy_type = TCP_PROXY_NONE;
|
2016-12-19 10:47:42 +08:00
|
|
|
crypto_new_keypair(self_public_key, self_secret_key);
|
2022-02-02 04:49:42 +08:00
|
|
|
TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
|
2016-09-22 03:23:46 +08:00
|
|
|
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
|
2015-06-30 10:27:47 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
crypto_new_keypair(self_public_key, self_secret_key);
|
2022-02-02 04:49:42 +08:00
|
|
|
TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
|
2016-09-22 03:23:46 +08:00
|
|
|
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
|
2015-06-30 10:27:47 +08:00
|
|
|
|
2015-07-02 10:13:27 +08:00
|
|
|
IP_Port ip_port_tcp_s;
|
|
|
|
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2017-11-15 16:13:31 +08:00
|
|
|
ip_port_tcp_s.ip = get_loopback();
|
2015-07-02 10:13:27 +08:00
|
|
|
|
2016-09-22 03:23:46 +08:00
|
|
|
int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
|
2015-07-02 10:13:27 +08:00
|
|
|
ck_assert_msg(connection == 0, "Connection id wrong");
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
|
2015-07-02 10:13:27 +08:00
|
|
|
"Could not add tcp relay to connection\n");
|
|
|
|
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2016-09-22 03:23:46 +08:00
|
|
|
connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123);
|
2015-07-02 10:13:27 +08:00
|
|
|
ck_assert_msg(connection == 0, "Connection id wrong");
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(add_tcp_relay_connection(tc_2, connection, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
|
2015-07-02 10:13:27 +08:00
|
|
|
"Could not add tcp relay to connection\n");
|
|
|
|
|
2016-09-22 03:23:46 +08:00
|
|
|
ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1,
|
2018-09-19 11:15:50 +08:00
|
|
|
"Managed to read same connection\n");
|
2015-07-03 09:59:52 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2015-07-02 10:13:27 +08:00
|
|
|
|
2016-08-18 07:37:45 +08:00
|
|
|
int ret = send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6);
|
2015-07-02 10:13:27 +08:00
|
|
|
ck_assert_msg(ret == 0, "could not send packet.");
|
|
|
|
set_packet_tcp_connection_callback(tc_2, &tcp_data_callback, (void *) 120397);
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2015-07-02 10:13:27 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2015-07-02 10:13:27 +08:00
|
|
|
|
|
|
|
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
|
2015-07-03 09:59:52 +08:00
|
|
|
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");
|
2015-07-05 10:43:05 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2015-07-05 10:43:05 +08:00
|
|
|
|
2016-08-18 07:37:45 +08:00
|
|
|
ck_assert_msg(send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6) == -1, "could send packet.");
|
2015-07-03 09:59:52 +08:00
|
|
|
ck_assert_msg(kill_tcp_connection_to(tc_2, 0) == 0, "could not kill connection to\n");
|
2015-06-30 10:27:47 +08:00
|
|
|
|
|
|
|
kill_TCP_server(tcp_s);
|
|
|
|
kill_tcp_connections(tc_1);
|
|
|
|
kill_tcp_connections(tc_2);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
logger_kill(logger);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time);
|
2015-06-30 10:27:47 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 20:13:29 +08:00
|
|
|
static bool tcp_oobdata_callback_called;
|
2015-07-06 11:08:14 +08:00
|
|
|
static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigned int id, const uint8_t *data,
|
2016-08-23 05:44:58 +08:00
|
|
|
uint16_t length, void *userdata)
|
2015-07-06 11:08:14 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
TCP_Connections *tcp_c = (TCP_Connections *)object;
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length != 6) {
|
2015-07-06 11:08:14 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (memcmp(data, "Gentoo", length) != 0) {
|
2015-07-06 11:08:14 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
if (tcp_send_oob_packet(tcp_c, id, public_key, data, length) == 0) {
|
2015-07-07 10:20:18 +08:00
|
|
|
tcp_oobdata_callback_called = 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-07-07 10:20:18 +08:00
|
|
|
|
2015-07-06 11:08:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-16 09:06:33 +08:00
|
|
|
static void test_tcp_connection2(void)
|
2015-07-06 11:08:14 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2020-05-04 07:40:59 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2015-07-06 11:08:14 +08:00
|
|
|
tcp_oobdata_callback_called = 0;
|
2015-07-07 10:20:18 +08:00
|
|
|
tcp_data_callback_called = 0;
|
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
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);
|
2020-05-04 07:40:59 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
|
2016-09-22 01:57:17 +08:00
|
|
|
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
|
2015-07-06 11:08:14 +08:00
|
|
|
|
|
|
|
TCP_Proxy_Info proxy_info;
|
|
|
|
proxy_info.proxy_type = TCP_PROXY_NONE;
|
2016-12-19 10:47:42 +08:00
|
|
|
crypto_new_keypair(self_public_key, self_secret_key);
|
2022-02-02 04:49:42 +08:00
|
|
|
TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
|
2016-09-22 03:23:46 +08:00
|
|
|
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
crypto_new_keypair(self_public_key, self_secret_key);
|
2022-02-02 04:49:42 +08:00
|
|
|
TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
|
2016-09-22 03:23:46 +08:00
|
|
|
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
|
2015-07-06 11:08:14 +08:00
|
|
|
|
|
|
|
IP_Port ip_port_tcp_s;
|
|
|
|
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
2017-11-15 16:13:31 +08:00
|
|
|
ip_port_tcp_s.ip = get_loopback();
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2016-09-22 03:23:46 +08:00
|
|
|
int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
|
2015-07-06 11:08:14 +08:00
|
|
|
ck_assert_msg(connection == 0, "Connection id wrong");
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
|
2015-07-06 11:08:14 +08:00
|
|
|
"Could not add tcp relay to connection\n");
|
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
ck_assert_msg(add_tcp_relay_global(tc_2, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
|
2016-09-22 01:57:17 +08:00
|
|
|
"Could not add global relay");
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2018-06-27 10:07:08 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2016-08-18 07:37:45 +08:00
|
|
|
int ret = send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6);
|
2015-07-06 11:08:14 +08:00
|
|
|
ck_assert_msg(ret == 0, "could not send packet.");
|
2015-07-07 10:20:18 +08:00
|
|
|
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);
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2015-07-06 11:08:14 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2015-07-06 11:08:14 +08:00
|
|
|
|
|
|
|
ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet.");
|
2015-07-07 10:20:18 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server_delay(tcp_s, mono_time, 50);
|
2015-07-07 10:20:18 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
do_tcp_connections(logger, tc_1, nullptr);
|
|
|
|
do_tcp_connections(logger, tc_2, nullptr);
|
2015-07-07 10:20:18 +08:00
|
|
|
|
|
|
|
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
|
2015-07-06 11:08:14 +08:00
|
|
|
ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
|
|
|
|
|
|
|
|
kill_TCP_server(tcp_s);
|
|
|
|
kill_tcp_connections(tc_1);
|
|
|
|
kill_tcp_connections(tc_2);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2020-05-04 07:40:59 +08:00
|
|
|
logger_kill(logger);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time);
|
2015-07-06 11:08:14 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 09:06:33 +08:00
|
|
|
static void TCP_suite(void)
|
2014-03-15 02:12:35 +08:00
|
|
|
{
|
2022-01-16 09:06:33 +08:00
|
|
|
test_basic();
|
|
|
|
test_some();
|
|
|
|
test_client();
|
|
|
|
test_client_invalid();
|
|
|
|
test_tcp_connection();
|
|
|
|
test_tcp_connection2();
|
2014-03-15 02:12:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2014-03-15 02:12:35 +08:00
|
|
|
{
|
2018-02-19 01:50:50 +08:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2022-01-16 09:06:33 +08:00
|
|
|
TCP_suite();
|
|
|
|
return 0;
|
2014-03-15 02:12:35 +08:00
|
|
|
}
|