Skip to content

Commit

Permalink
test: Add more logging to TCP connection constructor.
Browse files Browse the repository at this point in the history
Also add more asserts to the test so we don't do UB.
  • Loading branch information
iphydf committed Jan 10, 2025
1 parent 0f12f38 commit 380dde9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
9 changes: 3 additions & 6 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ bazel-opt_task:
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
- cat /src/workspace/.bazelrc.local
test_all_script:
- cd /src/workspace && bazel
test -k
Expand All @@ -27,11 +26,9 @@ bazel-dbg_task:
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
- cat /src/workspace/.bazelrc.local
test_all_script:
- cd /src/workspace && bazel
test -k
--remote_cache=http://$CIRRUS_HTTP_CACHE_HOST
--build_tag_filters=-haskell
--test_tag_filters=-haskell
--
Expand All @@ -47,11 +44,9 @@ bazel-msan_task:
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
- cat /src/workspace/.bazelrc.local
test_all_script:
- cd /src/workspace && bazel
test -k
--remote_cache=http://$CIRRUS_HTTP_CACHE_HOST
--
//c-toxcore/auto_tests:lossless_packet_test

Expand Down Expand Up @@ -85,6 +80,7 @@ freebsd_task:
libconfig
libsodium
libvpx
ninja
opus
pkgconf
- git submodule update --init --recursive
Expand All @@ -98,6 +94,7 @@ freebsd_task:
-DNON_HERMETIC_TESTS=OFF \
-DTEST_TIMEOUT_SECONDS=50 \
-DUSE_IPV6=OFF \
-DAUTOTEST=ON
-DAUTOTEST=ON \
-GNinja
cmake --build . --target install
ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6
1 change: 1 addition & 0 deletions .github/scripts/cmake-freebsd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ RUN "cmake -B_build -Hc-toxcore \
-DCMAKE_EXE_LINKER_FLAGS='$LD_FLAGS' \
-DCMAKE_SHARED_LINKER_FLAGS='$LD_FLAGS' \
-DCMAKE_INSTALL_PREFIX:PATH='_install' \
-DENABLE_SHARED=OFF \
-DMIN_LOGGER_LEVEL=TRACE \
-DMUST_BUILD_TOXAV=ON \
-DNON_HERMETIC_TESTS=ON \
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ jobs:
libopus
libsodium
libvpx
ninja
pkg-config

run: |
Expand All @@ -138,7 +139,8 @@ jobs:
-DNON_HERMETIC_TESTS=ON \
-DTEST_TIMEOUT_SECONDS=90 \
-DUSE_IPV6=OFF \
-DAUTOTEST=ON
-DAUTOTEST=ON \
-GNinja
cmake --build . --target install
ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6
Expand All @@ -163,6 +165,7 @@ jobs:
libconfig
libsodium
libvpx
ninja
opus
pkgconf

Expand All @@ -175,7 +178,8 @@ jobs:
-DNON_HERMETIC_TESTS=ON \
-DTEST_TIMEOUT_SECONDS=50 \
-DUSE_IPV6=OFF \
-DAUTOTEST=ON
-DAUTOTEST=ON \
-GNinja
cmake --build . --target install
ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6
Expand Down
8 changes: 7 additions & 1 deletion auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"

#define NUM_PORTS 3
Expand Down Expand Up @@ -534,6 +533,7 @@ static void test_client(void)
ip_port_tcp_s.ip = get_loopback();

TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr);
ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection.");
// TCP sockets might need a moment before they can be written to.
c_sleep(50);
do_tcp_connection(logger, mono_time, conn, nullptr);
Expand Down Expand Up @@ -570,6 +570,7 @@ static void test_client(void)
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
TCP_Client_Connection *conn2 = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f2_public_key,
f2_secret_key, nullptr);
ck_assert_msg(conn2 != nullptr, "Failed to create a second TCP client connection.");
c_sleep(50);

// The client should call this function (defined earlier) during the routing process.
Expand Down Expand Up @@ -667,6 +668,7 @@ static void test_client_invalid(void)
ip_port_tcp_s.ip = get_loopback();
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s,
self_public_key, f_public_key, f_secret_key, nullptr);
ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection.");

// Run the client's main loop but not the server.
mono_time_update(mono_time);
Expand Down Expand Up @@ -743,10 +745,12 @@ static void test_tcp_connection(void)
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);
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);
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");

IP_Port ip_port_tcp_s;
Expand Down Expand Up @@ -858,10 +862,12 @@ static void test_tcp_connection2(void)
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);
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);
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");

IP_Port ip_port_tcp_s;
Expand Down
7 changes: 7 additions & 0 deletions auto_tests/tcp_relay_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@

#include "auto_test_support.h"

#ifndef USE_IPV6
#define USE_IPV6 1
#endif

int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

struct Tox_Options *opts = tox_options_new(nullptr);
tox_options_set_udp_enabled(opts, false);
#if !USE_IPV6
tox_options_set_ipv6_enabled(opts, false);
#endif
Tox *tox_tcp = tox_new_log(opts, nullptr, nullptr);
tox_options_free(opts);

Expand Down
16 changes: 15 additions & 1 deletion toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ TCP_Client_Connection *new_tcp_connection(
assert(ns != nullptr);

if (!net_family_is_ipv4(ip_port->ip.family) && !net_family_is_ipv6(ip_port->ip.family)) {
LOGGER_ERROR(logger, "Invalid IP family: %d", ip_port->ip.family.value);
return nullptr;
}

Expand All @@ -609,22 +610,34 @@ TCP_Client_Connection *new_tcp_connection(
const Socket sock = net_socket(ns, family, TOX_SOCK_STREAM, TOX_PROTO_TCP);

if (!sock_valid(sock)) {
LOGGER_ERROR(logger, "Failed to create TCP socket with family %d", family.value);
return nullptr;
}

if (!set_socket_nosigpipe(ns, sock)) {
LOGGER_ERROR(logger, "Failed to set TCP socket to ignore SIGPIPE");
kill_sock(ns, sock);
return nullptr;
}

if (!(set_socket_nonblock(ns, sock) && connect_sock_to(ns, logger, mem, sock, ip_port, proxy_info))) {
if (!set_socket_nonblock(ns, sock)) {
LOGGER_ERROR(logger, "Failed to set TCP socket to non-blocking");
kill_sock(ns, sock);
return nullptr;
}

if (!connect_sock_to(ns, logger, mem, sock, ip_port, proxy_info)) {
Ip_Ntoa ip_ntoa;
LOGGER_WARNING(logger, "Failed to connect TCP socket to %s:%u",
net_ip_ntoa(&ip_port->ip, &ip_ntoa), net_ntohs(ip_port->port));
kill_sock(ns, sock);
return nullptr;
}

TCP_Client_Connection *temp = (TCP_Client_Connection *)mem_alloc(mem, sizeof(TCP_Client_Connection));

if (temp == nullptr) {
LOGGER_ERROR(logger, "Failed to allocate memory for TCP_Client_Connection");
kill_sock(ns, sock);
return nullptr;
}
Expand Down Expand Up @@ -657,6 +670,7 @@ TCP_Client_Connection *new_tcp_connection(
temp->status = TCP_CLIENT_CONNECTING;

if (generate_handshake(temp) == -1) {
LOGGER_ERROR(logger, "Failed to generate handshake");
kill_sock(ns, sock);
mem_delete(mem, temp);
return nullptr;
Expand Down

0 comments on commit 380dde9

Please sign in to comment.