/work/auto_tests/scenarios/scenario_friend_read_receipt_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 message_id; |
8 | | bool received_receipt; |
9 | | } AliceState; |
10 | | |
11 | | static void on_read_receipt(const Tox_Event_Friend_Read_Receipt *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 | | |
16 | 1 | uint32_t friend_number = tox_event_friend_read_receipt_get_friend_number(event); |
17 | 1 | uint32_t message_id = tox_event_friend_read_receipt_get_message_id(event); |
18 | | |
19 | 1 | tox_node_log(self, "Received read receipt for friend %u, message %u", friend_number, message_id); |
20 | | |
21 | 1 | if (friend_number == 0 && message_id == state->message_id) { |
22 | 1 | state->received_receipt = true; |
23 | 1 | } |
24 | 1 | } |
25 | | |
26 | | static void alice_script(ToxNode *self, void *ctx) |
27 | 2 | { |
28 | 2 | AliceState *state = (AliceState *)ctx; |
29 | 2 | tox_events_callback_friend_read_receipt(tox_node_get_dispatch(self), on_read_receipt); |
30 | | |
31 | 2 | tox_node_wait_for_self_connected(self); |
32 | 2 | tox_node_wait_for_friend_connected(self, 0); |
33 | | |
34 | 2 | tox_node_log(self, "Sending message to Bob..."); |
35 | 2 | uint8_t msg[] = "Hello Bob!"; |
36 | 2 | Tox_Err_Friend_Send_Message err; |
37 | 2 | state->message_id = tox_friend_send_message(tox_node_get_tox(self), 0, TOX_MESSAGE_TYPE_NORMAL, msg, sizeof(msg), &err); |
38 | 2 | ck_assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK); |
39 | 2 | tox_node_log(self, "Message sent with ID %u, waiting for read receipt...", state->message_id); |
40 | | |
41 | 2 | WAIT_UNTIL(state->received_receipt); |
42 | 2 | tox_node_log(self, "Read receipt received successfully!"); |
43 | 2 | } |
44 | | |
45 | | static void bob_script(ToxNode *self, void *ctx) |
46 | 6 | { |
47 | 6 | tox_node_wait_for_self_connected(self); |
48 | 6 | tox_node_wait_for_friend_connected(self, 0); |
49 | | |
50 | 6 | tox_node_log(self, "Waiting for message from Alice..."); |
51 | 6 | WAIT_FOR_EVENT(TOX_EVENT_FRIEND_MESSAGE); |
52 | 6 | tox_node_log(self, "Received message! Tox will automatically send a read receipt."); |
53 | | |
54 | | // We stay here to allow Alice to receive the receipt |
55 | | // We wait until alice is finished |
56 | 6 | ToxNode *alice = tox_scenario_get_node(tox_node_get_scenario(self), 0); |
57 | 6 | WAIT_UNTIL(tox_node_is_finished(alice)); |
58 | 6 | } |
59 | | |
60 | | int main(int argc, char *argv[]) |
61 | 31 | { |
62 | 31 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
63 | | |
64 | 31 | AliceState alice_state = {0, false}; |
65 | 31 | tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState)); |
66 | 31 | tox_scenario_add_node(s, "Bob", bob_script, nullptr, 0); |
67 | | |
68 | 31 | ToxNode *alice = tox_scenario_get_node(s, 0); |
69 | 31 | ToxNode *bob = tox_scenario_get_node(s, 1); |
70 | | |
71 | 31 | tox_node_bootstrap(alice, bob); |
72 | 31 | tox_node_friend_add(alice, bob); |
73 | 31 | tox_node_friend_add(bob, alice); |
74 | | |
75 | 31 | ToxScenarioStatus res = tox_scenario_run(s); |
76 | 31 | if (res != TOX_SCENARIO_DONE) { |
77 | 0 | fprintf(stderr, "Scenario failed with status %u\n", res); |
78 | 0 | return 1; |
79 | 0 | } |
80 | | |
81 | 31 | tox_scenario_free(s); |
82 | 31 | return 0; |
83 | 31 | } |