Coverage Report

Created: 2025-10-08 19:34

/work/auto_tests/friend_request_test.c
Line
Count
Source
1
/* Tests that we can add friends.
2
 */
3
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <time.h>
8
9
#include "../toxcore/ccompat.h"
10
#include "../toxcore/tox.h"
11
#include "../toxcore/util.h"
12
#include "../testing/misc_tools.h"
13
14
#include "auto_test_support.h"
15
#include "check_compat.h"
16
17
2
#define REQUEST_MESSAGE "Hello, I would like to be your friend. Please respond."
18
19
typedef struct Callback_Data {
20
    Tox      *tox1;  // request sender
21
    Tox      *tox2;  // request receiver
22
    uint8_t  message[TOX_MAX_FRIEND_REQUEST_LENGTH];
23
    uint16_t length;
24
} Callback_Data;
25
26
static void accept_friend_request(const Tox_Event_Friend_Request *event, void *userdata)
27
3
{
28
3
    Callback_Data *cb_data = (Callback_Data *)userdata;
29
30
3
    const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
31
3
    const uint8_t *data = tox_event_friend_request_get_message(event);
32
3
    const size_t length = tox_event_friend_request_get_message_length(event);
33
34
3
    ck_assert_msg(length == cb_data->length && memcmp(cb_data->message, data, cb_data->length) == 0,
35
3
                  "unexpected friend request message");
36
37
3
    fprintf(stderr, "Tox2 accepts friend request.\n");
38
39
3
    Tox_Err_Friend_Add err;
40
3
    tox_friend_add_norequest(cb_data->tox2, public_key, &err);
41
42
3
    ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "tox_friend_add_norequest failed: %u", err);
43
3
}
44
45
static void iterate2_wait(const Tox_Dispatch *dispatch, Callback_Data *cb_data)
46
179
{
47
179
    Tox_Err_Events_Iterate err;
48
179
    Tox_Events *events;
49
50
179
    events = tox_events_iterate(cb_data->tox1, true, &err);
51
179
    ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
52
179
    tox_dispatch_invoke(dispatch, events, cb_data);
53
179
    tox_events_free(events);
54
55
179
    events = tox_events_iterate(cb_data->tox2, true, &err);
56
179
    ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
57
179
    tox_dispatch_invoke(dispatch, events, cb_data);
58
179
    tox_events_free(events);
59
60
179
    c_sleep(ITERATION_INTERVAL);
61
179
}
62
63
static void test_friend_request(const uint8_t *message, uint16_t length)
64
3
{
65
3
    printf("Initialising 2 toxes.\n");
66
3
    uint32_t index[] = { 1, 2 };
67
3
    const time_t cur_time = time(nullptr);
68
3
    Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
69
3
    Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
70
71
3
    ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
72
73
3
    tox_events_init(tox1);
74
3
    tox_events_init(tox2);
75
76
3
    printf("Bootstrapping Tox2 off Tox1.\n");
77
3
    uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
78
3
    tox_self_get_dht_id(tox1, dht_key);
79
3
    const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
80
81
3
    tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
82
83
3
    Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
84
3
    ck_assert(dispatch != nullptr);
85
86
3
    Callback_Data cb_data = {nullptr};
87
3
    cb_data.tox1 = tox1;
88
3
    cb_data.tox2 = tox2;
89
90
3
    ck_assert(length <= sizeof(cb_data.message));
91
3
    memcpy(cb_data.message, message, length);
92
3
    cb_data.length = length;
93
94
123
    do {
95
123
        iterate2_wait(dispatch, &cb_data);
96
123
    } while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
97
123
             tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE);
98
99
3
    printf("Toxes are online, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
100
3
    const time_t con_time = time(nullptr);
101
102
3
    printf("Tox1 adds Tox2 as friend. Waiting for Tox2 to accept.\n");
103
3
    tox_events_callback_friend_request(dispatch, accept_friend_request);
104
105
3
    uint8_t address[TOX_ADDRESS_SIZE];
106
3
    tox_self_get_address(tox2, address);
107
108
3
    Tox_Err_Friend_Add err;
109
3
    const uint32_t test = tox_friend_add(tox1, address, message, length, &err);
110
111
3
    ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "tox_friend_add failed: %u", err);
112
3
    ck_assert_msg(test == 0, "failed to add friend error code: %u", test);
113
114
56
    do {
115
56
        iterate2_wait(dispatch, &cb_data);
116
56
    } while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
117
56
             tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP);
118
119
3
    printf("Tox clients connected took %lu seconds.\n", (unsigned long)(time(nullptr) - con_time));
120
3
    printf("friend_request_test succeeded, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
121
122
3
    tox_dispatch_free(dispatch);
123
3
    tox_kill(tox1);
124
3
    tox_kill(tox2);
125
3
}
126
127
int main(void)
128
1
{
129
1
    setvbuf(stdout, nullptr, _IONBF, 0);
130
131
1
    fprintf(stderr, "Testing friend request with the smallest allowed message length.\n");
132
1
    test_friend_request((const uint8_t *)"a", 1);
133
134
1
    fprintf(stderr, "Testing friend request with an average sized message length.\n");
135
1
    test_friend_request((const uint8_t *)REQUEST_MESSAGE, sizeof(REQUEST_MESSAGE) - 1);
136
137
1
    uint8_t long_message[TOX_MAX_FRIEND_REQUEST_LENGTH];
138
139
922
    for (uint16_t i = 0; i < sizeof(long_message); ++i) {
140
921
        long_message[i] = 'a';
141
921
    }
142
143
1
    fprintf(stderr, "Testing friend request with the largest allowed message length.\n");
144
1
    test_friend_request(long_message, sizeof(long_message));
145
146
1
    return 0;
147
1
}