/work/auto_tests/scenarios/scenario_friend_request_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 | | const uint8_t *message; |
8 | | size_t length; |
9 | | } RequestData; |
10 | | |
11 | | static void on_friend_request(const Tox_Event_Friend_Request *event, void *user_data) |
12 | 136 | { |
13 | 136 | ToxNode *self = (ToxNode *)user_data; |
14 | 136 | Tox *tox = tox_node_get_tox(self); |
15 | 136 | const RequestData *data = (const RequestData *)tox_node_get_script_ctx(self); |
16 | | |
17 | 136 | const uint8_t *msg = tox_event_friend_request_get_message(event); |
18 | 136 | size_t len = tox_event_friend_request_get_message_length(event); |
19 | | |
20 | 136 | if (len == data->length && memcmp(msg, data->message, len) == 0) { |
21 | 136 | tox_friend_add_norequest(tox, tox_event_friend_request_get_public_key(event), nullptr); |
22 | 136 | } |
23 | 136 | } |
24 | | |
25 | | static void alice_script(ToxNode *self, void *ctx) |
26 | 7 | { |
27 | 7 | Tox *tox = tox_node_get_tox(self); |
28 | 7 | RequestData *data = (RequestData *)ctx; |
29 | | |
30 | 7 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
31 | | |
32 | 7 | ToxNode *bob = tox_scenario_get_node(tox_node_get_scenario(self), 1); |
33 | 7 | uint8_t bob_addr[TOX_ADDRESS_SIZE]; |
34 | 7 | tox_node_get_address(bob, bob_addr); |
35 | | |
36 | 7 | tox_friend_add(tox, bob_addr, data->message, data->length, nullptr); |
37 | | |
38 | 7 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
39 | 7 | } |
40 | | |
41 | | static void bob_script(ToxNode *self, void *ctx) |
42 | 9 | { |
43 | 9 | tox_events_callback_friend_request(tox_node_get_dispatch(self), on_friend_request); |
44 | | |
45 | 9 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
46 | 9 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
47 | 9 | } |
48 | | |
49 | | static void test_with_message(int argc, char *argv[], const char *label, const uint8_t *message, size_t length) |
50 | 3 | { |
51 | 3 | ToxScenario *s = tox_scenario_new(argc, argv, 60000); |
52 | 3 | tox_scenario_log(s, "Testing friend request: %s (length %zu)", label, length); |
53 | 3 | RequestData data = {message, length}; |
54 | | |
55 | 3 | ToxNode *alice = tox_scenario_add_node(s, "Alice", alice_script, &data, sizeof(RequestData)); |
56 | 3 | ToxNode *bob = tox_scenario_add_node(s, "Bob", bob_script, &data, sizeof(RequestData)); |
57 | | |
58 | 3 | tox_node_bootstrap(bob, alice); |
59 | | |
60 | 3 | ToxScenarioStatus res = tox_scenario_run(s); |
61 | 3 | if (res != TOX_SCENARIO_DONE) { |
62 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
63 | 0 | exit(1); |
64 | 0 | } |
65 | 3 | tox_scenario_free(s); |
66 | 3 | } |
67 | | |
68 | | int main(int argc, char *argv[]) |
69 | 84 | { |
70 | 84 | test_with_message(argc, argv, "Short", (const uint8_t *)"a", 1); |
71 | 84 | test_with_message(argc, argv, "Medium", (const uint8_t *)"Hello, let\'s be friends!", 24); |
72 | | |
73 | 84 | uint8_t long_msg[TOX_MAX_FRIEND_REQUEST_LENGTH]; |
74 | 84 | memset(long_msg, 'F', sizeof(long_msg)); |
75 | 84 | test_with_message(argc, argv, "Max length", long_msg, sizeof(long_msg)); |
76 | | |
77 | 84 | return 0; |
78 | 84 | } |