Skip to content

Commit

Permalink
refactor: Don't rely on tox_dispatch passing tox in tests.
Browse files Browse the repository at this point in the history
This frees up the dispatcher from having to know that `Tox *` exists.
  • Loading branch information
iphydf committed Feb 4, 2024
1 parent 34df938 commit e202341
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 140 deletions.
6 changes: 3 additions & 3 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void handle_conference_invite(

ck_assert_msg(type == TOX_CONFERENCE_TYPE_AV, "tox #%u: wrong conference type: %d", autotox->index, type);

ck_assert_msg(toxav_join_av_groupchat(tox, friend_number, cookie, length, audio_callback, user_data) == 0,
ck_assert_msg(toxav_join_av_groupchat(autotox->tox, friend_number, cookie, length, audio_callback, user_data) == 0,
"tox #%u: failed to join group", autotox->index);
}

Expand All @@ -95,12 +95,12 @@ static void handle_conference_connected(
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

if (state->invited_next || tox_self_get_friend_list_size(tox) <= 1) {
if (state->invited_next || tox_self_get_friend_list_size(autotox->tox) <= 1) {
return;
}

Tox_Err_Conference_Invite err;
tox_conference_invite(tox, 1, 0, &err);
tox_conference_invite(autotox->tox, 1, 0, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox #%u failed to invite next friend: err = %d", autotox->index,
err);
printf("tox #%u: invited next friend\n", autotox->index);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/conference_double_invite_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void handle_conference_invite(

if (friend_number != -1) {
Tox_Err_Conference_Join err;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
state->conference = tox_conference_join(autotox->tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
fprintf(stderr, "tox%u joined conference %u\n", autotox->index, state->conference);
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 @@ -23,7 +23,7 @@ static void handle_conference_invite(

if (friend_number != -1) {
Tox_Err_Conference_Join err;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
state->conference = tox_conference_join(autotox->tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
fprintf(stderr, "#%u accepted invite to conference %u\n", autotox->index, state->conference);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/conference_peer_nick_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void handle_conference_invite(
fprintf(stderr, "tox%u joining conference\n", autotox->index);

Tox_Err_Conference_Join err;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
state->conference = tox_conference_join(autotox->tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
fprintf(stderr, "tox%u joined conference %u\n", autotox->index, state->conference);
Expand All @@ -45,7 +45,7 @@ static void handle_peer_list_changed(Tox *tox, const Tox_Event_Conference_Peer_L
autotox->index, conference_number);

Tox_Err_Conference_Peer_Query err;
uint32_t const count = tox_conference_peer_count(tox, conference_number, &err);
uint32_t const count = tox_conference_peer_count(autotox->tox, conference_number, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK,
"failed to get conference peer count: err = %d", err);
printf("tox%u has %u peers\n", autotox->index, count);
Expand Down
75 changes: 37 additions & 38 deletions auto_tests/conference_simple_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

typedef struct State {
uint32_t id;
Tox *tox;
bool self_online;
bool friend_online;
bool invited_next;
Expand Down Expand Up @@ -56,7 +57,7 @@ static void handle_conference_invite(Tox *tox, const Tox_Event_Conference_Invite

{
Tox_Err_Conference_Join err;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
state->conference = tox_conference_join(state->tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "failed to join a conference: err = %d", err);
fprintf(stderr, "tox%u Joined conference %u\n", state->id, state->conference);
state->joined = true;
Expand Down Expand Up @@ -89,7 +90,7 @@ static void handle_conference_peer_list_changed(Tox *tox, const Tox_Event_Confer
state->id, conference_number);

Tox_Err_Conference_Peer_Query err;
uint32_t count = tox_conference_peer_count(tox, conference_number, &err);
uint32_t count = tox_conference_peer_count(state->tox, conference_number, &err);

if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
fprintf(stderr, "ERROR: %d\n", err);
Expand All @@ -107,7 +108,7 @@ static void handle_conference_connected(Tox *tox, const Tox_Event_Conference_Con
// We're tox2, so now we invite tox3.
if (state->id == 2 && !state->invited_next) {
Tox_Err_Conference_Invite err;
tox_conference_invite(tox, 1, state->conference, &err);
tox_conference_invite(state->tox, 1, state->conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox2 failed to invite tox3: err = %d", err);

state->invited_next = true;
Expand All @@ -126,14 +127,12 @@ static void iterate_one(
}

static void iterate3_wait(
Tox *tox1, State *state1,
Tox *tox2, State *state2,
Tox *tox3, State *state3,
State *state1, State *state2, State *state3,
const Tox_Dispatch *dispatch, int interval)
{
iterate_one(tox1, state1, dispatch);
iterate_one(tox2, state2, dispatch);
iterate_one(tox3, state3, dispatch);
iterate_one(state1->tox, state1, dispatch);
iterate_one(state2->tox, state2, dispatch);
iterate_one(state3->tox, state3, dispatch);

c_sleep(interval);
}
Expand All @@ -147,32 +146,32 @@ int main(void)
State state3 = {3};

// Create toxes.
Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id);
Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id);
Tox *tox3 = tox_new_log(nullptr, nullptr, &state3.id);
state1.tox = tox_new_log(nullptr, nullptr, &state1.id);
state2.tox = tox_new_log(nullptr, nullptr, &state2.id);
state3.tox = tox_new_log(nullptr, nullptr, &state3.id);

tox_events_init(tox1);
tox_events_init(tox2);
tox_events_init(tox3);
tox_events_init(state1.tox);
tox_events_init(state2.tox);
tox_events_init(state3.tox);

// tox1 <-> tox2, tox2 <-> tox3
uint8_t key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_public_key(tox2, key);
tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2
tox_self_get_public_key(tox1, key);
tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1
tox_self_get_public_key(tox3, key);
tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox3
tox_self_get_public_key(tox2, key);
tox_friend_add_norequest(tox3, key, nullptr); // tox3 -> tox2
tox_self_get_public_key(state2.tox, key);
tox_friend_add_norequest(state1.tox, key, nullptr); // tox1 -> tox2
tox_self_get_public_key(state1.tox, key);
tox_friend_add_norequest(state2.tox, key, nullptr); // tox2 -> tox1
tox_self_get_public_key(state3.tox, key);
tox_friend_add_norequest(state2.tox, key, nullptr); // tox2 -> tox3
tox_self_get_public_key(state2.tox, key);
tox_friend_add_norequest(state3.tox, key, nullptr); // tox3 -> tox2

printf("bootstrapping tox2 and tox3 off tox1\n");
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox1, dht_key);
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
tox_self_get_dht_id(state1.tox, dht_key);
const uint16_t dht_port = tox_self_get_udp_port(state1.tox, nullptr);

tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr);
tox_bootstrap(state2.tox, "localhost", dht_port, dht_key, nullptr);
tox_bootstrap(state3.tox, "localhost", dht_port, dht_key, nullptr);

Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
ck_assert(dispatch != nullptr);
Expand All @@ -191,7 +190,7 @@ int main(void)
fprintf(stderr, "Waiting for toxes to come online\n");

do {
iterate3_wait(tox1, &state1, tox2, &state2, tox3, &state3, dispatch, 100);
iterate3_wait(&state1, &state2, &state3, dispatch, 100);
} while (!state1.self_online || !state2.self_online || !state3.self_online);

fprintf(stderr, "Toxes are online\n");
Expand All @@ -200,15 +199,15 @@ int main(void)
fprintf(stderr, "Waiting for friends to connect\n");

do {
iterate3_wait(tox1, &state1, tox2, &state2, tox3, &state3, dispatch, 100);
iterate3_wait(&state1, &state2, &state3, dispatch, 100);
} while (!state1.friend_online || !state2.friend_online || !state3.friend_online);

fprintf(stderr, "Friends are connected\n");

{
// Create new conference, tox1 is the founder.
Tox_Err_Conference_New err;
state1.conference = tox_conference_new(tox1, &err);
state1.conference = tox_conference_new(state1.tox, &err);
state1.joined = true;
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create a conference: err = %d", err);
fprintf(stderr, "Created conference: id = %u\n", state1.conference);
Expand All @@ -217,7 +216,7 @@ int main(void)
{
// Invite friend.
Tox_Err_Conference_Invite err;
tox_conference_invite(tox1, 0, state1.conference, &err);
tox_conference_invite(state1.tox, 0, state1.conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "failed to invite a friend: err = %d", err);
state1.invited_next = true;
fprintf(stderr, "tox1 invited tox2\n");
Expand All @@ -226,23 +225,23 @@ int main(void)
fprintf(stderr, "Waiting for invitation to arrive\n");

do {
iterate3_wait(tox1, &state1, tox2, &state2, tox3, &state3, dispatch, 100);
iterate3_wait(&state1, &state2, &state3, dispatch, 100);
} while (!state1.joined || !state2.joined || !state3.joined);

fprintf(stderr, "Invitations accepted\n");

fprintf(stderr, "Waiting for peers to come online\n");

do {
iterate3_wait(tox1, &state1, tox2, &state2, tox3, &state3, dispatch, 100);
iterate3_wait(&state1, &state2, &state3, dispatch, 100);
} while (state1.peers == 0 || state2.peers == 0 || state3.peers == 0);

fprintf(stderr, "All peers are online\n");

{
fprintf(stderr, "tox1 sends a message to the group: \"hello!\"\n");
Tox_Err_Conference_Send_Message err;
tox_conference_send_message(tox1, state1.conference, TOX_MESSAGE_TYPE_NORMAL,
tox_conference_send_message(state1.tox, state1.conference, TOX_MESSAGE_TYPE_NORMAL,
(const uint8_t *)"hello!", 7, &err);

if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) {
Expand All @@ -254,16 +253,16 @@ int main(void)
fprintf(stderr, "Waiting for messages to arrive\n");

do {
iterate3_wait(tox1, &state1, tox2, &state2, tox3, &state3, dispatch, 100);
iterate3_wait(&state1, &state2, &state3, dispatch, 100);
c_sleep(100);
} while (!state2.received || !state3.received);

fprintf(stderr, "Messages received. Test complete.\n");

tox_dispatch_free(dispatch);
tox_kill(tox3);
tox_kill(tox2);
tox_kill(tox1);
tox_kill(state3.tox);
tox_kill(state2.tox);
tox_kill(state1.tox);

return 0;
}
8 changes: 4 additions & 4 deletions auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ static void handle_conference_invite(
ck_assert_msg(type == TOX_CONFERENCE_TYPE_TEXT, "tox #%u: wrong conference type: %d", autotox->index, type);

Tox_Err_Conference_Join err;
uint32_t g_num = tox_conference_join(tox, friendnumber, data, length, &err);
uint32_t g_num = tox_conference_join(autotox->tox, friendnumber, data, length, &err);

ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "tox #%u: error joining group: %d", autotox->index, err);
ck_assert_msg(g_num == 0, "tox #%u: group number was not 0", autotox->index);

// Try joining again. We should only be allowed to join once.
tox_conference_join(tox, friendnumber, data, length, &err);
tox_conference_join(autotox->tox, friendnumber, data, length, &err);
ck_assert_msg(err != TOX_ERR_CONFERENCE_JOIN_OK,
"tox #%u: joining groupchat twice should be impossible.", autotox->index);
}
Expand All @@ -73,12 +73,12 @@ static void handle_conference_connected(
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

if (state->invited_next || tox_self_get_friend_list_size(tox) <= 1) {
if (state->invited_next || tox_self_get_friend_list_size(autotox->tox) <= 1) {
return;
}

Tox_Err_Conference_Invite err;
tox_conference_invite(tox, 1, 0, &err);
tox_conference_invite(autotox->tox, 1, 0, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox #%u failed to invite next friend: err = %d", autotox->index,
err);
printf("tox #%u: invited next friend\n", autotox->index);
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/dht_getnodes_api_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ static void getnodes_response_cb(Tox *tox, const Tox_Event_Dht_Get_Nodes_Respons
{
ck_assert(user_data != nullptr);

AutoTox *autotoxes = (AutoTox *)user_data;
State *state = (State *)autotoxes->state;
AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

const uint8_t *public_key = tox_event_dht_get_nodes_response_get_public_key(event);
const char *ip = (const char *)tox_event_dht_get_nodes_response_get_ip(event);
Expand All @@ -101,7 +101,7 @@ static void getnodes_response_cb(Tox *tox, const Tox_Event_Dht_Get_Nodes_Respons

// ask new node to give us their close nodes to every public key
for (size_t i = 0; i < NUM_TOXES; ++i) {
tox_dht_get_nodes(tox, public_key, ip, port, state->public_key_list[i], nullptr);
tox_dht_get_nodes(autotox->tox, public_key, ip, port, state->public_key_list[i], nullptr);
}
}

Expand Down
20 changes: 13 additions & 7 deletions auto_tests/file_transfer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@

static void accept_friend_request(Tox *m, const Tox_Event_Friend_Request *event, void *userdata)
{
Tox *tox = (Tox *)userdata;

const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
const uint8_t *data = tox_event_friend_request_get_message(event);
const size_t length = tox_event_friend_request_get_message_length(event);

if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
tox_friend_add_norequest(m, public_key, nullptr);
tox_friend_add_norequest(tox, public_key, nullptr);
}
}

Expand All @@ -45,6 +47,8 @@ static uint32_t file_accepted;
static uint64_t file_size;
static void tox_file_receive(Tox *tox, const Tox_Event_File_Recv *event, void *userdata)
{
Tox *state_tox = (Tox *)userdata;

const uint32_t friend_number = tox_event_file_recv_get_friend_number(event);
const uint32_t file_number = tox_event_file_recv_get_file_number(event);
const uint32_t kind = tox_event_file_recv_get_kind(event);
Expand All @@ -59,7 +63,7 @@ static void tox_file_receive(Tox *tox, const Tox_Event_File_Recv *event, void *u

uint8_t file_id[TOX_FILE_ID_LENGTH];

ck_assert_msg(tox_file_get_file_id(tox, friend_number, file_number, file_id, nullptr), "tox_file_get_file_id error");
ck_assert_msg(tox_file_get_file_id(state_tox, friend_number, file_number, file_id, nullptr), "tox_file_get_file_id error");

ck_assert_msg(memcmp(file_id, file_cmp_id, TOX_FILE_ID_LENGTH) == 0, "bad file_id");

Expand All @@ -74,7 +78,7 @@ static void tox_file_receive(Tox *tox, const Tox_Event_File_Recv *event, void *u

Tox_Err_File_Seek err_s;

ck_assert_msg(tox_file_seek(tox, friend_number, file_number, 1337, &err_s), "tox_file_seek error");
ck_assert_msg(tox_file_seek(state_tox, friend_number, file_number, 1337, &err_s), "tox_file_seek error");

ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_OK, "tox_file_seek wrong error");

Expand All @@ -84,13 +88,13 @@ static void tox_file_receive(Tox *tox, const Tox_Event_File_Recv *event, void *u

Tox_Err_File_Control error;

ck_assert_msg(tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, &error),
ck_assert_msg(tox_file_control(state_tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, &error),
"tox_file_control failed. %i", error);
++file_accepted;

Tox_Err_File_Seek err_s;

ck_assert_msg(!tox_file_seek(tox, friend_number, file_number, 1234, &err_s), "tox_file_seek no error");
ck_assert_msg(!tox_file_seek(state_tox, friend_number, file_number, 1234, &err_s), "tox_file_seek no error");

ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_DENIED, "tox_file_seek wrong error");
}
Expand All @@ -114,6 +118,8 @@ static uint8_t sending_num;
static bool file_sending_done;
static void tox_file_chunk_request(Tox *tox, const Tox_Event_File_Chunk_Request *event, void *user_data)
{
Tox *state_tox = (Tox *)user_data;

const uint32_t friend_number = tox_event_file_chunk_request_get_friend_number(event);
const uint32_t file_number = tox_event_file_chunk_request_get_file_number(event);
const uint64_t position = tox_event_file_chunk_request_get_position(event);
Expand Down Expand Up @@ -141,7 +147,7 @@ static void tox_file_chunk_request(Tox *tox, const Tox_Event_File_Chunk_Request
memset(f_data, sending_num, length);

Tox_Err_File_Send_Chunk error;
tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error);
tox_file_send_chunk(state_tox, friend_number, file_number, position, f_data, length, &error);

ck_assert_msg(error == TOX_ERR_FILE_SEND_CHUNK_OK,
"could not send chunk, error num=%d pos=%d len=%d", (int)error, (int)position, (int)length);
Expand Down Expand Up @@ -181,7 +187,7 @@ static void iterate_and_dispatch(const Tox_Dispatch *dispatch, Tox *tox)

events = tox_events_iterate(tox, true, &err);
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
tox_dispatch_invoke(dispatch, events, tox, nullptr);
tox_dispatch_invoke(dispatch, events, tox, tox);
tox_events_free(events);
}

Expand Down
Loading

0 comments on commit e202341

Please sign in to comment.