/work/auto_tests/scenarios/scenario_conference_double_invite_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "framework/framework.h" |
2 | | #include <stdio.h> |
3 | | #include <string.h> |
4 | | |
5 | | typedef struct { |
6 | | bool joined; |
7 | | uint32_t conference; |
8 | | } State; |
9 | | |
10 | | static void on_conference_invite(const Tox_Event_Conference_Invite *event, void *user_data) |
11 | 1 | { |
12 | 1 | ToxNode *self = (ToxNode *)user_data; |
13 | 1 | State *state = (State *)tox_node_get_script_ctx(self); |
14 | | |
15 | 1 | if (state->joined) { |
16 | 0 | tox_node_log(self, "ERROR! Received second invite for already joined conference"); |
17 | 0 | return; |
18 | 0 | } |
19 | | |
20 | 1 | Tox_Err_Conference_Join err; |
21 | 1 | state->conference = tox_conference_join(tox_node_get_tox(self), |
22 | 1 | tox_event_conference_invite_get_friend_number(event), |
23 | 1 | tox_event_conference_invite_get_cookie(event), |
24 | 1 | tox_event_conference_invite_get_cookie_length(event), |
25 | 1 | &err); |
26 | 1 | if (err == TOX_ERR_CONFERENCE_JOIN_OK) { |
27 | 1 | state->joined = true; |
28 | 1 | tox_node_log(self, "Joined conference %u", state->conference); |
29 | 1 | } |
30 | 1 | } |
31 | | |
32 | | static void alice_script(ToxNode *self, void *ctx) |
33 | 1 | { |
34 | 1 | Tox *tox = tox_node_get_tox(self); |
35 | 1 | State *state = (State *)ctx; |
36 | | |
37 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
38 | 1 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
39 | | |
40 | 1 | tox_node_log(self, "Creating conference..."); |
41 | 1 | Tox_Err_Conference_New err_new; |
42 | 1 | state->conference = tox_conference_new(tox, &err_new); |
43 | 1 | state->joined = true; |
44 | | |
45 | 1 | tox_node_log(self, "Inviting Bob (1st time)..."); |
46 | 1 | tox_conference_invite(tox, 0, state->conference, nullptr); |
47 | | |
48 | | // Wait for Bob to join |
49 | 1 | const ToxNode *bob = tox_scenario_get_node(tox_node_get_scenario(self), 1); |
50 | 1 | const State *bob_view = (const State *)tox_node_get_peer_ctx(bob); |
51 | 1 | WAIT_UNTIL(bob_view->joined); |
52 | | |
53 | 1 | tox_node_log(self, "Inviting Bob (2nd time); should be ignored by Bob's script logic..."); |
54 | 1 | tox_conference_invite(tox, 0, state->conference, nullptr); |
55 | | |
56 | | // Wait some ticks to see if Bob's script logs an error |
57 | 21 | for (int i = 0; i < 20; ++i) { |
58 | 20 | tox_scenario_yield(self); |
59 | 20 | } |
60 | 1 | } |
61 | | |
62 | | static void bob_script(ToxNode *self, void *ctx) |
63 | 1 | { |
64 | 1 | tox_events_callback_conference_invite(tox_node_get_dispatch(self), on_conference_invite); |
65 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
66 | | |
67 | 1 | const State *state = (const State *)ctx; |
68 | 1 | WAIT_UNTIL(state->joined); |
69 | | |
70 | | // Stay alive for Alice's second invite |
71 | 51 | for (int i = 0; i < 50; ++i) { |
72 | 50 | tox_scenario_yield(self); |
73 | 50 | } |
74 | 1 | } |
75 | | |
76 | | int main(int argc, char *argv[]) |
77 | 31 | { |
78 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
79 | 31 | State s1 = {0}, s2 = {0}; |
80 | | |
81 | 31 | ToxNode *alice = tox_scenario_add_node(s, "Alice", alice_script, &s1, sizeof(State)); |
82 | 31 | ToxNode *bob = tox_scenario_add_node(s, "Bob", bob_script, &s2, sizeof(State)); |
83 | | |
84 | 31 | tox_node_friend_add(alice, bob); |
85 | 31 | tox_node_friend_add(bob, alice); |
86 | | |
87 | 31 | tox_node_bootstrap(bob, alice); |
88 | | |
89 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
90 | 31 | if (res != TOX_SCENARIO_DONE) { |
91 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
92 | 0 | return 1; |
93 | 0 | } |
94 | | |
95 | 31 | tox_scenario_free(s); |
96 | 31 | return 0; |
97 | 31 | } |