Coverage Report

Created: 2025-10-08 19:34

/work/auto_tests/conference_av_test.c
Line
Count
Source (jump to first uncovered line)
1
/* Auto Tests: Conferences AV.
2
 */
3
4
#include <stdlib.h>
5
#include <string.h>
6
#include <time.h>
7
#include <stdint.h>
8
9
#include "../toxav/toxav.h"
10
#include "../toxcore/os_random.h"
11
#include "check_compat.h"
12
13
4.18k
#define NUM_AV_GROUP_TOX 16
14
17
#define NUM_AV_DISCONNECT (NUM_AV_GROUP_TOX / 2)
15
18
#define NUM_AV_DISABLE (NUM_AV_GROUP_TOX / 2)
16
17
#include "auto_test_support.h"
18
19
typedef struct State {
20
    bool invited_next;
21
22
    uint32_t received_audio_peers[NUM_AV_GROUP_TOX];
23
    uint32_t received_audio_num;
24
} State;
25
26
static void handle_self_connection_status(
27
    const Tox_Event_Self_Connection_Status *event, void *user_data)
28
50
{
29
50
    const AutoTox *autotox = (AutoTox *)user_data;
30
31
50
    const Tox_Connection connection_status = tox_event_self_connection_status_get_connection_status(event);
32
50
    if (connection_status != TOX_CONNECTION_NONE) {
33
25
        printf("tox #%u: is now connected\n", autotox->index);
34
25
    } else {
35
25
        printf("tox #%u: is now disconnected\n", autotox->index);
36
25
    }
37
50
}
38
39
static void handle_friend_connection_status(
40
    const Tox_Event_Friend_Connection_Status *event, void *user_data)
41
64
{
42
64
    const AutoTox *autotox = (AutoTox *)user_data;
43
44
64
    const uint32_t friendnumber = tox_event_friend_connection_status_get_friend_number(event);
45
64
    const Tox_Connection connection_status = tox_event_friend_connection_status_get_connection_status(event);
46
47
64
    if (connection_status != TOX_CONNECTION_NONE) {
48
32
        printf("tox #%u: is now connected to friend %u\n", autotox->index, friendnumber);
49
32
    } else {
50
32
        printf("tox #%u: is now disconnected from friend %u\n", autotox->index, friendnumber);
51
32
    }
52
64
}
53
54
static void audio_callback(void *tox, uint32_t groupnumber, uint32_t peernumber,
55
                           const int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t
56
                           sample_rate, void *user_data)
57
10.7k
{
58
10.7k
    if (samples == 0) {
59
0
        return;
60
0
    }
61
62
10.7k
    const AutoTox *autotox = (AutoTox *)user_data;
63
10.7k
    State *state = (State *)autotox->state;
64
65
81.0k
    for (uint32_t i = 0; i < state->received_audio_num; ++i) {
66
79.9k
        if (state->received_audio_peers[i] == peernumber) {
67
9.62k
            return;
68
9.62k
        }
69
79.9k
    }
70
71
1.08k
    ck_assert(state->received_audio_num < NUM_AV_GROUP_TOX);
72
73
1.08k
    state->received_audio_peers[state->received_audio_num] = peernumber;
74
1.08k
    ++state->received_audio_num;
75
1.08k
}
76
77
static void handle_conference_invite(
78
    const Tox_Event_Conference_Invite *event, void *user_data)
79
15
{
80
15
    const AutoTox *autotox = (AutoTox *)user_data;
81
82
15
    const uint32_t friend_number = tox_event_conference_invite_get_friend_number(event);
83
15
    const Tox_Conference_Type type = tox_event_conference_invite_get_type(event);
84
15
    const uint8_t *cookie = tox_event_conference_invite_get_cookie(event);
85
15
    const size_t length = tox_event_conference_invite_get_cookie_length(event);
86
87
15
    ck_assert_msg(type == TOX_CONFERENCE_TYPE_AV, "tox #%u: wrong conference type: %u", autotox->index, type);
88
89
15
    ck_assert_msg(toxav_join_av_groupchat(autotox->tox, friend_number, cookie, length, audio_callback, user_data) == 0,
90
15
                  "tox #%u: failed to join group", autotox->index);
91
15
}
92
93
static void handle_conference_connected(
94
    const Tox_Event_Conference_Connected *event, void *user_data)
