summaryrefslogtreecommitdiff
path: root/auto_tests/conference_test.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-01-30 01:00:06 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-02-01 14:58:00 +0000
commitd9413d557696e3aecde9d019e1738e99882d4579 (patch)
treea8ac23a6354f33afae9470cf9f7758a378eea9ca /auto_tests/conference_test.c
parentba176f767a7968092a7027754f36aa891f980643 (diff)
Improve conference test stability.
Diffstat (limited to 'auto_tests/conference_test.c')
-rw-r--r--auto_tests/conference_test.c238
1 files changed, 112 insertions, 126 deletions
diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c
index 1826324e..2fbb9c98 100644
--- a/auto_tests/conference_test.c
+++ b/auto_tests/conference_test.c
@@ -20,59 +20,61 @@
20 20
21#include "helpers.h" 21#include "helpers.h"
22 22
23#define NUM_GROUP_TOX 32 23#define NUM_GROUP_TOX 8
24 24
25static void g_accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, 25static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data)
26 void *userdata)
27{ 26{
28 if (*((uint32_t *)userdata) != 234212) { 27 int id = *(int *)user_data;
29 return;
30 }
31 28
32 if (length == 7 && memcmp("Gentoo", data, 7) == 0) { 29 if (connection_status != TOX_CONNECTION_NONE) {
33 tox_friend_add_norequest(m, public_key, nullptr); 30 printf("tox #%d: is now connected\n", id);
31 } else {
32 printf("tox #%d: is now disconnected\n", id);
34 } 33 }
35} 34}
36 35
37static Tox *invite_tox; 36static void handle_friend_connection_status(Tox *tox, uint32_t friendnumber, TOX_CONNECTION connection_status,
38static unsigned int invite_counter; 37 void *user_data)
39
40static void print_group_invite_callback(Tox *tox, uint32_t friendnumber, TOX_CONFERENCE_TYPE type, const uint8_t *data,
41 size_t length,
42 void *userdata)
43{ 38{
44 if (*((uint32_t *)userdata) != 234212) { 39 int id = *(int *)user_data;
45 return;
46 }
47 40
48 if (type != TOX_CONFERENCE_TYPE_TEXT) { 41 if (connection_status != TOX_CONNECTION_NONE) {
49 return; 42 printf("tox #%d: is now connected to friend %d\n", id, friendnumber);
43 } else {
44 printf("tox #%d: is now disconnected from friend %d\n", id, friendnumber);
50 } 45 }
46}
51 47
52 uint32_t g_num; 48static void handle_conference_invite(Tox *tox, uint32_t friendnumber, TOX_CONFERENCE_TYPE type, const uint8_t *data,
49 size_t length, void *user_data)
50{
51 int id = *(int *)user_data;
52 ck_assert_msg(type == TOX_CONFERENCE_TYPE_TEXT, "tox #%d: wrong conference type: %d", id, type);
53 53
54 if ((g_num = tox_conference_join(tox, friendnumber, data, length, nullptr)) == UINT32_MAX) { 54 TOX_ERR_CONFERENCE_JOIN err;
55 return; 55 uint32_t g_num = tox_conference_join(tox, friendnumber, data, length, &err);
56 }
57 56
58 ck_assert_msg(g_num == 0, "Group number was not 0"); 57 ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "tox #%d: error joining group: %d", id, err);
59 ck_assert_msg(tox_conference_join(tox, friendnumber, data, length, nullptr) == -1, 58 ck_assert_msg(g_num == 0, "tox #%d: group number was not 0", id);
60 "Joining groupchat twice should be impossible.");
61 59
62 invite_tox = tox; 60 // Try joining again. We should only be allowed to join once.
63 invite_counter = 4; 61 tox_conference_join(tox, friendnumber, data, length, &err);
62 ck_assert_msg(err != TOX_ERR_CONFERENCE_JOIN_OK,
63 "tox #%d: joining groupchat twice should be impossible.", id);
64
65 if (tox_self_get_friend_list_size(tox) > 1) {
66 printf("tox #%d: inviting next friend\n", id);
67 ck_assert_msg(tox_conference_invite(tox, 1, g_num, nullptr) != 0, "Failed to invite friend");
68 } else {
69 printf("tox #%d was the last tox, no further invites happening\n", id);
70 }
64} 71}
65 72
66static unsigned int num_recv; 73static unsigned int num_recv;
67 74
68static void print_group_message(Tox *tox, uint32_t groupnumber, uint32_t peernumber, TOX_MESSAGE_TYPE type, 75static void handle_conference_message(Tox *tox, uint32_t groupnumber, uint32_t peernumber, TOX_MESSAGE_TYPE type,
69 const uint8_t *message, size_t length, 76 const uint8_t *message, size_t length, void *user_data)
70 void *userdata)
71{ 77{
72 if (*((uint32_t *)userdata) != 234212) {
73 return;
74 }
75
76 if (length == (sizeof("Install Gentoo") - 1) && memcmp(message, "Install Gentoo", sizeof("Install Gentoo") - 1) == 0) { 78 if (length == (sizeof("Install Gentoo") - 1) && memcmp(message, "Install Gentoo", sizeof("Install Gentoo") - 1) == 0) {
77 ++num_recv; 79 ++num_recv;
78 } 80 }
@@ -80,28 +82,26 @@ static void print_group_message(Tox *tox, uint32_t groupnumber, uint32_t peernum
80 82
81START_TEST(test_many_group) 83START_TEST(test_many_group)
82{ 84{
83 long long unsigned int test_start_time = time(nullptr); 85 const time_t test_start_time = time(nullptr);
84
85group_test_restart:
86 ;
87 86
88 Tox *toxes[NUM_GROUP_TOX]; 87 Tox *toxes[NUM_GROUP_TOX];
89 uint32_t tox_index[NUM_GROUP_TOX]; 88 uint32_t tox_index[NUM_GROUP_TOX];
90 unsigned int i, j, k; 89 time_t cur_time = time(nullptr);
91 uint32_t to_comp = 234212;
92 int test_run = 0;
93 long long unsigned int cur_time = time(nullptr);
94 struct Tox_Options *opts = tox_options_new(nullptr); 90 struct Tox_Options *opts = tox_options_new(nullptr);
95 /* FIXME: Currently here is problems with IPv6 */ 91 tox_options_set_start_port(opts, 33445);
96 tox_options_set_ipv6_enabled(opts, false); 92 tox_options_set_end_port(opts, 34445);
93
94 printf("creating %d toxes\n", NUM_GROUP_TOX);
97 95
98 for (i = 0; i < NUM_GROUP_TOX; ++i) { 96 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
97 TOX_ERR_NEW err;
99 tox_index[i] = i + 1; 98 tox_index[i] = i + 1;
100 toxes[i] = tox_new_log(opts, nullptr, &tox_index[i]); 99 toxes[i] = tox_new_log(opts, &err, &tox_index[i]);
101 100
102 ck_assert_msg(toxes[i] != nullptr, "Failed to create tox instances %u", i); 101 ck_assert_msg(toxes[i] != nullptr, "Failed to create tox instance %u: error %d", i, err);
103 tox_callback_friend_request(toxes[i], &g_accept_friend_request); 102 tox_callback_self_connection_status(toxes[i], &handle_self_connection_status);
104 tox_callback_conference_invite(toxes[i], &print_group_invite_callback); 103 tox_callback_friend_connection_status(toxes[i], &handle_friend_connection_status);
104 tox_callback_conference_invite(toxes[i], &handle_conference_invite);
105 } 105 }
106 106
107 tox_options_free(opts); 107 tox_options_free(opts);
@@ -114,88 +114,80 @@ group_test_restart:
114 ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); 114 ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error");
115 } 115 }
116 116
117 uint8_t address[TOX_ADDRESS_SIZE]; 117 printf("creating a chain of friends\n");
118 tox_self_get_address(toxes[NUM_GROUP_TOX - 1], address);
119 118
120 for (i = 0; i < NUM_GROUP_TOX; ++i) { 119 for (unsigned i = 1; i < NUM_GROUP_TOX; ++i) {
121 ck_assert_msg(tox_friend_add(toxes[i], address, (const uint8_t *)"Gentoo", 7, nullptr) == 0, "Failed to add friend"); 120 TOX_ERR_FRIEND_ADD err;
121 uint8_t key[TOX_PUBLIC_KEY_SIZE];
122 122
123 tox_self_get_address(toxes[i], address); 123 tox_self_get_public_key(toxes[i - 1], key);
124 tox_friend_add_norequest(toxes[i], key, &err);
125 ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend: error %d", err);
126
127 tox_self_get_public_key(toxes[i], key);
128 tox_friend_add_norequest(toxes[i - 1], key, &err);
129 ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend: error %d", err);
124 } 130 }
125 131
126 while (1) { 132 printf("waiting for everyone to come online\n");
127 for (i = 0; i < NUM_GROUP_TOX; ++i) { 133 unsigned online_count = 0;
128 if (tox_friend_get_connection_status(toxes[i], 0, nullptr) != TOX_CONNECTION_UDP) {
129 break;
130 }
131 }
132 134
133 if (i == NUM_GROUP_TOX) { 135 while (online_count != NUM_GROUP_TOX) {
134 break; 136 online_count = 0;
135 }
136 137
137 for (i = 0; i < NUM_GROUP_TOX; ++i) { 138 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
138 tox_iterate(toxes[i], &to_comp); 139 tox_iterate(toxes[i], &tox_index[i]);
140 online_count += tox_friend_get_connection_status(toxes[i], 0, nullptr) != TOX_CONNECTION_NONE;
139 } 141 }
140 142
141 c_sleep(25); 143 printf("currently %d toxes are online\n", online_count);
144 fflush(stdout);
145
146 c_sleep(1000);
142 } 147 }
143 148
144 printf("friends connected, took %llu seconds\n", time(nullptr) - cur_time); 149 printf("friends connected, took %d seconds\n", (int)(time(nullptr) - cur_time));
145 150
146 ck_assert_msg(tox_conference_new(toxes[0], nullptr) != UINT32_MAX, "Failed to create group"); 151 ck_assert_msg(tox_conference_new(toxes[0], nullptr) != UINT32_MAX, "Failed to create group");
152 printf("tox #%d: inviting its first friend\n", tox_index[0]);
147 ck_assert_msg(tox_conference_invite(toxes[0], 0, 0, nullptr) != 0, "Failed to invite friend"); 153 ck_assert_msg(tox_conference_invite(toxes[0], 0, 0, nullptr) != 0, "Failed to invite friend");
148 ck_assert_msg(tox_conference_set_title(toxes[0], 0, (const uint8_t *)"Gentoo", sizeof("Gentoo") - 1, nullptr) != 0, 154 ck_assert_msg(tox_conference_set_title(toxes[0], 0, (const uint8_t *)"Gentoo", sizeof("Gentoo") - 1, nullptr) != 0,
149 "Failed to set group title"); 155 "Failed to set group title");
150 invite_counter = ~0;
151 156
152 unsigned int done = ~0; 157 // One iteration for all the invitations to happen.
153 done -= 5; 158 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
159 tox_iterate(toxes[i], &tox_index[i]);
160 }
154 161
155 while (1) { 162 cur_time = time(nullptr);
156 for (i = 0; i < NUM_GROUP_TOX; ++i) { 163 printf("waiting for all toxes to be in the group\n");
157 tox_iterate(toxes[i], &to_comp); 164 unsigned invited_count = 0;
158 }
159 165
160 if (!invite_counter) { 166 while (invited_count != NUM_GROUP_TOX) {
161 ck_assert_msg(tox_conference_invite(invite_tox, 0, 0, nullptr) != 0, "Failed to invite friend"); 167 invited_count = 0;
162 } 168 printf("current peer counts: [");
163 169
164 if (done == invite_counter) { 170 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
165 break; 171 tox_iterate(toxes[i], &tox_index[i]);
166 } 172 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr);
173 invited_count += peer_count == NUM_GROUP_TOX;
167 174
168 --invite_counter; 175 if (i != 0) {
169 c_sleep(50); 176 printf(", ");
170 } 177 }
171 178
172 for (i = 0; i < NUM_GROUP_TOX; ++i) { 179 printf("%d", peer_count);
173 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); 180 }
174 181
175 /** 182 printf("]\n");
176 * Group chats fail unpredictably, currently they'll rerun as many times 183 fflush(stdout);
177 * as they need to until they pass the test, or the time out is reached
178 * Either way in this case it's fine */
179 if (peer_count != NUM_GROUP_TOX) {
180 ++test_run;
181 printf("\tError starting up the first group (peer_count %" PRIu32 " != %d, test_run = %d)\n", peer_count, NUM_GROUP_TOX,
182 test_run);
183
184 for (j = 0; j < NUM_GROUP_TOX; ++j) {
185 tox_kill(toxes[j]);
186 }
187 184
188 c_sleep(1000); 185 c_sleep(1000);
186 }
189 187
190 goto group_test_restart; 188 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
191 } 189 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr);
192 190
193 /**
194 * This check will never fail because it'll jump before this event
195 * I've decided to leave it in because eventually, we may want to only
196 * restart this test once, in which case this check will become
197 * important again.
198 */
199 ck_assert_msg(peer_count == NUM_GROUP_TOX, "\n\tBad number of group peers (pre check)." 191 ck_assert_msg(peer_count == NUM_GROUP_TOX, "\n\tBad number of group peers (pre check)."
200 "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n", 192 "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n",
201 NUM_GROUP_TOX, i, peer_count); 193 NUM_GROUP_TOX, i, peer_count);
@@ -207,10 +199,10 @@ group_test_restart:
207 ck_assert_msg(memcmp("Gentoo", title, ret) == 0, "Wrong title"); 199 ck_assert_msg(memcmp("Gentoo", title, ret) == 0, "Wrong title");
208 } 200 }
209 201
210 printf("group connected\n"); 202 printf("group connected, took %d seconds\n", (int)(time(nullptr) - cur_time));
211 203
212 for (i = 0; i < NUM_GROUP_TOX; ++i) { 204 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
213 tox_callback_conference_message(toxes[i], &print_group_message); 205 tox_callback_conference_message(toxes[i], &handle_conference_message);
214 } 206 }
215 207
216 TOX_ERR_CONFERENCE_SEND_MESSAGE err; 208 TOX_ERR_CONFERENCE_SEND_MESSAGE err;
@@ -222,9 +214,9 @@ group_test_restart:
222 err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK, "Failed to send group message."); 214 err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK, "Failed to send group message.");
223 num_recv = 0; 215 num_recv = 0;
224 216
225 for (j = 0; j < 20; ++j) { 217 for (unsigned j = 0; j < 20; ++j) {
226 for (i = 0; i < NUM_GROUP_TOX; ++i) { 218 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
227 tox_iterate(toxes[i], &to_comp); 219 tox_iterate(toxes[i], &tox_index[i]);
228 } 220 }
229 221
230 c_sleep(25); 222 c_sleep(25);
@@ -233,18 +225,18 @@ group_test_restart:
233 c_sleep(25); 225 c_sleep(25);
234 ck_assert_msg(num_recv == NUM_GROUP_TOX, "Failed to recv group messages."); 226 ck_assert_msg(num_recv == NUM_GROUP_TOX, "Failed to recv group messages.");
235 227
236 for (k = NUM_GROUP_TOX; k != 0 ; --k) { 228 for (unsigned k = NUM_GROUP_TOX; k != 0 ; --k) {
237 tox_conference_delete(toxes[k - 1], 0, nullptr); 229 tox_conference_delete(toxes[k - 1], 0, nullptr);
238 230
239 for (j = 0; j < 10; ++j) { 231 for (unsigned j = 0; j < 10; ++j) {
240 for (i = 0; i < NUM_GROUP_TOX; ++i) { 232 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
241 tox_iterate(toxes[i], &to_comp); 233 tox_iterate(toxes[i], &tox_index[i]);
242 } 234 }
243 235
244 c_sleep(50); 236 c_sleep(50);
245 } 237 }
246 238
247 for (i = 0; i < (k - 1); ++i) { 239 for (unsigned i = 0; i < (k - 1); ++i) {
248 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); 240 uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr);
249 ck_assert_msg(peer_count == (k - 1), "\n\tBad number of group peers (post check)." 241 ck_assert_msg(peer_count == (k - 1), "\n\tBad number of group peers (post check)."
250 "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n", 242 "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n",
@@ -252,11 +244,11 @@ group_test_restart:
252 } 244 }
253 } 245 }
254 246
255 for (i = 0; i < NUM_GROUP_TOX; ++i) { 247 for (unsigned i = 0; i < NUM_GROUP_TOX; ++i) {
256 tox_kill(toxes[i]); 248 tox_kill(toxes[i]);
257 } 249 }
258 250
259 printf("test_many_group succeeded, took %llu seconds\n", time(nullptr) - test_start_time); 251 printf("test_many_group succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time));
260} 252}
261END_TEST 253END_TEST
262 254
@@ -264,13 +256,7 @@ static Suite *tox_suite(void)
264{ 256{
265 Suite *s = suite_create("Tox conference"); 257 Suite *s = suite_create("Tox conference");
266 258
267 /* This test works VERY unreliably. So it's worthless in its current state.
268 * Anyone reading this is welcome to try to fix it, but because there is a
269 * new version of group chats for Tox already completed, and nearly ready to
270 * merge, No one is willing/available to give this test the time in needs */
271#ifndef DISABLE_GROUP_TESTS
272 DEFTESTCASE_SLOW(many_group, 80); 259 DEFTESTCASE_SLOW(many_group, 80);
273#endif
274 260
275 return s; 261 return s;
276} 262}