Coverage Report

Created: 2024-01-26 01:52

/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
794
#define FR_TOX_COUNT 33
20
21
typedef struct State {
22
    bool unused;
23
} State;
24
25
static void accept_friend_request(Tox *tox, const Tox_Event_Friend_Request *event,
26
                                  void *userdata)
27
32
{
28
32
    const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
29
32
    const uint8_t *data = tox_event_friend_request_get_message(event);
30
32
    const size_t length = tox_event_friend_request_get_message_length(event);
31
32
32
    ck_assert_msg(length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0,
33
32
                  "unexpected friend request message");
34
32
    tox_friend_add_norequest(tox, public_key, nullptr);
35
32
}
36
37
static void test_friend_request(AutoTox *autotoxes)
38
1
{
39
1
    const time_t con_time = time(nullptr);
40
41
1
    printf("All toxes add tox1 as friend.\n");
42
1
    tox_events_callback_friend_request(autotoxes[0].dispatch, accept_friend_request);
43
44
1
    uint8_t address[TOX_ADDRESS_SIZE];
45
1
    tox_self_get_address(autotoxes[0].tox, address);
46
47
32
    for (uint32_t i = 2; i < FR_TOX_COUNT; ++i) {
48
31
        Tox_Err_Friend_Add err;
49
31
        tox_friend_add(autotoxes[i].tox, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), &err);
50
31
        ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "tox %u failed to add friend error code: %d", autotoxes[i].index, err);
51
31
    }
52
53
21
    for (uint32_t t = 0; t < 100; ++t) {
54
21
        if (all_friends_connected(autotoxes, FR_TOX_COUNT)) {
55
1
            break;
56
1
        }
57
58
20
        iterate_all_wait(autotoxes, FR_TOX_COUNT, ITERATION_INTERVAL);
59
20
    }
60
61
1
    const size_t size = tox_self_get_friend_list_size(autotoxes[0].tox);
62
1
    printf("Tox clients connected took %lu seconds; tox1 has %u friends.\n",
63
1
           (unsigned long)(time(nullptr) - con_time), (unsigned int)size);
64
1
}
65
66
int main(void)
67
721
{
68
721
    setvbuf(stdout, nullptr, _IONBF, 0);
69
70
721
    Run_Auto_Options options = default_run_auto_options();
71
721
    options.graph = GRAPH_LINEAR;
72
721
    run_auto_test(nullptr, FR_TOX_COUNT, test_friend_request, sizeof(State), &options);
73
74
721
    return 0;
75
721
}