Skip to content

Commit 0ec4978

Browse files
committed
refactor: Don't expose Tox_System in the public API
It makes no sense to include it in the public API as clients can't make any meaningful use of it via public API, it can only be used if one also includes other internal/private headers that we don't install. It's used only in the testing code, which has access to the internal headers. Fixes #2739, at least to some degree. I decided against moving things to a separate `tox_testing.h` and leaving only things in `tox_private.h` that we are fine with clients using, as otherwise `tox_lock()` / `tox_unlock()` would have to be moved out of `tox_private.h` to somewhere else, but `tox_private.h` actually sounds like the right place for them, naming-wise. So perhaps it's fine if we have things in `tox_private.h` that we don't want clients to use.
1 parent a3d1b85 commit 0ec4978

11 files changed

+82
-47
lines changed

testing/fuzzing/bootstrap_fuzz_test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ void TestBootstrap(Fuzz_Data &input)
104104

105105
Ptr<Tox_Options> opts(tox_options_new(nullptr), tox_options_free);
106106
assert(opts != nullptr);
107-
tox_options_set_operating_system(opts.get(), sys.sys.get());
108107

109108
tox_options_set_log_callback(opts.get(),
110109
[](Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func,
@@ -134,8 +133,12 @@ void TestBootstrap(Fuzz_Data &input)
134133
tox_options_set_tcp_port(opts.get(), 33445);
135134
}
136135

136+
Tox_Options_Testing tox_options_testing;
137+
tox_options_testing.operating_system = sys.sys.get();
138+
137139
Tox_Err_New error_new;
138-
Tox *tox = tox_new(opts.get(), &error_new);
140+
Tox_Err_New_Testing error_new_testing;
141+
Tox *tox = tox_new_testing(opts.get(), &error_new, &tox_options_testing, &error_new_testing);
139142

140143
if (tox == nullptr) {
141144
// It might fail, because some I/O happens in tox_new, and the fuzzer
@@ -144,6 +147,7 @@ void TestBootstrap(Fuzz_Data &input)
144147
}
145148

146149
assert(error_new == TOX_ERR_NEW_OK);
150+
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK);
147151

148152
uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0};
149153

testing/fuzzing/e2e_fuzz_test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ void TestEndToEnd(Fuzz_Data &input)
138138

139139
Ptr<Tox_Options> opts(tox_options_new(nullptr), tox_options_free);
140140
assert(opts != nullptr);
141-
tox_options_set_operating_system(opts.get(), sys.sys.get());
142141
tox_options_set_local_discovery_enabled(opts.get(), false);
143142

144143
tox_options_set_log_callback(opts.get(),
@@ -151,8 +150,12 @@ void TestEndToEnd(Fuzz_Data &input)
151150
}
152151
});
153152

153+
Tox_Options_Testing tox_options_testing;
154+
tox_options_testing.operating_system = sys.sys.get();
155+
154156
Tox_Err_New error_new;
155-
Tox *tox = tox_new(opts.get(), &error_new);
157+
Tox_Err_New_Testing error_new_testing;
158+
Tox *tox = tox_new_testing(opts.get(), &error_new, &tox_options_testing, &error_new_testing);
156159

157160
if (tox == nullptr) {
158161
// It might fail, because some I/O happens in tox_new, and the fuzzer
@@ -161,6 +164,7 @@ void TestEndToEnd(Fuzz_Data &input)
161164
}
162165

163166
assert(error_new == TOX_ERR_NEW_OK);
167+
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK);
164168

165169
tox_events_init(tox);
166170

testing/fuzzing/fuzz_support.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vector>
1717

1818
#include "../../toxcore/tox.h"
19+
#include "../../toxcore/tox_private.h"
1920

2021
struct Fuzz_Data {
2122
static constexpr bool DEBUG = false;

testing/fuzzing/protodump.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,16 @@ void RecordBootstrap(const char *init, const char *bootstrap)
195195
});
196196

197197
Tox_Err_New error_new;
198+
Tox_Err_New_Testing error_new_testing;
199+
Tox_Options_Testing tox_options_testing;
198200

199201
Record_System sys1(global, 4, "tox1"); // fair dice roll
200202
tox_options_set_log_user_data(opts, &sys1);
201-
tox_options_set_operating_system(opts, sys1.sys.get());
202-
Tox *tox1 = tox_new(opts, &error_new);
203+
tox_options_testing.operating_system = sys1.sys.get();
204+
Tox *tox1 = tox_new_testing(opts, &error_new, &tox_options_testing, &error_new_testing);
203205
assert(tox1 != nullptr);
204206
assert(error_new == TOX_ERR_NEW_OK);
207+
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK);
205208
std::array<uint8_t, TOX_ADDRESS_SIZE> address1;
206209
tox_self_get_address(tox1, address1.data());
207210
std::array<uint8_t, TOX_PUBLIC_KEY_SIZE> pk1;
@@ -211,10 +214,11 @@ void RecordBootstrap(const char *init, const char *bootstrap)
211214

