summaryrefslogtreecommitdiff
path: root/auto_tests
diff options
context:
space:
mode:
authorzugz <mbays+tox@sdf.org>2018-07-25 08:46:28 +0100
committerzugz (tox) <mbays+tox@sdf.org>2018-08-02 22:03:18 +0100
commitaa63c1330c49ee1c4b675037e2e54516950203a2 (patch)
treed9064b106cd62e7c7fbc212c4dbaefc0298d977b /auto_tests
parent3a4987da18b165d5a5297ddaf64f1336958bb3c7 (diff)
Fix problems with initial connections and name-setting in conferences
* test names in conference_test * raise error on attempt to invite friend to group before we are connected * revise handling of temporary invited connections We are now careful not to prematurely delete a connection to a peer established during the invitation process; namely, before we have sufficient other connections and have confirmed that we have an alternative route to the peer. * process out-of-order messages from a peer * don't reset names when handling a Peer Response
Diffstat (limited to 'auto_tests')
-rw-r--r--auto_tests/conference_simple_test.c34
-rw-r--r--auto_tests/conference_test.c141
2 files changed, 124 insertions, 51 deletions
diff --git a/auto_tests/conference_simple_test.c b/auto_tests/conference_simple_test.c
index 8d95bba8..f2bede30 100644
--- a/auto_tests/conference_simple_test.c
+++ b/auto_tests/conference_simple_test.c
@@ -13,6 +13,7 @@ typedef struct State {
13 uint32_t id; 13 uint32_t id;
14 bool self_online; 14 bool self_online;
15 bool friend_online; 15 bool friend_online;
16 bool invited_next;
16 17
17 bool joined; 18 bool joined;
18 uint32_t conference; 19 uint32_t conference;
@@ -55,19 +56,6 @@ static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFE
55 fprintf(stderr, "tox%d Joined conference %d\n", state->id, state->conference); 56 fprintf(stderr, "tox%d Joined conference %d\n", state->id, state->conference);
56 state->joined = true; 57 state->joined = true;
57 } 58 }
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} 59}
72 60
73static void handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, 61static void handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number,
@@ -99,6 +87,25 @@ static void handle_conference_peer_list_changed(Tox *tox, uint32_t conference_nu
99 87
100 fprintf(stderr, "tox%d has %d peers online\n", state->id, count); 88 fprintf(stderr, "tox%d has %d peers online\n", state->id, count);
101 state->peers = count; 89 state->peers = count;
90
91 // We're tox2, so now we invite tox3.
92 if (state->id == 2 && !state->invited_next) {
93 // TODO(zugz): neater way to determine whether we are connected, and when
94 // we become so
95 TOX_ERR_CONFERENCE_PEER_QUERY peer_err;
96 tox_conference_peer_number_is_ours(tox, 0, 0, &peer_err);
97
98 if (peer_err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
99 return;
100 }
101
102 TOX_ERR_CONFERENCE_INVITE err;
103 tox_conference_invite(tox, 1, state->conference, &err);
104 ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox2 failed to invite tox3: err = %d", err);
105
106 state->invited_next = true;
107 fprintf(stderr, "tox2 invited tox3\n");
108 }
102} 109}
103 110
104int main(void) 111int main(void)
@@ -195,6 +202,7 @@ int main(void)
195 TOX_ERR_CONFERENCE_INVITE err; 202 TOX_ERR_CONFERENCE_INVITE err;
196 tox_conference_invite(tox1, 0, state1.conference, &err); 203 tox_conference_invite(tox1, 0, state1.conference, &err);
197 ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "failed to invite a friend: err = %d", err); 204 ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "failed to invite a friend: err = %d", err);
205 state1.invited_next = true;
198 fprintf(stderr, "tox1 invited tox2\n"); 206 fprintf(stderr, "tox1 invited tox2\n");
199 } 207 }
200 208
diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c
index 2bdee78f..fd4f41b4 100644
--- a/auto_tests/conference_test.c
+++ b/auto_tests/conference_test.c
@@ -15,30 +15,39 @@
15#include "../toxcore/util.h" 15#include "../toxcore/util.h"
16#include "check_compat.h" 16#include "check_compat.h"
17 17
18#define NUM_GROUP_TOX 5 18#define NUM_GROUP_TOX 16
19#define GROUP_MESSAGE "Install Gentoo" 19#define GROUP_MESSAGE "Install Gentoo"
20 20
21#define NAME_FORMAT_STR "Tox #%4u"
22#define NAMELEN 9
23#define NAME_FORMAT "%9s"
24
25typedef struct State {
26 uint32_t id;
27 bool invited_next;
28} State;
29
21static void handle_self_connection_status( 30static void handle_self_connection_status(
22 Tox *tox, TOX_CONNECTION connection_status, void *user_data) 31 Tox *tox, TOX_CONNECTION connection_status, void *user_data)
23{ 32{
24 const uint16_t id = *(uint16_t *)user_data; 33 const State *state = (State *)user_data;
25 34
26 if (connection_status != TOX_CONNECTION_NONE) { 35 if (connection_status != TOX_CONNECTION_NONE) {
27 printf("tox #%d: is now connected\n", id); 36 printf("tox #%u: is now connected\n", state->id);
28 } else { 37 } else {
29 printf("tox #%d: is now disconnected\n", id); 38 printf("tox #%u: is now disconnected\n", state->id);
30 } 39 }
31} 40}
32 41
33static void handle_friend_connection_status( 42static void handle_friend_connection_status(
34 Tox *tox, uint32_t friendnumber, TOX_CONNECTION connection_status, void *user_data) 43 Tox *tox, uint32_t friendnumber, TOX_CONNECTION connection_status, void *user_data)
35{ 44{
36 const uint16_t id = *(uint16_t *)user_data; 45 const State *state = (State *)user_data;
37 46
38 if (connection_status != TOX_CONNECTION_NONE) { 47 if (connection_status != TOX_CONNECTION_NONE) {
39 printf("tox #%d: is now connected to friend %d\n", id, friendnumber); 48 printf("tox #%u: is now connected to friend %u\n", state->id, friendnumber);
40 } else { 49 } else {
41 printf("tox #%d: is now disconnected from friend %d\n", id, friendnumber); 50 printf("tox #%u: is now disconnected from friend %u\n", state->id, friendnumber);
42 } 51 }
43} 52}
44 53
@@ -46,26 +55,44 @@ static void handle_conference_invite(
46 Tox *tox, uint32_t friendnumber, TOX_CONFERENCE_TYPE type, 55 Tox *tox, uint32_t friendnumber, TOX_CONFERENCE_TYPE type,
47 const uint8_t *data, size_t length, void *user_data) 56 const uint8_t *data, size_t length, void *user_data)
48{ 57{
49 const uint16_t id = *(uint16_t *)user_data; 58 const State *state = (State *)user_data;
50 ck_assert_msg(type == TOX_CONFERENCE_TYPE_TEXT, "tox #%d: wrong conference type: %d", id, type); 59 ck_assert_msg(type == TOX_CONFERENCE_TYPE_TEXT, "tox #%u: wrong conference type: %d", state->id, type);
51 60
52 TOX_ERR_CONFERENCE_JOIN err; 61 TOX_ERR_CONFERENCE_JOIN err;
53 uint32_t g_num = tox_conference_join(tox, friendnumber, data, length, &err); 62 uint32_t g_num = tox_conference_join(tox, friendnumber, data, length, &err);
54 63
55 ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "tox #%d: error joining group: %d", id, err); 64 ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "tox #%u: error joining group: %d", state->id, err);
56 ck_assert_msg(g_num == 0, "tox #%d: group number was not 0", id); 65 ck_assert_msg(g_num == 0, "tox #%u: group number was not 0", state->id);
57 66
58 // Try joining again. We should only be allowed to join once. 67 // Try joining again. We should only be allowed to join once.
59 tox_conference_join(tox, friendnumber, data, length, &err); 68 tox_conference_join(tox, friendnumber, data, length, &err);
60 ck_assert_msg(err != TOX_ERR_CONFERENCE_JOIN_OK, 69 ck_assert_msg(err != TOX_ERR_CONFERENCE_JOIN_OK,
61 "tox #%d: joining groupchat twice should be impossible.", id); 70 "tox #%u: joining groupchat twice should be impossible.", state->id);
71}
62 72
63 if (tox_self_get_friend_list_size(tox) > 1) { 73static void handle_conference_peer_list_changed(
64 printf("tox #%d: inviting next friend\n", id); 74 Tox *tox, uint32_t conference_number, void *user_data)
65 ck_assert_msg(tox_conference_invite(tox, 1, g_num, nullptr) != 0, "failed to invite friend"); 75{
66 } else { 76 State *state = (State *)user_data;
67 printf("tox #%d was the last tox, no further invites happening\n", id); 77
78 if (state->invited_next || tox_self_get_friend_list_size(tox) <= 1) {
79 return;
68 } 80 }
81
82 // TODO(zugz): neater way to determine whether we are connected, and when
83 // we become so
84 TOX_ERR_CONFERENCE_PEER_QUERY peer_err;
85 tox_conference_peer_number_is_ours(tox, 0, 0, &peer_err);
86
87 if (peer_err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
88 return;
89 }
90
91 TOX_ERR_CONFERENCE_INVITE err;
92 tox_conference_invite(tox, 1, 0, &err);
93 ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox #%u failed to invite next friend: err = %d", state->id, err);
94 printf("tox #%u: invited next friend\n", state->id);
95 state->invited_next = true;
69} 96}
70 97
71static uint16_t num_recv; 98static uint16_t num_recv;
@@ -79,7 +106,7 @@ static void handle_conference_message(
79 } 106 }
80} 107}
81 108
82static void run_conference_tests(Tox **toxes, uint32_t *tox_index) 109static void run_conference_tests(Tox **toxes, State *state)
83{ 110{
84 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 111 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
85 tox_callback_conference_message(toxes[i], &handle_conference_message); 112 tox_callback_conference_message(toxes[i], &handle_conference_message);
@@ -96,7 +123,7 @@ static void run_conference_tests(Tox **toxes, uint32_t *tox_index)
96 123
97 for (uint8_t j = 0; j < 20; ++j) { 124 for (uint8_t j = 0; j < 20; ++j) {
98 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 125 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
99 tox_iterate(toxes[i], &tox_index[i]); 126 tox_iterate(toxes[i], &state[i]);
100 } 127 }
101 128
102 c_sleep(25); 129 c_sleep(25);
@@ -105,12 +132,27 @@ static void run_conference_tests(Tox **toxes, uint32_t *tox_index)
105 c_sleep(25); 132 c_sleep(25);
106 ck_assert_msg(num_recv == NUM_GROUP_TOX, "failed to recv group messages"); 133 ck_assert_msg(num_recv == NUM_GROUP_TOX, "failed to recv group messages");
107 134
135 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
136 for (uint16_t j = 0; j < NUM_GROUP_TOX; ++j) {
137 const size_t len = tox_conference_peer_get_name_size(toxes[i], 0, j, nullptr);
138 ck_assert_msg(len == NAMELEN, "name of #%u according to #%u has incorrect length %u", state[j].id, state[i].id,
139 (unsigned int)len);
140 uint8_t name[NAMELEN];
141 tox_conference_peer_get_name(toxes[i], 0, j, name, nullptr);
142 char expected_name[NAMELEN + 1];
143 snprintf(expected_name, NAMELEN + 1, NAME_FORMAT_STR, state[j].id);
144 ck_assert_msg(memcmp(name, expected_name, NAMELEN) == 0,
145 "name of #%u according to #%u is \"" NAME_FORMAT "\"; expected \"%s\"",
146 state[j].id, state[i].id, name, expected_name);
147 }
148 }
149
108 for (uint16_t k = NUM_GROUP_TOX; k != 0 ; --k) { 150 for (uint16_t k = NUM_GROUP_TOX; k != 0 ; --k) {
109 tox_conference_delete(toxes[k - 1], 0, nullptr); 151 tox_conference_delete(toxes[k - 1], 0, nullptr);
110 152
111 for (uint8_t j = 0; j < 10; ++j) { 153 for (uint8_t j = 0; j < 10; ++j) {
112 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 154 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
113 tox_iterate(toxes[i], &tox_index[i]); 155 tox_iterate(toxes[i], &state[i]);
114 } 156 }
115 157
116 c_sleep(50); 158 c_sleep(50);
@@ -130,7 +172,8 @@ static void test_many_group(void)
130 const time_t test_start_time = time(nullptr); 172 const time_t test_start_time = time(nullptr);
131 173
132 Tox *toxes[NUM_GROUP_TOX]; 174 Tox *toxes[NUM_GROUP_TOX];
133 uint32_t tox_index[NUM_GROUP_TOX]; 175 State state[NUM_GROUP_TOX];
176 memset(state, 0, NUM_GROUP_TOX * sizeof(State));
134 time_t cur_time = time(nullptr); 177 time_t cur_time = time(nullptr);
135 struct Tox_Options *opts = tox_options_new(nullptr); 178 struct Tox_Options *opts = tox_options_new(nullptr);
136 tox_options_set_start_port(opts, 33445); 179 tox_options_set_start_port(opts, 33445);
@@ -140,13 +183,18 @@ static void test_many_group(void)
140 183
141 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 184 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
142 TOX_ERR_NEW err; 185 TOX_ERR_NEW err;
143 tox_index[i] = i + 1; 186 state[i].id = i + 1;
144 toxes[i] = tox_new_log(opts, &err, &tox_index[i]); 187 toxes[i] = tox_new_log(opts, &err, &state[i]);
145 188
146 ck_assert_msg(toxes[i] != nullptr, "failed to create tox instance %u: error %d", i, err); 189 ck_assert_msg(toxes[i] != nullptr, "failed to create tox instance %u: error %d", i, err);
147 tox_callback_self_connection_status(toxes[i], &handle_self_connection_status); 190 tox_callback_self_connection_status(toxes[i], &handle_self_connection_status);
148 tox_callback_friend_connection_status(toxes[i], &handle_friend_connection_status); 191 tox_callback_friend_connection_status(toxes[i], &handle_friend_connection_status);
149 tox_callback_conference_invite(toxes[i], &handle_conference_invite); 192 tox_callback_conference_invite(toxes[i], &handle_conference_invite);
193 tox_callback_conference_peer_list_changed(toxes[i], &handle_conference_peer_list_changed);
194
195 char name[NAMELEN + 1];
196 snprintf(name, NAMELEN + 1, NAME_FORMAT_STR, state[i].id);
197 tox_self_set_name(toxes[i], (const uint8_t *)name, NAMELEN, nullptr);
150 198
151 if (i != 0) { 199 if (i != 0) {
152 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; 200 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
@@ -181,11 +229,11 @@ static void test_many_group(void)
181 online_count = 0; 229 online_count = 0;
182 230
183 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 231 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
184 tox_iterate(toxes[i], &tox_index[i]); 232 tox_iterate(toxes[i], &state[i]);
185 online_count += tox_friend_get_connection_status(toxes[i], 0, nullptr) != TOX_CONNECTION_NONE; 233 online_count += tox_friend_get_connection_status(toxes[i], 0, nullptr) != TOX_CONNECTION_NONE;
186 } 234 }
187 235
188 printf("currently %d toxes are online\n", online_count); 236 printf("currently %u toxes are online\n", online_count);
189 fflush(stdout); 237 fflush(stdout);
190 238
191 c_sleep(1000); 239 c_sleep(1000);
@@ -194,40 +242,57 @@ static void test_many_group(void)
194 printf("friends connected, took %d seconds\n", (int)(time(nullptr) - cur_time)); 242 printf("friends connected, took %d seconds\n", (int)(time(nullptr) - cur_time));
195 243
196 ck_assert_msg(tox_conference_new(toxes[0], nullptr) != UINT32_MAX, "failed to create group"); 244 ck_assert_msg(tox_conference_new(toxes[0], nullptr) != UINT32_MAX, "failed to create group");
197 printf("tox #%d: inviting its first friend\n", tox_index[0]); 245 printf("tox #%u: inviting its first friend\n", state[0].id);
198 ck_assert_msg(tox_conference_invite(toxes[0], 0, 0, nullptr) != 0, "failed to invite friend"); 246 ck_assert_msg(tox_conference_invite(toxes[0], 0, 0, nullptr) != 0, "failed to invite friend");
247 state[0].invited_next = true;
199 ck_assert_msg(tox_conference_set_title(toxes[0], 0, (const uint8_t *)"Gentoo", sizeof("Gentoo") - 1, nullptr) != 0, 248 ck_assert_msg(tox_conference_set_title(toxes[0], 0, (const uint8_t *)"Gentoo", sizeof("Gentoo") - 1, nullptr) != 0,
200 "failed to set group title"); 249 "failed to set group title");
201 250
202 // One iteration for all the invitations to happen. 251
203 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 252 printf("waiting for invitations to be made\n");
204 tox_iterate(toxes[i], &tox_index[i]); 253 uint16_t invited_count = 0;
254
255 while (invited_count != NUM_GROUP_TOX - 1) {
256 invited_count = 0;
257
258 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
259 tox_iterate(toxes[i], &state[i]);
260 invited_count += state[i].invited_next;
261 }
262
263 c_sleep(50);
205 } 264 }
206 265
207 cur_time = time(nullptr); 266 cur_time = time(nullptr);
208 printf("waiting for all toxes to be in the group\n"); 267 printf("waiting for all toxes to be in the group\n");
209 unsigned invited_count = 0; 268 uint16_t fully_connected_count = 0;
210 269
211 while (invited_count != NUM_GROUP_TOX) { 270 while (fully_connected_count != NUM_GROUP_TOX) {
212 invited_count = 0; 271 fully_connected_count = 0;
213 printf("current peer counts: ["); 272 printf("current peer counts: [");
214 273
215 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 274 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
216 tox_iterate(toxes[i], &tox_index[i]); 275 tox_iterate(toxes[i], &state[i]);
217 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); 276 TOX_ERR_CONFERENCE_PEER_QUERY err;
218 invited_count += peer_count == NUM_GROUP_TOX; 277 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, &err);
278
279 if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
280 peer_count = 0;
281 }
282
283 fully_connected_count += peer_count == NUM_GROUP_TOX;
219 284
220 if (i != 0) { 285 if (i != 0) {
221 printf(", "); 286 printf(", ");
222 } 287 }
223 288
224 printf("%d", peer_count); 289 printf("%u", peer_count);
225 } 290 }
226 291
227 printf("]\n"); 292 printf("]\n");
228 fflush(stdout); 293 fflush(stdout);
229 294
230 c_sleep(1000); 295 c_sleep(200);
231 } 296 }
232 297
233 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) { 298 for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
@@ -246,7 +311,7 @@ static void test_many_group(void)
246 311
247 printf("group connected, took %d seconds\n", (int)(time(nullptr) - cur_time)); 312 printf("group connected, took %d seconds\n", (int)(time(nullptr) - cur_time));
248 313
249 run_conference_tests(toxes, tox_index); 314 run_conference_tests(toxes, state);
250 315
251 printf("tearing down toxes\n"); 316 printf("tearing down toxes\n");
252 317