/work/auto_tests/scenarios/scenario_conference_peer_nick_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "framework/framework.h" |
2 | | #include <stdio.h> |
3 | | #include <string.h> |
4 | | #include <stdlib.h> |
5 | | |
6 | | typedef struct { |
7 | | bool joined; |
8 | | uint32_t conference; |
9 | | bool friend_in_group; |
10 | | } State; |
11 | | |
12 | | static void on_conference_invite(const Tox_Event_Conference_Invite *event, void *user_data) |
13 | 1 | { |
14 | 1 | ToxNode *self = (ToxNode *)user_data; |
15 | 1 | State *state = (State *)tox_node_get_script_ctx(self); |
16 | | |
17 | 1 | Tox_Err_Conference_Join err; |
18 | 1 | state->conference = tox_conference_join(tox_node_get_tox(self), |
19 | 1 | tox_event_conference_invite_get_friend_number(event), |
20 | 1 | tox_event_conference_invite_get_cookie(event), |
21 | 1 | tox_event_conference_invite_get_cookie_length(event), |
22 | 1 | &err); |
23 | 1 | if (err == TOX_ERR_CONFERENCE_JOIN_OK) { |
24 | 1 | state->joined = true; |
25 | 1 | } |
26 | 1 | } |
27 | | |
28 | | static void on_peer_list_changed(const Tox_Event_Conference_Peer_List_Changed *event, void *user_data) |
29 | 4 | { |
30 | 4 | ToxNode *self = (ToxNode *)user_data; |
31 | 4 | State *state = (State *)tox_node_get_script_ctx(self); |
32 | | |
33 | 4 | uint32_t count = tox_conference_peer_count(tox_node_get_tox(self), tox_event_conference_peer_list_changed_get_conference_number(event), nullptr); |
34 | 4 | state->friend_in_group = (count == 2); |
35 | 4 | } |
36 | | |
37 | | static void alice_script(ToxNode *self, void *ctx) |
38 | 1 | { |
39 | 1 | Tox *tox = tox_node_get_tox(self); |
40 | 1 | State *state = (State *)ctx; |
41 | 1 | tox_events_callback_conference_peer_list_changed(tox_node_get_dispatch(self), on_peer_list_changed); |
42 | | |
43 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
44 | 1 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
45 | | |
46 | 1 | tox_self_set_name(tox, (const uint8_t *)"Alice", 5, nullptr); |
47 | | |
48 | 1 | tox_node_log(self, "Creating conference..."); |
49 | 1 | Tox_Err_Conference_New err_new; |
50 | 1 | state->conference = tox_conference_new(tox, &err_new); |
51 | 1 | state->joined = true; |
52 | | |
53 | 1 | tox_node_log(self, "Inviting Bob..."); |
54 | 1 | tox_conference_invite(tox, 0, state->conference, nullptr); |
55 | | |
56 | 1 | tox_node_log(self, "Waiting for Bob to join..."); |
57 | 1 | WAIT_UNTIL(state->friend_in_group); |
58 | 1 | tox_node_log(self, "Bob joined!"); |
59 | | |
60 | | // Wait for Bob to drop out (simulated by Bob finishing) |
61 | 1 | tox_node_log(self, "Waiting for Bob to leave..."); |
62 | 1 | WAIT_UNTIL(!state->friend_in_group); |
63 | 1 | tox_node_log(self, "Bob left!"); |
64 | 1 | } |
65 | | |
66 | | static void bob_script(ToxNode *self, void *ctx) |
67 | 1 | { |
68 | 1 | Tox *tox = tox_node_get_tox(self); |
69 | 1 | const State *state = (const State *)ctx; |
70 | 1 | tox_events_callback_conference_invite(tox_node_get_dispatch(self), on_conference_invite); |
71 | 1 | tox_events_callback_conference_peer_list_changed(tox_node_get_dispatch(self), on_peer_list_changed); |
72 | | |
73 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
74 | 1 | tox_self_set_name(tox, (const uint8_t *)"Bob", 3, nullptr); |
75 | | |
76 | 1 | WAIT_UNTIL(state->joined); |
77 | 1 | tox_node_log(self, "Joined conference!"); |
78 | | |
79 | 1 | WAIT_UNTIL(state->friend_in_group); |
80 | | |
81 | | // Stay a bit then leave |
82 | 21 | for (int i = 0; i < 20; ++i) { |
83 | 20 | tox_scenario_yield(self); |
84 | 20 | } |
85 | | |
86 | 1 | tox_node_log(self, "Leaving conference..."); |
87 | 1 | tox_conference_delete(tox, state->conference, nullptr); |
88 | 1 | } |
89 | | |
90 | | int main(int argc, char *argv[]) |
91 | 31 | { |
92 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
93 | 31 | State s1 = {0}, s2 = {0}; |
94 | | |
95 | 31 | ToxNode *alice = tox_scenario_add_node(s, "Alice", alice_script, &s1, sizeof(State)); |
96 | 31 | ToxNode *bob = tox_scenario_add_node(s, "Bob", bob_script, &s2, sizeof(State)); |
97 | | |
98 | 31 | tox_node_friend_add(alice, bob); |
99 | 31 | tox_node_friend_add(bob, alice); |
100 | | |
101 | 31 | tox_node_bootstrap(bob, alice); |
102 | | |
103 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
104 | 31 | if (res != TOX_SCENARIO_DONE) { |
105 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | 31 | tox_scenario_free(s); |
110 | 31 | return 0; |
111 | 31 | } |