Skip to content

Commit

Permalink
test: Migrate auto_tests to new events API.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Dec 9, 2023
1 parent 6645343 commit d01434d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 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
24 changes: 13 additions & 11 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ 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]);
Tox_Err_Events_Iterate err;
Tox_Events *events = tox_events_iterate(autotoxes[i].tox, true, &err);
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);

Check warning on line 149 in auto_tests/auto_test_support.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/auto_test_support.c#L149

MISRA 21.8 rule
tox_dispatch_invoke(autotoxes[i].dispatch, events, autotoxes[i].tox, &autotoxes[i]);
tox_events_free(events);
autotoxes[i].clock += wait;
}
}
Expand Down Expand Up @@ -219,11 +223,11 @@ void reload(AutoTox *autotox)
}

static void initialise_autotox(struct Tox_Options *options, AutoTox *autotox, uint32_t index, uint32_t state_size,
Run_Auto_Options *autotest_opts)
const Tox_Dispatch *dispatch, Run_Auto_Options *autotest_opts)
{
Tox_Err_New err;
autotox->index = index;

Tox_Err_New err = TOX_ERR_NEW_OK;
autotox->dispatch = dispatch;

if (index == 0) {
struct Tox_Options *default_opts = tox_options_new(nullptr);
Expand Down Expand Up @@ -265,22 +269,19 @@ static void initialise_autotox(struct Tox_Options *options, AutoTox *autotox, ui

ck_assert_msg(autotox->tox != nullptr, "failed to create tox instance #%u (error = %d)", index, err);

tox_events_init(autotox->tox);

set_mono_time_callback(autotox);

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

if (state_size > 0) {
autotox->state = calloc(1, state_size);
ck_assert(autotox->state != nullptr);
ck_assert_msg(autotox->state != nullptr, "failed to allocate state");
} else {
autotox->state = nullptr;
}

if (autotest_opts->init_autotox != nullptr) {
autotest_opts->init_autotox(autotox, index);
}
}

static void autotox_add_friend(AutoTox *autotoxes, uint32_t adding, uint32_t added)
Expand Down Expand Up @@ -348,7 +349,7 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count,
}

void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(AutoTox *autotoxes),
uint32_t state_size, Run_Auto_Options *autotest_opts)
uint32_t state_size, const Tox_Dispatch *dispatch, const Run_Auto_Options *autotest_opts)
{
printf("initialising %u toxes\n", tox_count);

Expand All @@ -357,7 +358,8 @@ void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(Au
ck_assert(autotoxes != nullptr);

for (uint32_t i = 0; i < tox_count; ++i) {
initialise_autotox(options, &autotoxes[i], i, state_size, autotest_opts);
Run_Auto_Options opts = *autotest_opts;
initialise_autotox(options, &autotoxes[i], i, state_size, dispatch, &opts);
}

initialise_friend_graph(autotest_opts->graph, tox_count, autotoxes);
Expand Down
4 changes: 3 additions & 1 deletion auto_tests/auto_test_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include "../testing/misc_tools.h"
#include "../toxcore/Messenger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/tox_dispatch.h"

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

uint32_t index;
uint64_t clock;
Expand Down Expand Up @@ -47,7 +49,7 @@ typedef struct Run_Auto_Options {
Run_Auto_Options default_run_auto_options(void);

void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(AutoTox *autotoxes),
uint32_t state_size, Run_Auto_Options *autotest_opts);
uint32_t state_size, const Tox_Dispatch *dispatch, const Run_Auto_Options *autotest_opts);

void bootstrap_tox_live_network(Tox *tox, bool enable_tcp);

Expand Down
46 changes: 30 additions & 16 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ 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 +37,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 Tox_Connection connection_status = tox_event_friend_connection_status_get_connection_status(event);
const uint32_t friendnumber = tox_event_friend_connection_status_get_friend_number(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 @@ -69,19 +74,22 @@ static void audio_callback(void *tox, uint32_t groupnumber, uint32_t peernumber,
++state->received_audio_num;
}

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

const uint32_t friendnumber = tox_event_conference_invite_get_friend_number(event);
const Tox_Conference_Type type = tox_event_conference_invite_get_type(event);
const uint8_t *data = 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,
"tox #%u: failed to join group", autotox->index);
}