95
30
{
96
30
    const AutoTox *autotox = (AutoTox *)user_data;
97
30
    State *state = (State *)autotox->state;
98
99
30
    if (state->invited_next || tox_self_get_friend_list_size(autotox->tox) <= 1) {
100
2
        return;
101
2
    }
102
103
28
    Tox_Err_Conference_Invite err;
104
28
    tox_conference_invite(autotox->tox, 1, 0, &err);
105
28
    ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox #%u failed to invite next friend: err = %u", autotox->index,
106
28
                  err);
107
28
    printf("tox #%u: invited next friend\n", autotox->index);
108
28
    state->invited_next = true;
109
28
}
110
111
static bool toxes_are_disconnected_from_group(uint32_t tox_count, AutoTox *autotoxes,
112
        const bool *disconnected)
113
232
{
114
232
    uint32_t num_disconnected = 0;
115
116
3.94k
    for (uint32_t i = 0; i < tox_count; ++i) {
117
3.71k
        num_disconnected += disconnected[i];
118
3.71k
    }
119
120
470
    for (uint32_t i = 0; i < tox_count; ++i) {
121
466
        if (disconnected[i]) {
122
214
            continue;
123
214
        }
124
125
252
        if (tox_conference_peer_count(autotoxes[i].tox, 0, nullptr) > tox_count - num_disconnected) {
126
228
            return false;
127
228
        }
128
252
    }
129
130
4
    return true;
131
232
}
132
133
static void disconnect_toxes(uint32_t tox_count, AutoTox *autotoxes,
134
                             const bool *disconnect, const bool *exclude)
135
2
{
136
    /* Fake a network outage for a set of peers D by iterating only the other
137
     * peers D' until the connections time out according to D', then iterating
138
     * only D until the connections time out according to D. */
139
140
2
    VLA(bool, disconnect_now, tox_count);
141
2
    bool invert = false;
142
143
4
    do {
144
68
        for (uint32_t i = 0; i < tox_count; ++i) {
145
64
            disconnect_now[i] = exclude[i] || (invert ^ disconnect[i]);
146
64
        }
147
148
232
        do {
149
3.94k
            for (uint32_t i = 0; i < tox_count; ++i) {
150
3.71k
                if (!disconnect_now[i]) {
151
1.39k
                    Tox_Err_Events_Iterate err;
152
1.39k
                    Tox_Events *events = tox_events_iterate(autotoxes[i].tox, true, &err);
153
1.39k
                    tox_dispatch_invoke(autotoxes[i].dispatch, events, &autotoxes[i]);
154
1.39k
                    tox_events_free(events);
155
1.39k
                    autotoxes[i].clock += 1000;
156
1.39k
                }
157
3.71k
            }
158
159
232
            c_sleep(20);
160
232
        } while (!toxes_are_disconnected_from_group(tox_count, autotoxes, disconnect_now));
161
162
4
        invert = !invert;
163
4
    } while (invert);
164
2
}
165
166
static bool all_connected_to_group(uint32_t tox_count, AutoTox *autotoxes)
167
126
{
168
175
    for (uint32_t i = 0; i < tox_count; ++i) {
169
173
        if (tox_conference_peer_count(autotoxes[i].tox, 0, nullptr) < tox_count) {
170
124
            return false;
171
124
        }
172
173
    }
173
174
2
    return true;
175
126
}
176
177
/**
178
 * returns a random index at which a list of booleans is false
179
 * (some such index is required to exist)
180
 */
181
static uint32_t random_false_index(const Random *rng, const bool *list, const uint32_t length)
182
24
{
183
24
    uint32_t index;
184
185
33
    do {
186
33
        index = random_u32(rng) % length;
187
33
    } while (list[index]);
188
189
24
    return index;
190
24
}
191
192
static bool all_got_audio(AutoTox *autotoxes, const bool *disabled)
193
48
{
194
48
    uint32_t num_disabled = 0;
195
196
816
    for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
197
768
        num_disabled += disabled[i];
198
768
    }
199
200
164
    for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
201
158
        const State *state = (const State *)autotoxes[i].state;
