/work/auto_tests/scenarios/scenario_tox_many_tcp_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 | 2.54k | #define NUM_TOXES 40 |
7 | 51 | #define NUM_FRIEND_PAIRS 50 |
8 | 186 | #define FR_MESSAGE "Gentoo" |
9 | 1 | #define RELAY_TCP_PORT 33448 |
10 | | |
11 | | typedef struct { |
12 | | uint32_t expected_friends; |
13 | | } State; |
14 | | |
15 | | static void on_friend_request(const Tox_Event_Friend_Request *event, void *user_data) |
16 | 136 | { |
17 | 136 | ToxNode *self = (ToxNode *)user_data; |
18 | 136 | const uint8_t *msg = tox_event_friend_request_get_message(event); |
19 | 136 | size_t len = tox_event_friend_request_get_message_length(event); |
20 | | |
21 | 136 | if (len == 6 && memcmp(msg, FR_MESSAGE, 6) == 0) { |
22 | 136 | tox_friend_add_norequest(tox_node_get_tox(self), tox_event_friend_request_get_public_key(event), nullptr); |
23 | 136 | } |
24 | 136 | } |
25 | | |
26 | | static void peer_script(ToxNode *self, void *ctx) |
27 | 40 | { |
28 | 40 | State *state = (State *)ctx; |
29 | 40 | tox_events_callback_friend_request(tox_node_get_dispatch(self), on_friend_request); |
30 | | |
31 | 40 | tox_node_log(self, "Waiting for connection to relay..."); |
32 | 40 | tox_node_wait_for_self_connected(self); |
33 | 40 | tox_node_log(self, "Connected. Expected friends: %u", state->expected_friends); |
34 | | |
35 | | // Wait until we have the expected number of friends connected via TCP |
36 | 40 | uint32_t last_connected = 0; |
37 | 23.5k | while (1) { |
38 | 23.5k | uint32_t connected_friends = 0; |
39 | 23.5k | uint32_t total_friends = tox_self_get_friend_list_size(tox_node_get_tox(self)); |
40 | 88.7k | for (uint32_t i = 0; i < total_friends; ++i) { |
41 | 65.1k | if (tox_node_get_friend_connection_status(self, i) == TOX_CONNECTION_TCP) { |
42 | 959 | connected_friends++; |
43 | 959 | } |
44 | 65.1k | } |
45 | | |
46 | 23.5k | if (connected_friends != last_connected) { |
47 | 86 | tox_node_log(self, "Connected friends: %u/%u", connected_friends, state->expected_friends); |
48 | 86 | last_connected = connected_friends; |
49 | 86 | } |
50 | | |
51 | 23.5k | if (connected_friends >= state->expected_friends) { |
52 | 40 | break; |
53 | 40 | } |
54 | 23.5k | tox_scenario_yield(self); |
55 | 23.5k | } |
56 | | |
57 | 40 | tox_node_log(self, "All %u friends connected via TCP.", state->expected_friends); |
58 | 40 | } |
59 | | |
60 | | static void relay_script(ToxNode *self, void *ctx) |
61 | 1 | { |
62 | 1 | (void)ctx; |
63 | 1 | ToxScenario *s = tox_node_get_scenario(self); |
64 | | |
65 | 844 | while (tox_scenario_is_running(self)) { |
66 | 844 | bool all_finished = true; |
67 | 2.34k | for (uint32_t i = 0; i < NUM_TOXES; ++i) { |
68 | 2.34k | ToxNode *peer = tox_scenario_get_node(s, i + 1); // Peers start at index 1 |
69 | 2.34k | if (!tox_node_is_finished(peer)) { |
70 | 843 | all_finished = false; |
71 | 843 | break; |
72 | 843 | } |
73 | 2.34k | } |
74 | 844 | if (all_finished) { |
75 | 1 | break; |
76 | 1 | } |
77 | 843 | tox_scenario_yield(self); |
78 | 843 | } |
79 | 1 | } |
80 | | |
81 | | int main(int argc, char *argv[]) |
82 | 1 | { |
83 | 1 | ToxScenario *s = tox_scenario_new(argc, argv, 300000); |
84 | | |
85 | 1 | struct Tox_Options *opts_relay = tox_options_new(nullptr); |
86 | 1 | tox_options_set_tcp_port(opts_relay, RELAY_TCP_PORT); |
87 | 1 | ToxNode *relay = tox_scenario_add_node_ex(s, "Relay", relay_script, nullptr, 0, opts_relay); |
88 | 1 | tox_options_free(opts_relay); |
89 | | |
90 | 1 | uint8_t relay_dht_id[TOX_PUBLIC_KEY_SIZE]; |
91 | 1 | tox_self_get_dht_id(tox_node_get_tox(relay), relay_dht_id); |
92 | | |
93 | 1 | uint16_t relay_tcp_port = tox_self_get_tcp_port(tox_node_get_tox(relay), nullptr); |
94 | | |
95 | 1 | State states[NUM_TOXES] = {0}; |
96 | 1 | ToxNode *nodes[NUM_TOXES]; |
97 | | |
98 | 1 | struct Tox_Options *opts_peer = tox_options_new(nullptr); |
99 | 1 | tox_options_set_udp_enabled(opts_peer, false); |
100 | 1 | tox_options_set_local_discovery_enabled(opts_peer, false); |
101 | | |
102 | 41 | for (int i = 0; i < NUM_TOXES; ++i) { |
103 | 40 | char alias[16]; |
104 | 40 | snprintf(alias, sizeof(alias), "Tox%d", i); |
105 | 40 | nodes[i] = tox_scenario_add_node_ex(s, alias, peer_script, &states[i], sizeof(State), opts_peer); |
106 | | |
107 | | // All peers use the Relay for TCP and DHT bootstrapping |
108 | 40 | tox_add_tcp_relay(tox_node_get_tox(nodes[i]), "127.0.0.1", relay_tcp_port, relay_dht_id, nullptr); |
109 | 40 | tox_node_bootstrap(nodes[i], relay); |
110 | 40 | } |
111 | 1 | tox_options_free(opts_peer); |
112 | | |
113 | 1 | struct { |
114 | 1 | uint16_t t1; |
115 | 1 | uint16_t t2; |
116 | 1 | } pairs[NUM_FRIEND_PAIRS]; |
117 | | |
118 | | // Generate friend pairs |
119 | 51 | for (int i = 0; i < NUM_FRIEND_PAIRS; ++i) { |
120 | 50 | bool unique; |
121 | 51 | do { |
122 | 51 | unique = true; |
123 | 51 | pairs[i].t1 = rand() % NUM_TOXES; |
124 | 51 | pairs[i].t2 = (pairs[i].t1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; |
125 | | |
126 | | // Avoid reciprocal or duplicate pairs |
127 | 1.28k | for (int j = 0; j < i; ++j) { |
128 | 1.23k | if ((pairs[j].t1 == pairs[i].t1 && pairs[j].t2 == pairs[i].t2) || |
129 | 1.23k | (pairs[j].t1 == pairs[i].t2 && pairs[j].t2 == pairs[i].t1)) { |
130 | 1 | unique = false; |
131 | 1 | break; |
132 | 1 | } |
133 | 1.23k | } |
134 | 51 | } while (!unique); |
135 | | |
136 | 50 | uint8_t addr[TOX_ADDRESS_SIZE]; |
137 | 50 | tox_self_get_address(tox_node_get_tox(nodes[pairs[i].t1]), addr); |
138 | | |
139 | 50 | Tox_Err_Friend_Add err; |
140 | 50 | tox_friend_add(tox_node_get_tox(nodes[pairs[i].t2]), addr, (const uint8_t *)FR_MESSAGE, 6, &err); |
141 | 50 | ck_assert(err == TOX_ERR_FRIEND_ADD_OK); |
142 | | |
143 | 50 | states[pairs[i].t1].expected_friends++; |
144 | 50 | states[pairs[i].t2].expected_friends++; |
145 | 50 | } |
146 | | |
147 | 1 | ToxScenarioStatus res = tox_scenario_run(s); |
148 | 1 | if (res != TOX_SCENARIO_DONE) { |
149 | 0 | tox_scenario_log(s, "Test failed with status %u", res); |
150 | 0 | return 1; |
151 | 0 | } |
152 | | |
153 | 1 | tox_scenario_free(s); |
154 | 1 | return 0; |
155 | 1 | } |
156 | | |
157 | | #undef FR_MESSAGE |
158 | | #undef NUM_TOXES |
159 | | #undef NUM_FRIEND_PAIRS |
160 | | #undef RELAY_TCP_PORT |