Skip to content

Commit

Permalink
refactor: Migrate auto_tests to new events API.
Browse files Browse the repository at this point in the history
Co-authored-by: Green Sky <[email protected]>
  • Loading branch information
Green-Sky authored and iphydf committed Jan 18, 2024
1 parent bdd42b5 commit e07248d
Show file tree
Hide file tree
Showing 35 changed files with 719 additions and 314 deletions.
2 changes: 2 additions & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ cc_library(
"//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:mono_time",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
],
)

Expand Down
28 changes: 26 additions & 2 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "../testing/misc_tools.h"
#include "../toxcore/Messenger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/tox_dispatch.h"
#include "../toxcore/tox_events.h"
#include "../toxcore/tox_struct.h"

#include "auto_test_support.h"
Expand All @@ -23,6 +25,7 @@ Run_Auto_Options default_run_auto_options(void)
.graph = GRAPH_COMPLETE,
.init_autotox = nullptr,
.tcp_port = 33188,
.events = true,
};
}

Expand Down Expand Up @@ -131,7 +134,15 @@ void iterate_all_wait(AutoTox *autotoxes, uint32_t tox_count, uint32_t wait)

for (uint32_t i = 0; i < tox_count; ++i) {
if (autotoxes[i].alive) {
tox_iterate(autotoxes[i].tox, &autotoxes[i]);
if (autotoxes[i].events) {
Tox_Err_Events_Iterate err;
Tox_Events *events = tox_events_iterate(autotoxes[i].tox, true, &err);
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
tox_dispatch_invoke(autotoxes[i].dispatch, events, autotoxes[i].tox, &autotoxes[i]);
tox_events_free(events);
} else {
tox_iterate(autotoxes[i].tox, &autotoxes[i]);
}
autotoxes[i].clock += wait;
}
}
Expand Down Expand Up @@ -181,6 +192,7 @@ void kill_autotox(AutoTox *autotox)
ck_assert(autotox->alive);
fprintf(stderr, "Killing #%u\n", autotox->index);
autotox->alive = false;
tox_dispatch_free(autotox->dispatch);
tox_kill(autotox->tox);
}

Expand All @@ -202,6 +214,11 @@ void reload(AutoTox *autotox)
tox_options_set_savedata_data(options, autotox->save_state, autotox->save_size);
autotox->tox = tox_new_log(options, nullptr, &autotox->index);
ck_assert(autotox->tox != nullptr);
autotox->dispatch = tox_dispatch_new(nullptr);
ck_assert(autotox->dispatch != nullptr);
if (autotox->events) {
tox_events_init(autotox->tox);
}
tox_options_free(options);