212215
Record_System sys2(global, 5, "tox2"); // unfair dice roll
213216
tox_options_set_log_user_data(opts, &sys2);
214-
tox_options_set_operating_system(opts, sys2.sys.get());
215-
Tox *tox2 = tox_new(opts, &error_new);
217+
tox_options_testing.operating_system = sys2.sys.get();
218+
Tox *tox2 = tox_new_testing(opts, &error_new, &tox_options_testing, &error_new_testing);
216219
assert(tox2 != nullptr);
217220
assert(error_new == TOX_ERR_NEW_OK);
221+
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK);
218222
std::array<uint8_t, TOX_ADDRESS_SIZE> address2;
219223
tox_self_get_address(tox2, address2.data());
220224
std::array<uint8_t, TOX_PUBLIC_KEY_SIZE> pk2;

testing/fuzzing/protodump_reduce.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,11 @@ void TestEndToEnd(Fuzz_Data &input)
142142

143143
Ptr<Tox_Options> opts(tox_options_new(nullptr), tox_options_free);
144144
assert(opts != nullptr);
145-
tox_options_set_operating_system(opts.get(), sys.sys.get());
146145
tox_options_set_local_discovery_enabled(opts.get(), false);
147146

147+
Tox_Options_Testing tox_options_testing;
148+
tox_options_testing.operating_system = sys.sys.get();
149+
148150
tox_options_set_log_callback(opts.get(),
149151
[](Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func,
150152
const char *message, void *user_data) {
@@ -156,7 +158,8 @@ void TestEndToEnd(Fuzz_Data &input)
156158
});
157159

158160
Tox_Err_New error_new;
159-
Tox *tox = tox_new(opts.get(), &error_new);
161+
Tox_Err_New_Testing error_new_testing;
162+
Tox *tox = tox_new_testing(opts.get(), &error_new, &tox_options_testing, &error_new_testing);
160163

161164
if (tox == nullptr) {
162165
// It might fail, because some I/O happens in tox_new, and the fuzzer
@@ -165,6 +168,7 @@ void TestEndToEnd(Fuzz_Data &input)
165168
}
166169

167170
assert(error_new == TOX_ERR_NEW_OK);
171+
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK);
168172

169173
tox_events_init(tox);
170174

testing/fuzzing/toxsave_fuzz_test.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ void TestSaveDataLoading(Fuzz_Data &input)
2020
const size_t savedata_size = input.size();
2121
CONSUME_OR_RETURN(const uint8_t *savedata, input, savedata_size);
2222

23-
Null_System sys;
24-
tox_options_set_operating_system(tox_options, sys.sys.get());
25-
2623
// pass test data to Tox
2724
tox_options_set_savedata_data(tox_options, savedata, savedata_size);
2825
tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);
2926

30-
Tox *tox = tox_new(tox_options, nullptr);
27+
Tox_Options_Testing tox_options_testing;
28+
Null_System sys;
29+
tox_options_testing.operating_system = sys.sys.get();
30+
31+
Tox *tox = tox_new_testing(tox_options, nullptr, &tox_options_testing, nullptr);
3132
tox_options_free(tox_options);
3233
if (tox == nullptr) {
3334
// Tox save was invalid, we're finished here

toxcore/tox.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ static int tox_load(Tox *tox, const uint8_t *data, uint32_t length)
712712
length - cookie_len, STATE_COOKIE_TYPE);
713713
}
714714

