|
| 1 | +/* Auto Tests: Many clients. |
| 2 | + */ |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include <time.h> |
| 8 | + |
| 9 | +#include "../testing/misc_tools.h" |
| 10 | +#include "../toxcore/tox.h" |
| 11 | +#include "../toxcore/tox_dispatch.h" |
| 12 | +#include "../toxcore/tox_events.h" |
| 13 | +#include "check_compat.h" |
| 14 | + |
| 15 | +static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Message *event, void *user_data) |
| 16 | +{ |
| 17 | + bool *success = (bool *)user_data; |
| 18 | + |
| 19 | + ck_assert(tox_event_friend_message_get_message_length(event) == sizeof("hello")); |
| 20 | + const uint8_t *msg = tox_event_friend_message_get_message(event); |
| 21 | + ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0, |
| 22 | + "message was not expected 'hello' but '%s'", (const char *)msg); |
| 23 | + |
| 24 | + *success = true; |
| 25 | +} |
| 26 | + |
| 27 | +static bool await_message(Tox **toxes, const Tox_Dispatch *dispatch) |
| 28 | +{ |
| 29 | + for (uint32_t i = 0; i < 100; ++i) { |
| 30 | + // Ignore events on tox 1. |
| 31 | + tox_events_free(tox_events_iterate(toxes[0], nullptr)); |
| 32 | + // Check if tox 2 got the message from tox 1. |
| 33 | + Tox_Events *events = tox_events_iterate(toxes[1], nullptr); |
| 34 | + |
| 35 | + bool success = false; |
| 36 | + tox_dispatch_invoke(dispatch, events, toxes[1], &success); |
| 37 | + tox_events_free(events); |
| 38 | + |
| 39 | + if (success) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + c_sleep(tox_iteration_interval(toxes[0])); |
| 44 | + } |
| 45 | + |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +static void test_tox_events(void) |
| 50 | +{ |
| 51 | + uint8_t message[sizeof("hello")]; |
| 52 | + memcpy(message, "hello", sizeof(message)); |
| 53 | + |
| 54 | + Tox *toxes[2]; |
| 55 | + uint32_t index[2]; |
| 56 | + |
| 57 | + for (uint32_t i = 0; i < 2; ++i) { |
| 58 | + index[i] = i + 1; |
| 59 | + toxes[i] = tox_new_log(nullptr, nullptr, &index[i]); |
| 60 | + tox_events_init(toxes[i]); |
| 61 | + ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i); |
| 62 | + } |
| 63 | + |
| 64 | + Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); |
| 65 | + ck_assert_msg(dispatch != nullptr, "failed to create event dispatcher"); |
| 66 | + |
| 67 | + tox_events_callback_friend_message(dispatch, handle_events_friend_message); |
| 68 | + |
| 69 | + uint8_t pk[TOX_PUBLIC_KEY_SIZE]; |
| 70 | + tox_self_get_dht_id(toxes[0], pk); |
| 71 | + tox_bootstrap(toxes[1], "localhost", tox_self_get_udp_port(toxes[0], nullptr), pk, nullptr); |
| 72 | + |
| 73 | + tox_self_get_public_key(toxes[0], pk); |
| 74 | + tox_friend_add_norequest(toxes[1], pk, nullptr); |
| 75 | + |
| 76 | + tox_self_get_public_key(toxes[1], pk); |
| 77 | + tox_friend_add_norequest(toxes[0], pk, nullptr); |
| 78 | + |
| 79 | + printf("bootstrapping and connecting 2 toxes\n"); |
| 80 | + |
| 81 | + while (tox_self_get_connection_status(toxes[0]) == TOX_CONNECTION_NONE || |
| 82 | + tox_self_get_connection_status(toxes[1]) == TOX_CONNECTION_NONE) { |
| 83 | + // Ignore connection events for now. |
| 84 | + tox_events_free(tox_events_iterate(toxes[0], nullptr)); |
| 85 | + tox_events_free(tox_events_iterate(toxes[1], nullptr)); |
| 86 | + |
| 87 | + c_sleep(tox_iteration_interval(toxes[0])); |
| 88 | + } |
| 89 | + |
| 90 | + printf("toxes online, waiting for friend connection\n"); |
| 91 | + |
| 92 | + while (tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_NONE || |
| 93 | + tox_friend_get_connection_status(toxes[1], 0, nullptr) == TOX_CONNECTION_NONE) { |
| 94 | + // Ignore connection events for now. |
| 95 | + tox_events_free(tox_events_iterate(toxes[0], nullptr)); |
| 96 | + tox_events_free(tox_events_iterate(toxes[1], nullptr)); |
| 97 | + |
| 98 | + c_sleep(tox_iteration_interval(toxes[0])); |
| 99 | + } |
| 100 | + |
| 101 | + printf("friends are connected via %s, now sending message\n", |
| 102 | + tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_TCP ? "TCP" : "UDP"); |
| 103 | + |
| 104 | + Tox_Err_Friend_Send_Message err; |
| 105 | + tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof(message), &err); |
| 106 | + ck_assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK); |
| 107 | + |
| 108 | + ck_assert(await_message(toxes, dispatch)); |
| 109 | + |
| 110 | + tox_dispatch_free(dispatch); |
| 111 | + |
| 112 | + for (uint32_t i = 0; i < 2; ++i) { |
| 113 | + tox_kill(toxes[i]); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +int main(void) |
| 118 | +{ |
| 119 | + setvbuf(stdout, nullptr, _IONBF, 0); |
| 120 | + test_tox_events(); |
| 121 | + return 0; |
| 122 | +} |
0 commit comments