Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove tox_options_default(...) and make Tox_Options opaque #1825

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static void run_conference_tests(Tox **toxes, State *state)

for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
if (restarting[i]) {
struct Tox_Options *const options = tox_options_new(nullptr);
Tox_Options *const options = tox_options_new(nullptr);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, save[i], save_size[i]);
toxes[i] = tox_new_log(options, nullptr, &state[i].index);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/conference_invite_merge_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void reload(Tox **toxes, State *state, uint32_t n)
fprintf(stderr, "Reloading #%u\n", state[n].index);
ck_assert(state[n].save_state != nullptr);

struct Tox_Options *const options = tox_options_new(nullptr);
Tox_Options *const options = tox_options_new(nullptr);
ck_assert(options != nullptr);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, state[n].save_state, state[n].save_size);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void run_conference_tests(Tox **toxes, State *state)

for (uint32_t i = 0; i < NUM_GROUP_TOX; ++i) {
if (restarting[i]) {
struct Tox_Options *const options = tox_options_new(nullptr);
Tox_Options *const options = tox_options_new(nullptr);
ck_assert(options != nullptr);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, save[i], save_size[i]);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/encryptsave_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void test_save_friend(void)
ck_assert_msg(ret, "failed to encrypted save: %d", error1);
ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing");

struct Tox_Options *options = tox_options_new(nullptr);
Tox_Options *options = tox_options_new(nullptr);
ck_assert(options != nullptr);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, enc_data, size2);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/file_saving_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static const char *savefile = "./save";

static void save_data_encrypted(void)
{
struct Tox_Options *options = tox_options_new(nullptr);
Tox_Options *options = tox_options_new(nullptr);
Tox *t = tox_new_log(options, nullptr, nullptr);
tox_options_free(options);

Expand Down Expand Up @@ -79,7 +79,7 @@ static void load_data_decrypted(void)
ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr),
"Could not decrypt, error code %d.", derr);

struct Tox_Options *options = tox_options_new(nullptr);
Tox_Options *options = tox_options_new(nullptr);
ck_assert(options != nullptr);

tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/invalid_tcp_proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

struct Tox_Options *opts = tox_options_new(nullptr);
Tox_Options *opts = tox_options_new(nullptr);
tox_options_set_udp_enabled(opts, false);
tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_proxy_host(opts, "localhost");
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/invalid_udp_proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

struct Tox_Options *opts = tox_options_new(nullptr);
Tox_Options *opts = tox_options_new(nullptr);
tox_options_set_udp_enabled(opts, true);
tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_proxy_host(opts, "localhost");
Expand Down
12 changes: 6 additions & 6 deletions auto_tests/save_compatibility_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,19 @@ static uint8_t *read_save(const char *save_path, size_t *length)

static void test_save_compatibility(const char *save_path)
{
struct Tox_Options options = {0};
tox_options_default(&options);
Tox_Options *options = tox_options_new(nullptr);
ck_assert(options != nullptr);

size_t size = 0;
uint8_t *save_data = read_save(save_path, &size);
ck_assert_msg(save_data != nullptr, "error while reading save file '%s'", save_path);

options.savedata_data = save_data;
options.savedata_length = size;
options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, save_data, size);

size_t index = 0;
Tox_Err_New err;
Tox *tox = tox_new_log(&options, &err, &index);
Tox *tox = tox_new_log(options, &err, &index);
ck_assert_msg(tox, "failed to create tox, error number: %d", err);

free(save_data);
Expand Down Expand Up @@ -128,6 +127,7 @@ static void test_save_compatibility(const char *save_path)
tox_iterate(tox, nullptr);

tox_kill(tox);
tox_options_free(options);
}

int main(int argc, char *argv[])
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/save_friend_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int main(void)
VLA(uint8_t, savedata, save_size);
tox_get_savedata(tox1, savedata);

struct Tox_Options *const options = tox_options_new(nullptr);
Tox_Options *const options = tox_options_new(nullptr);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, savedata, save_size);

Expand Down
10 changes: 5 additions & 5 deletions auto_tests/save_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void tox_connection_status(Tox *tox, Tox_Connection connection_status, vo
* b) a saved state can be loaded back successfully
* c) a second save is of equal size
* d) the second save is of equal content */
static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_data)
static void reload_tox(Tox **tox, Tox_Options *const in_opts, void *user_data)
{
const size_t extra = 64;
const size_t save_size1 = tox_get_savedata_size(*tox);
Expand All @@ -71,7 +71,7 @@ static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_
ck_assert_msg(buffer[extra + save_size1 + i] == 0xCD, "Buffer overwritten from tox_get_savedata() @%u", (unsigned)i);
}

struct Tox_Options *const options = (in_opts == nullptr) ? tox_options_new(nullptr) : in_opts;
Tox_Options *const options = (in_opts == nullptr) ? tox_options_new(nullptr) : in_opts;

tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);

Expand Down Expand Up @@ -108,20 +108,20 @@ static void test_few_clients(void)
uint32_t index[] = { 1, 2, 3 };
time_t con_time = 0, cur_time = time(nullptr);

