Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/scenarios/scenario_conference_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
    uint32_t conf_num;
8
    bool peer_joined;
9
} AliceState;
10
11
static void on_peer_list_changed(const Tox_Event_Conference_Peer_List_Changed *event, void *user_data)
12
1
{
13
1
    ToxNode *self = (ToxNode *)user_data;
14
1
    AliceState *state = (AliceState *)tox_node_get_script_ctx(self);
15
1
    if (tox_conference_peer_count(tox_node_get_tox(self), state->conf_num, nullptr) == 2) {
16
1
        state->peer_joined = true;
17
1
    }
18
1
}
19
20
static void alice_script(ToxNode *self, void *ctx)
21
1
{
22
1
    AliceState *state = (AliceState *)ctx;
23
1
    tox_events_callback_conference_peer_list_changed(tox_node_get_dispatch(self), on_peer_list_changed);
24
25
1
    tox_node_wait_for_self_connected(self);
26
1
    tox_node_wait_for_friend_connected(self, 0);
27
28
1
    Tox *tox = tox_node_get_tox(self);
29
1
    Tox_Err_Conference_New err_new;
30
1
    state->conf_num = tox_conference_new(tox, &err_new);
31
1
    ck_assert(err_new == TOX_ERR_CONFERENCE_NEW_OK);
32
33
1
    tox_node_log(self, "Created conference %u", state->conf_num);
34
1
    tox_conference_invite(tox, 0, state->conf_num, nullptr);
35
36
1
    WAIT_UNTIL(state->peer_joined);
37
1
    tox_node_log(self, "Bob joined. Querying peer info...");
38
39
    // Test tox_conference_peer_number_is_ours
40
    // Our own peer number should be 0 or 1.
41
1
    Tox_Err_Conference_Peer_Query q_err;
42
1
    bool p0_ours = tox_conference_peer_number_is_ours(tox, state->conf_num, 0, &q_err);
43
1
    ck_assert(q_err == TOX_ERR_CONFERENCE_PEER_QUERY_OK);
44
1
    bool p1_ours = tox_conference_peer_number_is_ours(tox, state->conf_num, 1, &q_err);
45
1
    ck_assert(q_err == TOX_ERR_CONFERENCE_PEER_QUERY_OK);
46
47
1
    ck_assert(p0_ours != p1_ours); // One must be ours, the other must not be.
48
49
1
    uint32_t our_peer_num = p0_ours ? 0 : 1;
50
1
    uint32_t bobs_peer_num = p0_ours ? 1 : 0;
51
52
    // Test tox_conference_peer_get_public_key
53
1
    uint8_t our_pk[TOX_PUBLIC_KEY_SIZE];
54
1
    uint8_t our_self_pk[TOX_PUBLIC_KEY_SIZE];
55
1
    tox_conference_peer_get_public_key(tox, state->conf_num, our_peer_num, our_pk, nullptr);
56
1
    tox_self_get_public_key(tox, our_self_pk);
57
1
    ck_assert(memcmp(our_pk, our_self_pk, TOX_PUBLIC_KEY_SIZE) == 0);
58
59
1
    uint8_t bobs_pk[TOX_PUBLIC_KEY_SIZE];
60
1
    uint8_t bobs_self_pk[TOX_PUBLIC_KEY_SIZE];
61
1
    tox_conference_peer_get_public_key(tox, state->conf_num, bobs_peer_num, bobs_pk, nullptr);
62
63
1
    ToxNode *bob_node = tox_scenario_get_node(tox_node_get_scenario(self), 1);
64
1
    tox_self_get_public_key(tox_node_get_tox(bob_node), bobs_self_pk);
65
1
    ck_assert(memcmp(bobs_pk, bobs_self_pk, TOX_PUBLIC_KEY_SIZE) == 0);
66
67
    // Test tox_conference_get_type
68
1
    Tox_Err_Conference_Get_Type gt_err;
69
1
    Tox_Conference_Type type = tox_conference_get_type(tox, state->conf_num, &gt_err);
70
1
    ck_assert(gt_err == TOX_ERR_CONFERENCE_GET_TYPE_OK);
71
1
    ck_assert(type == TOX_CONFERENCE_TYPE_TEXT);
72
73
1
    tox_node_log(self, "Conference query tests passed!");
74
1
}
75
76
typedef struct {
77
    bool invited;
78
    uint8_t cookie[512];
79
    size_t cookie_len;
80
} BobState;
81
82
static void bob_on_conference_invite(const Tox_Event_Conference_Invite *event, void *user_data)
83
2
{
84
2
    ToxNode *self = (ToxNode *)user_data;
85
2
    BobState *state = (BobState *)tox_node_get_script_ctx(self);
86
2
    state->invited = true;
87
2
    state->cookie_len = tox_event_conference_invite_get_cookie_length(event);
88
2
    memcpy(state->cookie, tox_event_conference_invite_get_cookie(event), state->cookie_len);
89
2
}
90
91
static void bob_script(ToxNode *self, void *ctx)
92
9
{
93
9
    const BobState *state = (const BobState *)ctx;
94
9
    tox_events_callback_conference_invite(tox_node_get_dispatch(self), bob_on_conference_invite);
95
96
9
    tox_node_wait_for_self_connected(self);
97
9
    tox_node_wait_for_friend_connected(self, 0);
98
99
9
    WAIT_UNTIL(state->invited);
100
9
    tox_conference_join(tox_node_get_tox(self), 0, state->cookie, state->cookie_len, nullptr);
101
102
9
    ToxNode *alice = tox_scenario_get_node(tox_node_get_scenario(self), 0);
103
9
    WAIT_UNTIL(tox_node_is_finished(alice));
104
9
}
105
106
int main(int argc, char *argv[])
107
31
{
108
31
    ToxScenario *s = tox_scenario_new(argc, argv, 60000);
109
110
31
    AliceState alice_state = {0, false};
111
31
    BobState bob_state = {0};
112
113
31
    tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState));
114
31
    tox_scenario_add_node(s, "Bob", bob_script, &bob_state, sizeof(BobState));
115
116
31
    ToxNode *alice = tox_scenario_get_node(s, 0);
117
31
    ToxNode *bob = tox_scenario_get_node(s, 1);
118
119
31
    tox_node_bootstrap(alice, bob);
120
31
    tox_node_friend_add(alice, bob);
121
31
    tox_node_friend_add(bob, alice);
122
123
31
    ToxScenarioStatus res = tox_scenario_run(s);
124
31
    if (res != TOX_SCENARIO_DONE) {
125
0
        fprintf(stderr, "Scenario failed with status %u\n", res);
126
0
        return 1;
127
0
    }
128
129
31
    tox_scenario_free(s);
130
31
    return 0;
131
31
}