diff options
Diffstat (limited to 'auto_tests/conference_simple_test.c')
-rw-r--r-- | auto_tests/conference_simple_test.c | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/auto_tests/conference_simple_test.c b/auto_tests/conference_simple_test.c new file mode 100644 index 00000000..3e2f0f94 --- /dev/null +++ b/auto_tests/conference_simple_test.c | |||
@@ -0,0 +1,254 @@ | |||
1 | #ifndef _XOPEN_SOURCE | ||
2 | #define _XOPEN_SOURCE 600 | ||
3 | #endif | ||
4 | |||
5 | #include "../toxcore/tox.h" | ||
6 | |||
7 | #include <assert.h> | ||
8 | #include <stdlib.h> | ||
9 | |||
10 | #include "helpers.h" | ||
11 | |||
12 | typedef struct State { | ||
13 | uint32_t id; | ||
14 | bool self_online; | ||
15 | bool friend_online; | ||
16 | |||
17 | bool joined; | ||
18 | uint32_t conference; | ||
19 | |||
20 | bool received; | ||
21 | |||
22 | uint32_t peers; | ||
23 | } State; | ||
24 | |||
25 | static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) | ||
26 | { | ||
27 | State *state = (State *)user_data; | ||
28 | |||
29 | fprintf(stderr, "self_connection_status(#%d, %d, _)\n", state->id, connection_status); | ||
30 | state->self_online = connection_status != TOX_CONNECTION_NONE; | ||
31 | } | ||
32 | |||
33 | static void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, | ||
34 | void *user_data) | ||
35 | { | ||
36 | State *state = (State *)user_data; | ||
37 | |||
38 | fprintf(stderr, "handle_friend_connection_status(#%d, %d, %d, _)\n", state->id, friend_number, connection_status); | ||
39 | state->friend_online = connection_status != TOX_CONNECTION_NONE; | ||
40 | } | ||
41 | |||
42 | static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, const uint8_t *cookie, | ||
43 | size_t length, void *user_data) | ||
44 | { | ||
45 | State *state = (State *)user_data; | ||
46 | |||
47 | fprintf(stderr, "handle_conference_invite(#%d, %d, %d, uint8_t[%u], _)\n", | ||
48 | state->id, friend_number, type, (unsigned)length); | ||
49 | fprintf(stderr, "tox%d joining conference\n", state->id); | ||
50 | |||
51 | { | ||
52 | TOX_ERR_CONFERENCE_JOIN err; | ||
53 | state->conference = tox_conference_join(tox, friend_number, cookie, length, &err); | ||
54 | assert(err == TOX_ERR_CONFERENCE_JOIN_OK); | ||
55 | fprintf(stderr, "tox%d Joined conference %d\n", state->id, state->conference); | ||
56 | state->joined = true; | ||
57 | } | ||
58 | |||
59 | // We're tox2, so now we invite tox3. | ||
60 | if (state->id == 2) { | ||
61 | TOX_ERR_CONFERENCE_INVITE err; | ||
62 | tox_conference_invite(tox, 1, state->conference, &err); | ||
63 | |||
64 | if (err != TOX_ERR_CONFERENCE_INVITE_OK) { | ||
65 | fprintf(stderr, "ERROR: %d\n", err); | ||
66 | exit(EXIT_FAILURE); | ||
67 | } | ||
68 | |||
69 | fprintf(stderr, "tox2 invited tox3\n"); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | static void handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, | ||
74 | TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *user_data) | ||
75 | { | ||
76 | State *state = (State *)user_data; | ||
77 | |||
78 | fprintf(stderr, "handle_conference_message(#%d, %d, %d, %d, uint8_t[%u], _)\n", | ||
79 | state->id, conference_number, peer_number, type, (unsigned)length); | ||
80 | |||
81 | fprintf(stderr, "tox%d got message: %s\n", state->id, (const char *)message); | ||
82 | state->received = true; | ||
83 | } | ||
84 | |||
85 | static void handle_conference_peer_list_changed(Tox *tox, uint32_t conference_number, void *user_data) | ||
86 | { | ||
87 | State *state = (State *)user_data; | ||
88 | |||
89 | fprintf(stderr, "handle_conference_peer_list_changed(#%d, %d, _)\n", | ||
90 | state->id, conference_number); | ||
91 | |||
92 | TOX_ERR_CONFERENCE_PEER_QUERY err; | ||
93 | uint32_t count = tox_conference_peer_count(tox, conference_number, &err); | ||
94 | |||
95 | if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) { | ||
96 | fprintf(stderr, "ERROR: %d\n", err); | ||
97 | exit(EXIT_FAILURE); | ||
98 | } | ||
99 | |||
100 | fprintf(stderr, "tox%d has %d peers online\n", state->id, count); | ||
101 | state->peers = count; | ||
102 | } | ||
103 | |||
104 | int main(void) | ||
105 | { | ||
106 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
107 | |||
108 | State state1 = {1}; | ||
109 | State state2 = {2}; | ||
110 | State state3 = {3}; | ||
111 | |||
112 | // Create toxes. | ||
113 | Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id); | ||
114 | Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id); | ||
115 | Tox *tox3 = tox_new_log(nullptr, nullptr, &state3.id); | ||
116 | |||
117 | // tox1 <-> tox2, tox2 <-> tox3 | ||
118 | uint8_t key[TOX_PUBLIC_KEY_SIZE]; | ||
119 | tox_self_get_public_key(tox2, key); | ||
120 | tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2 | ||
121 | tox_self_get_public_key(tox1, key); | ||
122 | tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1 | ||
123 | tox_self_get_public_key(tox3, key); | ||
124 | tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox3 | ||
125 | tox_self_get_public_key(tox2, key); | ||
126 | tox_friend_add_norequest(tox3, key, nullptr); // tox3 -> tox2 | ||
127 | |||
128 | printf("bootstrapping tox2 and tox3 off tox1\n"); | ||
129 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
130 | tox_self_get_dht_id(tox1, dht_key); | ||
131 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
132 | |||
133 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
134 | tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr); | ||
135 | |||
136 | // Connection callbacks. | ||
137 | tox_callback_self_connection_status(tox1, handle_self_connection_status); | ||
138 | tox_callback_self_connection_status(tox2, handle_self_connection_status); | ||
139 | tox_callback_self_connection_status(tox3, handle_self_connection_status); | ||
140 | |||
141 | tox_callback_friend_connection_status(tox1, handle_friend_connection_status); | ||
142 | tox_callback_friend_connection_status(tox2, handle_friend_connection_status); | ||
143 | tox_callback_friend_connection_status(tox3, handle_friend_connection_status); | ||
144 | |||
145 | // Conference callbacks. | ||
146 | tox_callback_conference_invite(tox1, handle_conference_invite); | ||
147 | tox_callback_conference_invite(tox2, handle_conference_invite); | ||
148 | tox_callback_conference_invite(tox3, handle_conference_invite); | ||
149 | |||
150 | tox_callback_conference_message(tox1, handle_conference_message); | ||
151 | tox_callback_conference_message(tox2, handle_conference_message); | ||
152 | tox_callback_conference_message(tox3, handle_conference_message); | ||
153 | |||
154 | tox_callback_conference_peer_list_changed(tox1, handle_conference_peer_list_changed); | ||
155 | tox_callback_conference_peer_list_changed(tox2, handle_conference_peer_list_changed); | ||
156 | tox_callback_conference_peer_list_changed(tox3, handle_conference_peer_list_changed); | ||
157 | |||
158 | // Wait for self connection. | ||
159 | fprintf(stderr, "Waiting for toxes to come online\n"); | ||
160 | |||
161 | while (!state1.self_online || !state2.self_online || !state3.self_online) { | ||
162 | tox_iterate(tox1, &state1); | ||
163 | tox_iterate(tox2, &state2); | ||
164 | tox_iterate(tox3, &state3); | ||
165 | |||
166 | c_sleep(100); | ||
167 | } | ||
168 | |||
169 | fprintf(stderr, "Toxes are online\n"); | ||
170 | |||
171 | // Wait for friend connection. | ||
172 | fprintf(stderr, "Waiting for friends to connect\n"); | ||
173 | |||
174 | while (!state1.friend_online || !state2.friend_online || !state3.friend_online) { | ||
175 | tox_iterate(tox1, &state1); | ||
176 | tox_iterate(tox2, &state2); | ||
177 | tox_iterate(tox3, &state3); | ||
178 | |||
179 | c_sleep(100); | ||
180 | } | ||
181 | |||
182 | fprintf(stderr, "Friends are connected\n"); | ||
183 | |||
184 | { | ||
185 | // Create new conference, tox1 is the founder. | ||
186 | TOX_ERR_CONFERENCE_NEW err; | ||
187 | state1.conference = tox_conference_new(tox1, &err); | ||
188 | state1.joined = true; | ||
189 | assert(err == TOX_ERR_CONFERENCE_NEW_OK); | ||
190 | fprintf(stderr, "Created conference: id=%d\n", state1.conference); | ||
191 | } | ||
192 | |||
193 | { | ||
194 | // Invite friend. | ||
195 | TOX_ERR_CONFERENCE_INVITE err; | ||
196 | tox_conference_invite(tox1, 0, state1.conference, &err); | ||
197 | assert(err == TOX_ERR_CONFERENCE_INVITE_OK); | ||
198 | fprintf(stderr, "tox1 invited tox2\n"); | ||
199 | } | ||
200 | |||
201 | fprintf(stderr, "Waiting for invitation to arrive\n"); | ||
202 | |||
203 | while (!state1.joined || !state2.joined || !state3.joined) { | ||
204 | tox_iterate(tox1, &state1); | ||
205 | tox_iterate(tox2, &state2); | ||
206 | tox_iterate(tox3, &state3); | ||
207 | |||
208 | c_sleep(100); | ||
209 | } | ||
210 | |||
211 | fprintf(stderr, "Invitations accepted\n"); | ||
212 | |||
213 | fprintf(stderr, "Waiting for peers to come online\n"); | ||
214 | |||
215 | while (state1.peers == 0 || state2.peers == 0 || state3.peers == 0) { | ||
216 | tox_iterate(tox1, &state1); | ||
217 | tox_iterate(tox2, &state2); | ||
218 | tox_iterate(tox3, &state3); | ||
219 | |||
220 | c_sleep(100); | ||
221 | } | ||
222 | |||
223 | fprintf(stderr, "All peers are online\n"); | ||
224 | |||
225 | { | ||
226 | fprintf(stderr, "tox1 sends a message to the group: \"hello!\"\n"); | ||
227 | TOX_ERR_CONFERENCE_SEND_MESSAGE err; | ||
228 | tox_conference_send_message(tox1, state1.conference, TOX_MESSAGE_TYPE_NORMAL, | ||
229 | (const uint8_t *)"hello!", 7, &err); | ||
230 | |||
231 | if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) { | ||
232 | fprintf(stderr, "ERROR: %d\n", err); | ||
233 | exit(EXIT_FAILURE); | ||
234 | } | ||
235 | } | ||
236 | |||
237 | fprintf(stderr, "Waiting for messages to arrive\n"); | ||
238 | |||
239 | while (!state2.received || !state3.received) { | ||
240 | tox_iterate(tox1, &state1); | ||
241 | tox_iterate(tox2, &state2); | ||
242 | tox_iterate(tox3, &state3); | ||
243 | |||
244 | c_sleep(100); | ||
245 | } | ||
246 | |||
247 | fprintf(stderr, "Messages received. Test complete.\n"); | ||
248 | |||
249 | tox_kill(tox3); | ||
250 | tox_kill(tox2); | ||
251 | tox_kill(tox1); | ||
252 | |||
253 | return 0; | ||
254 | } | ||