tomato/auto_tests/tox_dispatch_test.c
Green Sky 9ace11a0e2 Squashed 'external/toxcore/c-toxcore/' changes from f1df709b87..8f0d505f9a
8f0d505f9a feat: add ngc events
9b8216e70c refactor: Make event dispatch ordered by receive time.
814c12a6f4 cleanup: Add dynamically derived array sizes to the API.
226b23be12 cleanup: Add explicit array sizes to toxencryptsave.
ef33cb4de0 cleanup: Add Toxav alias for ToxAV.
1da723b34d cleanup: Make Tox_Options a typedef.
b148a2afff chore: Simplify msvc build using vcpkg.
5cac6d7eb1 cleanup: Move `tox_get_system` out of the public API.
c9ca4007e3 refactor: Align group message sending with other send functions.
6c6c0b1b1b cleanup: Make setters take non-const `Tox *`.
a76f758d70 cleanup: Mark arrays in the tox API as `[]` instead of `*`.
baf6d1f6cf cleanup: Make array params in toxav `[]` instead of `*`.
79f55bd06a cleanup: Put the size of fixed arrays into the API types.
1e73698db2 cleanup: Add typedefs for public API int identifiers.
cac074c57f chore: Add fetch-sha256 script to update bootstrap node hash.
32576656bb Make the comment capitalization uniform
aff4dda17c Spellcheck tox-bootstrapd
40b5fbbe9d chore: Remove settings.yml in favour of hs-github-tools.
ebafd51be7 chore: Use GPL license with https.
0e42752f0f cleanup: Move all vptr-to-ptr casts to the beginning of a function.
5407384211 cleanup: Use github actions matrix to simplify CI.
82d8265688 fix: Use QueryPerformanceCounter on windows for monotonic time.
1224e656e3 chore: Add `net_(new|kill)_strerror` to cppcheck's allocators.
6a90ddfe4e cleanup: Run clang-tidy on headers, as well.
bd930cc80a cleanup: Make TCP connection failures a warning instead of error.
fad6e4e173 cleanup: Make all .c files include the headers they need.
ef4897a898 cleanup: Upgrade to clang-tidy-17 and fix some warnings.
REVERT: f1df709b87 feat: add ngc events
REVERT: 1b6c907235 refactor: Make event dispatch ordered by receive time.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 8f0d505f9a598cc41c682178e1589bcc01efe9cb
2024-01-09 16:39:05 +01:00

191 lines
5.9 KiB
C

