Squashed 'external/toxcore/c-toxcore/' changes from 59d3f6674..76bc4c496

76bc4c496 refactor: TCP connection netprof objects are now owned by Messenger

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 76bc4c496d407cbbd11f0a0d9d3eebfbf64ccd2c
This commit is contained in:
Green Sky
2025-03-19 11:19:59 +01:00
parent 72325822b8
commit 119bd4fb1d
15 changed files with 96 additions and 65 deletions

View File

@ -8,6 +8,7 @@
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/net_profile.h"
#include "../toxcore/network.h"
#include "auto_test_support.h"
@ -737,6 +738,9 @@ static void test_tcp_connection(void)
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new(mem);
Net_Profile *tcp_np = netprof_new(logger, mem);
ck_assert(tcp_np != nullptr);
tcp_data_callback_called = 0;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@ -747,12 +751,12 @@ static void test_tcp_connection(void)
TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
@ -815,6 +819,8 @@ static void test_tcp_connection(void)
kill_tcp_connections(tc_1);
kill_tcp_connections(tc_2);
netprof_kill(mem, tcp_np);
logger_kill(logger);
mono_time_free(mem, mono_time);
}
@ -852,6 +858,9 @@ static void test_tcp_connection2(void)
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new(mem);
Net_Profile *tcp_np = netprof_new(logger, mem);
ck_assert(tcp_np != nullptr);
tcp_oobdata_callback_called = 0;
tcp_data_callback_called = 0;
@ -864,12 +873,12 @@ static void test_tcp_connection2(void)
TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
@ -921,6 +930,8 @@ static void test_tcp_connection2(void)
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
netprof_kill(mem, tcp_np);
kill_tcp_server(tcp_s);
kill_tcp_connections(tc_1);
kill_tcp_connections(tc_2);

View File

@ -96,6 +96,7 @@ typedef struct Forwarding_Subtox {
Logger *log;
Mono_Time *mono_time;
Networking_Core *net;
Net_Profile *tcp_np;
DHT *dht;
Net_Crypto *c;
Forwarding *forwarding;
@ -126,8 +127,11 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
subtox->tcp_np = netprof_new(subtox->log, mem);
ck_assert(subtox->tcp_np != nullptr);
const TCP_Proxy_Info inf = {{{{0}}}};
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf);
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf, subtox->tcp_np);
subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht);
ck_assert(subtox->forwarding != nullptr);
@ -143,6 +147,7 @@ static void kill_forwarding_subtox(const Memory *mem, Forwarding_Subtox *subtox)
kill_announcements(subtox->announce);
kill_forwarding(subtox->forwarding);
kill_net_crypto(subtox->c);
netprof_kill(mem, subtox->tcp_np);
kill_dht(subtox->dht);
kill_networking(subtox->net);
mono_time_free(mem, subtox->mono_time);

View File

@ -396,6 +396,7 @@ static void test_basic(void)
typedef struct {
Logger *log;
Mono_Time *mono_time;
Net_Profile *tcp_np;
Onion *onion;
Onion_Announce *onion_a;
Onion_Client *onion_c;
@ -471,10 +472,24 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr;
}
on->tcp_np = netprof_new(on->log, mem);
if (!on->tcp_np) {
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_dht(dht);
kill_networking(net);
mono_time_free(mem, on->mono_time);
logger_kill(on->log);
free(on);
return nullptr;
}
TCP_Proxy_Info inf = {{{{0}}}};
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf));
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf, on->tcp_np));
if (!on->onion_c) {
netprof_kill(mem, on->tcp_np);
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_dht(dht);
@ -506,6 +521,7 @@ static void kill_onions(const Memory *mem, Onions *on)
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_net_crypto(c);
netprof_kill(mem, on->tcp_np);
kill_dht(dht);
kill_networking(net);
mono_time_free(mem, on->mono_time);