202
203
158
        if (disabled[i] ^ (state->received_audio_num
204
158
                           != NUM_AV_GROUP_TOX - num_disabled - 1)) {
205
42
            return false;
206
42
        }
207
158
    }
208
209
6
    return true;
210
48
}
211
212
static void reset_received_audio(AutoTox *autotoxes)
213
7
{
214
119
    for (uint32_t j = 0; j < NUM_AV_GROUP_TOX; ++j) {
215
112
        ((State *)autotoxes[j].state)->received_audio_num = 0;
216
112
    }
217
7
}
218
219
528
#define GROUP_AV_TEST_SAMPLES 960
220
221
/* must have
222
 * GROUP_AV_AUDIO_ITERATIONS - NUM_AV_GROUP_TOX >= 2^n >= GROUP_JBUF_SIZE
223
 * for some n, to give messages time to be relayed and to let the jitter
224
 * buffers fill up. */
225
51
#define GROUP_AV_AUDIO_ITERATIONS (8 + NUM_AV_GROUP_TOX)
226
227
static bool test_audio(AutoTox *autotoxes, const bool *disabled, bool quiet)
228
7
{
229
7
    if (!quiet) {
230
2
        printf("testing sending and receiving audio\n");
231
2
    }
232
233
7
    const int16_t pcm[GROUP_AV_TEST_SAMPLES] = {0};
234
235
7
    reset_received_audio(autotoxes);
236
237
49
    for (uint32_t n = 0; n < GROUP_AV_AUDIO_ITERATIONS; ++n) {
238
816
        for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
239
768
            if (disabled[i]) {
240
240
                continue;
241
240
            }
242
243
528
            if (toxav_group_send_audio(autotoxes[i].tox, 0, pcm, GROUP_AV_TEST_SAMPLES, 1, 48000) != 0) {
244
0
                if (!quiet) {
245
0
                    ck_abort_msg("#%u failed to send audio", autotoxes[i].index);
246
0
                }
247
248
0
                return false;
249
0
            }
250
528
        }
251
252
48
        iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL);
253
254
48
        if (all_got_audio(autotoxes, disabled)) {
255
6
            return true;
256
6
        }
257
48
    }
258
259
1
    if (!quiet) {
260
0
        ck_abort_msg("group failed to receive audio");
261
0
    }
262
263
1
    return false;
264
1
}
265
266
static void test_eventual_audio(AutoTox *autotoxes, const bool *disabled, uint64_t timeout)
267
2
{
268
2
    uint64_t start = autotoxes[0].clock;
269
270
2
    while (autotoxes[0].clock < start + timeout) {
271
2
        if (!test_audio(autotoxes, disabled, true)) {
272
0
            continue;
273
0
        }
274
275
        // It needs to succeed twice in a row for the test to pass.
276
2
        if (test_audio(autotoxes, disabled, true)) {
277
2
            printf("audio test successful after %d seconds\n", (int)((autotoxes[0].clock - start) / 1000));
278
2
            return;
279
2
        }
280
2
    }
281
282
0
    printf("audio seems not to be getting through: testing again with errors.\n");
283
0
    test_audio(autotoxes, disabled, false);
284
0
}
285
286
static void do_audio(AutoTox *autotoxes, uint32_t iterations)
287
1
{
288
1
    const int16_t pcm[GROUP_AV_TEST_SAMPLES] = {0};
289
1
    printf("running audio for %u iterations\n", iterations);
290
291
21
    for (uint32_t f = 0; f < iterations; ++f) {
292
340
        for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
293
320
            ck_assert_msg(toxav_group_send_audio(autotoxes[i].tox, 0, pcm, GROUP_AV_TEST_SAMPLES, 1, 48000) == 0,
294
320
                          "#%u failed to send audio", autotoxes[i].index);
295
320
            iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL);
296
320
        }
297
20
    }
