2016-10-11 19:52:06 +08:00
|
|
|
/* Auto Tests: Many clients.
|
2016-09-27 16:40:29 +08:00
|
|
|
*/
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
#ifndef _XOPEN_SOURCE
|
2017-01-06 20:19:12 +08:00
|
|
|
#define _XOPEN_SOURCE 600
|
2018-01-29 05:30:39 +08:00
|
|
|
#endif
|
2017-01-06 20:19:12 +08:00
|
|
|
|
2016-09-27 16:40:29 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-06-05 04:58:28 +08:00
|
|
|
#include "check_compat.h"
|
|
|
|
|
2016-09-27 16:40:29 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-11-02 22:34:33 +08:00
|
|
|
#include <time.h>
|
2016-09-27 16:40:29 +08:00
|
|
|
|
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#include "../toxcore/util.h"
|
|
|
|
|
|
|
|
#include "helpers.h"
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
|
|
|
|
{
|
|
|
|
if (*((uint32_t *)userdata) != 974536) {
|
|
|
|
return;
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
|
2018-01-29 05:30:39 +08:00
|
|
|
tox_friend_add_norequest(m, public_key, nullptr);
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
#define NUM_TOXES 90
|
|
|
|
#define NUM_FRIENDS 50
|
|
|
|
|
|
|
|
START_TEST(test_many_clients)
|
2016-09-27 16:40:29 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
long long unsigned int cur_time = time(nullptr);
|
2016-10-11 19:52:06 +08:00
|
|
|
Tox *toxes[NUM_TOXES];
|
2016-11-06 00:52:41 +08:00
|
|
|
uint32_t index[NUM_TOXES];
|
2016-09-27 16:40:29 +08:00
|
|
|
uint32_t i, j;
|
|
|
|
uint32_t to_comp = 974536;
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
for (i = 0; i < NUM_TOXES; ++i) {
|
2016-11-06 00:52:41 +08:00
|
|
|
index[i] = i + 1;
|
2018-01-29 05:30:39 +08:00
|
|
|
toxes[i] = tox_new_log(nullptr, nullptr, &index[i]);
|
|
|
|
ck_assert_msg(toxes[i] != nullptr, "Failed to create tox instances %u", i);
|
2016-09-27 16:40:29 +08:00
|
|
|
tox_callback_friend_request(toxes[i], accept_friend_request);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TOX_ERR_GET_PORT error;
|
2017-08-25 14:30:40 +08:00
|
|
|
uint16_t port = tox_self_get_udp_port(toxes[0], &error);
|
|
|
|
ck_assert_msg(33445 <= port && port <= 33545,
|
|
|
|
"First Tox instance did not bind to udp port inside [33445, 33545].\n");
|
2016-09-27 16:40:29 +08:00
|
|
|
ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t tox1;
|
|
|
|
uint16_t tox2;
|
|
|
|
} pairs[NUM_FRIENDS];
|
|
|
|
|
|
|
|
uint8_t address[TOX_ADDRESS_SIZE];
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
unsigned int num_f = 0;
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
for (i = 0; i < NUM_TOXES; ++i) {
|
|
|
|
num_f += tox_self_get_friend_list_size(toxes[i]);
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
ck_assert_msg(num_f == 0, "bad num friends: %u", num_f);
|
2016-09-27 16:40:29 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_FRIENDS; ++i) {
|
|
|
|
loop_top:
|
2016-10-11 19:52:06 +08:00
|
|
|
pairs[i].tox1 = rand() % NUM_TOXES;
|
|
|
|
pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES;
|
2016-09-27 16:40:29 +08:00
|
|
|
|
|
|
|
for (j = 0; j < i; ++j) {
|
|
|
|
if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) {
|
|
|
|
goto loop_top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tox_self_get_address(toxes[pairs[i].tox1], address);
|
|
|
|
|
|
|
|
TOX_ERR_FRIEND_ADD test;
|
|
|
|
uint32_t num = tox_friend_add(toxes[pairs[i].tox2], address, (const uint8_t *)"Gentoo", 7, &test);
|
|
|
|
|
|
|
|
if (test == TOX_ERR_FRIEND_ADD_ALREADY_SENT) {
|
|
|
|
goto loop_top;
|
|
|
|
}
|
|
|
|
|
|
|
|
ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test);
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
for (i = 0; i < NUM_TOXES; ++i) {
|
|
|
|
num_f += tox_self_get_friend_list_size(toxes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
ck_assert_msg(num_f == NUM_FRIENDS, "bad num friends: %u", num_f);
|
|
|
|
|
2016-09-27 16:40:29 +08:00
|
|
|
uint16_t last_count = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint16_t counter = 0;
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
for (i = 0; i < NUM_TOXES; ++i) {
|
2016-09-27 16:40:29 +08:00
|
|
|
for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) {
|
2018-01-29 05:30:39 +08:00
|
|
|
if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_UDP) {
|
2016-09-27 16:40:29 +08:00
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counter != last_count) {
|
2016-10-11 19:52:06 +08:00
|
|
|
printf("many_clients got to %u\n", counter);
|
2016-09-27 16:40:29 +08:00
|
|
|
last_count = counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counter == NUM_FRIENDS * 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
for (i = 0; i < NUM_TOXES; ++i) {
|
2016-09-27 16:40:29 +08:00
|
|
|
tox_iterate(toxes[i], &to_comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
c_sleep(50);
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:52:06 +08:00
|
|
|
for (i = 0; i < NUM_TOXES; ++i) {
|
2016-09-27 16:40:29 +08:00
|
|
|
tox_kill(toxes[i]);
|
|
|
|
}
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
printf("test_many_clients succeeded, took %llu seconds\n", time(nullptr) - cur_time);
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
#ifdef TRAVIS_ENV
|
|
|
|
static const uint8_t timeout_mux = 20;
|
|
|
|
#else
|
|
|
|
static const uint8_t timeout_mux = 10;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Suite *tox_suite(void)
|
|
|
|
{
|
|
|
|
Suite *s = suite_create("Tox");
|
|
|
|
|
|
|
|
DEFTESTCASE_SLOW(many_clients, 8 * timeout_mux);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
srand((unsigned int) time(nullptr));
|
2016-09-27 16:40:29 +08:00
|
|
|
|
|
|
|
Suite *tox = tox_suite();
|
|
|
|
SRunner *test_runner = srunner_create(tox);
|
|
|
|
|
|
|
|
int number_failed = 0;
|
|
|
|
srunner_run_all(test_runner, CK_NORMAL);
|
|
|
|
number_failed = srunner_ntests_failed(test_runner);
|
|
|
|
|
|
|
|
srunner_free(test_runner);
|
|
|
|
|
|
|
|
return number_failed;
|
|
|
|
}
|