Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/scenarios/scenario_tox_many_test.c
Line
Count
Source (jump to first uncovered line)
1
#include "framework/framework.h"
2
#include <stdio.h>
3
#include <string.h>
4
#include <stdlib.h>
5
6
512
#define NUM_TOXES 90
7
51
#define NUM_FRIEND_PAIRS 50
8
186
#define FR_MESSAGE "Gentoo"
9
10
typedef struct {
11
    uint32_t friend_count;
12
} State;
13
14
static void on_friend_request(const Tox_Event_Friend_Request *event, void *user_data)
15
136
{
16
136
    ToxNode *self = (ToxNode *)user_data;
17
136
    const uint8_t *msg = tox_event_friend_request_get_message(event);
18
136
    size_t len = tox_event_friend_request_get_message_length(event);
19
20
136
    if (len == 6 && memcmp(msg, FR_MESSAGE, 6) == 0) {
21
136
        tox_friend_add_norequest(tox_node_get_tox(self), tox_event_friend_request_get_public_key(event), nullptr);
22
136
    }
23
136
}
24
25
static void peer_script(ToxNode *self, void *ctx)
26
90
{
27
90
    State *state = (State *)ctx;
28
90
    tox_events_callback_friend_request(tox_node_get_dispatch(self), on_friend_request);
29
30
90
    tox_node_wait_for_self_connected(self);
31
32
    // Wait until we have the expected number of friends connected
33
2.51k
    while (1) {
34
2.51k
        uint32_t connected_friends = 0;
35
2.51k
        uint32_t total_friends = tox_self_get_friend_list_size(tox_node_get_tox(self));
36
6.21k
        for (uint32_t i = 0; i < total_friends; ++i) {
37
3.70k
            if (tox_node_is_friend_connected(self, i)) {
38
467
                connected_friends++;
39
467
            }
40
3.70k
        }
41
2.51k
        if (connected_friends >= state->friend_count) {
42
90
            break;
43
90
        }
44
2.42k
        tox_scenario_yield(self);
45
2.42k
    }
46
47
90
    tox_node_log(self, "All %u friends connected.", state->friend_count);
48
90
}
49
50
int main(int argc, char *argv[])
51
1
{
52
1
    ToxScenario *s = tox_scenario_new(argc, argv, 300000);
53
1
    State states[NUM_TOXES] = {0};
54
1
    ToxNode *nodes[NUM_TOXES];
55
56
91
    for (int i = 0; i < NUM_TOXES; ++i) {
57
90
        char alias[16];
58
90
        snprintf(alias, sizeof(alias), "Tox%d", i);
59
90
        nodes[i] = tox_scenario_add_node(s, alias, peer_script, &states[i], sizeof(State));
60
90
    }
61
62
91
    for (int i = 0; i < NUM_TOXES; ++i) {
63
90
        tox_node_bootstrap(nodes[i], nodes[(i + 1) % NUM_TOXES]);
64
90
        tox_node_bootstrap(nodes[(i + 1) % NUM_TOXES], nodes[i]);
65
90
    }
66
67
1
    struct {
68
1
        uint16_t t1;
69
1
        uint16_t t2;
70
1
    } pairs[NUM_FRIEND_PAIRS];
71
72
    // Generate friend pairs
73
51
    for (int i = 0; i < NUM_FRIEND_PAIRS; ++i) {
74
50
        bool unique;
75
50
        do {
76
50
            unique = true;
77
50
            pairs[i].t1 = rand() % NUM_TOXES;
78
50
            pairs[i].t2 = (pairs[i].t1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES;
79
80
            // Avoid reciprocal or duplicate pairs
81
1.27k
            for (int j = 0; j < i; ++j) {
82
1.22k
                if ((pairs[j].t1 == pairs[i].t1 && pairs[j].t2 == pairs[i].t2) ||
83
1.22k
                        (pairs[j].t1 == pairs[i].t2 && pairs[j].t2 == pairs[i].t1)) {
84
0
                    unique = false;
85
0
                    break;
86
0
                }
87
1.22k
            }
88
50
        } while (!unique);
89
90
50
        const int t1 = pairs[i].t1;
91
50
        const int t2 = pairs[i].t2;
92
93
50
        uint8_t addr[TOX_ADDRESS_SIZE];
94
50
        tox_self_get_address(tox_node_get_tox(nodes[t1]), addr);
95
96
50
        Tox_Err_Friend_Add err;
97
50
        tox_friend_add(tox_node_get_tox(nodes[t2]), addr, (const uint8_t *)FR_MESSAGE, 6, &err);
98
50
        ck_assert(err == TOX_ERR_FRIEND_ADD_OK);
99
100
50
        ++states[t1].friend_count;
101
50
        ++states[t2].friend_count;
102
103
        // Bootstrap off each other
104
50
        tox_node_bootstrap(nodes[t2], nodes[t1]);
105
50
        tox_node_bootstrap(nodes[t1], nodes[t2]);
106
50
    }
107
108
1
    ToxScenarioStatus res = tox_scenario_run(s);
109
1
    if (res != TOX_SCENARIO_DONE) {
110
0
        tox_scenario_log(s, "Test failed with status %u", res);
111
0
        return 1;
112
0
    }
113
114
1
    tox_scenario_free(s);
115
1
    return 0;
116
1
}
117
118
#undef FR_MESSAGE
119
#undef NUM_TOXES
120
#undef NUM_FRIEND_PAIRS