298
1
}
299
300
// should agree with value in groupav.c
301
2
#define GROUP_JBUF_DEAD_SECONDS 4
302
303
2
#define JITTER_SETTLE_TIME (GROUP_JBUF_DEAD_SECONDS*1000 + NUM_AV_GROUP_TOX*ITERATION_INTERVAL*(GROUP_AV_AUDIO_ITERATIONS+1))
304
305
static void run_conference_tests(AutoTox *autotoxes)
306
1
{
307
1
    const Random *rng = os_random();
308
1
    ck_assert(rng != nullptr);
309
1
    bool disabled[NUM_AV_GROUP_TOX] = {0};
310
311
1
    test_audio(autotoxes, disabled, false);
312
313
    /* have everyone send audio for a bit so we can test that the audio
314
     * sequnums dropping to 0 on restart isn't a problem */
315
1
    do_audio(autotoxes, 20);
316
317
1
    printf("letting random toxes timeout\n");
318
1
    bool disconnected[NUM_AV_GROUP_TOX] = {0};
319
1
    bool restarting[NUM_AV_GROUP_TOX] = {0};
320
321
1
    ck_assert(NUM_AV_DISCONNECT < NUM_AV_GROUP_TOX);
322
323
9
    for (uint32_t i = 0; i < NUM_AV_DISCONNECT; ++i) {
324
8
        uint32_t disconnect = random_false_index(rng, disconnected, NUM_AV_GROUP_TOX);
325
8
        disconnected[disconnect] = true;
326
327
8
        if (i < NUM_AV_DISCONNECT / 2) {
328
4
            restarting[disconnect] = true;
329
4
            printf("Restarting #%u\n", autotoxes[disconnect].index);
330
4
        } else {
331
4
            printf("Disconnecting #%u\n", autotoxes[disconnect].index);
332
4
        }
333
8
    }
334
335
17
    for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
336
16
        if (restarting[i]) {
337
4
            save_autotox(&autotoxes[i]);
338
4
            kill_autotox(&autotoxes[i]);
339
4
        }
340
16
    }
341
342
1
    disconnect_toxes(NUM_AV_GROUP_TOX, autotoxes, disconnected, restarting);
343
344
17
    for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
345
16
        if (restarting[i]) {
346
4
            reload(&autotoxes[i]);
347
4
        }
348
16
    }
349
350
1
    printf("reconnecting toxes\n");
351
352
61
    do {
353
61
        iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL);
354
61
    } while (!all_connected_to_group(NUM_AV_GROUP_TOX, autotoxes));
355
356
17
    for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
357
16
        if (restarting[i]) {
358
4
            ck_assert_msg(!toxav_groupchat_av_enabled(autotoxes[i].tox, 0),
359
4
                          "#%u restarted but av enabled", autotoxes[i].index);
360
4
            ck_assert_msg(toxav_groupchat_enable_av(autotoxes[i].tox, 0, audio_callback, &autotoxes[i]) == 0,
361
4
                          "#%u failed to re-enable av", autotoxes[i].index);
362
4
            ck_assert_msg(toxav_groupchat_av_enabled(autotoxes[i].tox, 0),
363
4
                          "#%u av not enabled even after enabling", autotoxes[i].index);
364
4
        }
365
16
    }
366
367
1
    printf("testing audio\n");
368
369
    /* Allow time for the jitter buffers to reset and for the group to become
370
     * connected enough for lossy messages to get through
371
     * (all_connected_to_group() only checks lossless connectivity, which is a
372
     * looser condition). */
373
1
    test_eventual_audio(autotoxes, disabled, JITTER_SETTLE_TIME + NUM_AV_GROUP_TOX * 1000);
374
375
1
    printf("testing disabling av\n");
376
377
1
    ck_assert(NUM_AV_DISABLE < NUM_AV_GROUP_TOX);
378
379
9
    for (uint32_t i = 0; i < NUM_AV_DISABLE; ++i) {
380
8
        uint32_t disable = random_false_index(rng, disabled, NUM_AV_GROUP_TOX);
381
8
        disabled[disable] = true;
382
8
        printf("Disabling #%u\n", autotoxes[disable].index);
383
8
        ck_assert_msg(toxav_groupchat_enable_av(autotoxes[disable].tox, 0, audio_callback, &autotoxes[disable]) != 0,
384
8
                      "#%u could enable already enabled av!", autotoxes[i].index);
385
8
        ck_assert_msg(toxav_groupchat_disable_av(autotoxes[disable].tox, 0) == 0,
386
8
                      "#%u failed to disable av", autotoxes[i].index);
387
8
    }
388
389
    // Run test without error to clear out messages from now-disabled peers.
