Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/scenarios/scenario_lossless_packet_test.c
Line
Count
Source (jump to first uncovered line)
1
#include "framework/framework.h"
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
6
1
#define LOSSLESS_PACKET_FILLER 160
7
8
static void alice_script(ToxNode *self, void *ctx)
9
1
{
10
1
    Tox *tox = tox_node_get_tox(self);
11
1
    tox_node_log(self, "Waiting for connection...");
12
1
    WAIT_UNTIL(tox_node_is_self_connected(self));
13
1
    tox_node_log(self, "Self connected, waiting for friend Bob...");
14
1
    WAIT_UNTIL(tox_node_is_friend_connected(self, 0));
15
1
    tox_node_log(self, "Connected to Bob. Connection type: %u", tox_friend_get_connection_status(tox, 0, nullptr));
16
17
1
    const size_t packet_size = tox_max_custom_packet_size();
18
1
    uint8_t *packet = (uint8_t *)malloc(packet_size);
19
1
    memset(packet, LOSSLESS_PACKET_FILLER, packet_size);
20
21
1
    tox_node_log(self, "Sending lossless packet to Bob (size %zu)...", packet_size);
22
1
    Tox_Err_Friend_Custom_Packet err;
23
1
    if (tox_friend_send_lossless_packet(tox, 0, packet, packet_size, &err)) {
24
1
        tox_node_log(self, "Lossless packet sent successfully!");
25
1
    } else {
26
0
        tox_node_log(self, "Failed to send lossless packet: %u", err);
27
0
    }
28
1
    free(packet);
29
1
}
30
31
static void bob_script(ToxNode *self, void *ctx)
32
1
{
33
1
    const Tox *tox = tox_node_get_tox(self);
34
1
    tox_node_log(self, "Waiting for connection...");
35
1
    WAIT_UNTIL(tox_node_is_self_connected(self));
36
1
    tox_node_log(self, "Self connected, waiting for friend Alice...");
37
1
    WAIT_UNTIL(tox_node_is_friend_connected(self, 0));
38
1
    tox_node_log(self, "Connected to Alice. Connection type: %u", tox_friend_get_connection_status(tox, 0, nullptr));
39
40
1
    tox_node_log(self, "Waiting for lossless packet from Alice...");
41
1
    WAIT_FOR_EVENT(TOX_EVENT_FRIEND_LOSSLESS_PACKET);
42
1
    if (tox_scenario_is_running(self)) {
43
1
        tox_node_log(self, "Received lossless packet from Alice!");
44
1
    } else {
45
0
        tox_node_log(self, "Timed out waiting for lossless packet.");
46
0
    }
47
1
}
48
49
int main(int argc, char *argv[])
50
31
{
51
31
    ToxScenario *s = tox_scenario_new(argc, argv, 60000);
52
53
31
    ToxNode *alice = tox_scenario_add_node(s, "Alice", alice_script, nullptr, 0);
54
31
    ToxNode *bob = tox_scenario_add_node(s, "Bob", bob_script, nullptr, 0);
55
56
31
    tox_node_bootstrap(bob, alice);
57
31
    tox_node_friend_add(alice, bob);
58
31
    tox_node_friend_add(bob, alice);
59
60
31
    ToxScenarioStatus res = tox_scenario_run(s);
61
31
    if (res != TOX_SCENARIO_DONE) {
62
0
        tox_scenario_log(s, "Test failed with status %u", res);
63
0
        return 1;
64
0
    }
65
66
31
    tox_scenario_free(s);
67
31
    return 0;
68
31
}