Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CheckOptions:
- key: concurrency-mt-unsafe.FunctionSet
value: posix
- key: readability-function-cognitive-complexity.Threshold
value: 159 # TODO(iphydf): Decrease. tox_new_system is the highest at the moment.
value: 167 # TODO(iphydf): Decrease. tox_new_system is the highest at the moment.
- key: cppcoreguidelines-avoid-do-while.IgnoreMacros
value: true
- key: readability-simplify-boolean-expr.SimplifyDeMorgan
Expand Down
5 changes: 5 additions & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ cc_library(
"//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore:DHT",
"//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:ev",
"//c-toxcore/toxcore:mono_time",
"//c-toxcore/toxcore:net_crypto",
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:os_event",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
Expand Down Expand Up @@ -66,6 +69,7 @@ extra_data = {
"//c-toxcore/toxcore:announce",
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:crypto_core",
"//c-toxcore/toxcore:ev",
"//c-toxcore/toxcore:forwarding",
"//c-toxcore/toxcore:friend_connection",
"//c-toxcore/toxcore:logger",
Expand All @@ -76,6 +80,7 @@ extra_data = {
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:onion_client",
"//c-toxcore/toxcore:os_event",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
Expand Down
271 changes: 138 additions & 133 deletions auto_tests/TCP_test.c

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../toxcore/forwarding.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "../toxcore/os_event.h"
#include "auto_test_support.h"
#include "check_compat.h"

Expand Down Expand Up @@ -60,7 +61,9 @@ static void test_store_data(void)
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
ck_assert(mono_time != nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns);
Ev *ev = os_event_new(mem, log);
ck_assert(ev != nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns, ev);
ck_assert(net != nullptr);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
ck_assert(dht != nullptr);
Expand Down Expand Up @@ -108,6 +111,7 @@ static void test_store_data(void)
kill_forwarding(forwarding);
kill_dht(dht);
kill_networking(net);
ev_kill(ev);
mono_time_free(mem, mono_time);
logger_kill(log);
}
Expand Down
10 changes: 7 additions & 3 deletions auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../toxcore/net_crypto.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "../toxcore/os_event.h"
#include "auto_test_support.h"
#include "check_compat.h"

Expand Down Expand Up @@ -96,6 +97,7 @@ static bool all_returned(Test_Data *test_data)
typedef struct Forwarding_Subtox {
Logger *log;
Mono_Time *mono_time;
Ev *ev;
Networking_Core *net;
Net_Profile *tcp_np;
DHT *dht;
Expand All @@ -118,12 +120,13 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
ck_assert(subtox->log != nullptr);
logger_callback_log(subtox->log, print_debug_logger, nullptr, index);
subtox->mono_time = mono_time_new(mem, nullptr, nullptr);
subtox->ev = os_event_new(mem, subtox->log);

if (no_udp) {
subtox->net = new_networking_no_udp(subtox->log, mem, ns);
subtox->net = new_networking_no_udp(subtox->log, mem, ns, subtox->ev);
} else {
const IP ip = get_loopback();
subtox->net = new_networking_ex(subtox->log, mem, ns, &ip, port, port, nullptr);
subtox->net = new_networking_ex(subtox->log, mem, ns, subtox->ev, &ip, port, port, nullptr);
}

subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
Expand All @@ -132,7 +135,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
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->net, subtox->dht, &auto_test_dht_funcs, &inf, subtox->tcp_np);
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->ev, subtox->net, subtox->dht, &auto_test_dht_funcs, &inf, subtox->tcp_np);

subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht, subtox->net);
ck_assert(subtox->forwarding != nullptr);
Expand All @@ -151,6 +154,7 @@ static void kill_forwarding_subtox(const Memory *mem, Forwarding_Subtox *subtox)
netprof_kill(mem, subtox->tcp_np);
kill_dht(subtox->dht);
kill_networking(subtox->net);
ev_kill(subtox->ev);
mono_time_free(mem, subtox->mono_time);
logger_kill(subtox->log);
free(subtox);
Expand Down
31 changes: 22 additions & 9 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../toxcore/onion_client.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "../toxcore/os_event.h"
#include "auto_test_support.h"
#include "check_compat.h"

