/work/auto_tests/scenarios/scenario_file_cancel_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 cancelled; |
8 | | } AliceState; |
9 | | |
10 | | static void on_file_recv_control(const Tox_Event_File_Recv_Control *event, void *user_data) |
11 | 2 | { |
12 | 2 | ToxNode *self = (ToxNode *)user_data; |
13 | 2 | AliceState *state = (AliceState *)tox_node_get_script_ctx(self); |
14 | | |
15 | 2 | if (tox_event_file_recv_control_get_control(event) == TOX_FILE_CONTROL_CANCEL) { |
16 | 2 | state->cancelled = true; |
17 | 2 | tox_node_log(self, "Bob cancelled the file transfer."); |
18 | 2 | } |
19 | 2 | } |
20 | | |
21 | | static void alice_script(ToxNode *self, void *ctx) |
22 | 2 | { |
23 | 2 | const AliceState *state = (const AliceState *)ctx; |
24 | 2 | tox_events_callback_file_recv_control(tox_node_get_dispatch(self), on_file_recv_control); |
25 | | |
26 | 2 | tox_node_wait_for_self_connected(self); |
27 | 2 | tox_node_wait_for_friend_connected(self, 0); |
28 | | |
29 | 2 | Tox *tox = tox_node_get_tox(self); |
30 | 2 | tox_file_send(tox, 0, TOX_FILE_KIND_DATA, 1000, nullptr, (const uint8_t *)"test.txt", 8, nullptr); |
31 | | |
32 | 2 | WAIT_UNTIL(state->cancelled); |
33 | 2 | tox_node_log(self, "File cancellation verified from Alice side."); |
34 | 2 | } |
35 | | |
36 | | static void on_file_recv(const Tox_Event_File_Recv *event, void *user_data) |
37 | 2 | { |
38 | 2 | ToxNode *self = (ToxNode *)user_data; |
39 | 2 | uint32_t friend_number = tox_event_file_recv_get_friend_number(event); |
40 | 2 | uint32_t file_number = tox_event_file_recv_get_file_number(event); |
41 | | |
42 | 2 | tox_node_log(self, "Received file request, rejecting (CANCEL)..."); |
43 | 2 | tox_file_control(tox_node_get_tox(self), friend_number, file_number, TOX_FILE_CONTROL_CANCEL, nullptr); |
44 | 2 | } |
45 | | |
46 | | static void bob_script(ToxNode *self, void *ctx) |
47 | 6 | { |
48 | 6 | tox_events_callback_file_recv(tox_node_get_dispatch(self), on_file_recv); |
49 | | |
50 | 6 | tox_node_wait_for_self_connected(self); |
51 | 6 | tox_node_wait_for_friend_connected(self, 0); |
52 | | |
53 | 6 | ToxNode *alice = tox_scenario_get_node(tox_node_get_scenario(self), 0); |
54 | 6 | WAIT_UNTIL(tox_node_is_finished(alice)); |
55 | 6 | } |
56 | | |
57 | | int main(int argc, char *argv[]) |
58 | 31 | { |
59 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
60 | | |
61 | 31 | AliceState alice_state = {false}; |
62 | | |
63 | 31 | tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState)); |
64 | 31 | tox_scenario_add_node(s, "Bob", bob_script, nullptr, 0); |
65 | | |
66 | 31 | ToxNode *alice = tox_scenario_get_node(s, 0); |
67 | 31 | ToxNode *bob = tox_scenario_get_node(s, 1); |
68 | | |
69 | 31 | tox_node_bootstrap(alice, bob); |
70 | 31 | tox_node_friend_add(alice, bob); |
71 | 31 | tox_node_friend_add(bob, alice); |
72 | | |
73 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
74 | 31 | if (res != TOX_SCENARIO_DONE) { |
75 | 0 | fprintf(stderr, "Scenario failed with status %u\n", res); |
76 | 0 | return 1; |
77 | 0 | } |
78 | | |
79 | 31 | tox_scenario_free(s); |
80 | 31 | return 0; |
81 | 31 | } |