diff --git a/auto_tests/BUILD.bazel b/auto_tests/BUILD.bazel index 2a2052508d..17606cafe5 100644 --- a/auto_tests/BUILD.bazel +++ b/auto_tests/BUILD.bazel @@ -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", ], ) diff --git a/auto_tests/auto_test_support.c b/auto_tests/auto_test_support.c index 320acc4bc7..cf3d6267db 100644 --- a/auto_tests/auto_test_support.c +++ b/auto_tests/auto_test_support.c @@ -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); + tox_dispatch_invoke(autotoxes[i].dispatch, events, autotoxes[i].tox, &autotoxes[i]); + tox_events_free(events); autotoxes[i].clock += wait; } } @@ -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); @@ -265,6 +269,8 @@ 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; @@ -272,15 +278,10 @@ static void initialise_autotox(struct Tox_Options *options, AutoTox *autotox, ui 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) @@ -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); @@ -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); diff --git a/auto_tests/auto_test_support.h b/auto_tests/auto_test_support.h index eab121aa36..d1d1a263f6 100644 --- a/auto_tests/auto_test_support.h +++ b/auto_tests/auto_test_support.h @@ -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; @@ -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); diff --git a/auto_tests/conference_av_test.c b/auto_tests/conference_av_test.c index c366be42dc..1522e4a2dc 100644 --- a/auto_tests/conference_av_test.c +++ b/auto_tests/conference_av_test.c @@ -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 { @@ -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 { @@ -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; @@ -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); + tox_dispatch_invoke(autotoxes[i].dispatch, events, autotoxes[i].tox, &autotoxes[i]); + tox_events_free(events); autotoxes[i].clock += 1000; } } @@ -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); @@ -459,10 +464,19 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); + Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); + ck_assert(dispatch != nullptr); + 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; } diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 52470f3331..701a0d8ddf 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -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"); } @@ -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; } @@ -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); @@ -62,18 +60,23 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); + Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); + ck_assert(dispatch != nullptr); + 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; }