Skip to content

Commit

Permalink
refactor: Move some OS-specifics into tox_system.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Aug 29, 2023
1 parent ea5e1e3 commit ecc6f37
Show file tree
Hide file tree
Showing 137 changed files with 1,821 additions and 798 deletions.
3 changes: 3 additions & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cc_library(
"//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:mono_time",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_time",
],
)

Expand Down Expand Up @@ -61,9 +62,11 @@ flaky_tests = {
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:onion_client",
"//c-toxcore/toxcore:os",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
"//c-toxcore/toxcore:tox_time",
"//c-toxcore/toxcore:util",
"//c-toxcore/toxencryptsave",
"@libsodium",
Expand Down
51 changes: 27 additions & 24 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/os_random.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"

Expand Down Expand Up @@ -45,14 +48,14 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};

static void test_basic(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new();
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);

Expand Down Expand Up @@ -303,14 +306,14 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui

static void test_some(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new();

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -498,15 +501,15 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint

static void test_client(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand Down Expand Up @@ -632,14 +635,14 @@ static void test_client(void)
// Test how the client handles servers that don't respond.
static void test_client_invalid(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new();

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -711,14 +714,14 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t

static void test_tcp_connection(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new();

tcp_data_callback_called = 0;
Expand Down Expand Up @@ -824,14 +827,14 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne

static void test_tcp_connection2(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new();

tcp_oobdata_callback_called = 0;
Expand Down
13 changes: 8 additions & 5 deletions auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
#include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_random.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"

static void test_bucketnum(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
uint8_t key1[CRYPTO_PUBLIC_KEY_SIZE], key2[CRYPTO_PUBLIC_KEY_SIZE];
random_bytes(rng, key1, sizeof(key1));
Expand Down Expand Up @@ -50,17 +53,17 @@ static void test_announce_data(void *object, const uint8_t *data, uint16_t lengt

static void test_store_data(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Logger *log = logger_new();
ck_assert(log != nullptr);
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
Expand Down
16 changes: 14 additions & 2 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../toxcore/Messenger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/tox_struct.h"
#include "../toxcore/tox_time_impl.h"

#include "auto_test_support.h"

Expand Down Expand Up @@ -159,15 +160,23 @@ static uint64_t get_state_clock_callback(void *user_data)
return *clock;
}

static const Tox_Time_Funcs autotox_time_funcs = {
get_state_clock_callback,
};

void set_mono_time_callback(AutoTox *autotox)
{
ck_assert(autotox != nullptr);

if (autotox->tm == nullptr) {
autotox->tm = tox_time_new(&autotox_time_funcs, &autotox->clock, autotox->tox->sys.mem);
}

Mono_Time *mono_time = autotox->tox->mono_time;

autotox->clock = current_time_monotonic(mono_time);
mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock);
mono_time_set_current_time_callback(mono_time, nullptr); // set to default first
mono_time_set_current_time_callback(mono_time, autotox->tm);
}

void save_autotox(AutoTox *autotox)
Expand All @@ -193,6 +202,8 @@ void kill_autotox(AutoTox *autotox)
fprintf(stderr, "Killing #%u\n", autotox->index);
autotox->alive = false;
tox_kill(autotox->tox);
tox_time_free(autotox->tm);
autotox->tm = nullptr;
}

void reload(AutoTox *autotox)
Expand Down Expand Up @@ -380,6 +391,7 @@ void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(Au

for (uint32_t i = 0; i < tox_count; ++i) {
tox_kill(autotoxes[i].tox);
tox_time_free(autotoxes[i].tm);
free(autotoxes[i].state);
free(autotoxes[i].save_state);
}
Expand Down
1 change: 1 addition & 0 deletions auto_tests/auto_test_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

typedef struct AutoTox {
Tox *tox;
Tox_Time *tm;

uint32_t index;
uint64_t clock;
Expand Down
3 changes: 2 additions & 1 deletion auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdint.h>

#include "../toxav/toxav.h"
#include "../toxcore/os_random.h"
#include "check_compat.h"

#define NUM_AV_GROUP_TOX 16
Expand Down Expand Up @@ -287,7 +288,7 @@ static void do_audio(AutoTox *autotoxes, uint32_t iterations)

static void run_conference_tests(AutoTox *autotoxes)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
bool disabled[NUM_AV_GROUP_TOX] = {0};

Expand Down
3 changes: 2 additions & 1 deletion auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <time.h>
#include <stdint.h>

#include "../toxcore/os_random.h"
#include "../toxcore/util.h"

#include "check_compat.h"
Expand Down Expand Up @@ -192,7 +193,7 @@ static uint32_t random_false_index(const Random *rng, bool *list, const uint32_t

static void run_conference_tests(AutoTox *autotoxes)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
/* disabling name change propagation check for now, as it occasionally
* fails due to disconnections too short to trigger freezing */
Expand Down
9 changes: 5 additions & 4 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "../testing/misc_tools.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
#include "../toxcore/net_crypto.h"
#include "check_compat.h"

Expand Down Expand Up @@ -129,7 +130,7 @@ static void test_fast_known(void)

static void test_endtoend(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);

// Test 100 random messages and keypairs
Expand Down Expand Up @@ -196,7 +197,7 @@ static void test_endtoend(void)

static void test_large_data(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
uint8_t k[CRYPTO_SHARED_KEY_SIZE];
uint8_t n[CRYPTO_NONCE_SIZE];
Expand Down Expand Up @@ -240,7 +241,7 @@ static void test_large_data(void)

static void test_large_data_symmetric(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
uint8_t k[CRYPTO_SYMMETRIC_KEY_SIZE];

Expand Down Expand Up @@ -296,7 +297,7 @@ static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num)

static void test_increment_nonce(void)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);

uint32_t i;
Expand Down
3 changes: 2 additions & 1 deletion auto_tests/encryptsave_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h"
#include "../toxencryptsave/toxencryptsave.h"
#include "auto_test_support.h"
Expand Down Expand Up @@ -169,7 +170,7 @@ static void test_keys(void)
ck_assert(encrypted2a != nullptr);
uint8_t *in_plaintext2a = (uint8_t *)malloc(plaintext_length2a);
ck_assert(in_plaintext2a != nullptr);
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
random_bytes(rng, in_plaintext2a, plaintext_length2a);
ret = tox_pass_encrypt(in_plaintext2a, plaintext_length2a, key_char, 12, encrypted2a, &encerr);
Expand Down
15 changes: 9 additions & 6 deletions auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_random.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"
Expand Down Expand Up @@ -104,9 +107,9 @@ typedef struct Forwarding_Subtox {

static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, uint32_t *index, uint16_t port)
{
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);

Forwarding_Subtox *subtox = (Forwarding_Subtox *)calloc(1, sizeof(Forwarding_Subtox));
Expand All @@ -115,7 +118,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
subtox->log = logger_new();
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->mono_time = mono_time_new(mem, nullptr);

if (no_udp) {
subtox->net = new_networking_no_udp(subtox->log, mem, ns);
Expand Down Expand Up @@ -152,11 +155,11 @@ static void kill_forwarding_subtox(const Memory *mem, Forwarding_Subtox *subtox)

static void test_forwarding(void)
{
const Memory *mem = system_memory();
const Memory *mem = os_memory();
ck_assert(mem != nullptr);
const Random *rng = system_random();
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = system_network();
const Network *ns = os_network();
ck_assert(ns != nullptr);

uint32_t index[NUM_FORWARDER];
Expand Down
Loading

0 comments on commit ecc6f37

Please sign in to comment.