static void handle_conference_connected(
Tox *tox, uint32_t conference_number, void *user_data)
static void handle_conference_connected(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 +146,11 @@ 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);
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);

Check warning on line 151 in auto_tests/conference_av_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/conference_av_test.c#L151

MISRA 21.8 rule
tox_dispatch_invoke(autotoxes[i].dispatch, events, autotoxes[i].tox, &autotoxes[i]);
tox_events_free(events);
autotoxes[i].clock += 1000;
}
}
Expand Down Expand Up @@ -401,13 +413,6 @@ 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);
}

ck_assert_msg(toxav_add_av_groupchat(autotoxes[0].tox, audio_callback, &autotoxes[0]) != UINT32_MAX,
"failed to create group");
printf("tox #%u: inviting its first friend\n", autotoxes[0].index);
Expand Down Expand Up @@ -459,10 +464,19 @@ int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
ck_assert(dispatch != nullptr);

Check warning on line 468 in auto_tests/conference_av_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/conference_av_test.c#L468

MISRA 21.8 rule
tox_events_callback_self_connection_status(dispatch, &handle_self_connection_status);
tox_events_callback_friend_connection_status(dispatch, &handle_friend_connection_status);
tox_events_callback_conference_invite(dispatch, &handle_conference_invite);
tox_events_callback_conference_connected(dispatch, &handle_conference_connected);

Run_Auto_Options options = default_run_auto_options();
options.graph = GRAPH_LINEAR;

run_auto_test(nullptr, NUM_AV_GROUP_TOX, test_groupav, sizeof(State), &options);
run_auto_test(nullptr, NUM_AV_GROUP_TOX, test_groupav, sizeof(State), dispatch, &options);

tox_dispatch_free(dispatch);

return 0;
}
19 changes: 11 additions & 8 deletions auto_tests/send_message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ typedef struct State {
#define MESSAGE_FILLER 'G'

static void message_callback(
Tox *m, uint32_t friendnumber, Tox_Message_Type type,
const uint8_t *string, size_t length, void *user_data)
Tox *m, const Tox_Event_Friend_Message *event, void *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

if (type != TOX_MESSAGE_TYPE_NORMAL) {
if (tox_event_friend_message_get_type(event) != TOX_MESSAGE_TYPE_NORMAL) {
ck_abort_msg("Bad type");
}

Expand All @@ -29,7 +28,8 @@ static void message_callback(
ck_assert(cmp_msg != nullptr);
memset(cmp_msg, MESSAGE_FILLER, cmp_msg_len);

if (length == tox_max_message_length() && memcmp(string, cmp_msg, cmp_msg_len) == 0) {
if (tox_event_friend_message_get_message_length(event) == tox_max_message_length() &&
memcmp(tox_event_friend_message_get_message(event), cmp_msg, cmp_msg_len) == 0) {
state->message_received = true;
}

Expand All @@ -38,8 +38,6 @@ static void message_callback(

static void send_message_test(AutoTox *autotoxes)
{
tox_callback_friend_message(autotoxes[1].tox, &message_callback);

const size_t msgs_len = tox_max_message_length() + 1;
uint8_t *msgs = (uint8_t *)malloc(msgs_len);
memset(msgs, MESSAGE_FILLER, msgs_len);
Expand All @@ -62,18 +60,23 @@ int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
ck_assert(dispatch != nullptr);

Check warning on line 64 in auto_tests/send_message_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/send_message_test.c#L64

MISRA 21.8 rule
tox_events_callback_friend_message(dispatch, &message_callback);

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

Run_Auto_Options options = default_run_auto_options();
options.graph = GRAPH_LINEAR;
tox_options_set_ipv6_enabled(tox_options, true);
run_auto_test(tox_options, 2, send_message_test, sizeof(State), &options);
run_auto_test(tox_options, 2, send_message_test, sizeof(State), dispatch, &options);

tox_options_set_ipv6_enabled(tox_options, false);
run_auto_test(tox_options, 2, send_message_test, sizeof(State), &options);
run_auto_test(tox_options, 2, send_message_test, sizeof(State), dispatch, &options);

tox_options_free(tox_options);
tox_dispatch_free(dispatch);

return 0;
}

0 comments on commit d01434d

Please sign in to comment.