Skip to content

Commit

Permalink
Fix some minor issues with autotests
Browse files Browse the repository at this point in the history
- Do null check on autotox pointer for all public functions
- Reverse argument order: autotoxes -> count instead of opposite
  • Loading branch information
JFreegman committed Jan 26, 2022
1 parent 1157e4e commit d2f98b8
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 39 deletions.
30 changes: 23 additions & 7 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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]);
Expand All @@ -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;

Expand Down Expand Up @@ -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");

Expand Down
6 changes: 3 additions & 3 deletions auto_tests/auto_test_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/conference_double_invite_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ 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");

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)
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/conference_invite_merge_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
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 @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ 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");

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;
Expand All @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/lossless_packet_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/lossy_packet_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/overflow_recvq_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/reconnect_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/send_message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions auto_tests/typing_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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,
"tox_friend_get_typing should have returned true, but it didn't");
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;
Expand Down

0 comments on commit d2f98b8

Please sign in to comment.