390
1
    test_audio(autotoxes, disabled, true);
391
392
1
    printf("testing audio with some peers having disabled their av\n");
393
1
    test_audio(autotoxes, disabled, false);
394
395
9
    for (uint32_t i = 0; i < NUM_AV_DISABLE; ++i) {
396
8
        if (!disabled[i]) {
397
5
            continue;
398
5
        }
399
400
3
        disabled[i] = false;
401
3
        ck_assert_msg(toxav_groupchat_disable_av(autotoxes[i].tox, 0) != 0,
402
3
                      "#%u could disable already disabled av!", autotoxes[i].index);
403
3
        ck_assert_msg(!toxav_groupchat_av_enabled(autotoxes[i].tox, 0),
404
3
                      "#%u av enabled after disabling", autotoxes[i].index);
405
3
        ck_assert_msg(toxav_groupchat_enable_av(autotoxes[i].tox, 0, audio_callback, &autotoxes[i]) == 0,
406
3
                      "#%u failed to re-enable av", autotoxes[i].index);
407
3
    }
408
409
1
    printf("testing audio after re-enabling all av\n");
410
1
    test_eventual_audio(autotoxes, disabled, JITTER_SETTLE_TIME);
411
1
}
412
413
static void test_groupav(AutoTox *autotoxes)
414
1
{
415
1
    const time_t test_start_time = time(nullptr);
416
417
17
    for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
418
16
        tox_events_callback_self_connection_status(autotoxes[i].dispatch, handle_self_connection_status);
419
16
        tox_events_callback_friend_connection_status(autotoxes[i].dispatch, handle_friend_connection_status);
420
16
        tox_events_callback_conference_invite(autotoxes[i].dispatch, handle_conference_invite);
421
16
        tox_events_callback_conference_connected(autotoxes[i].dispatch, handle_conference_connected);
422
16
    }
423
424
1
    ck_assert_msg(toxav_add_av_groupchat(autotoxes[0].tox, audio_callback, &autotoxes[0]) != -1,
425
1
                  "failed to create group");
426
1
    printf("tox #%u: inviting its first friend\n", autotoxes[0].index);
427
1
    ck_assert_msg(tox_conference_invite(autotoxes[0].tox, 0, 0, nullptr) != 0, "failed to invite friend");
428
1
    ((State *)autotoxes[0].state)->invited_next = true;
429
430
1
    printf("waiting for invitations to be made\n");
431
1
    uint32_t invited_count = 0;
432
433
15
    do {
434
15
        iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL);
435
436
15
        invited_count = 0;
437
438
255
        for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
439
240
            invited_count += ((State *)autotoxes[i].state)->invited_next;
440
240
        }
441
15
    } while (invited_count != NUM_AV_GROUP_TOX - 1);
442
443
1
    uint64_t pregroup_clock = autotoxes[0].clock;
444
1
    printf("waiting for all toxes to be in the group\n");
445
1
    uint32_t fully_connected_count = 0;
446
447
8
    do {
448
8
        fully_connected_count = 0;
449
8
        iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL);
450
451
136
        for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
452
128
            Tox_Err_Conference_Peer_Query err;
453
128
            uint32_t peer_count = tox_conference_peer_count(autotoxes[i].tox, 0, &err);
454
455
128
            if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
456
0
                peer_count = 0;
457
0
            }
458
459
128
            fully_connected_count += peer_count == NUM_AV_GROUP_TOX;
460
128
        }
461
8
    } while (fully_connected_count != NUM_AV_GROUP_TOX);
462
463
1
    printf("group connected, took %d seconds\n", (int)((autotoxes[0].clock - pregroup_clock) / 1000));
464
465
1
    run_conference_tests(autotoxes);
466
467
1
    printf("test_many_group succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time));
468
1
}
469
470
int main(void)
471
545
{
472
545
    setvbuf(stdout, nullptr, _IONBF, 0);
473
474
545
    Run_Auto_Options options = default_run_auto_options();
475
545
    options.graph = GRAPH_LINEAR;
476
477
545
    run_auto_test(nullptr, NUM_AV_GROUP_TOX, test_groupav, sizeof(State), &options);
478
479
545
    return 0;
480
545
}