/work/auto_tests/scenarios/scenario_conference_offline_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 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 | 1 | { |
13 | 1 | ToxNode *self = (ToxNode *)user_data; |
14 | 1 | AliceState *state = (AliceState *)tox_node_get_script_ctx(self); |
15 | 1 | uint32_t conf_num = tox_event_conference_peer_list_changed_get_conference_number(event); |
16 | | |
17 | 1 | if (conf_num != state->conf_num) { |
18 | 0 | return; |
19 | 0 | } |
20 | | |
21 | 1 | uint32_t count = tox_conference_peer_count(tox_node_get_tox(self), conf_num, nullptr); |
22 | 1 | if (count == 2) { |
23 | 1 | state->bob_joined = true; |
24 | 1 | } |
25 | 1 | } |
26 | | |
27 | | static void alice_script(ToxNode *self, void *ctx) |
28 | 1 | { |
29 | 1 | AliceState *state = (AliceState *)ctx; |
30 | 1 | tox_events_callback_conference_peer_list_changed(tox_node_get_dispatch(self), alice_on_peer_list_changed); |
31 | | |
32 | 1 | tox_node_wait_for_self_connected(self); |
33 | 1 | tox_node_wait_for_friend_connected(self, 0); |
34 | | |
35 | 1 | Tox *tox = tox_node_get_tox(self); |
36 | 1 | Tox_Err_Conference_New err_new; |
37 | 1 | state->conf_num = tox_conference_new(tox, &err_new); |
38 | 1 | ck_assert(err_new == TOX_ERR_CONFERENCE_NEW_OK); |
39 | | |
40 | 1 | tox_conference_set_max_offline(tox, state->conf_num, 10, nullptr); |
41 | | |
42 | 1 | tox_node_log(self, "Inviting Bob to conference %u", state->conf_num); |
43 | 1 | tox_conference_invite(tox, 0, state->conf_num, nullptr); |
44 | | |
45 | 1 | WAIT_UNTIL(state->bob_joined); |
46 | 1 | tox_node_log(self, "Bob joined. Reloading Alice..."); |
47 | | |
48 | 1 | tox_node_reload(self); |
49 | 1 | tox_node_log(self, "Alice reloaded."); |
50 | | |
51 | | // Re-register callback after reload |
52 | 1 | 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 | 1 | uint32_t chatlist[1]; |
56 | 1 | tox_conference_get_chatlist(tox_node_get_tox(self), chatlist); |
57 | 1 | state->conf_num = chatlist[0]; |
58 | | |
59 | 1 | tox_node_log(self, "Checking offline peer list for conference %u...", state->conf_num); |
60 | | |
61 | 1 | Tox_Err_Conference_Peer_Query q_err; |
62 | 1 | uint32_t offline_count = tox_conference_offline_peer_count(tox_node_get_tox(self), state->conf_num, &q_err); |
63 | 1 | ck_assert(q_err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); |
64 | 1 | 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 | 1 | ck_assert(offline_count >= 1); |
68 | | |
69 | 1 | tox_node_log(self, "Conference offline peer tests passed!"); |
70 | 1 | } |
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 | 2 | { |
81 | 2 | ToxNode *self = (ToxNode *)user_data; |
82 | 2 | BobState *state = (BobState *)tox_node_get_script_ctx(self); |
83 | | |
84 | 2 | state->invited = true; |
85 | 2 | state->cookie_len = tox_event_conference_invite_get_cookie_length(event); |
86 | 2 | memcpy(state->cookie, tox_event_conference_invite_get_cookie(event), state->cookie_len); |
87 | 2 | tox_node_log(self, "Received conference invite"); |
88 | 2 | } |
89 | | |
90 | | static void bob_script(ToxNode *self, void *ctx) |
91 | 9 | { |
92 | 9 | const BobState *state = (const BobState *)ctx; |
93 | 9 | tox_events_callback_conference_invite(tox_node_get_dispatch(self), bob_on_conference_invite); |
94 | | |
95 | 9 | tox_node_wait_for_self_connected(self); |
96 | 9 | tox_node_wait_for_friend_connected(self, 0); |
97 | | |
98 | 9 | WAIT_UNTIL(state->invited); |
99 | | |
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 | | |
107 | | int main(int argc, char *argv[]) |
108 | 31 | { |
109 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
110 | | |
111 | 31 | AliceState alice_state = {0}; |
112 | 31 | BobState bob_state = {0}; |
113 | | |
114 | 31 | tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState)); |
115 | 31 | tox_scenario_add_node(s, "Bob", bob_script, &bob_state, sizeof(BobState)); |
116 | | |
117 | 31 | ToxNode *alice = tox_scenario_get_node(s, 0); |
118 | 31 | ToxNode *bob = tox_scenario_get_node(s, 1); |
119 | | |
120 | 31 | tox_node_bootstrap(alice, bob); |
121 | 31 | tox_node_friend_add(alice, bob); |
122 | 31 | tox_node_friend_add(bob, alice); |
123 | | |
124 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
125 | 31 | if (res != TOX_SCENARIO_DONE) { |
126 | 0 | fprintf(stderr, "Scenario failed with status %u\n", res); |
127 | 0 | return 1; |
128 | 0 | } |
129 | | |
130 | 31 | tox_scenario_free(s); |
131 | 31 | return 0; |
132 | 31 | } |