From c3515c49e012f76c5d2ca104476e372d924a79f9 Mon Sep 17 00:00:00 2001 From: Robin Lindén Date: Sun, 18 Feb 2018 01:57:45 +0100 Subject: Split up tox_test into multiple smaller tests --- auto_tests/friend_request_test.c | 92 ++++++++ auto_tests/helpers.h | 3 + auto_tests/lossless_packet_test.c | 112 +++++++++ auto_tests/lossy_packet_test.c | 109 +++++++++ auto_tests/monolith_test.cpp | 32 ++- auto_tests/save_load_test.c | 149 ++++++++++++ auto_tests/send_message_test.c | 120 ++++++++++ auto_tests/set_name_test.c | 106 +++++++++ auto_tests/set_status_message_test.c | 113 +++++++++ auto_tests/tox_test.c | 445 ----------------------------------- auto_tests/typing_test.c | 111 +++++++++ 11 files changed, 945 insertions(+), 447 deletions(-) create mode 100644 auto_tests/friend_request_test.c create mode 100644 auto_tests/lossless_packet_test.c create mode 100644 auto_tests/lossy_packet_test.c create mode 100644 auto_tests/save_load_test.c create mode 100644 auto_tests/send_message_test.c create mode 100644 auto_tests/set_name_test.c create mode 100644 auto_tests/set_status_message_test.c delete mode 100644 auto_tests/tox_test.c create mode 100644 auto_tests/typing_test.c (limited to 'auto_tests') diff --git a/auto_tests/friend_request_test.c b/auto_tests/friend_request_test.c new file mode 100644 index 00000000..d9987cfc --- /dev/null +++ b/auto_tests/friend_request_test.c @@ -0,0 +1,92 @@ +/* Tests that we can add friends. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +#define FR_MESSAGE "Gentoo" + +static void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, size_t length, + void *userdata) +{ + ck_assert_msg(length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0, + "unexpected friend request message"); + tox_friend_add_norequest(tox, public_key, nullptr); +} + +static void test_friend_request(void) +{ + 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("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); + + printf("tox1 adds tox2 as friend, tox2 accepts\n"); + tox_callback_friend_request(tox2, accept_friend_request); + + uint8_t address[TOX_ADDRESS_SIZE]; + tox_self_get_address(tox2, address); + + const uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), nullptr); + ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); + + 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); + printf("test_friend_request succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_friend_request(); + return 0; +} diff --git a/auto_tests/helpers.h b/auto_tests/helpers.h index de106340..72f57ead 100644 --- a/auto_tests/helpers.h +++ b/auto_tests/helpers.h @@ -17,6 +17,8 @@ #define c_sleep(x) usleep(1000 * (x)) #endif +#define ITERATION_INTERVAL 200 + static const char *tox_log_level_name(TOX_LOG_LEVEL level) { switch (level) { @@ -58,6 +60,7 @@ Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_d if (log_options == nullptr) { log_options = tox_options_new(nullptr); + // tox_options_set_local_discovery_enabled(log_options, false); } assert(log_options != nullptr); diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c new file mode 100644 index 00000000..b5185757 --- /dev/null +++ b/auto_tests/lossless_packet_test.c @@ -0,0 +1,112 @@ +/* Tests that we can send lossless packets. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +#define LOSSLESS_PACKET_FILLER 160 + +static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data) +{ + uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE]; + memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet)); + + if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { + bool *custom_packet_received = (bool *)user_data; + *custom_packet_received = true; + } +} + +static void test_lossless_packet(void) +{ + 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(200); + } + + 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(200); + } + + printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); + + tox_callback_friend_lossless_packet(tox2, &handle_lossless_packet); + uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; + memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet)); + bool ret = tox_friend_send_lossless_packet(tox1, 0, packet, sizeof(packet), nullptr); + ck_assert_msg(ret == false, "tox_friend_send_lossless_packet bigger fail %i", ret); + ret = tox_friend_send_lossless_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); + ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret); + + bool received_lossless_packet = false; + + while (!received_lossless_packet) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, &received_lossless_packet); + + c_sleep(200); + } + + printf("test_lossless_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_lossless_packet(); + return 0; +} diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c new file mode 100644 index 00000000..d225a62b --- /dev/null +++ b/auto_tests/lossy_packet_test.c @@ -0,0 +1,109 @@ +/* Tests that we can send lossy packets. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +#define LOSSY_PACKET_FILLER 200 + +static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) +{ + uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE]; + memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet)); + + if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { + bool *custom_packet_received = (bool *)user_data; + *custom_packet_received = true; + } +} + +static void test_lossy_packet(void) +{ + 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(200); + } + + 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(200); + } + + printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); + + tox_callback_friend_lossy_packet(tox2, &handle_lossy_packet); + uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; + memset(packet, LOSSY_PACKET_FILLER, sizeof(packet)); + bool ret = tox_friend_send_lossy_packet(tox1, 0, packet, sizeof(packet), nullptr); + ck_assert_msg(ret == false, "tox_friend_send_lossy_packet bigger fail %i", ret); + ret = tox_friend_send_lossy_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); + ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret); + + bool received_lossy_packet = false; + + while (!received_lossy_packet) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, &received_lossy_packet); + c_sleep(200); + } + + printf("test_lossy_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_lossy_packet(); + return 0; +} diff --git a/auto_tests/monolith_test.cpp b/auto_tests/monolith_test.cpp index b09d8985..23055bb0 100644 --- a/auto_tests/monolith_test.cpp +++ b/auto_tests/monolith_test.cpp @@ -52,10 +52,22 @@ namespace file_saving_test { #include "file_saving_test.c" } +namespace friend_request_test +{ +#include "friend_request_test.c" +} namespace lan_discovery_test { #include "lan_discovery_test.c" } +namespace lossless_packet_test +{ +#include "lossless_packet_test.c" +} +namespace lossy_packet_test +{ +#include "lossy_packet_test.c" +} namespace messenger_test { #include "messenger_test.c" @@ -76,6 +88,10 @@ namespace save_friend_test { #include "save_friend_test.c" } +namespace save_load_test +{ +#include "save_load_test.c" +} namespace selfname_change_conference_test { #include "selfname_change_conference_test.c" @@ -84,6 +100,18 @@ namespace self_conference_title_change_test { #include "self_conference_title_change_test.c" } +namespace send_message_test +{ +#include "send_message_test.c" +} +namespace set_name_test +{ +#include "set_name_test.c" +} +namespace set_status_message_test +{ +#include "set_status_message_test.c" +} namespace simple_conference_test { #include "simple_conference_test.c" @@ -116,9 +144,9 @@ namespace tox_strncasecmp_test { #include "tox_strncasecmp_test.c" } -namespace tox_test +namespace typing_test { -#include "tox_test.c" +#include "typing_test.c" } namespace version_test { diff --git a/auto_tests/save_load_test.c b/auto_tests/save_load_test.c new file mode 100644 index 00000000..ab4e0a8c --- /dev/null +++ b/auto_tests/save_load_test.c @@ -0,0 +1,149 @@ +/* Tests that we can save and load Tox data. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) +{ + if (length == 7 && memcmp("Gentoo", data, 7) == 0) { + tox_friend_add_norequest(m, public_key, nullptr); + } +} + +static unsigned int connected_t1; +static void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) +{ + if (connected_t1 && !connection_status) { + ck_abort_msg("Tox went offline"); + } + + ck_assert_msg(connection_status == TOX_CONNECTION_UDP, "wrong status %u", connection_status); + + connected_t1 = connection_status; +} + +static void test_few_clients(void) +{ + uint32_t index[] = { 1, 2, 3 }; + time_t con_time = 0, cur_time = time(nullptr); + Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); + Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); + Tox *tox3 = tox_new_log(nullptr, nullptr, &index[2]); + + ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); + + printf("bootstrapping tox2 and tox3 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); + tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr); + + connected_t1 = 0; + tox_callback_self_connection_status(tox1, tox_connection_status); + tox_callback_friend_request(tox2, accept_friend_request); + uint8_t address[TOX_ADDRESS_SIZE]; + tox_self_get_address(tox2, address); + uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr); + ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); + + uint8_t off = 1; + + while (1) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, nullptr); + tox_iterate(tox3, nullptr); + + if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) + && tox_self_get_connection_status(tox3)) { + if (off) { + printf("Toxes are online, took %ld seconds\n", time(nullptr) - cur_time); + con_time = time(nullptr); + off = 0; + } + + if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP + && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { + break; + } + } + + c_sleep(ITERATION_INTERVAL); + } + + ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); + printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); + + const size_t save_size1 = tox_get_savedata_size(tox2); + ck_assert_msg(save_size1 != 0, "save is invalid size %u", (unsigned)save_size1); + printf("%u\n", (unsigned)save_size1); + VLA(uint8_t, save1, save_size1); + tox_get_savedata(tox2, save1); + tox_kill(tox2); + + struct Tox_Options *const options = tox_options_new(nullptr); + tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); + tox_options_set_savedata_data(options, save1, save_size1); + tox_options_set_local_discovery_enabled(options, false); + tox2 = tox_new_log(options, nullptr, &index[1]); + cur_time = time(nullptr); + off = 1; + + while (1) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, nullptr); + tox_iterate(tox3, nullptr); + + if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) + && tox_self_get_connection_status(tox3)) { + if (off) { + printf("Toxes are online again after reloading, took %ld seconds\n", time(nullptr) - cur_time); + con_time = time(nullptr); + off = 0; + } + + if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP + && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { + break; + } + } + + c_sleep(ITERATION_INTERVAL); + } + + printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); + + printf("test_few_clients succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_options_free(options); + tox_kill(tox1); + tox_kill(tox2); + tox_kill(tox3); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_few_clients(); + return 0; +} diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c new file mode 100644 index 00000000..3a5088f3 --- /dev/null +++ b/auto_tests/send_message_test.c @@ -0,0 +1,120 @@ +/* Tests that we can send messages to friends. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.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) +{ + if (type != TOX_MESSAGE_TYPE_NORMAL) { + ck_abort_msg("Bad type"); + } + + uint8_t cmp_msg[TOX_MAX_MESSAGE_LENGTH]; + 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; + } +} + +static void test_send_message(void) +{ + 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); + + 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); + 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); + 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); + + c_sleep(ITERATION_INTERVAL); + } + + printf("tox clients messaging succeeded\n"); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_send_message(); + return 0; +} diff --git a/auto_tests/set_name_test.c b/auto_tests/set_name_test.c new file mode 100644 index 00000000..a7f01ec7 --- /dev/null +++ b/auto_tests/set_name_test.c @@ -0,0 +1,106 @@ +/* Tests that we can set our name. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +#define NICKNAME "Gentoo" + +static void nickchange_callback(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata) +{ + ck_assert_msg(length == sizeof(NICKNAME) && memcmp(string, NICKNAME, sizeof(NICKNAME)) == 0, "Name not correct"); + bool *nickname_updated = (bool *)userdata; + *nickname_updated = true; +} + +static void test_set_name(void) +{ + 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_name(tox2, nickchange_callback); + TOX_ERR_SET_INFO err_n; + bool ret = tox_self_set_name(tox1, (const uint8_t *)NICKNAME, sizeof(NICKNAME), &err_n); + ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %u\n", err_n); + + bool nickname_updated = false; + + while (!nickname_updated) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, &nickname_updated); + c_sleep(ITERATION_INTERVAL); + } + + ck_assert_msg(tox_friend_get_name_size(tox2, 0, nullptr) == sizeof(NICKNAME), "Name length not correct"); + uint8_t temp_name[sizeof(NICKNAME)]; + tox_friend_get_name(tox2, 0, temp_name, nullptr); + ck_assert_msg(memcmp(temp_name, NICKNAME, sizeof(NICKNAME)) == 0, "Name not correct"); + + printf("test_set_name succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_set_name(); + return 0; +} diff --git a/auto_tests/set_status_message_test.c b/auto_tests/set_status_message_test.c new file mode 100644 index 00000000..f1508720 --- /dev/null +++ b/auto_tests/set_status_message_test.c @@ -0,0 +1,113 @@ +/* Tests that we can set our status message + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +#define STATUS_MESSAGE "Installing Gentoo" + +static void status_callback(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, void *user_data) +{ + ck_assert_msg(length == sizeof(STATUS_MESSAGE) && + memcmp(message, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0, + "incorrect data in status callback"); + bool *status_updated = (bool *)user_data; + *status_updated = true; +} + +static void test_set_status_message(void) +{ + 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_ERR_SET_INFO err_n; + tox_callback_friend_status_message(tox2, status_callback); + bool ret = tox_self_set_status_message(tox1, (const uint8_t *)STATUS_MESSAGE, sizeof(STATUS_MESSAGE), + &err_n); + ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n); + + bool status_updated = false; + + while (!status_updated) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, &status_updated); + c_sleep(ITERATION_INTERVAL); + } + + ck_assert_msg(tox_friend_get_status_message_size(tox2, 0, nullptr) == sizeof(STATUS_MESSAGE), + "status message length not correct"); + uint8_t cmp_status[sizeof(STATUS_MESSAGE)]; + tox_friend_get_status_message(tox2, 0, cmp_status, nullptr); + ck_assert_msg(memcmp(cmp_status, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0, + "status message not correct"); + + printf("test_set_status_message succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_set_status_message(); + return 0; +} diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c deleted file mode 100644 index d6613a9a..00000000 --- a/auto_tests/tox_test.c +++ /dev/null @@ -1,445 +0,0 @@ -/* Auto Tests - * - * Tox Tests - * - * The following tests were written with a small Tox network in mind. Therefore, - * each test timeout was set to one for a small Tox Network. If connected to the - * 'Global' Tox Network, traversing the DHT would take MUCH longer than the - * timeouts allow. Because of this running these tests require NO other Tox - * clients running or accessible on/to localhost. - * - */ - -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "check_compat.h" - -#include -#include -#include - -#include "../toxcore/ccompat.h" -#include "../toxcore/tox.h" -#include "../toxcore/util.h" - -#include "helpers.h" - -/* The Travis-CI container responds poorly to ::1 as a localhost address - * You're encouraged to -D FORCE_TESTS_IPV6 on a local test */ -#ifdef FORCE_TESTS_IPV6 -#define TOX_LOCALHOST "::1" -#else -#define TOX_LOCALHOST "127.0.0.1" -#endif - -static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) -{ - if (*((uint32_t *)userdata) != 974536) { - return; - } - - if (length == 7 && memcmp("Gentoo", data, 7) == 0) { - tox_friend_add_norequest(m, public_key, nullptr); - } -} -static uint32_t messages_received; - -static void print_message(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length, - void *userdata) -{ - if (*((uint32_t *)userdata) != 974536) { - return; - } - - if (type != TOX_MESSAGE_TYPE_NORMAL) { - ck_abort_msg("Bad type"); - } - - uint8_t cmp_msg[TOX_MAX_MESSAGE_LENGTH]; - memset(cmp_msg, 'G', sizeof(cmp_msg)); - - if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) { - ++messages_received; - } -} - -static uint32_t name_changes; - -static void print_nickchange(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata) -{ - if (*((uint32_t *)userdata) != 974536) { - return; - } - - if (length == sizeof("Gentoo") && memcmp(string, "Gentoo", sizeof("Gentoo")) == 0) { - ++name_changes; - } -} - -static uint32_t status_m_changes; -static void print_status_m_change(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, - void *user_data) -{ - if (*((uint32_t *)user_data) != 974536) { - return; - } - - if (length == sizeof("Installing Gentoo") && - memcmp(message, "Installing Gentoo", sizeof("Installing Gentoo")) == 0) { - ++status_m_changes; - } -} - -static uint32_t typing_changes; - -static void print_typingchange(Tox *m, uint32_t friendnumber, bool typing, void *userdata) -{ - if (*((uint32_t *)userdata) != 974536) { - return; - } - - if (!typing) { - typing_changes = 1; - } else { - typing_changes = 2; - } -} - -static uint32_t custom_packet; - -static void handle_custom_packet(Tox *m, uint32_t friend_num, const uint8_t *data, size_t len, void *object) -{ - uint8_t number = *((uint32_t *)object); - - if (len != TOX_MAX_CUSTOM_PACKET_SIZE) { - return; - } - - VLA(uint8_t, f_data, len); - memset(f_data, number, len); - - if (memcmp(f_data, data, len) == 0) { - ++custom_packet; - } else { - ck_abort_msg("Custom packet fail. %u", number); - } - - return; -} - -static unsigned int connected_t1; -static void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) -{ - if (*((uint32_t *)user_data) != 974536) { - return; - } - - if (connected_t1 && !connection_status) { - ck_abort_msg("Tox went offline"); - } - - ck_assert_msg(connection_status == TOX_CONNECTION_UDP, "wrong status %u", connection_status); - - connected_t1 = connection_status; -} - -START_TEST(test_few_clients) -{ - uint32_t index[] = { 1, 2, 3 }; - long long unsigned int con_time = 0, cur_time = time(nullptr); - TOX_ERR_NEW t_n_error; - Tox *tox1 = tox_new_log(nullptr, &t_n_error, &index[0]); - ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); - Tox *tox2 = tox_new_log(nullptr, &t_n_error, &index[1]); - ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); - Tox *tox3 = tox_new_log(nullptr, &t_n_error, &index[2]); - ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); - - ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); - - { - TOX_ERR_GET_PORT error; - uint16_t first_port = tox_self_get_udp_port(tox1, &error); - ck_assert_msg(33445 <= first_port && first_port <= 33545 - 2, - "First Tox instance did not bind to udp port inside [33445, 33543].\n"); - ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); - - ck_assert_msg(tox_self_get_udp_port(tox2, &error) == first_port + 1, - "Second Tox instance did not bind to udp port %d.\n", first_port + 1); - ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); - - ck_assert_msg(tox_self_get_udp_port(tox3, &error) == first_port + 2, - "Third Tox instance did not bind to udp port %d.\n", first_port + 2); - ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); - } - - uint32_t to_compare = 974536; - connected_t1 = 0; - tox_callback_self_connection_status(tox1, tox_connection_status); - tox_callback_friend_request(tox2, accept_friend_request); - uint8_t address[TOX_ADDRESS_SIZE]; - tox_self_get_address(tox2, address); - uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr); - ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); - - uint8_t off = 1; - - while (1) { - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) - && tox_self_get_connection_status(tox3)) { - if (off) { - printf("Toxes are online, took %llu seconds\n", time(nullptr) - cur_time); - con_time = time(nullptr); - off = 0; - } - - if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP - && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { - break; - } - } - - c_sleep(50); - } - - ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); - printf("tox clients connected took %llu seconds\n", time(nullptr) - con_time); - to_compare = 974536; - tox_callback_friend_message(tox3, print_message); - uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1]; - memset(msgs, 'G', sizeof(msgs)); - TOX_ERR_FRIEND_SEND_MESSAGE errm; - tox_friend_send_message(tox2, 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\n"); - tox_friend_send_message(tox2, 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\n"); - - while (1) { - messages_received = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (messages_received) { - break; - } - - c_sleep(50); - } - - printf("tox clients messaging succeeded\n"); - - unsigned int save_size1 = tox_get_savedata_size(tox2); - ck_assert_msg(save_size1 != 0, "save is invalid size %u", save_size1); - printf("%u\n", save_size1); - VLA(uint8_t, save1, save_size1); - tox_get_savedata(tox2, save1); - tox_kill(tox2); - - struct Tox_Options *options = tox_options_new(nullptr); - tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); - tox_options_set_savedata_data(options, save1, save_size1); - tox2 = tox_new_log(options, nullptr, &index[1]); - cur_time = time(nullptr); - off = 1; - - while (1) { - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2) - && tox_self_get_connection_status(tox3)) { - if (off) { - printf("Toxes are online again after reloading, took %llu seconds\n", time(nullptr) - cur_time); - con_time = time(nullptr); - off = 0; - } - - if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP - && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { - break; - } - } - - c_sleep(50); - } - - printf("tox clients connected took %llu seconds\n", time(nullptr) - con_time); - tox_callback_friend_name(tox3, print_nickchange); - TOX_ERR_SET_INFO err_n; - bool succ = tox_self_set_name(tox2, (const uint8_t *)"Gentoo", sizeof("Gentoo"), &err_n); - ck_assert_msg(succ && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %u\n", err_n); - - while (1) { - name_changes = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (name_changes) { - break; - } - - c_sleep(50); - } - - ck_assert_msg(tox_friend_get_name_size(tox3, 0, nullptr) == sizeof("Gentoo"), "Name length not correct"); - uint8_t temp_name[sizeof("Gentoo")]; - tox_friend_get_name(tox3, 0, temp_name, nullptr); - ck_assert_msg(memcmp(temp_name, "Gentoo", sizeof("Gentoo")) == 0, "Name not correct"); - - tox_callback_friend_status_message(tox3, print_status_m_change); - succ = tox_self_set_status_message(tox2, (const uint8_t *)"Installing Gentoo", sizeof("Installing Gentoo"), &err_n); - ck_assert_msg(succ && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n); - - while (1) { - status_m_changes = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (status_m_changes) { - break; - } - - c_sleep(50); - } - - ck_assert_msg(tox_friend_get_status_message_size(tox3, 0, nullptr) == sizeof("Installing Gentoo"), - "status message length not correct"); - uint8_t temp_status_m[sizeof("Installing Gentoo")]; - tox_friend_get_status_message(tox3, 0, temp_status_m, nullptr); - ck_assert_msg(memcmp(temp_status_m, "Installing Gentoo", sizeof("Installing Gentoo")) == 0, - "status message not correct"); - - tox_callback_friend_typing(tox2, &print_typingchange); - tox_self_set_typing(tox3, 0, 1, nullptr); - - while (1) { - typing_changes = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (typing_changes == 2) { - break; - } - - ck_assert_msg(typing_changes == 0, "Typing fail"); - - c_sleep(50); - } - - ck_assert_msg(tox_friend_get_typing(tox2, 0, nullptr) == 1, "Typing fail"); - tox_self_set_typing(tox3, 0, 0, nullptr); - - while (1) { - typing_changes = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &to_compare); - - if (typing_changes == 1) { - break; - } - - ck_assert_msg(typing_changes == 0, "Typing fail"); - - c_sleep(50); - } - - TOX_ERR_FRIEND_QUERY err_t; - ck_assert_msg(tox_friend_get_typing(tox2, 0, &err_t) == 0, "Typing fail"); - ck_assert_msg(err_t == TOX_ERR_FRIEND_QUERY_OK, "Typing fail"); - - uint32_t packet_number = 160; - tox_callback_friend_lossless_packet(tox3, &handle_custom_packet); - uint8_t data_c[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; - memset(data_c, ((uint8_t)packet_number), sizeof(data_c)); - int ret = tox_friend_send_lossless_packet(tox2, 0, data_c, sizeof(data_c), nullptr); - ck_assert_msg(ret == 0, "tox_friend_send_lossless_packet bigger fail %i", ret); - ret = tox_friend_send_lossless_packet(tox2, 0, data_c, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); - ck_assert_msg(ret == 1, "tox_friend_send_lossless_packet fail %i", ret); - - while (1) { - custom_packet = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &packet_number); - - if (custom_packet == 1) { - break; - } - - ck_assert_msg(custom_packet == 0, "Lossless packet fail"); - - c_sleep(50); - } - - packet_number = 200; - tox_callback_friend_lossy_packet(tox3, &handle_custom_packet); - memset(data_c, ((uint8_t)packet_number), sizeof(data_c)); - ret = tox_friend_send_lossy_packet(tox2, 0, data_c, sizeof(data_c), nullptr); - ck_assert_msg(ret == 0, "tox_friend_send_lossy_packet bigger fail %i", ret); - ret = tox_friend_send_lossy_packet(tox2, 0, data_c, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); - ck_assert_msg(ret == 1, "tox_friend_send_lossy_packet fail %i", ret); - - while (1) { - custom_packet = 0; - tox_iterate(tox1, &to_compare); - tox_iterate(tox2, &to_compare); - tox_iterate(tox3, &packet_number); - - if (custom_packet == 1) { - break; - } - - ck_assert_msg(custom_packet == 0, "lossy packet fail"); - - c_sleep(50); - } - - printf("test_few_clients succeeded, took %llu seconds\n", time(nullptr) - cur_time); - - tox_options_free(options); - tox_kill(tox1); - tox_kill(tox2); - tox_kill(tox3); -} -END_TEST - -static Suite *tox_suite(void) -{ - Suite *s = suite_create("Tox few clients"); - - DEFTESTCASE(few_clients); - - return s; -} - -int main(int argc, char *argv[]) -{ - srand((unsigned int) time(nullptr)); - - Suite *tox = tox_suite(); - SRunner *test_runner = srunner_create(tox); - - int number_failed = 0; - srunner_run_all(test_runner, CK_NORMAL); - number_failed = srunner_ntests_failed(test_runner); - - srunner_free(test_runner); - - return number_failed; -} diff --git a/auto_tests/typing_test.c b/auto_tests/typing_test.c new file mode 100644 index 00000000..837ed757 --- /dev/null +++ b/auto_tests/typing_test.c @@ -0,0 +1,111 @@ +/* Tests that our typing notifications work. + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "check_compat.h" + +#include +#include +#include + +#include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" + +#include "helpers.h" + +static void typing_callback(Tox *m, uint32_t friendnumber, bool typing, void *userdata) +{ + bool *is_typing = (bool *)userdata; + *is_typing = typing; +} + +static void test_typing(void) +{ + 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(200); + } + + 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(200); + } + + printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); + + tox_callback_friend_typing(tox2, &typing_callback); + tox_self_set_typing(tox1, 0, true, nullptr); + + bool is_typing = false; + + while (!is_typing) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, &is_typing); + c_sleep(200); + } + + ck_assert_msg(tox_friend_get_typing(tox2, 0, nullptr) == 1, "Typing failure"); + tox_self_set_typing(tox1, 0, false, nullptr); + + while (is_typing) { + tox_iterate(tox1, nullptr); + tox_iterate(tox2, &is_typing); + c_sleep(200); + } + + TOX_ERR_FRIEND_QUERY err_t; + ck_assert_msg(tox_friend_get_typing(tox2, 0, &err_t) == 0, "Typing failure"); + ck_assert_msg(err_t == TOX_ERR_FRIEND_QUERY_OK, "Typing failure"); + + printf("test_typing succeeded, took %ld seconds\n", time(nullptr) - cur_time); + + tox_kill(tox1); + tox_kill(tox2); +} + +int main(int argc, char *argv[]) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_typing(); + return 0; +} -- cgit v1.2.3