/work/auto_tests/scenarios/scenario_friend_delete_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 | | bool bob_went_offline; |
8 | | } AliceState; |
9 | | |
10 | | static void on_alice_friend_connection_status(const Tox_Event_Friend_Connection_Status *event, void *user_data) |
11 | 1 | { |
12 | 1 | ToxNode *self = (ToxNode *)user_data; |
13 | 1 | AliceState *state = (AliceState *)tox_node_get_script_ctx(self); |
14 | | |
15 | 1 | Tox_Connection status = tox_event_friend_connection_status_get_connection_status(event); |
16 | 1 | tox_node_log(self, "Friend connection status changed to %u", status); |
17 | | |
18 | 1 | if (status == TOX_CONNECTION_NONE) { |
19 | 0 | state->bob_went_offline = true; |
20 | 0 | } |
21 | 1 | } |
22 | | |
23 | | static void alice_script(ToxNode *self, void *ctx) |
24 | 1 | { |
25 | 1 | tox_events_callback_friend_connection_status(tox_node_get_dispatch(self), on_alice_friend_connection_status); |
26 | | |
27 | 1 | tox_node_wait_for_self_connected(self); |
28 | 1 | tox_node_wait_for_friend_connected(self, 0); |
29 | 1 | tox_node_log(self, "Connected to Bob. Deleting Bob now..."); |
30 | | |
31 | 1 | Tox_Err_Friend_Delete err; |
32 | 1 | bool success = tox_friend_delete(tox_node_get_tox(self), 0, &err); |
33 | 1 | ck_assert(success); |
34 | 1 | ck_assert(err == TOX_ERR_FRIEND_DELETE_OK); |
35 | | |
36 | 1 | tox_node_log(self, "Bob deleted. Bob should see Alice as offline."); |
37 | | |
38 | | // Wait for Bob to finish |
39 | 1 | ToxNode *bob = tox_scenario_get_node(tox_node_get_scenario(self), 1); |
40 | 1 | WAIT_UNTIL(tox_node_is_finished(bob)); |
41 | 1 | } |
42 | | |
43 | | typedef struct { |
44 | | bool alice_went_offline; |
45 | | bool alice_connected; |
46 | | } BobState; |
47 | | |
48 | | static void on_bob_friend_connection_status(const Tox_Event_Friend_Connection_Status *event, void *user_data) |
49 | 2 | { |
50 | 2 | ToxNode *self = (ToxNode *)user_data; |
51 | 2 | BobState *state = (BobState *)tox_node_get_script_ctx(self); |
52 | | |
53 | 2 | Tox_Connection status = tox_event_friend_connection_status_get_connection_status(event); |
54 | 2 | tox_node_log(self, "Friend connection status changed to %u", status); |
55 | | |
56 | 2 | if (status != TOX_CONNECTION_NONE) { |
57 | 1 | state->alice_connected = true; |
58 | 1 | } |
59 | | |
60 | 2 | if (status == TOX_CONNECTION_NONE) { |
61 | 1 | state->alice_went_offline = true; |
62 | 1 | } |
63 | 2 | } |
64 | | |
65 | | static void bob_script(ToxNode *self, void *ctx) |
66 | 9 | { |
67 | 9 | const BobState *state = (const BobState *)ctx; |
68 | 9 | tox_events_callback_friend_connection_status(tox_node_get_dispatch(self), on_bob_friend_connection_status); |
69 | | |
70 | 9 | tox_node_wait_for_self_connected(self); |
71 | 9 | WAIT_UNTIL(state->alice_connected); |
72 | 9 | tox_node_log(self, "Connected to Alice. Waiting for Alice to delete me..."); |
73 | | |
74 | 9 | WAIT_UNTIL(state->alice_went_offline); |
75 | 9 | tox_node_log(self, "Alice went offline as expected!"); |
76 | 9 | } |
77 | | |
78 | | int main(int argc, char *argv[]) |
79 | 31 | { |
80 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
81 | | |
82 | 31 | AliceState alice_state = {false}; |
83 | 31 | BobState bob_state = {false, false}; |
84 | | |
85 | 31 | tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState)); |
86 | 31 | tox_scenario_add_node(s, "Bob", bob_script, &bob_state, sizeof(BobState)); |
87 | | |
88 | 31 | ToxNode *alice = tox_scenario_get_node(s, 0); |
89 | 31 | ToxNode *bob = tox_scenario_get_node(s, 1); |
90 | | |
91 | 31 | tox_node_bootstrap(alice, bob); |
92 | 31 | tox_node_friend_add(alice, bob); |
93 | 31 | tox_node_friend_add(bob, alice); |
94 | | |
95 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
96 | 31 | if (res != TOX_SCENARIO_DONE) { |
97 | 0 | fprintf(stderr, "Scenario failed with status %u\n", res); |
98 | 0 | return 1; |
99 | 0 | } |
100 | | |
101 | 31 | tox_scenario_free(s); |
102 | 31 | return 0; |
103 | 31 | } |