/work/auto_tests/scenarios/scenario_friend_request_spam_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "framework/framework.h" |
2 | | #include <stdio.h> |
3 | | #include <string.h> |
4 | | #include <stdlib.h> |
5 | | |
6 | 472 | #define FR_MESSAGE "Gentoo" |
7 | 387 | #define FR_TOX_COUNT 33 |
8 | | |
9 | | typedef struct { |
10 | | uint32_t requests_received; |
11 | | } ReceiverState; |
12 | | |
13 | | static void on_friend_request(const Tox_Event_Friend_Request *event, void *user_data) |
14 | 136 | { |
15 | 136 | ToxNode *self = (ToxNode *)user_data; |
16 | 136 | ReceiverState *state = (ReceiverState *)tox_node_get_script_ctx(self); |
17 | | |
18 | 136 | const uint8_t *public_key = tox_event_friend_request_get_public_key(event); |
19 | 136 | const uint8_t *data = tox_event_friend_request_get_message(event); |
20 | 136 | const size_t length = tox_event_friend_request_get_message_length(event); |
21 | | |
22 | 136 | if (length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0) { |
23 | 136 | tox_friend_add_norequest(tox_node_get_tox(self), public_key, nullptr); |
24 | 136 | state->requests_received++; |
25 | 136 | tox_node_log(self, "Friend request received: %u", state->requests_received); |
26 | 136 | } |
27 | 136 | } |
28 | | |
29 | | static void receiver_script(ToxNode *self, void *ctx) |
30 | 1 | { |
31 | 1 | const ReceiverState *state = (const ReceiverState *)ctx; |
32 | 1 | tox_events_callback_friend_request(tox_node_get_dispatch(self), on_friend_request); |
33 | | |
34 | 1 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
35 | | |
36 | | // Wait for all requests to be received and friends connected |
37 | 1 | WAIT_UNTIL(state->requests_received == FR_TOX_COUNT - 1); |
38 | | |
39 | | // Also wait until they are all connected as friends |
40 | 1 | bool all_connected = false; |
41 | 27 | while (!all_connected && tox_scenario_is_running(self)) { |
42 | 26 | all_connected = true; |
43 | 347 | for (uint32_t i = 0; i < FR_TOX_COUNT - 1; ++i) { |
44 | 346 | if (!tox_node_is_friend_connected(self, i)) { |
45 | 25 | all_connected = false; |
46 | 25 | break; |
47 | 25 | } |
48 | 346 | } |
49 | 26 | if (!all_connected) { |
50 | 25 | tox_scenario_yield(self); |
51 | 25 | } |
52 | 26 | } |
53 | | |
54 | 1 | tox_node_log(self, "All friends connected"); |
55 | 1 | } |
56 | | |
57 | | static void sender_script(ToxNode *self, void *ctx) |
58 | 32 | { |
59 | 32 | WAIT_UNTIL(tox_node_is_self_connected(self)); |
60 | | |
61 | 32 | ToxNode *receiver = (ToxNode *)ctx; |
62 | 32 | uint8_t address[TOX_ADDRESS_SIZE]; |
63 | 32 | tox_node_get_address(receiver, address); |
64 | | |
65 | 32 | Tox_Err_Friend_Add err; |
66 | 32 | tox_friend_add(tox_node_get_tox(self), address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), &err); |
67 | 32 | if (err != TOX_ERR_FRIEND_ADD_OK) { |
68 | 0 | tox_node_log(self, "Failed to add friend: %u", err); |
69 | 0 | return; |
70 | 0 | } |
71 | | |
72 | 32 | WAIT_UNTIL(tox_node_is_friend_connected(self, 0)); |
73 | 32 | tox_node_log(self, "Connected to receiver"); |
74 | 32 | } |
75 | | |
76 | | int main(int argc, char *argv[]) |
77 | 2 | { |
78 | 2 | ToxScenario *s = tox_scenario_new(argc, argv, 300000); |
79 | 2 | ReceiverState receiver_state = {0}; |
80 | | |
81 | 2 | ToxNode *receiver = tox_scenario_add_node(s, "Receiver", receiver_script, &receiver_state, sizeof(ReceiverState)); |
82 | 2 | ToxNode *senders[FR_TOX_COUNT - 1]; |
83 | | |
84 | 40 | for (int i = 0; i < FR_TOX_COUNT - 1; ++i) { |
85 | 38 | char alias[16]; |
86 | 38 | snprintf(alias, sizeof(alias), "Sender%d", i); |
87 | 38 | senders[i] = tox_scenario_add_node(s, alias, sender_script, receiver, 0); |
88 | 38 | tox_node_bootstrap(senders[i], receiver); |
89 | 38 | tox_node_bootstrap(receiver, senders[i]); |
90 | 38 | } |
91 | | |
92 | 2 | ToxScenarioStatus res = tox_scenario_run(s); |
93 | 2 | if (res != TOX_SCENARIO_DONE) { |
94 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
95 | 0 | return 1; |
96 | 0 | } |
97 | | |
98 | 2 | tox_scenario_free(s); |
99 | 2 | return 0; |
100 | 2 | } |
101 | | |
102 | | #undef FR_MESSAGE |