Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/scenarios/scenario_self_query_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
typedef struct {
7
    bool connection_status_called;
8
    Tox_Connection last_status;
9
} State;
10
11
static void on_self_connection_status(const Tox_Event_Self_Connection_Status *event, void *user_data)
12
1
{
13
1
    ToxNode *self = (ToxNode *)user_data;
14
1
    State *state = (State *)tox_node_get_script_ctx(self);
15
1
    state->connection_status_called = true;
16
1
    state->last_status = tox_event_self_connection_status_get_connection_status(event);
17
1
    tox_node_log(self, "Self connection status: %s", tox_connection_to_string(state->last_status));
18
1
}
19
20
static void alice_script(ToxNode *self, void *ctx)
21
1
{
22
1
    State *state = (State *)ctx;
23
24
    // Register dispatch callback
25
1
    tox_events_callback_self_connection_status(tox_node_get_dispatch(self), on_self_connection_status);
26
27
1
    tox_node_wait_for_self_connected(self);
28
1
    ck_assert(state->connection_status_called);
29
1
    ck_assert(state->last_status != TOX_CONNECTION_NONE);
30
31
1
    Tox *tox = tox_node_get_tox(self);
32
    // Test ports
33
1
    Tox_Err_Get_Port err_port;
34
1
    uint16_t udp_port = tox_self_get_udp_port(tox, &err_port);
35
1
    ck_assert(err_port == TOX_ERR_GET_PORT_OK);
36
1
    ck_assert(udp_port > 0);
37
1
    tox_node_log(self, "UDP Port: %u", udp_port);
38
39
    // Friend list test
40
1
    tox_node_wait_for_friend_connected(self, 0);
41
1
    tox_node_wait_for_friend_connected(self, 1);
42
43
1
    size_t friend_count = tox_self_get_friend_list_size(tox);
44
1
    ck_assert(friend_count == 2);
45
46
1
    uint32_t friends[2];
47
1
    tox_self_get_friend_list(tox, friends);
48
1
    ck_assert(friends[0] == 0);
49
1
    ck_assert(friends[1] == 1);
50
51
1
    tox_node_log(self, "Self query tests passed!");
52
1
}
53
54
static void peer_script(ToxNode *self, void *ctx)
55
2
{
56
2
    tox_node_wait_for_self_connected(self);
57
2
    ToxNode *alice = tox_scenario_get_node(tox_node_get_scenario(self), 0);
58
2
    WAIT_UNTIL(tox_node_is_finished(alice));
59
2
}
60
61
int main(int argc, char *argv[])
62
31
{
63
31
    ToxScenario *s = tox_scenario_new(argc, argv, 60000);
64
65
31
    State alice_state = {false, TOX_CONNECTION_NONE};
66
67
31
    tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(State));
68
31
    tox_scenario_add_node(s, "Bob", peer_script, nullptr, 0);
69
31
    tox_scenario_add_node(s, "Charlie", peer_script, nullptr, 0);
70
71
31
    ToxNode *alice = tox_scenario_get_node(s, 0);
72
31
    ToxNode *bob = tox_scenario_get_node(s, 1);
73
31
    ToxNode *charlie = tox_scenario_get_node(s, 2);
74
75
31
    tox_node_bootstrap(alice, bob);
76
31
    tox_node_bootstrap(charlie, bob);
77
78
31
    tox_node_friend_add(alice, bob);
79
31
    tox_node_friend_add(bob, alice);
80
31
    tox_node_friend_add(alice, charlie);
81
31
    tox_node_friend_add(charlie, alice);
82
83
31
    ToxScenarioStatus res = tox_scenario_run(s);
84
31
    if (res != TOX_SCENARIO_DONE) {
85
0
        fprintf(stderr, "Scenario failed with status %u\n", res);
86
0
        return 1;
87
0
    }
88
89
31
    tox_scenario_free(s);
90
31
    return 0;
91
31
}
92