Coverage Report

Created: 2025-10-08 19:34

/work/auto_tests/friend_request_spam_test.c
Line
Count
Source
1
/* Tests what happens when spamming friend requests from lots of temporary toxes.
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
#include "auto_test_support.h"
14
#include "check_compat.h"
15
16
62
#define FR_MESSAGE "Gentoo"
17
// TODO(iphydf): Investigate friend request spam: receiving more than 32 at a time means any further
18
// friend requests are dropped on the floor and aren't seen again.
19
618
#define FR_TOX_COUNT 33
20
21
typedef struct State {
22
    bool unused;
23
} State;
24
25
static void accept_friend_request(const Tox_Event_Friend_Request *event,
26
                                  void *userdata)
27
31
{
28
31
    AutoTox *autotox = (AutoTox *)userdata;
29
30
31
    const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
31
31
    const uint8_t *data = tox_event_friend_request_get_message(event);
32
31
    const size_t length = tox_event_friend_request_get_message_length(event);
33
34
31
    ck_assert_msg(length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0,
35
31
                  "unexpected friend request message");
36
31
    tox_friend_add_norequest(autotox->tox, public_key, nullptr);
37
31
}
38
39
static void test_friend_request(AutoTox *autotoxes)
40
1
{
41
1
    const time_t con_time = time(nullptr);
42
43
1
    printf("All toxes add tox1 as friend.\n");
44
1
    tox_events_callback_friend_request(autotoxes[0].dispatch, accept_friend_request);
45
46
1
    uint8_t address[TOX_ADDRESS_SIZE];
47
1
    tox_self_get_address(autotoxes[0].tox, address);
48
49
32
    for (uint32_t i = 2; i < FR_TOX_COUNT; ++i) {
50
31
        Tox_Err_Friend_Add err;
51
31
        tox_friend_add(autotoxes[i].tox, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), &err);
52
31
        ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "tox %u failed to add friend error code: %u", autotoxes[i].index, err);
53
31
    }
54
55
21
    for (uint32_t t = 0; t < 100; ++t) {
56
21
        if (all_friends_connected(autotoxes, FR_TOX_COUNT)) {
57
1
            break;
58
1
        }
59
60
20
        iterate_all_wait(autotoxes, FR_TOX_COUNT, ITERATION_INTERVAL);
61
20
    }
62
63
1
    const size_t size = tox_self_get_friend_list_size(autotoxes[0].tox);
64
1
    printf("Tox clients connected took %lu seconds; tox1 has %u friends.\n",
65
1
           (unsigned long)(time(nullptr) - con_time), (unsigned int)size);
66
1
}
67
68
int main(void)
69
545
{
70
545
    setvbuf(stdout, nullptr, _IONBF, 0);
71
72
545
    Run_Auto_Options options = default_run_auto_options();
73
545
    options.graph = GRAPH_LINEAR;
74
545
    run_auto_test(nullptr, FR_TOX_COUNT, test_friend_request, sizeof(State), &options);
75
76
545
    return 0;
77
545
}