Expand Down Expand Up @@ -215,9 +216,9 @@ static void send_onion_packet(const Networking_Core *net, const Memory *mem, con
/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
static Networking_Core *new_networking(const Logger *log, const Memory *mem, const Network *ns, const IP *ip, uint16_t port)
static Networking_Core *new_networking(const Logger *log, const Memory *mem, const Network *ns, Ev *ev, const IP *ip, uint16_t port)
{
return new_networking_ex(log, mem, ns, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
return new_networking_ex(log, mem, ns, ev, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
}

static void test_basic(void)
Expand All @@ -238,10 +239,13 @@ static void test_basic(void)
Mono_Time *mono_time1 = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time2 = mono_time_new(mem, nullptr, nullptr);

Ev *ev1 = os_event_new(mem, log1);
Ev *ev2 = os_event_new(mem, log2);

IP ip = get_loopback();
Networking_Core *net1 = new_networking(log1, mem, ns, &ip, 36567);
Networking_Core *net1 = new_networking(log1, mem, ns, ev1, &ip, 36567);
Onion *onion1 = new_onion(log1, mem, mono_time1, rng, new_dht(log1, mem, rng, ns, mono_time1, net1, true, false), net1);
Networking_Core *net2 = new_networking(log2, mem, ns, &ip, 36568);
Networking_Core *net2 = new_networking(log2, mem, ns, ev2, &ip, 36568);
Onion *onion2 = new_onion(log2, mem, mono_time2, rng, new_dht(log2, mem, rng, ns, mono_time2, net2, true, false), net2);
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);
Expand Down Expand Up @@ -337,8 +341,9 @@ static void test_basic(void)
logger_callback_log(log3, print_debug_logger, nullptr, &index[2]);

Mono_Time *mono_time3 = mono_time_new(mem, nullptr, nullptr);
Ev *ev3 = os_event_new(mem, log3);

Networking_Core *net3 = new_networking(log3, mem, ns, &ip, 36569);
Networking_Core *net3 = new_networking(log3, mem, ns, ev3, &ip, 36569);
Onion *onion3 = new_onion(log3, mem, mono_time3, rng, new_dht(log3, mem, rng, ns, mono_time3, net3, true, false), net3);
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");

Expand Down Expand Up @@ -369,6 +374,7 @@ static void test_basic(void)
kill_onion(onion);
kill_dht(dht);
kill_networking(net);
ev_kill(ev3);
mono_time_free(mem, mono_time3);
logger_kill(log3);
}
Expand All @@ -381,6 +387,7 @@ static void test_basic(void)
kill_onion(onion);
kill_dht(dht);
kill_networking(net);
ev_kill(ev2);
mono_time_free(mem, mono_time2);
logger_kill(log2);
}
Expand All @@ -393,6 +400,7 @@ static void test_basic(void)
kill_onion(onion);
kill_dht(dht);
kill_networking(net);
ev_kill(ev1);
mono_time_free(mem, mono_time1);
logger_kill(log1);
}
Expand All @@ -401,6 +409,7 @@ static void test_basic(void)
typedef struct {
Logger *log;
Mono_Time *mono_time;
Ev *ev;
Net_Crypto *nc;
Net_Profile *tcp_np;
Onion *onion;
Expand Down Expand Up @@ -429,14 +438,17 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
logger_callback_log(on->log, print_debug_logger, nullptr, index);

on->mono_time = mono_time_new(mem, nullptr, nullptr);
on->ev = os_event_new(mem, on->log);

if (!on->mono_time) {
if (!on->mono_time || !on->ev) {
ev_kill(on->ev);
mono_time_free(mem, on->mono_time);
logger_kill(on->log);
free(on);
return nullptr;
}

Networking_Core *net = new_networking(on->log, mem, ns, &ip, port);
Networking_Core *net = new_networking(on->log, mem, ns, on->ev, &ip, port);

if (!net) {
mono_time_free(mem, on->mono_time);
Expand Down Expand Up @@ -491,8 +503,8 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr;
}

TCP_Proxy_Info inf = {{{{0}}}};
on->nc = new_net_crypto(on->log, mem, rng, ns, on->mono_time, net, dht, &auto_test_dht_funcs, &inf, on->tcp_np);
const TCP_Proxy_Info inf = {{{{0}}}};
on->nc = new_net_crypto(on->log, mem, rng, ns, on->mono_time, on->ev, net, dht, &auto_test_dht_funcs, &inf, on->tcp_np);
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, on->nc, dht, net);

if (!on->onion_c) {
Expand Down Expand Up @@ -531,6 +543,7 @@ static void kill_onions(const Memory *mem, Onions *on)
netprof_kill(mem, on->tcp_np);
kill_dht(dht);
kill_networking(net);
ev_kill(on->ev);
mono_time_free(mem, on->mono_time);
logger_kill(on->log);
free(on);
Expand Down
1 change: 1 addition & 0 deletions other/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cc_binary(
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:os_event",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
Expand Down
6 changes: 4 additions & 2 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "../toxcore/network.h"
#include "../toxcore/onion.h"
#include "../toxcore/onion_announce.h"
#include "../toxcore/os_event.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h"
Expand Down Expand Up @@ -157,9 +158,10 @@ int main(int argc, char *argv[])
}

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Ev *ev = os_event_new(mem, logger);
const uint16_t start_port = PORT;
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
Networking_Core *net = new_networking_ex(logger, mem, ns, ev, &ip, start_port, end_port, nullptr);
DHT *dht = new_dht(logger, mem, rng, ns, mono_time, net, true, true);
Onion *onion = new_onion(logger, mem, mono_time, rng, dht, net);
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht, net);
Expand Down Expand Up @@ -189,7 +191,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED
#define NUM_PORTS 3
const uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion, forwarding);
TCP_Server *tcp_s = new_tcp_server(logger, mem, rng, ns, ev, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion, forwarding);