set_mono_time_callback(autotox);
Expand All @@ -212,6 +229,7 @@ static void initialise_autotox(struct Tox_Options *options, AutoTox *autotox, ui
Run_Auto_Options *autotest_opts)
{
autotox->index = index;
autotox->events = autotest_opts->events;

Tox_Err_New err = TOX_ERR_NEW_OK;

Expand Down Expand Up @@ -259,6 +277,12 @@ static void initialise_autotox(struct Tox_Options *options, AutoTox *autotox, ui

set_mono_time_callback(autotox);

autotox->dispatch = tox_dispatch_new(nullptr);
ck_assert(autotox->dispatch != nullptr);
if (autotox->events) {
tox_events_init(autotox->tox);
}

autotox->alive = true;
autotox->save_state = nullptr;

Expand Down Expand Up @@ -371,6 +395,7 @@ void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(Au
test(autotoxes);

for (uint32_t i = 0; i < tox_count; ++i) {
tox_dispatch_free(autotoxes[i].dispatch);
tox_kill(autotoxes[i].tox);
free(autotoxes[i].state);
free(autotoxes[i].save_state);
Expand Down Expand Up @@ -455,4 +480,3 @@ Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_d
{
return tox_new_log_lan(options, err, log_user_data, false);
}

4 changes: 4 additions & 0 deletions auto_tests/auto_test_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
#include "../testing/misc_tools.h"
#include "../toxcore/Messenger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/tox_dispatch.h"

typedef struct AutoTox {
Tox *tox;
Tox_Dispatch *dispatch;

uint32_t index;
uint64_t clock;

size_t save_size;
uint8_t *save_state;
bool alive;
bool events;

void *state;
} AutoTox;
Expand All @@ -42,6 +45,7 @@ typedef struct Run_Auto_Options {
Graph_Type graph;
void (*init_autotox)(AutoTox *autotox, uint32_t n);
uint16_t tcp_port;
bool events;
} Run_Auto_Options;

Run_Auto_Options default_run_auto_options(void);
Expand Down
34 changes: 23 additions & 11 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ typedef struct State {
} State;

static void handle_self_connection_status(
Tox *tox, Tox_Connection connection_status, void *user_data)
Tox *tox, const Tox_Event_Self_Connection_Status *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;

const Tox_Connection connection_status = tox_event_self_connection_status_get_connection_status(event);
if (connection_status != TOX_CONNECTION_NONE) {
printf("tox #%u: is now connected\n", autotox->index);
} else {
Expand All @@ -35,10 +36,13 @@ static void handle_self_connection_status(
}

static void handle_friend_connection_status(
Tox *tox, uint32_t friendnumber, Tox_Connection connection_status, void *user_data)
Tox *tox, const Tox_Event_Friend_Connection_Status *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;

const uint32_t friendnumber = tox_event_friend_connection_status_get_friend_number(event);
const Tox_Connection connection_status = tox_event_friend_connection_status_get_connection_status(event);

if (connection_status != TOX_CONNECTION_NONE) {
printf("tox #%u: is now connected to friend %u\n", autotox->index, friendnumber);
} else {
Expand Down Expand Up @@ -70,18 +74,23 @@ static void audio_callback(void *tox, uint32_t groupnumber, uint32_t peernumber,
}

static void handle_conference_invite(
Tox *tox, uint32_t friendnumber, Tox_Conference_Type type,
const uint8_t *data, size_t length, void *user_data)
Tox *tox, const Tox_Event_Conference_Invite *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;

const uint32_t friend_number = tox_event_conference_invite_get_friend_number(event);
const Tox_Conference_Type type = tox_event_conference_invite_get_type(event);
const uint8_t *cookie = tox_event_conference_invite_get_cookie(event);
const size_t length = tox_event_conference_invite_get_cookie_length(event);

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, friendnumber, data, length, audio_callback, user_data) == 0,
ck_assert_msg(toxav_join_av_groupchat(tox, friend_number, cookie, length, audio_callback, user_data) == 0,
"tox #%u: failed to join group", autotox->index);
}

static void handle_conference_connected(
Tox *tox, uint32_t conference_number, void *user_data)
Tox *tox, const Tox_Event_Conference_Connected *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;
Expand Down Expand Up @@ -138,7 +147,10 @@ static void disconnect_toxes(uint32_t tox_count, AutoTox *autotoxes,
do {
for (uint32_t i = 0; i < tox_count; ++i) {
if (!disconnect_now[i]) {
tox_iterate(autotoxes[i].tox, &autotoxes[i]);
Tox_Err_Events_Iterate err;
Tox_Events *events = tox_events_iterate(autotoxes[i].tox, true, &err);
tox_dispatch_invoke(autotoxes[i].dispatch, events, autotoxes[i].tox, &autotoxes[i]);
tox_events_free(events);
autotoxes[i].clock += 1000;
}
}
Expand Down Expand Up @@ -402,10 +414,10 @@ static void test_groupav(AutoTox *autotoxes)
const time_t test_start_time = time(nullptr);

for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
tox_callback_self_connection_status(autotoxes[i].tox, &handle_self_connection_status);
tox_callback_friend_connection_status(autotoxes[i].tox, &handle_friend_connection_status);
tox_callback_conference_invite(autotoxes[i].tox, &handle_conference_invite);
tox_callback_conference_connected(autotoxes[i].tox, &handle_conference_connected);
tox_events_callback_self_connection_status(autotoxes[i].dispatch, handle_self_connection_status);
tox_events_callback_friend_connection_status(autotoxes[i].dispatch, handle_friend_connection_status);
tox_events_callback_conference_invite(autotoxes[i].dispatch, handle_conference_invite);
tox_events_callback_conference_connected(autotoxes[i].dispatch, handle_conference_connected);
}

ck_assert_msg(toxav_add_av_groupchat(autotoxes[0].tox, audio_callback, &autotoxes[0]) != UINT32_MAX,
Expand Down
12 changes: 8 additions & 4 deletions auto_tests/conference_double_invite_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ typedef struct State {
#include "auto_test_support.h"

static void handle_conference_invite(
Tox *tox, uint32_t friend_number, Tox_Conference_Type type,
const uint8_t *cookie, size_t length, void *user_data)
Tox *tox, const Tox_Event_Conference_Invite *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

const uint32_t friend_number = tox_event_conference_invite_get_friend_number(event);
const Tox_Conference_Type type = tox_event_conference_invite_get_type(event);
const uint8_t *cookie = tox_event_conference_invite_get_cookie(event);
const size_t length = tox_event_conference_invite_get_cookie_length(event);

fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n",
autotox->index, friend_number, type, (unsigned)length);
fprintf(stderr, "tox%u joining conference\n", autotox->index);
Expand All @@ -37,8 +41,8 @@ static void handle_conference_invite(
static void conference_double_invite_test(AutoTox *autotoxes)
{
// Conference callbacks.
tox_callback_conference_invite(autotoxes[0].tox, handle_conference_invite);
tox_callback_conference_invite(autotoxes[1].tox, handle_conference_invite);
tox_events_callback_conference_invite(autotoxes[0].dispatch, handle_conference_invite);
tox_events_callback_conference_invite(autotoxes[1].dispatch, handle_conference_invite);

State *state[2];
state[0] = (State *)autotoxes[0].state;
Expand Down
13 changes: 8 additions & 5 deletions auto_tests/conference_invite_merge_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ typedef struct State {
#include "auto_test_support.h"

static void handle_conference_invite(
Tox *tox, uint32_t friend_number, Tox_Conference_Type type,
const uint8_t *cookie, size_t length, void *user_data)
Tox *tox, const Tox_Event_Conference_Invite *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

const uint32_t friend_number = tox_event_conference_invite_get_friend_number(event);
const uint8_t *cookie = tox_event_conference_invite_get_cookie(event);
const size_t length = tox_event_conference_invite_get_cookie_length(event);

if (friend_number != -1) {
Tox_Err_Conference_Join err;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
Expand All @@ -28,7 +31,7 @@ static void handle_conference_invite(
}

static void handle_conference_connected(
Tox *tox, uint32_t conference_number, void *user_data)
Tox *tox, const Tox_Event_Conference_Connected *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;
Expand Down Expand Up @@ -95,8 +98,8 @@ static void conference_invite_merge_test(AutoTox *autotoxes)
// components will cause a split group to merge

for (int i = 0; i < NUM_INVITE_MERGE_TOX; i++) {
tox_callback_conference_invite(autotoxes[i].tox, &handle_conference_invite);
tox_callback_conference_connected(autotoxes[i].tox, &handle_conference_connected);
tox_events_callback_conference_invite(autotoxes[i].dispatch, handle_conference_invite);
tox_events_callback_conference_connected(autotoxes[i].dispatch, handle_conference_connected);
}

State *state2 = (State *)autotoxes[2].state;
Expand Down
19 changes: 12 additions & 7 deletions auto_tests/conference_peer_nick_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ typedef struct State {
#include "auto_test_support.h"

static void handle_conference_invite(
Tox *tox, uint32_t friend_number, Tox_Conference_Type type,
const uint8_t *cookie, size_t length, void *user_data)
Tox *tox, const Tox_Event_Conference_Invite *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

const uint32_t friend_number = tox_event_conference_invite_get_friend_number(event);
const Tox_Conference_Type type = tox_event_conference_invite_get_type(event);
const uint8_t *cookie = tox_event_conference_invite_get_cookie(event);
const size_t length = tox_event_conference_invite_get_cookie_length(event);

fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n",
autotox->index, friend_number, type, (unsigned)length);
fprintf(stderr, "tox%u joining conference\n", autotox->index);
Expand All @@ -31,11 +35,12 @@ static void handle_conference_invite(
state->joined = true;
}

static void handle_peer_list_changed(Tox *tox, uint32_t conference_number, void *user_data)
static void handle_peer_list_changed(Tox *tox, const Tox_Event_Conference_Peer_List_Changed *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

const uint32_t conference_number = tox_event_conference_peer_list_changed_get_conference_number(event);
fprintf(stderr, "handle_peer_list_changed(#%u, %u, _)\n",
autotox->index, conference_number);

Expand Down Expand Up @@ -75,10 +80,10 @@ static void rebuild_peer_list(Tox *tox)
static void conference_peer_nick_test(AutoTox *autotoxes)
{
// Conference callbacks.
tox_callback_conference_invite(autotoxes[0].tox, handle_conference_invite);
tox_callback_conference_invite(autotoxes[1].tox, handle_conference_invite);
tox_callback_conference_peer_list_changed(autotoxes[0].tox, handle_peer_list_changed);
tox_callback_conference_peer_list_changed(autotoxes[1].tox, handle_peer_list_changed);
tox_events_callback_conference_invite(autotoxes[0].dispatch, handle_conference_invite);
tox_events_callback_conference_invite(autotoxes[1].dispatch, handle_conference_invite);
tox_events_callback_conference_peer_list_changed(autotoxes[0].dispatch, handle_peer_list_changed);
tox_events_callback_conference_peer_list_changed(autotoxes[1].dispatch, handle_peer_list_changed);

// Set the names of the toxes.
tox_self_set_name(autotoxes[0].tox, (const uint8_t *)"test-tox-0", 10, nullptr);
Expand Down
Loading

0 comments on commit e07248d

Please sign in to comment.