diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-06-24 00:41:09 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-24 01:09:46 +0000 |
commit | 5a8790eab0ed9421445adce40526462b9e6742c3 (patch) | |
tree | d5ed380b73ac3de24e434400271856369078deca /auto_tests | |
parent | ca75c014a41c2d207f705f49a9e0ce696dc0f0a7 (diff) |
Add a test for double conference invite.
In Persistent Group Chats (PGC), this will cause a use-after-free. This
test ensures that we fix this bug before merging PGC.
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/conference_double_invite_test.c | 154 | ||||
-rw-r--r-- | auto_tests/simple_conference_test.c | 45 |
2 files changed, 174 insertions, 25 deletions
diff --git a/auto_tests/conference_double_invite_test.c b/auto_tests/conference_double_invite_test.c new file mode 100644 index 00000000..ea85de10 --- /dev/null +++ b/auto_tests/conference_double_invite_test.c | |||
@@ -0,0 +1,154 @@ | |||
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 | } State; | ||
20 | |||
21 | static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data) | ||
22 | { | ||
23 | State *state = (State *)user_data; | ||
24 | |||
25 | fprintf(stderr, "self_connection_status(#%u, %d, _)\n", state->id, connection_status); | ||
26 | state->self_online = connection_status != TOX_CONNECTION_NONE; | ||
27 | } | ||
28 | |||
29 | static void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, | ||
30 | void *user_data) | ||
31 | { | ||
32 | State *state = (State *)user_data; | ||
33 | |||
34 | fprintf(stderr, "handle_friend_connection_status(#%u, %u, %d, _)\n", state->id, friend_number, connection_status); | ||
35 | state->friend_online = connection_status != TOX_CONNECTION_NONE; | ||
36 | } | ||
37 | |||
38 | static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, const uint8_t *cookie, | ||
39 | size_t length, void *user_data) | ||
40 | { | ||
41 | State *state = (State *)user_data; | ||
42 | |||
43 | fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n", | ||
44 | state->id, friend_number, type, (unsigned)length); | ||
45 | fprintf(stderr, "tox%u joining conference\n", state->id); | ||
46 | |||
47 | if (friend_number != -1) { | ||
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%u Joined conference %u\n", state->id, state->conference); | ||
52 | state->joined = true; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | int main(void) | ||
57 | { | ||
58 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
59 | |||
60 | State state1 = {1}; | ||
61 | State state2 = {2}; | ||
62 | |||
63 | // Create toxes. | ||
64 | Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id); | ||
65 | Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id); | ||
66 | |||
67 | // tox1 <-> tox2 | ||
68 | uint8_t key[TOX_PUBLIC_KEY_SIZE]; | ||
69 | tox_self_get_public_key(tox2, key); | ||
70 | tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2 | ||
71 | tox_self_get_public_key(tox1, key); | ||
72 | tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1 | ||
73 | |||
74 | printf("bootstrapping tox2 off tox1\n"); | ||
75 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
76 | tox_self_get_dht_id(tox1, dht_key); | ||
77 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
78 | |||
79 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
80 | |||
81 | // Connection callbacks. | ||
82 | tox_callback_self_connection_status(tox1, handle_self_connection_status); | ||
83 | tox_callback_self_connection_status(tox2, handle_self_connection_status); | ||
84 | |||
85 | tox_callback_friend_connection_status(tox1, handle_friend_connection_status); | ||
86 | tox_callback_friend_connection_status(tox2, handle_friend_connection_status); | ||
87 | |||
88 | // Conference callbacks. | ||
89 | tox_callback_conference_invite(tox1, handle_conference_invite); | ||
90 | tox_callback_conference_invite(tox2, handle_conference_invite); | ||
91 | |||
92 | // Wait for self connection. | ||
93 | fprintf(stderr, "Waiting for toxes to come online\n"); | ||
94 | |||
95 | while (!state1.self_online || !state2.self_online) { | ||
96 | tox_iterate(tox1, &state1); | ||
97 | tox_iterate(tox2, &state2); | ||
98 | |||
99 | c_sleep(100); | ||
100 | } | ||
101 | |||
102 | fprintf(stderr, "Toxes are online\n"); | ||
103 | |||
104 | // Wait for friend connection. | ||
105 | fprintf(stderr, "Waiting for friends to connect\n"); | ||
106 | |||
107 | while (!state1.friend_online || !state2.friend_online) { | ||
108 | tox_iterate(tox1, &state1); | ||
109 | tox_iterate(tox2, &state2); | ||
110 | |||
111 | c_sleep(100); | ||
112 | } | ||
113 | |||
114 | fprintf(stderr, "Friends are connected\n"); | ||
115 | |||
116 | { | ||
117 | // Create new conference, tox1 is the founder. | ||
118 | TOX_ERR_CONFERENCE_NEW err; | ||
119 | state1.conference = tox_conference_new(tox1, &err); | ||
120 | state1.joined = true; | ||
121 | assert(err == TOX_ERR_CONFERENCE_NEW_OK); | ||
122 | fprintf(stderr, "Created conference: id=%u\n", state1.conference); | ||
123 | } | ||
124 | |||
125 | { | ||
126 | // Invite friend. | ||
127 | TOX_ERR_CONFERENCE_INVITE err; | ||
128 | tox_conference_invite(tox1, 0, state1.conference, &err); | ||
129 | assert(err == TOX_ERR_CONFERENCE_INVITE_OK); | ||
130 | fprintf(stderr, "tox1 invited tox2\n"); | ||
131 | } | ||
132 | |||
133 | fprintf(stderr, "Waiting for invitation to arrive\n"); | ||
134 | |||
135 | while (!state1.joined || !state2.joined) { | ||
136 | tox_iterate(tox1, &state1); | ||
137 | tox_iterate(tox2, &state2); | ||
138 | |||
139 | c_sleep(100); | ||
140 | } | ||
141 | |||
142 | fprintf(stderr, "Invitations accepted\n"); | ||
143 | |||
144 | // Invite one more time, resulting in friend -1 inviting tox2. | ||
145 | tox_conference_invite(tox1, 0, state1.conference, 0); | ||
146 | |||
147 | tox_iterate(tox1, &state1); | ||
148 | tox_iterate(tox2, &state2); | ||
149 | |||
150 | tox_kill(tox2); | ||
151 | tox_kill(tox1); | ||
152 | |||
153 | return 0; | ||
154 | } | ||
diff --git a/auto_tests/simple_conference_test.c b/auto_tests/simple_conference_test.c index c1c6e730..3e2f0f94 100644 --- a/auto_tests/simple_conference_test.c +++ b/auto_tests/simple_conference_test.c | |||
@@ -26,7 +26,7 @@ static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_st | |||
26 | { | 26 | { |
27 | State *state = (State *)user_data; | 27 | State *state = (State *)user_data; |
28 | 28 | ||
29 | fprintf(stderr, "\nself_connection_status(#%d, %d, _)\n", state->id, connection_status); | 29 | fprintf(stderr, "self_connection_status(#%d, %d, _)\n", state->id, connection_status); |
30 | state->self_online = connection_status != TOX_CONNECTION_NONE; | 30 | state->self_online = connection_status != TOX_CONNECTION_NONE; |
31 | } | 31 | } |
32 | 32 | ||
@@ -35,7 +35,7 @@ static void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TO | |||
35 | { | 35 | { |
36 | State *state = (State *)user_data; | 36 | State *state = (State *)user_data; |
37 | 37 | ||
38 | fprintf(stderr, "\nhandle_friend_connection_status(#%d, %d, %d, _)\n", state->id, friend_number, connection_status); | 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; | 39 | state->friend_online = connection_status != TOX_CONNECTION_NONE; |
40 | } | 40 | } |
41 | 41 | ||
@@ -44,7 +44,7 @@ static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFE | |||
44 | { | 44 | { |
45 | State *state = (State *)user_data; | 45 | State *state = (State *)user_data; |
46 | 46 | ||
47 | fprintf(stderr, "\nhandle_conference_invite(#%d, %d, %d, uint8_t[%u], _)\n", | 47 | fprintf(stderr, "handle_conference_invite(#%d, %d, %d, uint8_t[%u], _)\n", |
48 | state->id, friend_number, type, (unsigned)length); | 48 | state->id, friend_number, type, (unsigned)length); |
49 | fprintf(stderr, "tox%d joining conference\n", state->id); | 49 | fprintf(stderr, "tox%d joining conference\n", state->id); |
50 | 50 | ||
@@ -75,7 +75,7 @@ static void handle_conference_message(Tox *tox, uint32_t conference_number, uint | |||
75 | { | 75 | { |
76 | State *state = (State *)user_data; | 76 | State *state = (State *)user_data; |
77 | 77 | ||
78 | fprintf(stderr, "\nhandle_conference_message(#%d, %d, %d, %d, uint8_t[%u], _)\n", | 78 | fprintf(stderr, "handle_conference_message(#%d, %d, %d, %d, uint8_t[%u], _)\n", |
79 | state->id, conference_number, peer_number, type, (unsigned)length); | 79 | state->id, conference_number, peer_number, type, (unsigned)length); |
80 | 80 | ||
81 | fprintf(stderr, "tox%d got message: %s\n", state->id, (const char *)message); | 81 | fprintf(stderr, "tox%d got message: %s\n", state->id, (const char *)message); |
@@ -86,7 +86,7 @@ static void handle_conference_peer_list_changed(Tox *tox, uint32_t conference_nu | |||
86 | { | 86 | { |
87 | State *state = (State *)user_data; | 87 | State *state = (State *)user_data; |
88 | 88 | ||
89 | fprintf(stderr, "\nhandle_conference_peer_list_changed(#%d, %d, _)\n", | 89 | fprintf(stderr, "handle_conference_peer_list_changed(#%d, %d, _)\n", |
90 | state->id, conference_number); | 90 | state->id, conference_number); |
91 | 91 | ||
92 | TOX_ERR_CONFERENCE_PEER_QUERY err; | 92 | TOX_ERR_CONFERENCE_PEER_QUERY err; |
@@ -156,32 +156,30 @@ int main(void) | |||
156 | tox_callback_conference_peer_list_changed(tox3, handle_conference_peer_list_changed); | 156 | tox_callback_conference_peer_list_changed(tox3, handle_conference_peer_list_changed); |
157 | 157 | ||
158 | // Wait for self connection. | 158 | // Wait for self connection. |
159 | fprintf(stderr, "Waiting for toxes to come online"); | 159 | fprintf(stderr, "Waiting for toxes to come online\n"); |
160 | 160 | ||
161 | while (!state1.self_online || !state2.self_online || !state3.self_online) { | 161 | while (!state1.self_online || !state2.self_online || !state3.self_online) { |
162 | tox_iterate(tox1, &state1); | 162 | tox_iterate(tox1, &state1); |
163 | tox_iterate(tox2, &state2); | 163 | tox_iterate(tox2, &state2); |
164 | tox_iterate(tox3, &state3); | 164 | tox_iterate(tox3, &state3); |
165 | 165 | ||
166 | c_sleep(1000); | 166 | c_sleep(100); |
167 | fprintf(stderr, "."); | ||
168 | } | 167 | } |
169 | 168 | ||
170 | fprintf(stderr, "\nToxes are online\n"); | 169 | fprintf(stderr, "Toxes are online\n"); |
171 | 170 | ||
172 | // Wait for friend connection. | 171 | // Wait for friend connection. |
173 | fprintf(stderr, "Waiting for friends to connect"); | 172 | fprintf(stderr, "Waiting for friends to connect\n"); |
174 | 173 | ||
175 | while (!state1.friend_online || !state2.friend_online || !state3.friend_online) { | 174 | while (!state1.friend_online || !state2.friend_online || !state3.friend_online) { |
176 | tox_iterate(tox1, &state1); | 175 | tox_iterate(tox1, &state1); |
177 | tox_iterate(tox2, &state2); | 176 | tox_iterate(tox2, &state2); |
178 | tox_iterate(tox3, &state3); | 177 | tox_iterate(tox3, &state3); |
179 | 178 | ||
180 | c_sleep(1000); | 179 | c_sleep(100); |
181 | fprintf(stderr, "."); | ||
182 | } | 180 | } |
183 | 181 | ||
184 | fprintf(stderr, "\nFriends are connected\n"); | 182 | fprintf(stderr, "Friends are connected\n"); |
185 | 183 | ||
186 | { | 184 | { |
187 | // Create new conference, tox1 is the founder. | 185 | // Create new conference, tox1 is the founder. |
@@ -200,31 +198,29 @@ int main(void) | |||
200 | fprintf(stderr, "tox1 invited tox2\n"); | 198 | fprintf(stderr, "tox1 invited tox2\n"); |
201 | } | 199 | } |
202 | 200 | ||
203 | fprintf(stderr, "Waiting for invitation to arrive"); | 201 | fprintf(stderr, "Waiting for invitation to arrive\n"); |
204 | 202 | ||
205 | while (!state1.joined || !state2.joined || !state3.joined) { | 203 | while (!state1.joined || !state2.joined || !state3.joined) { |
206 | tox_iterate(tox1, &state1); | 204 | tox_iterate(tox1, &state1); |
207 | tox_iterate(tox2, &state2); | 205 | tox_iterate(tox2, &state2); |
208 | tox_iterate(tox3, &state3); | 206 | tox_iterate(tox3, &state3); |
209 | 207 | ||
210 | c_sleep(1000); | 208 | c_sleep(100); |
211 | fprintf(stderr, "."); | ||
212 | } | 209 | } |
213 | 210 | ||
214 | fprintf(stderr, "\nInvitations accepted\n"); | 211 | fprintf(stderr, "Invitations accepted\n"); |
215 | 212 | ||
216 | fprintf(stderr, "Waiting for peers to come online"); | 213 | fprintf(stderr, "Waiting for peers to come online\n"); |
217 | 214 | ||
218 | while (state1.peers == 0 || state2.peers == 0 || state3.peers == 0) { | 215 | while (state1.peers == 0 || state2.peers == 0 || state3.peers == 0) { |
219 | tox_iterate(tox1, &state1); | 216 | tox_iterate(tox1, &state1); |
220 | tox_iterate(tox2, &state2); | 217 | tox_iterate(tox2, &state2); |
221 | tox_iterate(tox3, &state3); | 218 | tox_iterate(tox3, &state3); |
222 | 219 | ||
223 | c_sleep(1000); | 220 | c_sleep(100); |
224 | fprintf(stderr, "."); | ||
225 | } | 221 | } |
226 | 222 | ||
227 | fprintf(stderr, "\nAll peers are online\n"); | 223 | fprintf(stderr, "All peers are online\n"); |
228 | 224 | ||
229 | { | 225 | { |
230 | fprintf(stderr, "tox1 sends a message to the group: \"hello!\"\n"); | 226 | fprintf(stderr, "tox1 sends a message to the group: \"hello!\"\n"); |
@@ -238,18 +234,17 @@ int main(void) | |||
238 | } | 234 | } |
239 | } | 235 | } |
240 | 236 | ||
241 | fprintf(stderr, "Waiting for messages to arrive"); | 237 | fprintf(stderr, "Waiting for messages to arrive\n"); |
242 | 238 | ||
243 | while (!state2.received || !state3.received) { | 239 | while (!state2.received || !state3.received) { |
244 | tox_iterate(tox1, &state1); | 240 | tox_iterate(tox1, &state1); |
245 | tox_iterate(tox2, &state2); | 241 | tox_iterate(tox2, &state2); |
246 | tox_iterate(tox3, &state3); | 242 | tox_iterate(tox3, &state3); |
247 | 243 | ||
248 | c_sleep(1000); | 244 | c_sleep(100); |
249 | fprintf(stderr, "."); | ||
250 | } | 245 | } |
251 | 246 | ||
252 | fprintf(stderr, "\nMessages received. Test complete.\n"); | 247 | fprintf(stderr, "Messages received. Test complete.\n"); |
253 | 248 | ||
254 | tox_kill(tox3); | 249 | tox_kill(tox3); |
255 | tox_kill(tox2); | 250 | tox_kill(tox2); |