if (tcp_s == nullptr) {
printf("TCP server failed to initialize.\n");
Expand Down
1 change: 1 addition & 0 deletions other/bootstrap_daemon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cc_binary(
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:os_event",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
Expand Down
9 changes: 6 additions & 3 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "../../../toxcore/network.h"
#include "../../../toxcore/onion.h"
#include "../../../toxcore/onion_announce.h"
#include "../../../toxcore/os_event.h"
#include "../../../toxcore/os_memory.h"
#include "../../../toxcore/os_random.h"

Expand Down Expand Up @@ -298,15 +299,16 @@ int main(int argc, char *argv[])

logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr);

Ev *ev = os_event_new(mem, logger);
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
Networking_Core *net = new_networking_ex(logger, mem, ns, ev, &ip, start_port, end_port, nullptr);

if (net == nullptr) {
if (enable_ipv6 && enable_ipv4_fallback) {
LOG_WRITE(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = false;
ip_init(&ip, enable_ipv6);
net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
net = new_networking_ex(logger, mem, ns, ev, &ip, start_port, end_port, nullptr);

if (net == nullptr) {
LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
Expand Down Expand Up @@ -494,7 +496,7 @@ int main(int argc, char *argv[])
return 1;
}

tcp_server = new_tcp_server(logger, mem, rng, ns, enable_ipv6,
tcp_server = new_tcp_server(logger, mem, rng, ns, ev, enable_ipv6,
tcp_relay_port_count, tcp_relay_ports,
dht_get_self_secret_key(dht), onion, forwarding);

Expand Down Expand Up @@ -639,6 +641,7 @@ int main(int argc, char *argv[])
kill_dht(dht);
mono_time_free(mem, mono_time);
kill_networking(net);
ev_kill(ev);
logger_kill(logger);

return 0;
Expand Down
14 changes: 14 additions & 0 deletions testing/support/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
cc_library(
name = "support",
srcs = [
"doubles/fake_event.cc",
"doubles/fake_network_stack.cc",
"doubles/fake_sockets.cc",
"doubles/network_universe.cc",
Expand All @@ -22,6 +23,7 @@ cc_library(
],
hdrs = [
"doubles/fake_clock.hh",
"doubles/fake_event.hh",
"doubles/fake_memory.hh",
"doubles/fake_network_stack.hh",
"doubles/fake_random.hh",
Expand All @@ -47,9 +49,11 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//c-toxcore/toxcore:attributes",
"//c-toxcore/toxcore:ev",
"//c-toxcore/toxcore:mem",
"//c-toxcore/toxcore:net",
"//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:os_event",
"//c-toxcore/toxcore:rng",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_events",
Expand All @@ -58,6 +62,16 @@ cc_library(
],
)

cc_test(
name = "fake_event_test",
srcs = ["doubles/fake_event_test.cc"],
deps = [
":support",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "fake_sockets_test",
srcs = ["doubles/fake_sockets_test.cc"],
Expand Down
3 changes: 3 additions & 0 deletions testing/support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(NOT UNITTEST)
endif()

set(support_SOURCES
doubles/fake_event.cc
doubles/fake_network_stack.cc
doubles/fake_sockets.cc
doubles/network_universe.cc
Expand All @@ -19,6 +20,7 @@ set(support_SOURCES
src/simulation.cc
src/tox_network.cc
src/tox_runner.cc
doubles/fake_event.hh
doubles/fake_clock.hh
doubles/fake_memory.hh
doubles/fake_network_stack.hh
Expand Down Expand Up @@ -68,6 +70,7 @@ if(TARGET GTest::gtest_main)
endfunction()

support_test(fake_sockets_test doubles/fake_sockets_test.cc)
support_test(fake_event_test doubles/fake_event_test.cc)
support_test(fake_network_stack_test doubles/fake_network_stack_test.cc)
support_test(fake_network_udp_test doubles/fake_network_udp_test.cc)
support_test(fake_network_tcp_test doubles/fake_network_tcp_test.cc)
Expand Down
Loading
Loading