715-
Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
715+
nullable(1, 2, 3)
716+
static Tox *tox_new_system(const struct Tox_Options *options, Tox_Err_New *error, const Tox_System *sys)
716717
{
717718
struct Tox_Options *default_options = nullptr;
718719

@@ -736,7 +737,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
736737
const struct Tox_Options *const opts = options != nullptr ? options : default_options;
737738
assert(opts != nullptr);
738739

739-
const Tox_System *sys = tox_options_get_operating_system(opts);
740740
const Tox_System default_system = tox_default_system();
741741

742742
if (sys == nullptr) {
@@ -1020,6 +1020,37 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
10201020
return tox;
10211021
}
10221022

1023+
Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
1024+
{
1025+
return tox_new_system(options, error, nullptr);
1026+
}
1027+
1028+
Tox *tox_new_testing(const Tox_Options *options, Tox_Err_New *error, const Tox_Options_Testing *testing, Tox_Err_New_Testing *testing_error)
1029+
{
1030+
if (testing == nullptr) {
1031+
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_NULL);
1032+
SET_ERROR_PARAMETER(testing_error, TOX_ERR_NEW_TESTING_NULL);
1033+
return nullptr;
1034+
}
1035+
1036+
if (testing->operating_system == nullptr) {
1037+
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_NULL);
1038+
SET_ERROR_PARAMETER(testing_error, TOX_ERR_NEW_TESTING_NULL);
1039+
return nullptr;
1040+
}
1041+
1042+
const Tox_System *sys = testing->operating_system;
1043+
1044+
if (sys->rng == nullptr || sys->ns == nullptr || sys->mem == nullptr) {
1045+
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_NULL);
1046+
SET_ERROR_PARAMETER(testing_error, TOX_ERR_NEW_TESTING_NULL);
1047+
return nullptr;
1048+
}
1049+
1050+
SET_ERROR_PARAMETER(testing_error, TOX_ERR_NEW_TESTING_OK);
1051+
return tox_new_system(options, error, sys);
1052+
}
1053+
10231054
void tox_kill(Tox *tox)
10241055
{
10251056
if (tox == nullptr) {

toxcore/tox.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,6 @@ const char *tox_log_level_to_string(Tox_Log_Level value);
496496
typedef void tox_log_cb(Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func,
497497
const char *message, void *user_data);
498498

499-
/**
500-
* @brief Operating system functions used by Tox.
501-
*
502-
* This struct is opaque and generally shouldn't be used in clients, but in
503-
* combination with tox_private.h, it allows tests to inject non-IO (hermetic)
504-
* versions of low level network, RNG, and time keeping functions.
505-
*/
506-
typedef struct Tox_System Tox_System;
507-
508499
/**
509500
* @brief This struct contains all the startup options for Tox.
510501
*
@@ -665,12 +656,6 @@ struct Tox_Options {
665656
*/
666657
bool experimental_thread_safety;
667658

668-
/**
669-
* Low level operating system functionality such as send/recv, random
670-
* number generation, and memory allocation.
671-
*/
672-
const Tox_System *operating_system;
673-
674659
/**
675660
* Enable saving DHT-based group chats to Tox save data (via
676661
* `tox_get_savedata`). This format will change in the future, so don't rely
@@ -753,10 +738,6 @@ bool tox_options_get_experimental_thread_safety(const Tox_Options *options);
753738

754739
void tox_options_set_experimental_thread_safety(Tox_Options *options, bool experimental_thread_safety);
755740

756-
const Tox_System *tox_options_get_operating_system(const Tox_Options *options);
757-
758-
void tox_options_set_operating_system(Tox_Options *options, const Tox_System *operating_system);
759-
760741
bool tox_options_get_experimental_groups_persistence(const Tox_Options *options);
761742

762743
void tox_options_set_experimental_groups_persistence(Tox_Options *options, bool experimental_groups_persistence);

toxcore/tox_api.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,6 @@ void tox_options_set_experimental_thread_safety(
265265
{
266266
options->experimental_thread_safety = experimental_thread_safety;
267267
}
268-
const Tox_System *tox_options_get_operating_system(const Tox_Options *options)
269-
{
270-
return options->operating_system;
271-
}
272-
void tox_options_set_operating_system(Tox_Options *options, const Tox_System *operating_system)
273-
{
274-
options->operating_system = operating_system;
275-
}
276268
bool tox_options_get_experimental_groups_persistence(const Tox_Options *options)
277269
{
278270
return options->experimental_groups_persistence;

toxcore/tox_events.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ void tox_events_free(Tox_Events *events);
570570
uint32_t tox_events_bytes_size(const Tox_Events *events);
571571
bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes);
572572

573+
typedef struct Tox_System Tox_System;
574+
573575
Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size);
574576

575577
bool tox_events_equal(const Tox_System *sys, const Tox_Events *a, const Tox_Events *b);

toxcore/tox_private.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,32 @@ extern "C" {
1818

1919
typedef uint64_t tox_mono_time_cb(void *user_data);
2020

21-
struct Tox_System {
21+
typedef struct Tox_System {
2222
tox_mono_time_cb *mono_time_callback;
2323
void *mono_time_user_data;
2424
const struct Random *rng;
2525
const struct Network *ns;
2626
const struct Memory *mem;
27-
};
27+
} Tox_System;
2828

2929
Tox_System tox_default_system(void);
3030

31+
const Tox_System *tox_get_system(Tox *tox);
32+
33+
typedef struct Tox_Options_Testing {
34+
const struct Tox_System *operating_system;
35+
} Tox_Options_Testing;
36+
37+
typedef enum Tox_Err_New_Testing {
38+
TOX_ERR_NEW_TESTING_OK,
39+
TOX_ERR_NEW_TESTING_NULL,
40+
} Tox_Err_New_Testing;
41+
42+
Tox *tox_new_testing(const Tox_Options *options, Tox_Err_New *error, const Tox_Options_Testing *testing, Tox_Err_New_Testing *testing_error);
43+
3144
void tox_lock(const Tox *tox);
3245
void tox_unlock(const Tox *tox);
3346

34-
const Tox_System *tox_get_system(Tox *tox);
35-
3647
/**
3748
* Set the callback for the `friend_lossy_packet` event for a specific packet
3849
* ID. Pass NULL to unset.

0 commit comments

Comments
 (0)