Debian Codegraph @

auto_tests/scenarios/scenario_conference_offline_test.c

raw symbols SCIP
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 bob_joined;
9 } AliceState;
10
11 static void alice_on_peer_list_changed(const Tox_Event_Conference_Peer_List_Changed *event, void *user_data)
12 {
13 ToxNode *self = (ToxNode *)user_data;
14 AliceState *state = (AliceState *)tox_node_get_script_ctx(self);
15 uint32_t conf_num = tox_event_conference_peer_list_changed_get_conference_number(event);
16
17 if (conf_num != state->conf_num) {
18 return;
19 }
20
21 uint32_t count = tox_conference_peer_count(tox_node_get_tox(self), conf_num, nullptr);
22 if (count == 2) {
23 state->bob_joined = true;
24 }
25 }
26
27 static void alice_script(ToxNode *self, void *ctx)
28 {
29 AliceState *state = (AliceState *)ctx;
30 tox_events_callback_conference_peer_list_changed(tox_node_get_dispatch(self), alice_on_peer_list_changed);
31
32 tox_node_wait_for_self_connected(self);
33 tox_node_wait_for_friend_connected(self, 0);
34
35 Tox *tox = tox_node_get_tox(self);
36 Tox_Err_Conference_New err_new;
37 state->conf_num = tox_conference_new(tox, &err_new);
38 ck_assert(err_new == TOX_ERR_CONFERENCE_NEW_OK);
39
40 tox_conference_set_max_offline(tox, state->conf_num, 10, nullptr);
41
42 tox_node_log(self, "Inviting Bob to conference %u", state->conf_num);
43 tox_conference_invite(tox, 0, state->conf_num, nullptr);
44
45 WAIT_UNTIL(state->bob_joined);
46 tox_node_log(self, "Bob joined. Reloading Alice...");
47
48 tox_node_reload(self);
49 tox_node_log(self, "Alice reloaded.");
50
51 // Re-register callback after reload
52 tox_events_callback_conference_peer_list_changed(tox_node_get_dispatch(self), alice_on_peer_list_changed);
53
54 // After reload, we need to get the conference number again
55 uint32_t chatlist[1];
56 tox_conference_get_chatlist(tox_node_get_tox(self), chatlist);
57 state->conf_num = chatlist[0];
58
59 tox_node_log(self, "Checking offline peer list for conference %u...", state->conf_num);
60
61 Tox_Err_Conference_Peer_Query q_err;
62 uint32_t offline_count = tox_conference_offline_peer_count(tox_node_get_tox(self), state->conf_num, &q_err);
63 ck_assert(q_err == TOX_ERR_CONFERENCE_PEER_QUERY_OK);
64 tox_node_log(self, "Offline peer count: %u", offline_count);
65
66 // Bob should be offline now because we just started and haven't connected to him in the conference yet.
67 ck_assert(offline_count >= 1);
68
69 tox_node_log(self, "Conference offline peer tests passed!");
70 }
71
72 typedef struct {
73 bool invited;
74 uint32_t conf_num;
75 uint8_t cookie[512];
76 size_t cookie_len;
77 } BobState;
78
79 static void bob_on_conference_invite(const Tox_Event_Conference_Invite *event, void *user_data)
80 {
81 ToxNode *self = (ToxNode *)user_data;
82 BobState *state = (BobState *)tox_node_get_script_ctx(self);
83
84 state->invited = true;
85 state->cookie_len = tox_event_conference_invite_get_cookie_length(event);
86 memcpy(state->cookie, tox_event_conference_invite_get_cookie(event), state->cookie_len);
87 tox_node_log(self, "Received conference invite");
88 }
89
90 static void bob_script(ToxNode *self, void *ctx)
91 {
92 const BobState *state = (const BobState *)ctx;
93 tox_events_callback_conference_invite(tox_node_get_dispatch(self), bob_on_conference_invite);
94
95 tox_node_wait_for_self_connected(self);
96 tox_node_wait_for_friend_connected(self, 0);
97
98 WAIT_UNTIL(state->invited);
99
100 tox_conference_join(tox_node_get_tox(self), 0, state->cookie, state->cookie_len, nullptr);
101
102 ToxNode *alice = tox_scenario_get_node(tox_node_get_scenario(self), 0);
103 WAIT_UNTIL(tox_node_is_finished(alice));
104 }
105
106
107 int main(int argc, char *argv[])
108 {
109 ToxScenario *s = tox_scenario_new(argc, argv, 60000);
110
111 AliceState alice_state = {0};
112 BobState bob_state = {0};
113
114 tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState));
115 tox_scenario_add_node(s, "Bob", bob_script, &bob_state, sizeof(BobState));
116
117 ToxNode *alice = tox_scenario_get_node(s, 0);
118 ToxNode *bob = tox_scenario_get_node(s, 1);
119
120 tox_node_bootstrap(alice, bob);
121 tox_node_friend_add(alice, bob);
122 tox_node_friend_add(bob, alice);
123
124 ToxScenarioStatus res = tox_scenario_run(s);
125 if (res != TOX_SCENARIO_DONE) {
126 fprintf(stderr, "Scenario failed with status %u\n", res);
127 return 1;
128 }
129
130 tox_scenario_free(s);
131 return 0;
132 }