2018-02-18 08:57:45 +08:00
|
|
|
/* Tests that we can add friends.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-07-17 06:46:02 +08:00
|
|
|
#include <string.h>
|
2018-02-18 08:57:45 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "../toxcore/ccompat.h"
|
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#include "../toxcore/util.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2022-02-22 01:34:02 +08:00
|
|
|
#include "auto_test_support.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "check_compat.h"
|
2018-02-18 08:57:45 +08:00
|
|
|
|
|
|
|
#define FR_MESSAGE "Gentoo"
|
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
static void accept_friend_request(Tox *tox, const Tox_Event_Friend_Request *event,
|
2018-02-18 08:57:45 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
2024-01-14 06:32:37 +08:00
|
|
|
const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
|
|
|
|
const uint8_t *data = tox_event_friend_request_get_message(event);
|
|
|
|
const size_t length = tox_event_friend_request_get_message_length(event);
|
2018-02-18 08:57:45 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-01-26 05:57:31 +08:00
|
|
|
static void iterate2_wait(const Tox_Dispatch *dispatch, Tox *tox1, Tox *tox2)
|
2024-01-14 06:32:37 +08:00
|
|
|
{
|
|
|
|
Tox_Err_Events_Iterate err;
|
|
|
|
Tox_Events *events;
|
|
|
|
|
|
|
|
events = tox_events_iterate(tox1, true, &err);
|
|
|
|
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
|
|
|
|
tox_dispatch_invoke(dispatch, events, tox1, nullptr);
|
|
|
|
tox_events_free(events);
|
|
|
|
|
|
|
|
events = tox_events_iterate(tox2, true, &err);
|
|
|
|
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
|
|
|
|
tox_dispatch_invoke(dispatch, events, tox2, nullptr);
|
|
|
|
tox_events_free(events);
|
|
|
|
|
|
|
|
c_sleep(ITERATION_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
static void test_friend_request(void)
|
|
|
|
{
|
2018-07-26 07:19:05 +08:00
|
|
|
printf("Initialising 2 toxes.\n");
|
2018-02-18 08:57:45 +08:00
|
|
|
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");
|
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
tox_events_init(tox1);
|
|
|
|
tox_events_init(tox2);
|
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
printf("Bootstrapping tox2 off tox1.\n");
|
2018-02-18 08:57:45 +08:00
|
|
|
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);
|
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
|
|
|
|
ck_assert(dispatch != nullptr);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
do {
|
|
|
|
iterate2_wait(dispatch, tox1, tox2);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
|
|
|
|
tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-08-16 15:09:12 +08:00
|
|
|
printf("Toxes are online, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
|
2018-02-18 08:57:45 +08:00
|
|
|
const time_t con_time = time(nullptr);
|
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
printf("Tox1 adds tox2 as friend, tox2 accepts.\n");
|
2024-01-14 06:32:37 +08:00
|
|
|
tox_events_callback_friend_request(dispatch, accept_friend_request);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
|
|
|
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);
|
2018-08-05 03:18:31 +08:00
|
|
|
ck_assert_msg(test == 0, "failed to add friend error code: %u", test);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2024-01-14 06:32:37 +08:00
|
|
|
iterate2_wait(dispatch, tox1, tox2);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
|
|
|
|
tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-08-16 15:09:12 +08:00
|
|
|
printf("Tox clients connected took %lu seconds.\n", (unsigned long)(time(nullptr) - con_time));
|
|
|
|
printf("friend_request_test succeeded, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
tox_dispatch_free(dispatch);
|
2018-02-18 08:57:45 +08:00
|
|
|
tox_kill(tox1);
|
|
|
|
tox_kill(tox2);
|
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
|
|
|
|
test_friend_request();
|
|
|
|
return 0;
|
|
|
|
}
|