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

Use tox_options_set_* instead of direct member access. #333

Merged
merged 3 commits into from
Dec 23, 2016
Merged
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ apidsl(
toxcore/tox.api.h)
add_module(toxcore
toxcore/misc_tools.h
toxcore/tox_api.c
toxcore/tox.c
toxcore/tox.h)
target_link_modules(toxcore toxgroup)
Expand Down
21 changes: 9 additions & 12 deletions auto_tests/encryptsave_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,21 @@ START_TEST(test_save_friend)
ck_assert_msg(ret, "failed to encrypted save: %u", error1);
ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing");

struct Tox_Options options;
tox_options_default(&options);
options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_data = enc_data;
options.savedata_length = size2;
struct Tox_Options *options = tox_options_new(NULL);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, enc_data, size2);

TOX_ERR_NEW err2;
Tox *tox3 = tox_new_log(&options, &err2, 0);
Tox *tox3 = tox_new_log(options, &err2, 0);
ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2,
TOX_ERR_NEW_LOAD_ENCRYPTED);
ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL");
uint8_t dec_data[size];
TOX_ERR_DECRYPTION err3;
ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3);
ck_assert_msg(ret, "failed to decrypt save: %u", err3);
options.savedata_data = dec_data;
options.savedata_length = size;
tox3 = tox_new_log(&options, &err2, 0);
tox_options_set_savedata_data(options, dec_data, size);
tox3 = tox_new_log(options, &err2, 0);
ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2);
uint8_t address2[TOX_PUBLIC_KEY_SIZE];
ret = tox_friend_get_public_key(tox3, 0, address2, 0);
Expand Down Expand Up @@ -123,16 +120,16 @@ START_TEST(test_save_friend)

// and now with the code in use (I only bothered with manually to debug this, and it seems a waste
// to remove the manual check now that it's there)
options.savedata_data = out1;
options.savedata_length = size;
Tox *tox4 = tox_new_log(&options, &err2, 0);
tox_options_set_savedata_data(options, out1, size);
Tox *tox4 = tox_new_log(options, &err2, 0);
ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third");
uint8_t address5[TOX_PUBLIC_KEY_SIZE];
ret = tox_friend_get_public_key(tox4, 0, address5, 0);
ck_assert_msg(ret, "no friends! the third");
ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third");

tox_pass_key_free(key);
tox_options_free(options);

tox_kill(tox1);
tox_kill(tox2);
Expand Down
21 changes: 14 additions & 7 deletions auto_tests/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../toxcore/tox.h"

#include <assert.h>
#include <check.h>
#include <stdio.h>

Expand Down Expand Up @@ -50,16 +51,22 @@ static void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *path, uint3

Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data)
{
struct Tox_Options *my_options = tox_options_new(NULL);
struct Tox_Options *log_options = options;

if (options != NULL) {
*my_options = *options;
if (log_options == NULL) {
log_options = tox_options_new(NULL);
}

assert(log_options != NULL);

tox_options_set_log_callback(log_options, &print_debug_log);
tox_options_set_log_user_data(log_options, log_user_data);
Tox *tox = tox_new(log_options, err);

if (options == NULL) {
tox_options_free(log_options);
}

tox_options_set_log_callback(my_options, &print_debug_log);
tox_options_set_log_user_data(my_options, log_user_data);
Tox *tox = tox_new(my_options, err);
tox_options_free(my_options);
return tox;
}

Expand Down
17 changes: 8 additions & 9 deletions auto_tests/save_friend_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Auto Tests: Save and load friends.
*/

#include "../toxcore/tox.h"
#include "helpers.h"

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -51,8 +51,8 @@ void statuschange_callback(Tox *tox, uint32_t friend_number, const uint8_t *mess

int main(int argc, char *argv[])
{
Tox *tox1 = tox_new(tox_options_new(NULL), 0);
Tox *tox2 = tox_new(tox_options_new(NULL), 0);
Tox *tox1 = tox_new_log(0, 0, 0);
Tox *tox2 = tox_new_log(0, 0, 0);

struct test_data to_compare = { { 0 } };

Expand Down Expand Up @@ -106,20 +106,19 @@ int main(int argc, char *argv[])
uint8_t savedata[save_size];
tox_get_savedata(tox1, savedata);

struct Tox_Options options;
tox_options_default(&options);
options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_data = savedata;
options.savedata_length = save_size;
struct Tox_Options *options = tox_options_new(NULL);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, savedata, save_size);

Tox *tox_to_compare = tox_new(&options, 0);
Tox *tox_to_compare = tox_new(options, 0);

tox_friend_get_name(tox_to_compare, 0, to_compare.name, 0);
tox_friend_get_status_message(tox_to_compare, 0, to_compare.status_message, 0);

assert(memcmp(reference_name, to_compare.name, TOX_MAX_NAME_LENGTH) == 0);
assert(memcmp(reference_status, to_compare.status_message, TOX_MAX_STATUS_MESSAGE_LENGTH) == 0);

tox_options_free(options);
tox_kill(tox1);
tox_kill(tox2);
tox_kill(tox_to_compare);
Expand Down
22 changes: 12 additions & 10 deletions auto_tests/tox_many_tcp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ 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_default(&opts);
struct Tox_Options *opts = tox_options_new(NULL);

if (i == 0) {
opts.tcp_port = TCP_RELAY_PORT;
tox_options_set_tcp_port(opts, TCP_RELAY_PORT);
} else {
opts.udp_enabled = 0;
tox_options_set_udp_enabled(opts, 0);
}

index[i] = i + 1;
toxes[i] = tox_new_log(&opts, 0, &index[i]);
toxes[i] = tox_new_log(opts, 0, &index[i]);
ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
tox_callback_friend_request(toxes[i], accept_friend_request);
uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
Expand All @@ -74,6 +73,8 @@ START_TEST(test_many_clients_tcp)
ck_assert_msg(tox_add_tcp_relay(toxes[i], TOX_LOCALHOST, TCP_RELAY_PORT, dpk, &error), "add relay error, %i, %i", i,
error);
ck_assert_msg(tox_bootstrap(toxes[i], TOX_LOCALHOST, 33445, dpk, 0), "Bootstrap error");

tox_options_free(opts);
}

{
Expand Down Expand Up @@ -156,17 +157,16 @@ 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_default(&opts);
struct Tox_Options *opts = tox_options_new(NULL);

if (i < NUM_TCP_RELAYS) {
opts.tcp_port = TCP_RELAY_PORT + i;
tox_options_set_tcp_port(opts, TCP_RELAY_PORT + i);
} else {
opts.udp_enabled = 0;
tox_options_set_udp_enabled(opts, 0);
}

index[i] = i + 1;
toxes[i] = tox_new_log(&opts, 0, &index[i]);
toxes[i] = tox_new_log(opts, 0, &index[i]);
ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
tox_callback_friend_request(toxes[i], accept_friend_request);
uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
Expand All @@ -175,6 +175,8 @@ START_TEST(test_many_clients_tcp_b)
"add relay error");
tox_self_get_dht_id(toxes[0], dpk);
ck_assert_msg(tox_bootstrap(toxes[i], TOX_LOCALHOST, 33445, dpk, 0), "Bootstrap error");

tox_options_free(opts);
}

