From 597f23bdd99e0962a6912231938543b42e6f43ab Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 4 Jul 2018 14:42:02 +0000 Subject: Use run_auto_test.h test fixture for some auto-tests. Most of the auto-tests should use this fixture, but I've only done a few to set an example. --- auto_tests/conference_double_invite_test.c | 142 ++++++++--------------------- auto_tests/friend_connection_test.c | 4 +- auto_tests/overflow_recvq_test.c | 4 +- auto_tests/overflow_sendq_test.c | 4 +- auto_tests/run_auto_test.h | 3 + auto_tests/send_message_test.c | 96 +++++-------------- 6 files changed, 65 insertions(+), 188 deletions(-) (limited to 'auto_tests') diff --git a/auto_tests/conference_double_invite_test.c b/auto_tests/conference_double_invite_test.c index ea85de10..7b646726 100644 --- a/auto_tests/conference_double_invite_test.c +++ b/auto_tests/conference_double_invite_test.c @@ -2,15 +2,11 @@ #define _XOPEN_SOURCE 600 #endif -#include "../toxcore/tox.h" - -#include -#include - -#include "helpers.h" +#include +#include typedef struct State { - uint32_t id; + uint32_t index; bool self_online; bool friend_online; @@ -18,137 +14,75 @@ typedef struct State { uint32_t conference; } State; -static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) -{ - State *state = (State *)user_data; - - fprintf(stderr, "self_connection_status(#%u, %d, _)\n", state->id, connection_status); - state->self_online = connection_status != TOX_CONNECTION_NONE; -} - -static void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, - void *user_data) -{ - State *state = (State *)user_data; - - fprintf(stderr, "handle_friend_connection_status(#%u, %u, %d, _)\n", state->id, friend_number, connection_status); - state->friend_online = connection_status != TOX_CONNECTION_NONE; -} +#include "run_auto_test.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) +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) { State *state = (State *)user_data; fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n", - state->id, friend_number, type, (unsigned)length); - fprintf(stderr, "tox%u joining conference\n", state->id); + state->index, friend_number, type, (unsigned)length); + fprintf(stderr, "tox%u joining conference\n", state->index); if (friend_number != -1) { TOX_ERR_CONFERENCE_JOIN err; state->conference = tox_conference_join(tox, friend_number, cookie, length, &err); - assert(err == TOX_ERR_CONFERENCE_JOIN_OK); - fprintf(stderr, "tox%u Joined conference %u\n", state->id, state->conference); + ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, + "attempting to join the conference returned with an error: %d", err); + fprintf(stderr, "tox%u joined conference %u\n", state->index, state->conference); state->joined = true; } } -int main(void) +static void conference_double_invite_test(Tox **toxes, State *state) { - setvbuf(stdout, nullptr, _IONBF, 0); - - State state1 = {1}; - State state2 = {2}; - - // Create toxes. - Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id); - Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id); - - // tox1 <-> tox2 - uint8_t key[TOX_PUBLIC_KEY_SIZE]; - tox_self_get_public_key(tox2, key); - tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2 - tox_self_get_public_key(tox1, key); - tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1 - - printf("bootstrapping tox2 off tox1\n"); - uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; - tox_self_get_dht_id(tox1, dht_key); - const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); - - tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); - - // Connection callbacks. - tox_callback_self_connection_status(tox1, handle_self_connection_status); - tox_callback_self_connection_status(tox2, handle_self_connection_status); - - tox_callback_friend_connection_status(tox1, handle_friend_connection_status); - tox_callback_friend_connection_status(tox2, handle_friend_connection_status); - // Conference callbacks. - tox_callback_conference_invite(tox1, handle_conference_invite); - tox_callback_conference_invite(tox2, handle_conference_invite); - - // Wait for self connection. - fprintf(stderr, "Waiting for toxes to come online\n"); - - while (!state1.self_online || !state2.self_online) { - tox_iterate(tox1, &state1); - tox_iterate(tox2, &state2); - - c_sleep(100); - } - - fprintf(stderr, "Toxes are online\n"); - - // Wait for friend connection. - fprintf(stderr, "Waiting for friends to connect\n"); - - while (!state1.friend_online || !state2.friend_online) { - tox_iterate(tox1, &state1); - tox_iterate(tox2, &state2); - - c_sleep(100); - } - - fprintf(stderr, "Friends are connected\n"); + tox_callback_conference_invite(toxes[0], handle_conference_invite); + tox_callback_conference_invite(toxes[1], handle_conference_invite); { - // Create new conference, tox1 is the founder. + // Create new conference, tox0 is the founder. TOX_ERR_CONFERENCE_NEW err; - state1.conference = tox_conference_new(tox1, &err); - state1.joined = true; - assert(err == TOX_ERR_CONFERENCE_NEW_OK); - fprintf(stderr, "Created conference: id=%u\n", state1.conference); + state[0].conference = tox_conference_new(toxes[0], &err); + state[0].joined = true; + ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, + "attempting to create a new conference returned with an error: %d", err); + fprintf(stderr, "Created conference: index=%u\n", state[0].conference); } { // Invite friend. TOX_ERR_CONFERENCE_INVITE err; - tox_conference_invite(tox1, 0, state1.conference, &err); - assert(err == TOX_ERR_CONFERENCE_INVITE_OK); - fprintf(stderr, "tox1 invited tox2\n"); + tox_conference_invite(toxes[0], 0, state[0].conference, &err); + ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, + "attempting to invite a friend returned with an error: %d", err); + fprintf(stderr, "tox0 invited tox1\n"); } fprintf(stderr, "Waiting for invitation to arrive\n"); - while (!state1.joined || !state2.joined) { - tox_iterate(tox1, &state1); - tox_iterate(tox2, &state2); + while (!state[0].joined || !state[1].joined) { + tox_iterate(toxes[0], &state[0]); + tox_iterate(toxes[1], &state[1]); - c_sleep(100); + c_sleep(ITERATION_INTERVAL); } fprintf(stderr, "Invitations accepted\n"); - // Invite one more time, resulting in friend -1 inviting tox2. - tox_conference_invite(tox1, 0, state1.conference, 0); + // Invite one more time, resulting in friend -1 inviting tox1 (toxes[1]). + tox_conference_invite(toxes[0], 0, state[0].conference, 0); - tox_iterate(tox1, &state1); - tox_iterate(tox2, &state2); + tox_iterate(toxes[0], &state[0]); + tox_iterate(toxes[1], &state[1]); +} - tox_kill(tox2); - tox_kill(tox1); +int main(void) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + run_auto_test(2, conference_double_invite_test); return 0; } diff --git a/auto_tests/friend_connection_test.c b/auto_tests/friend_connection_test.c index 7af1b575..13d08b65 100644 --- a/auto_tests/friend_connection_test.c +++ b/auto_tests/friend_connection_test.c @@ -9,9 +9,7 @@ #define _XOPEN_SOURCE 600 #endif -#include "check_compat.h" - -#include "../toxcore/tox.h" +#include typedef struct State { uint32_t index; diff --git a/auto_tests/overflow_recvq_test.c b/auto_tests/overflow_recvq_test.c index 3227c5f4..cdad2442 100644 --- a/auto_tests/overflow_recvq_test.c +++ b/auto_tests/overflow_recvq_test.c @@ -5,9 +5,7 @@ #define _XOPEN_SOURCE 600 #endif -#include "check_compat.h" - -#include "../toxcore/tox.h" +#include typedef struct State { uint32_t index; diff --git a/auto_tests/overflow_sendq_test.c b/auto_tests/overflow_sendq_test.c index 94f5c12a..cc6eeda9 100644 --- a/auto_tests/overflow_sendq_test.c +++ b/auto_tests/overflow_sendq_test.c @@ -5,9 +5,7 @@ #define _XOPEN_SOURCE 600 #endif -#include "check_compat.h" - -#include "../toxcore/tox.h" +#include typedef struct State { uint32_t index; diff --git a/auto_tests/run_auto_test.h b/auto_tests/run_auto_test.h index 6ae8ad6a..a4c638ff 100644 --- a/auto_tests/run_auto_test.h +++ b/auto_tests/run_auto_test.h @@ -1,3 +1,6 @@ +#include // calloc, free + +#include "check_compat.h" #include "helpers.h" static bool all_connected(uint32_t tox_count, Tox **toxes) diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 35ef5a62..822ea64f 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -5,27 +5,24 @@ #define _XOPEN_SOURCE 600 #endif -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "check_compat.h" - -#include -#include -#include +#include +#include -#include "../toxcore/ccompat.h" -#include "../toxcore/tox.h" -#include "../toxcore/util.h" +typedef struct State { + uint32_t index; + bool message_received; +} State; -#include "helpers.h" +#include "run_auto_test.h" #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 *userdata) +static void message_callback( + Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, + const uint8_t *string, size_t length, void *userdata) { + State *state = (State *)userdata; + if (type != TOX_MESSAGE_TYPE_NORMAL) { ck_abort_msg("Bad type"); } @@ -34,87 +31,36 @@ static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE typ memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg)); if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) { - bool *message_received = (bool *)userdata; - *message_received = true; + state->message_received = true; } } -static void test_send_message(void) +static void send_message_test(Tox **toxes, State *state) { - printf("initialising 2 toxes\n"); - uint32_t index[] = { 1, 2 }; - const time_t cur_time = time(nullptr); - Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); - Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); - - ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); - - printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); - uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; - tox_self_get_public_key(tox2, public_key); - tox_friend_add_norequest(tox1, public_key, nullptr); - tox_self_get_public_key(tox1, public_key); - tox_friend_add_norequest(tox2, public_key, nullptr); - - printf("bootstrapping tox2 off tox1\n"); - uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; - tox_self_get_dht_id(tox1, dht_key); - const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); - - tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); - - while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || - tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { - tox_iterate(tox1, nullptr); - tox_iterate(tox2, nullptr); - - c_sleep(ITERATION_INTERVAL); - } - - printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); - const time_t con_time = time(nullptr); - - while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || - tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { - tox_iterate(tox1, nullptr); - tox_iterate(tox2, nullptr); - - c_sleep(ITERATION_INTERVAL); - } - - printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); - - tox_callback_friend_message(tox2, &message_callback); + tox_callback_friend_message(toxes[1], &message_callback); uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1]; memset(msgs, MESSAGE_FILLER, sizeof(msgs)); TOX_ERR_FRIEND_SEND_MESSAGE errm; - tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm); + tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm); ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm); - tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm); + tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm); ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm); - bool message_received = false; - - while (!message_received) { - tox_iterate(tox1, nullptr); - tox_iterate(tox2, &message_received); + while (!state[1].message_received) { + tox_iterate(toxes[0], &state[0]); + tox_iterate(toxes[1], &state[1]); c_sleep(ITERATION_INTERVAL); } - - printf("tox clients messaging succeeded\n"); - - tox_kill(tox1); - tox_kill(tox2); } int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - test_send_message(); + run_auto_test(2, send_message_test); return 0; } -- cgit v1.2.3