struct Tox_Options *opts1 = tox_options_new(nullptr);
Tox_Options *opts1 = tox_options_new(nullptr);
tox_options_set_tcp_port(opts1, TCP_RELAY_PORT);
Tox_Err_New t_n_error;
Tox *tox1 = tox_new_log(opts1, &t_n_error, &index[0]);
ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "Failed to create tox instance: %d", t_n_error);
tox_options_free(opts1);

struct Tox_Options *opts2 = tox_options_new(nullptr);
Tox_Options *opts2 = tox_options_new(nullptr);
tox_options_set_udp_enabled(opts2, false);
tox_options_set_local_discovery_enabled(opts2, false);
Tox *tox2 = tox_new_log(opts2, &t_n_error, &index[1]);
ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "Failed to create tox instance: %d", t_n_error);

struct Tox_Options *opts3 = tox_options_new(nullptr);
Tox_Options *opts3 = tox_options_new(nullptr);
tox_options_set_local_discovery_enabled(opts3, false);
Tox *tox3 = tox_new_log(opts3, &t_n_error, &index[2]);
ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "Failed to create tox instance: %d", t_n_error);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/tox_many_tcp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ START_TEST(test_many_clients_tcp)
uint32_t to_comp = 974536;

for (i = 0; i < NUM_TOXES_TCP; ++i) {
struct Tox_Options *opts = tox_options_new(nullptr);
Tox_Options *opts = tox_options_new(nullptr);

if (i == 0) {
tox_options_set_tcp_port(opts, TCP_RELAY_PORT);
Expand Down Expand Up @@ -144,7 +144,7 @@ START_TEST(test_many_clients_tcp_b)
uint32_t to_comp = 974536;

for (i = 0; i < NUM_TOXES_TCP; ++i) {
struct Tox_Options *opts = tox_options_new(nullptr);
Tox_Options *opts = tox_options_new(nullptr);

if (i < NUM_TCP_RELAYS) {
tox_options_set_tcp_port(opts, TCP_RELAY_PORT + i);
Expand Down
7 changes: 5 additions & 2 deletions auto_tests/tox_one_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static void test_one(void)
tox_kill(tox2);
Tox_Err_New err_n;

struct Tox_Options *options = tox_options_new(nullptr);
Tox_Options *options = tox_options_new(nullptr);
ck_assert(options != nullptr);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, data, save_size);
Expand Down Expand Up @@ -111,7 +111,10 @@ static void test_one(void)
tox_self_get_secret_key(tox2, sk);
tox_kill(tox2);

tox_options_default(options);
tox_options_free(options);
options = tox_options_new(nullptr);
ck_assert(options != nullptr);

tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_SECRET_KEY);
tox_options_set_savedata_data(options, sk, sizeof(sk));
tox2 = tox_new_log(options, &err_n, &index[1]);
Expand Down
2 changes: 1 addition & 1 deletion testing/afl_toxsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char **argv)

Tox_Err_Options_New error_options;

struct Tox_Options *tox_options = tox_options_new(&error_options);
Tox_Options *tox_options = tox_options_new(&error_options);

if (error_options != TOX_ERR_OPTIONS_NEW_OK) {
free(buffer);
Expand Down
6 changes: 3 additions & 3 deletions testing/misc_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t lin
}
}

Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery)
Tox *tox_new_log_lan(Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery)
{
struct Tox_Options *log_options = options;
Tox_Options *log_options = options;

if (log_options == nullptr) {
log_options = tox_options_new(nullptr);
Expand All @@ -198,7 +198,7 @@ Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_us
return tox;
}

Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data)
Tox *tox_new_log(Tox_Options *options, Tox_Err_New *err, void *log_user_data)
{
return tox_new_log_lan(options, err, log_user_data, false);
}
Expand Down
4 changes: 2 additions & 2 deletions testing/misc_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ int cmdline_parsefor_ipv46(int argc, char **argv, bool *ipv6enabled);
void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t line, const char *func,
const char *message, void *user_data);

Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data);
Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery);
Tox *tox_new_log(Tox_Options *options, Tox_Err_New *err, void *log_user_data);
Tox *tox_new_log_lan(Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery);

int use_test_rng(uint32_t seed);

Expand Down
6 changes: 3 additions & 3 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ static int tox_load(Tox *tox, const uint8_t *data, uint32_t length)
}


Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
Tox *tox_new(const Tox_Options *options, Tox_Err_New *error)
{
Tox *tox = (Tox *)calloc(1, sizeof(Tox));

Expand All @@ -387,7 +387,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
bool load_savedata_sk = false;
bool load_savedata_tox = false;

struct Tox_Options *default_options = nullptr;
Tox_Options *default_options = nullptr;

if (options == nullptr) {
Tox_Err_Options_New err;
Expand All @@ -406,7 +406,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
}
}

const struct Tox_Options *const opts = options != nullptr ? options : default_options;
const Tox_Options *const opts = options != nullptr ? options : default_options;
assert(opts != nullptr);

if (tox_options_get_savedata_type(opts) != TOX_SAVEDATA_TYPE_NONE) {
Expand Down
Loading