From d2f98b83c2de71507a730d156d3edce607f6c001 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Tue, 25 Jan 2022 22:18:05 -0500 Subject: [PATCH] Fix some minor issues with autotests - Do null check on autotox pointer for all public functions - Reverse argument order: autotoxes -> count instead of opposite --- auto_tests/auto_test_support.c | 30 +++++++++++++++++----- auto_tests/auto_test_support.h | 6 ++--- auto_tests/conference_av_test.c | 10 ++++---- auto_tests/conference_double_invite_test.c | 4 +-- auto_tests/conference_invite_merge_test.c | 8 +++--- auto_tests/conference_peer_nick_test.c | 4 +-- auto_tests/conference_test.c | 14 +++++----- auto_tests/lossless_packet_test.c | 2 +- auto_tests/lossy_packet_test.c | 2 +- auto_tests/overflow_recvq_test.c | 2 +- auto_tests/reconnect_test.c | 6 ++--- auto_tests/send_message_test.c | 2 +- auto_tests/typing_test.c | 4 +-- 13 files changed, 55 insertions(+), 39 deletions(-) diff --git a/auto_tests/auto_test_support.c b/auto_tests/auto_test_support.c index 1ea437cd..b9a1fb9e 100644 --- a/auto_tests/auto_test_support.c +++ b/auto_tests/auto_test_support.c @@ -58,6 +58,8 @@ static const struct BootstrapNodes { void bootstrap_tox_live_network(Tox *tox, bool enable_tcp) { + ck_assert(tox != nullptr); + for (size_t j = 0; BootstrapNodes[j].ip != nullptr; ++j) { const char *ip = BootstrapNodes[j].ip; uint16_t port = BootstrapNodes[j].port; @@ -80,8 +82,12 @@ void bootstrap_tox_live_network(Tox *tox, bool enable_tcp) } } -bool all_connected(uint32_t tox_count, AutoTox *autotoxes) +bool all_connected(AutoTox *autotoxes, uint32_t tox_count) { + if (tox_count) { + ck_assert(autotoxes != nullptr); + } + for (uint32_t i = 0; i < tox_count; ++i) { if (tox_self_get_connection_status(autotoxes[i].tox) == TOX_CONNECTION_NONE) { return false; @@ -91,8 +97,12 @@ bool all_connected(uint32_t tox_count, AutoTox *autotoxes) return true; } -bool all_friends_connected(uint32_t tox_count, AutoTox *autotoxes) +bool all_friends_connected(AutoTox *autotoxes, uint32_t tox_count) { + if (tox_count) { + ck_assert(autotoxes != nullptr); + } + for (uint32_t i = 0; i < tox_count; ++i) { const size_t friend_count = tox_self_get_friend_list_size(autotoxes[i].tox); @@ -106,8 +116,12 @@ bool all_friends_connected(uint32_t tox_count, AutoTox *autotoxes) return true; } -void iterate_all_wait(uint32_t tox_count, AutoTox *autotoxes, uint32_t wait) +void iterate_all_wait(AutoTox *autotoxes, uint32_t tox_count, uint32_t wait) { + if (tox_count) { + ck_assert(autotoxes != nullptr); + } + for (uint32_t i = 0; i < tox_count; ++i) { if (autotoxes[i].alive) { tox_iterate(autotoxes[i].tox, &autotoxes[i]); @@ -127,6 +141,8 @@ static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data) void set_mono_time_callback(AutoTox *autotox) { + ck_assert(autotox != nullptr); + // TODO(iphydf): Don't rely on toxcore internals. Mono_Time *mono_time = ((Messenger *)autotox->tox)->mono_time; @@ -286,14 +302,14 @@ void run_auto_test(struct Tox_Options *options, uint32_t tox_count, void test(Au bootstrap_autotoxes(options, tox_count, autotest_opts, autotoxes); do { - iterate_all_wait(tox_count, autotoxes, ITERATION_INTERVAL); - } while (!all_connected(tox_count, autotoxes)); + iterate_all_wait(autotoxes, tox_count, ITERATION_INTERVAL); + } while (!all_connected(autotoxes, tox_count)); printf("toxes are online\n"); do { - iterate_all_wait(tox_count, autotoxes, ITERATION_INTERVAL); - } while (!all_friends_connected(tox_count, autotoxes)); + iterate_all_wait(autotoxes, tox_count, ITERATION_INTERVAL); + } while (!all_friends_connected(autotoxes, tox_count)); printf("tox clients connected\n"); diff --git a/auto_tests/auto_test_support.h b/auto_tests/auto_test_support.h index 1c49cd77..b4f3a208 100644 --- a/auto_tests/auto_test_support.h +++ b/auto_tests/auto_test_support.h @@ -21,11 +21,11 @@ typedef struct AutoTox { void *state; } AutoTox; -bool all_connected(uint32_t tox_count, AutoTox *toxes); +bool all_connected(AutoTox *toxes, uint32_t tox_count); -bool all_friends_connected(uint32_t tox_count, AutoTox *toxes); +bool all_friends_connected(AutoTox *toxes, uint32_t tox_count); -void iterate_all_wait(uint32_t tox_count, AutoTox *toxes, uint32_t wait); +void iterate_all_wait(AutoTox *toxes, uint32_t tox_count, uint32_t wait); void save_autotox(AutoTox *autotox); void kill_autotox(AutoTox *autotox); diff --git a/auto_tests/conference_av_test.c b/auto_tests/conference_av_test.c index 7ee25ab0..b8c05574 100644 --- a/auto_tests/conference_av_test.c +++ b/auto_tests/conference_av_test.c @@ -236,7 +236,7 @@ static bool test_audio(AutoTox *autotoxes, const bool *disabled, bool quiet) } } - iterate_all_wait(NUM_AV_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL); if (all_got_audio(autotoxes, disabled)) { return true; @@ -275,7 +275,7 @@ static void do_audio(AutoTox *autotoxes, uint32_t iterations) for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) { ck_assert_msg(toxav_group_send_audio(autotoxes[i].tox, 0, PCM, GROUP_AV_TEST_SAMPLES, 1, 48000) == 0, "#%u failed to send audio", autotoxes[i].index); - iterate_all_wait(NUM_AV_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL); } } } @@ -331,7 +331,7 @@ static void run_conference_tests(AutoTox *autotoxes) printf("reconnecting toxes\n"); do { - iterate_all_wait(NUM_AV_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL); } while (!all_connected_to_group(NUM_AV_GROUP_TOX, autotoxes)); for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) { @@ -413,7 +413,7 @@ static void test_groupav(AutoTox *autotoxes) uint32_t invited_count = 0; do { - iterate_all_wait(NUM_AV_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL); invited_count = 0; @@ -428,7 +428,7 @@ static void test_groupav(AutoTox *autotoxes) do { fully_connected_count = 0; - iterate_all_wait(NUM_AV_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL); for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) { Tox_Err_Conference_Peer_Query err; diff --git a/auto_tests/conference_double_invite_test.c b/auto_tests/conference_double_invite_test.c index 48283101..59228a23 100644 --- a/auto_tests/conference_double_invite_test.c +++ b/auto_tests/conference_double_invite_test.c @@ -66,7 +66,7 @@ static void conference_double_invite_test(AutoTox *autotoxes) fprintf(stderr, "Waiting for invitation to arrive\n"); do { - iterate_all_wait(2, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); } while (!state[0]->joined || !state[1]->joined); fprintf(stderr, "Invitations accepted\n"); @@ -74,7 +74,7 @@ static void conference_double_invite_test(AutoTox *autotoxes) fprintf(stderr, "Sending second invitation; should be ignored\n"); tox_conference_invite(autotoxes[0].tox, 0, state[0]->conference, nullptr); - iterate_all_wait(2, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); } int main(void) diff --git a/auto_tests/conference_invite_merge_test.c b/auto_tests/conference_invite_merge_test.c index d2e6700c..bfd99bbe 100644 --- a/auto_tests/conference_invite_merge_test.c +++ b/auto_tests/conference_invite_merge_test.c @@ -40,7 +40,7 @@ static void handle_conference_connected( static void wait_connected(AutoTox *autotoxes, AutoTox *autotox, uint32_t friendnumber) { do { - iterate_all_wait(NUM_INVITE_MERGE_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_INVITE_MERGE_TOX, ITERATION_INTERVAL); } while (tox_friend_get_connection_status(autotox->tox, friendnumber, nullptr) == TOX_CONNECTION_NONE); } @@ -55,7 +55,7 @@ static void do_invite(AutoTox *autotoxes, AutoTox *inviter, AutoTox *invitee, ui friendnum, err); do { - iterate_all_wait(NUM_INVITE_MERGE_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_INVITE_MERGE_TOX, ITERATION_INTERVAL); } while (!((State *)invitee->state)->connected); } @@ -85,7 +85,7 @@ static bool group_complete(AutoTox *autotoxes) static void wait_group_complete(AutoTox *autotoxes) { do { - iterate_all_wait(NUM_INVITE_MERGE_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_INVITE_MERGE_TOX, ITERATION_INTERVAL); } while (!group_complete(autotoxes)); } @@ -120,7 +120,7 @@ static void conference_invite_merge_test(AutoTox *autotoxes) kill_autotox(&autotoxes[1]); do { - iterate_all_wait(NUM_INVITE_MERGE_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_INVITE_MERGE_TOX, ITERATION_INTERVAL); } while (tox_conference_peer_count(autotoxes[2].tox, state2->conference, nullptr) != 1); do_invite(autotoxes, &autotoxes[2], &autotoxes[3], 1); diff --git a/auto_tests/conference_peer_nick_test.c b/auto_tests/conference_peer_nick_test.c index 3813027e..2a1f8992 100644 --- a/auto_tests/conference_peer_nick_test.c +++ b/auto_tests/conference_peer_nick_test.c @@ -110,13 +110,13 @@ static void conference_peer_nick_test(AutoTox *autotoxes) fprintf(stderr, "Waiting for invitation to arrive and peers to be in the group\n"); do { - iterate_all_wait(2, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); } while (!state[0]->joined || !state[1]->joined || !state[0]->friend_in_group || !state[1]->friend_in_group); fprintf(stderr, "Running tox0, but not tox1, waiting for tox1 to drop out\n"); do { - iterate_all_wait(1, autotoxes, 1000); + iterate_all_wait(autotoxes, 1, 1000); // Rebuild peer list after every iteration. rebuild_peer_list(autotoxes[0].tox); diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c index f2e9f385..fd0ebdb6 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c @@ -278,7 +278,7 @@ static void run_conference_tests(AutoTox *autotoxes) printf("reconnecting toxes\n"); do { - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); } while (!all_connected_to_group(NUM_GROUP_TOX, autotoxes)); printf("running conference tests\n"); @@ -286,7 +286,7 @@ static void run_conference_tests(AutoTox *autotoxes) for (uint32_t i = 0; i < NUM_GROUP_TOX; ++i) { tox_callback_conference_message(autotoxes[i].tox, &handle_conference_message); - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); } Tox_Err_Conference_Send_Message err; @@ -299,7 +299,7 @@ static void run_conference_tests(AutoTox *autotoxes) num_recv = 0; for (uint8_t j = 0; j < NUM_GROUP_TOX * 2; ++j) { - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); } ck_assert_msg(num_recv == NUM_GROUP_TOX, "failed to recv group messages"); @@ -320,7 +320,7 @@ static void run_conference_tests(AutoTox *autotoxes) tox_conference_delete(autotoxes[k - 1].tox, 0, nullptr); for (uint8_t j = 0; j < 10 || j < NUM_GROUP_TOX; ++j) { - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); } for (uint32_t i = 0; i < k - 1; ++i) { @@ -360,7 +360,7 @@ static void test_many_group(AutoTox *autotoxes) uint32_t invited_count = 0; do { - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); invited_count = 0; @@ -377,7 +377,7 @@ static void test_many_group(AutoTox *autotoxes) fully_connected_count = 0; printf("current peer counts: ["); - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); for (uint32_t i = 0; i < NUM_GROUP_TOX; ++i) { Tox_Err_Conference_Peer_Query err; @@ -417,7 +417,7 @@ static void test_many_group(AutoTox *autotoxes) printf("waiting for names to propagate\n"); do { - iterate_all_wait(NUM_GROUP_TOX, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL); } while (!names_propagated(NUM_GROUP_TOX, autotoxes)); printf("group connected, took %d seconds\n", (int)((autotoxes[0].clock - pregroup_clock) / 1000)); diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c index be751590..cf7c37b5 100644 --- a/auto_tests/lossless_packet_test.c +++ b/auto_tests/lossless_packet_test.c @@ -47,7 +47,7 @@ static void test_lossless_packet(AutoTox *autotoxes) ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret); do { - iterate_all_wait(2, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); } while (!((State *)autotoxes[1].state)->custom_packet_received); } diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c index c9eddfbd..a7c7f905 100644 --- a/auto_tests/lossy_packet_test.c +++ b/auto_tests/lossy_packet_test.c @@ -43,7 +43,7 @@ static void test_lossy_packet(AutoTox *autotoxes) ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret); do { - iterate_all_wait(2, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); } while (!((State *)autotoxes[1].state)->custom_packet_received); } diff --git a/auto_tests/overflow_recvq_test.c b/auto_tests/overflow_recvq_test.c index 48a13b96..071749b3 100644 --- a/auto_tests/overflow_recvq_test.c +++ b/auto_tests/overflow_recvq_test.c @@ -46,7 +46,7 @@ static void net_crypto_overflow_test(AutoTox *autotoxes) // TODO(iphydf): Wait until all messages have arrived. Currently, not all // messages arrive, so this test would always fail. for (uint32_t i = 0; i < 200; i++) { - iterate_all_wait(3, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 3, ITERATION_INTERVAL); } printf("tox%u received %u messages\n", autotoxes[0].index, ((State *)autotoxes[0].state)->recv_count); diff --git a/auto_tests/reconnect_test.c b/auto_tests/reconnect_test.c index 5af48e0c..91c60cda 100644 --- a/auto_tests/reconnect_test.c +++ b/auto_tests/reconnect_test.c @@ -56,7 +56,7 @@ static void test_reconnect(AutoTox *autotoxes) printf("letting connections settle\n"); do { - iterate_all_wait(TOX_COUNT, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, TOX_COUNT, ITERATION_INTERVAL); } while (time(nullptr) - test_start_time < 2); uint16_t disconnect = random_u16() % TOX_COUNT; @@ -78,8 +78,8 @@ static void test_reconnect(AutoTox *autotoxes) printf("reconnecting\n"); do { - iterate_all_wait(TOX_COUNT, autotoxes, ITERATION_INTERVAL); - } while (!all_friends_connected(TOX_COUNT, autotoxes)); + iterate_all_wait(autotoxes, TOX_COUNT, ITERATION_INTERVAL); + } while (!all_friends_connected(autotoxes, TOX_COUNT)); const uint64_t reconnect_time = autotoxes[0].clock - reconnect_start_time; ck_assert_msg(reconnect_time <= RECONNECT_TIME_MAX * 1000, "reconnection took %d seconds; expected at most %d seconds", diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 15e68630..ac63b687 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -47,7 +47,7 @@ static void send_message_test(AutoTox *autotoxes) ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm); do { - iterate_all_wait(2, autotoxes, ITERATION_INTERVAL); + iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); } while (!((State *)autotoxes[1].state)->message_received); } diff --git a/auto_tests/typing_test.c b/auto_tests/typing_test.c index eb439832..2081920e 100644 --- a/auto_tests/typing_test.c +++ b/auto_tests/typing_test.c @@ -32,7 +32,7 @@ static void test_typing(AutoTox *autotoxes) tox_self_set_typing(autotoxes[0].tox, 0, true, nullptr); do { - iterate_all_wait(2, autotoxes, 200); + iterate_all_wait(autotoxes, 2, 200); } while (!((State *)autotoxes[1].state)->friend_is_typing); ck_assert_msg(tox_friend_get_typing(autotoxes[1].tox, 0, nullptr) == 1, @@ -40,7 +40,7 @@ static void test_typing(AutoTox *autotoxes) tox_self_set_typing(autotoxes[0].tox, 0, false, nullptr); do { - iterate_all_wait(2, autotoxes, 200); + iterate_all_wait(autotoxes, 2, 200); } while (((State *)autotoxes[1].state)->friend_is_typing); Tox_Err_Friend_Query err_t;