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