{
Expand Down
30 changes: 9 additions & 21 deletions auto_tests/tox_one_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ static void set_random_name_and_status_message(Tox *tox, uint8_t *name, uint8_t

START_TEST(test_one)
{
{
TOX_ERR_OPTIONS_NEW o_err;
struct Tox_Options *o1 = tox_options_new(&o_err);
struct Tox_Options o2;
tox_options_default(&o2);
ck_assert_msg(o_err == TOX_ERR_OPTIONS_NEW_OK, "tox_options_new wrong error");
ck_assert_msg(memcmp(o1, &o2, sizeof(struct Tox_Options)) == 0, "tox_options_new error");
tox_options_free(o1);
}

uint8_t name[TOX_MAX_NAME_LENGTH];
uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH];

Expand Down Expand Up @@ -93,12 +83,10 @@ START_TEST(test_one)
tox_kill(tox2);
TOX_ERR_NEW err_n;

struct Tox_Options options;
tox_options_default(&options);
options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_data = data;
options.savedata_length = save_size;
tox2 = tox_new_log(&options, &err_n, &index[1]);
struct Tox_Options *options = tox_options_new(NULL);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, data, save_size);
tox2 = tox_new_log(options, &err_n, &index[1]);
ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed");

ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size.");
Expand All @@ -123,11 +111,10 @@ START_TEST(test_one)
tox_self_get_secret_key(tox2, sk);
tox_kill(tox2);

tox_options_default(&options);
options.savedata_type = TOX_SAVEDATA_TYPE_SECRET_KEY;
options.savedata_data = sk;
options.savedata_length = sizeof(sk);
tox2 = tox_new_log(&options, &err_n, &index[1]);
tox_options_default(options);
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]);
ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed");
uint8_t address3[TOX_ADDRESS_SIZE];
tox_self_get_address(tox2, address3);
Expand All @@ -142,6 +129,7 @@ START_TEST(test_one)
ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error");
}

tox_options_free(options);
tox_kill(tox1);
tox_kill(tox2);
}
Expand Down
11 changes: 5 additions & 6 deletions auto_tests/tox_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,10 @@ START_TEST(test_few_clients)
tox_get_savedata(tox2, save1);
tox_kill(tox2);

struct Tox_Options options;
tox_options_default(&options);
options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
options.savedata_data = save1;
options.savedata_length = save_size1;
tox2 = tox_new_log(&options, NULL, &index[1]);
struct Tox_Options *options = tox_options_new(NULL);
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, save1, save_size1);
tox2 = tox_new_log(options, NULL, &index[1]);
cur_time = time(NULL);
off = 1;

Expand Down Expand Up @@ -727,6 +725,7 @@ START_TEST(test_few_clients)

printf("test_few_clients succeeded, took %llu seconds\n", time(NULL) - cur_time);

tox_options_free(options);
tox_kill(tox1);
tox_kill(tox2);
tox_kill(tox3);
Expand Down
1 change: 1 addition & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
../toxcore/ping.c \
../toxcore/tox.h \
../toxcore/tox.c \
../toxcore/tox_api.c \
../toxcore/util.h \
../toxcore/util.c \
../toxcore/group.h \
Expand Down
Loading