Skip to content

Commit 7e56ac9

Browse files
committed
refactor: Move some OS-specifics into tox_system.
1 parent ea5e1e3 commit 7e56ac9

File tree

137 files changed

+1800
-799
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+1800
-799
lines changed

auto_tests/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cc_library(
1919
"//c-toxcore/toxcore:Messenger",
2020
"//c-toxcore/toxcore:mono_time",
2121
"//c-toxcore/toxcore:tox",
22+
"//c-toxcore/toxcore:tox_time",
2223
],
2324
)
2425

@@ -61,9 +62,11 @@ flaky_tests = {
6162
"//c-toxcore/toxcore:onion",
6263
"//c-toxcore/toxcore:onion_announce",
6364
"//c-toxcore/toxcore:onion_client",
65+
"//c-toxcore/toxcore:os",
6466
"//c-toxcore/toxcore:tox",
6567
"//c-toxcore/toxcore:tox_dispatch",
6668
"//c-toxcore/toxcore:tox_events",
69+
"//c-toxcore/toxcore:tox_time",
6770
"//c-toxcore/toxcore:util",
6871
"//c-toxcore/toxencryptsave",
6972
"@libsodium",

auto_tests/TCP_test.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "../toxcore/TCP_server.h"
99
#include "../toxcore/crypto_core.h"
1010
#include "../toxcore/mono_time.h"
11+
#include "../toxcore/os_random.h"
12+
#include "../toxcore/os_network.h"
13+
#include "../toxcore/os_memory.h"
1114
#include "../toxcore/util.h"
1215
#include "auto_test_support.h"
1316

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

4649
static void test_basic(void)
4750
{
48-
const Random *rng = system_random();
51+
const Random *rng = os_random();
4952
ck_assert(rng != nullptr);
50-
const Network *ns = system_network();
53+
const Network *ns = os_network();
5154
ck_assert(ns != nullptr);
52-
const Memory *mem = system_memory();
55+
const Memory *mem = os_memory();
5356
ck_assert(mem != nullptr);
5457

55-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
58+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
5659
Logger *logger = logger_new();
5760
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);
5861

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

304307
static void test_some(void)
305308
{
306-
const Random *rng = system_random();
309+
const Random *rng = os_random();
307310
ck_assert(rng != nullptr);
308-
const Network *ns = system_network();
311+
const Network *ns = os_network();
309312
ck_assert(ns != nullptr);
310-
const Memory *mem = system_memory();
313+
const Memory *mem = os_memory();
311314
ck_assert(mem != nullptr);
312315

313-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
316+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
314317
Logger *logger = logger_new();
315318

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

499502
static void test_client(void)
500503
{
501-
const Random *rng = system_random();
504+
const Random *rng = os_random();
502505
ck_assert(rng != nullptr);
503-
const Network *ns = system_network();
506+
const Network *ns = os_network();
504507
ck_assert(ns != nullptr);
505-
const Memory *mem = system_memory();
508+
const Memory *mem = os_memory();
506509
ck_assert(mem != nullptr);
507510

508511
Logger *logger = logger_new();
509-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
512+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
510513

511514
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
512515
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -632,14 +635,14 @@ static void test_client(void)
632635
// Test how the client handles servers that don't respond.
633636
static void test_client_invalid(void)
634637
{
635-
const Random *rng = system_random();
638+
const Random *rng = os_random();
636639
ck_assert(rng != nullptr);
637-
const Network *ns = system_network();
640+
const Network *ns = os_network();
638641
ck_assert(ns != nullptr);
639-
const Memory *mem = system_memory();
642+
const Memory *mem = os_memory();
640643
ck_assert(mem != nullptr);
641644

642-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
645+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
643646
Logger *logger = logger_new();
644647

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

712715
static void test_tcp_connection(void)
713716
{
714-
const Random *rng = system_random();
717+
const Random *rng = os_random();
715718
ck_assert(rng != nullptr);
716-
const Network *ns = system_network();
719+
const Network *ns = os_network();
717720
ck_assert(ns != nullptr);
718-
const Memory *mem = system_memory();
721+
const Memory *mem = os_memory();
719722
ck_assert(mem != nullptr);
720723

721-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
724+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
722725
Logger *logger = logger_new();
723726

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

825828
static void test_tcp_connection2(void)
826829
{
827-
const Random *rng = system_random();
830+
const Random *rng = os_random();
828831
ck_assert(rng != nullptr);
829-
const Network *ns = system_network();
832+
const Network *ns = os_network();
830833
ck_assert(ns != nullptr);
831-
const Memory *mem = system_memory();
834+
const Memory *mem = os_memory();
832835
ck_assert(mem != nullptr);
833836

834-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
837+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
835838
Logger *logger = logger_new();
836839

837840
tcp_oobdata_callback_called = 0;

auto_tests/announce_test.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
#include "../toxcore/mono_time.h"
88
#include "../toxcore/forwarding.h"
99
#include "../toxcore/net_crypto.h"
10+
#include "../toxcore/os_memory.h"
11+
#include "../toxcore/os_network.h"
12+
#include "../toxcore/os_random.h"
1013
#include "../toxcore/util.h"
1114
#include "auto_test_support.h"
1215
#include "check_compat.h"
1316

1417
static void test_bucketnum(void)
1518
{
16-
const Random *rng = system_random();
19+
const Random *rng = os_random();
1720
ck_assert(rng != nullptr);
1821
uint8_t key1[CRYPTO_PUBLIC_KEY_SIZE], key2[CRYPTO_PUBLIC_KEY_SIZE];
1922
random_bytes(rng, key1, sizeof(key1));
@@ -50,17 +53,17 @@ static void test_announce_data(void *object, const uint8_t *data, uint16_t lengt
5053

5154
static void test_store_data(void)
5255
{
53-
const Random *rng = system_random();
56+
const Random *rng = os_random();
5457
ck_assert(rng != nullptr);
55-
const Network *ns = system_network();
58+
const Network *ns = os_network();
5659
ck_assert(ns != nullptr);
57-
const Memory *mem = system_memory();
60+
const Memory *mem = os_memory();
5861
ck_assert(mem != nullptr);
5962

6063
Logger *log = logger_new();
6164
ck_assert(log != nullptr);
6265
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
63-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
66+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
6467
Networking_Core *net = new_networking_no_udp(log, mem, ns);
6568
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
6669
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);

auto_tests/auto_test_support.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../toxcore/Messenger.h"
77
#include "../toxcore/mono_time.h"
88
#include "../toxcore/tox_struct.h"
9+
#include "../toxcore/tox_time_impl.h"
910

1011
#include "auto_test_support.h"
1112

@@ -159,15 +160,21 @@ static uint64_t get_state_clock_callback(void *user_data)
159160
return *clock;
160161
}
161162

163+
static const Tox_Time_Funcs autotox_time_funcs = {
164+
get_state_clock_callback,
165+
};
166+
162167
void set_mono_time_callback(AutoTox *autotox)
163168
{
164169
ck_assert(autotox != nullptr);
165170

171+
autotox->tm = tox_time_new(&autotox_time_funcs, &autotox->clock, autotox->tox->sys.mem);
172+
166173
Mono_Time *mono_time = autotox->tox->mono_time;
167174

168175
autotox->clock = current_time_monotonic(mono_time);
169-
mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first
170-
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock);
176+
mono_time_set_current_time_callback(mono_time, nullptr); // set to default first
177+
mono_time_set_current_time_callback(mono_time, autotox->tm);
171178
}
172179

173180
void save_autotox(AutoTox *autotox)

auto_tests/auto_test_support.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
typedef struct AutoTox {
1212
Tox *tox;
13+
Tox_Time *tm;
1314

1415
uint32_t index;
1516
uint64_t clock;

auto_tests/conference_av_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88

99
#include "../toxav/toxav.h"
10+
#include "../toxcore/os_random.h"
1011
#include "check_compat.h"
1112

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

288289
static void run_conference_tests(AutoTox *autotoxes)
289290
{
290-
const Random *rng = system_random();
291+
const Random *rng = os_random();
291292
ck_assert(rng != nullptr);
292293
bool disabled[NUM_AV_GROUP_TOX] = {0};
293294

auto_tests/conference_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <time.h>
77
#include <stdint.h>
88

9+
#include "../toxcore/os_random.h"
910
#include "../toxcore/util.h"
1011

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

193194
static void run_conference_tests(AutoTox *autotoxes)
194195
{
195-
const Random *rng = system_random();
196+
const Random *rng = os_random();
196197
ck_assert(rng != nullptr);
197198
/* disabling name change propagation check for now, as it occasionally
198199
* fails due to disconnections too short to trigger freezing */

auto_tests/crypto_test.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "../testing/misc_tools.h"
66
#include "../toxcore/crypto_core.h"
7+
#include "../toxcore/os_random.h"
78
#include "../toxcore/net_crypto.h"
89
#include "check_compat.h"
910

@@ -129,7 +130,7 @@ static void test_fast_known(void)
129130

130131
static void test_endtoend(void)
131132
{
132-
const Random *rng = system_random();
133+
const Random *rng = os_random();
133134
ck_assert(rng != nullptr);
134135

135136
// Test 100 random messages and keypairs
@@ -196,7 +197,7 @@ static void test_endtoend(void)
196197

197198
static void test_large_data(void)
198199
{
199-
const Random *rng = system_random();
200+
const Random *rng = os_random();
200201
ck_assert(rng != nullptr);
201202
uint8_t k[CRYPTO_SHARED_KEY_SIZE];
202203
uint8_t n[CRYPTO_NONCE_SIZE];
@@ -240,7 +241,7 @@ static void test_large_data(void)
240241

241242
static void test_large_data_symmetric(void)
242243
{
243-
const Random *rng = system_random();
244+
const Random *rng = os_random();
244245
ck_assert(rng != nullptr);
245246
uint8_t k[CRYPTO_SYMMETRIC_KEY_SIZE];
246247

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

297298
static void test_increment_nonce(void)
298299
{
299-
const Random *rng = system_random();
300+
const Random *rng = os_random();
300301
ck_assert(rng != nullptr);
301302

302303
uint32_t i;

auto_tests/encryptsave_test.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../testing/misc_tools.h"
1010
#include "../toxcore/ccompat.h"
1111
#include "../toxcore/crypto_core.h"
12+
#include "../toxcore/os_random.h"
1213
#include "../toxcore/tox.h"
1314
#include "../toxencryptsave/toxencryptsave.h"
1415
#include "auto_test_support.h"
@@ -169,7 +170,7 @@ static void test_keys(void)
169170
ck_assert(encrypted2a != nullptr);
170171
uint8_t *in_plaintext2a = (uint8_t *)malloc(plaintext_length2a);
171172
ck_assert(in_plaintext2a != nullptr);
172-
const Random *rng = system_random();
173+
const Random *rng = os_random();
173174
ck_assert(rng != nullptr);
174175
random_bytes(rng, in_plaintext2a, plaintext_length2a);
175176
ret = tox_pass_encrypt(in_plaintext2a, plaintext_length2a, key_char, 12, encrypted2a, &encerr);

auto_tests/forwarding_test.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "../toxcore/mono_time.h"
99
#include "../toxcore/forwarding.h"
1010
#include "../toxcore/net_crypto.h"
11+
#include "../toxcore/os_memory.h"
12+
#include "../toxcore/os_network.h"
13+
#include "../toxcore/os_random.h"
1114
#include "../toxcore/util.h"
1215
#include "auto_test_support.h"
1316
#include "check_compat.h"
@@ -104,9 +107,9 @@ typedef struct Forwarding_Subtox {
104107

105108
static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, uint32_t *index, uint16_t port)
106109
{
107-
const Random *rng = system_random();
110+
const Random *rng = os_random();
108111
ck_assert(rng != nullptr);
109-
const Network *ns = system_network();
112+
const Network *ns = os_network();
110113
ck_assert(ns != nullptr);
111114

112115
Forwarding_Subtox *subtox = (Forwarding_Subtox *)calloc(1, sizeof(Forwarding_Subtox));
@@ -115,7 +118,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
115118
subtox->log = logger_new();
116119
ck_assert(subtox->log != nullptr);
117120
logger_callback_log(subtox->log, print_debug_logger, nullptr, index);
118-
subtox->mono_time = mono_time_new(mem, nullptr, nullptr);
121+
subtox->mono_time = mono_time_new(mem, nullptr);
119122

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

153156
static void test_forwarding(void)
154157
{
155-
const Memory *mem = system_memory();
158+
const Memory *mem = os_memory();
156159
ck_assert(mem != nullptr);
157-
const Random *rng = system_random();
160+
const Random *rng = os_random();
158161
ck_assert(rng != nullptr);
159-
const Network *ns = system_network();
162+
const Network *ns = os_network();
160163
ck_assert(ns != nullptr);
161164

162165
uint32_t index[NUM_FORWARDER];

0 commit comments

Comments
 (0)