2016-10-11 19:52:06 +08:00
|
|
|
/* Auto Tests: Many clients.
|
2016-09-27 16:40:29 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-07-17 06:46:02 +08:00
|
|
|
#include <string.h>
|
2016-11-02 22:34:33 +08:00
|
|
|
#include <time.h>
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2018-03-28 21:36:14 +08:00
|
|
|
#include "../toxcore/crypto_core.h"
|
2016-09-27 16:40:29 +08:00
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#include "../toxcore/util.h"
|
2022-02-22 01:34:02 +08:00
|
|
|
#include "auto_test_support.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "check_compat.h"
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
static void accept_friend_request(Tox *m, const Tox_Event_Friend_Request *event, void *userdata)
|
2016-10-11 19:52:06 +08:00
|
|
|
{
|
2024-01-14 06:32:37 +08:00
|
|
|
const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
|
|
|
|
const uint8_t *message = tox_event_friend_request_get_message(event);
|
|
|
|
const uint32_t message_length = tox_event_friend_request_get_message_length(event);
|
|
|
|
|
|
|
|
if (message_length == 7 && memcmp("Gentoo", message, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
#define TCP_TEST_NUM_TOXES 90
|
|
|
|
#define TCP_TEST_NUM_FRIENDS 50
|
2016-10-11 19:52:06 +08:00
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
static void test_many_clients(void)
|
2016-09-27 16:40:29 +08:00
|
|
|
{
|
2024-01-25 20:54:59 +08:00
|
|
|
const Random *rng = os_random();
|
2022-03-28 05:34:12 +08:00
|
|
|
ck_assert(rng != nullptr);
|
2018-02-19 01:50:50 +08:00
|
|
|
time_t cur_time = time(nullptr);
|
2018-07-17 09:18:04 +08:00
|
|
|
Tox *toxes[TCP_TEST_NUM_TOXES];
|
|
|
|
uint32_t index[TCP_TEST_NUM_TOXES];
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_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]);
|
2018-02-19 01:50:50 +08:00
|
|
|
ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
|
2024-01-14 06:32:37 +08:00
|
|
|
tox_events_init(toxes[i]);
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
|
|
|
|
ck_assert(dispatch != nullptr);
|
|
|
|
|
|
|
|
tox_events_callback_friend_request(dispatch, accept_friend_request);
|
|
|
|
|
2016-09-27 16:40:29 +08:00
|
|
|
struct {
|
|
|
|
uint16_t tox1;
|
|
|
|
uint16_t tox2;
|
2018-07-17 09:18:04 +08:00
|
|
|
} pairs[TCP_TEST_NUM_FRIENDS];
|
2016-09-27 16:40:29 +08:00
|
|
|
|
|
|
|
uint8_t address[TOX_ADDRESS_SIZE];
|
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
uint32_t num_f = 0;
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_NUM_TOXES; ++i) {
|
2016-10-11 19:52:06 +08:00
|
|
|
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
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_NUM_FRIENDS; ++i) {
|
2016-09-27 16:40:29 +08:00
|
|
|
loop_top:
|
2022-03-28 05:34:12 +08:00
|
|
|
pairs[i].tox1 = random_u32(rng) % TCP_TEST_NUM_TOXES;
|
|
|
|
pairs[i].tox2 = (pairs[i].tox1 + random_u32(rng) % (TCP_TEST_NUM_TOXES - 1) + 1) % TCP_TEST_NUM_TOXES;
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
for (uint32_t j = 0; j < i; ++j) {
|
2016-09-27 16:40:29 +08:00
|
|
|
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);
|
|
|
|
|
2018-10-09 05:05:14 +08:00
|
|
|
Tox_Err_Friend_Add test;
|
2016-09-27 16:40:29 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
|
|
|
|
tox_self_get_dht_id(toxes[pairs[i].tox1], dht_key);
|
|
|
|
const uint16_t dht_port = tox_self_get_udp_port(toxes[pairs[i].tox1], nullptr);
|
|
|
|
|
|
|
|
tox_bootstrap(toxes[pairs[i].tox2], "localhost", dht_port, dht_key, nullptr);
|
|
|
|
|
|
|
|
ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "failed to add friend error code: %i", test);
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_NUM_TOXES; ++i) {
|
2016-10-11 19:52:06 +08:00
|
|
|
num_f += tox_self_get_friend_list_size(toxes[i]);
|
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
ck_assert_msg(num_f == TCP_TEST_NUM_FRIENDS, "bad num friends: %u", num_f);
|
2016-10-11 19:52:06 +08:00
|
|
|
|
2016-09-27 16:40:29 +08:00
|
|
|
uint16_t last_count = 0;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
while (true) {
|
2016-09-27 16:40:29 +08:00
|
|
|
uint16_t counter = 0;
|
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_NUM_TOXES; ++i) {
|
2018-02-19 01:50:50 +08:00
|
|
|
for (uint32_t 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;
|
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
if (counter == TCP_TEST_NUM_FRIENDS * 2) {
|
2016-09-27 16:40:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_NUM_TOXES; ++i) {
|
2024-01-14 06:32:37 +08:00
|
|
|
Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK;
|
|
|
|
Tox_Events *events = tox_events_iterate(toxes[i], true, &err);
|
|
|
|
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
|
|
|
|
tox_dispatch_invoke(dispatch, events, toxes[i], nullptr);
|
|
|
|
tox_events_free(events);
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c_sleep(50);
|
|
|
|
}
|
|
|
|
|
2024-01-14 06:32:37 +08:00
|
|
|
tox_dispatch_free(dispatch);
|
2018-07-17 09:18:04 +08:00
|
|
|
for (uint32_t i = 0; i < TCP_TEST_NUM_TOXES; ++i) {
|
2016-09-27 16:40:29 +08:00
|
|
|
tox_kill(toxes[i]);
|
|
|
|
}
|
|
|
|
|
2018-08-16 15:09:12 +08:00
|
|
|
printf("test_many_clients succeeded, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time));
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2016-09-27 16:40:29 +08:00
|
|
|
{
|
2018-02-19 01:50:50 +08:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2016-09-27 16:40:29 +08:00
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
test_many_clients();
|
|
|
|
return 0;
|
2016-09-27 16:40:29 +08:00
|
|
|
}
|