/* Auto Tests: Many clients.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../testing/misc_tools.h"
#include "../toxcore/tox.h"
#include "../toxcore/tox_dispatch.h"
#include "../toxcore/tox_events.h"
#include "../toxcore/tox_private.h"
#include "../toxcore/tox_unpack.h"
#include "auto_test_support.h"
#include "check_compat.h"
// Set to true to produce an msgpack file at /tmp/test.mp.
static const bool want_dump_events = false;
static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Message *event, void *user_data)
{
bool *success = (bool *)user_data;
ck_assert(tox_event_friend_message_get_message_length(event) == sizeof("hello"));
const uint8_t *msg = tox_event_friend_message_get_message(event);
ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0,
"message was not expected 'hello' but '%s'", (const char *)msg);
*success = true;
}
static void dump_events(const char *path, const Tox_Events *events)
{
FILE *fh = fopen(path, "w");
ck_assert(fh != nullptr);
const uint32_t len = tox_events_bytes_size(events);
uint8_t *buf = (uint8_t *)malloc(len);
ck_assert(buf != nullptr);
ck_assert(tox_events_get_bytes(events, buf));
fwrite(buf, 1, len, fh);
free(buf);
fclose(fh);
}
static void print_events(const Tox_System *sys, Tox_Events *events)
{
const uint32_t size = tox_events_bytes_size(events);
uint8_t *bytes1 = (uint8_t *)malloc(size);
uint8_t *bytes2 = (uint8_t *)malloc(size);
ck_assert(bytes1 != nullptr);
ck_assert(bytes2 != nullptr);
ck_assert(tox_events_get_bytes(events, bytes1));
ck_assert(tox_events_get_bytes(events, bytes2));
// Make sure get_bytes is deterministic.
ck_assert(memcmp(bytes1, bytes2, size) == 0);
Tox_Events *events_copy = tox_events_load(sys, bytes1, size);
ck_assert(events_copy != nullptr);
free(bytes1);
free(bytes2);
ck_assert(tox_events_equal(sys, events, events_copy));
tox_events_free(events_copy);
tox_events_free(events);
}
static bool await_message(Tox **toxes, const Tox_Dispatch *dispatch)
{
const Tox_System *sys = tox_get_system(toxes[0]);
for (uint32_t i = 0; i < 100; ++i) {
// Ignore events on tox 1.
print_events(sys, tox_events_iterate(toxes[0], false, nullptr));
// Check if tox 2 got the message from tox 1.
Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr);
if (want_dump_events) {
dump_events("/tmp/test.mp", events);
}
bool success = false;
tox_dispatch_invoke(dispatch, events, toxes[1], &success);
print_events(sys, events);
if (success) {
return true;
}
c_sleep(tox_iteration_interval(toxes[0]));
}
return false;
}
static void test_tox_events(void)
{
uint8_t message[sizeof("hello")];
memcpy(message, "hello", sizeof(message));
Tox *toxes[2];
uint32_t index[2];
for (uint32_t i = 0; i < 2; ++i) {
index[i] = i + 1;
toxes[i] = tox_new_log(nullptr, nullptr, &index[i]);
tox_events_init(toxes[i]);
ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
}
const Tox_System *sys = tox_get_system(toxes[0]);
Tox_Err_Dispatch_New err_new;
Tox_Dispatch *dispatch = tox_dispatch_new(&err_new);
ck_assert_msg(dispatch != nullptr, "failed to create event dispatcher");
ck_assert(err_new == TOX_ERR_DISPATCH_NEW_OK);
tox_events_callback_friend_message(dispatch, handle_events_friend_message);
uint8_t pk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(toxes[0], pk);
tox_bootstrap(toxes[1], "localhost", tox_self_get_udp_port(toxes[0], nullptr), pk, nullptr);
tox_self_get_public_key(toxes[0], pk);
tox_friend_add_norequest(toxes[1], pk, nullptr);
tox_self_get_public_key(toxes[1], pk);
tox_friend_add_norequest(toxes[0], pk, nullptr);
printf("bootstrapping and connecting 2 toxes\n");
while (tox_self_get_connection_status(toxes[0]) == TOX_CONNECTION_NONE ||
tox_self_get_connection_status(toxes[1]) == TOX_CONNECTION_NONE) {
// Ignore connection events for now.
print_events(sys, tox_events_iterate(toxes[0], false, nullptr));
print_events(sys, tox_events_iterate(toxes[1], false, nullptr));
c_sleep(tox_iteration_interval(toxes[0]));
}
printf("toxes online, waiting for friend connection\n");
while (tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_NONE ||
tox_friend_get_connection_status(toxes[1], 0, nullptr) == TOX_CONNECTION_NONE) {
// Ignore connection events for now.
print_events(sys, tox_events_iterate(toxes[0], false, nullptr));
print_events(sys, tox_events_iterate(toxes[1], false, nullptr));
c_sleep(tox_iteration_interval(toxes[0]));
}
printf("friends are connected via %s, now sending message\n",
tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_TCP ? "TCP" : "UDP");
Tox_Err_Friend_Send_Message err;
tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof(message), &err);
ck_assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK);
ck_assert(await_message(toxes, dispatch));
tox_dispatch_free(dispatch);
for (uint32_t i = 0; i < 2; ++i) {
tox_kill(toxes[i]);
}
}
static void fake_test_unpack(void)
{
// TODO(Green-Sky): add proper unpack tests and/or implement ngc events
(void)tox_group_privacy_state_unpack;
(void)tox_group_privacy_state_unpack;
(void)tox_group_voice_state_unpack;
(void)tox_group_topic_lock_unpack;
(void)tox_group_join_fail_unpack;
(void)tox_group_mod_event_unpack;
(void)tox_group_exit_type_unpack;
}
int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);
test_tox_events();
fake_test_unpack();
return 0;
}