tomato/auto_tests/proxy_test.c
Green Sky 3b6bb15e86 Squashed 'external/toxcore/c-toxcore/' changes from 11ab1d2a723..d9b8fa6098d
d9b8fa6098d fix: Fake broadcast address for 127.x.x.x
aa649165a57 chore: Add code for future netprof TCP testing
9e5693de5ac chore: add to_string functions for netprof enums
52d915e6a90 cleanup: Heap allocate network profile objects
80fabd4a729 feat: Implement Tox network profiler
05abe083cb6 cleanup: Some random cleanups, mostly related to mem.
5cca24513b8 cleanup: Check that onion IP/Port packing worked.
e092ecd1244 cleanup: Use tox memory allocator in some more places.
3cfe41c7587 fix: Avoid `memcpy`-ing structs into onion ping id data.
e32ac001938 fix: Add more information on why the frame was not sent.
ab887003687 fix: Allow TCP connections to fail `connect` calls.
7603170e663 refactor: Use tox memory in group connection allocations.
5bd8a85eb89 cleanup: Align internal logger with external on type of source line.
e9bf524d9e1 cleanup: Add missing `#include` to sort_test.cc.
d10c966b998 feat: Add `to_string` functions for toxencryptsave errors.
7bfd0dc8003 docs: Update the docs for group join functions
380dde9f2ae test: Add more logging to TCP connection constructor.
0f12f384c8c cleanup: Reduce stack frame sizes to below 4096 bytes.
bc43cec0626 chore: Happy new year!
fbe78f1702e cleanup: Add a `TOX_HIDE_DEPRECATED` check to hide deprecated symbols.
44d9da07e77 refactor: Use tox memory for group moderation/pack allocations.
7f26d520168 refactor: Use tox memory in group chats allocations.
2f62f3d0e77 refactor: Use tox Memory for group allocations.
8a968162041 chore: Add dispatch/events headers to bazel export.
2bbfb35abf6 docs: Output the error code string instead of int. in toxav logging
d55d0e4eaef cleanup: Remove redundant code for checking if group exists
2a6dc643338 chore: Upgrade dependencies for websockify.
fc0650601c1 fix: Allow peers to reconnect to group chats using a password

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: d9b8fa6098de6c074038b6664d2572627540b148
2025-01-18 15:53:06 +01:00

130 lines
4.2 KiB
C

/* Tests that we can send messages to friends.
*/
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include "auto_test_support.h"
static void *proxy_routine(void *arg)
{
const char *proxy_bin = (const char *)arg;
ck_assert(proxy_bin != nullptr);
printf("starting http/sock5 proxy: %s\n", proxy_bin);
ck_assert(system(proxy_bin) == 0);
return nullptr;
}
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4)
{
for (uint32_t i = 0; i < 400; ++i) {
if (tox_self_get_connection_status(tox1) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox2) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox3) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE) {
printf("%d %d %d %d\n",
tox_self_get_connection_status(tox1),
tox_self_get_connection_status(tox2),
tox_self_get_connection_status(tox3),
tox_self_get_connection_status(tox4));
return true;
}
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);
tox_iterate(tox3, nullptr);
tox_iterate(tox4, nullptr);
if (i % 10 == 0) {
printf("%d %d %d %d\n",
tox_self_get_connection_status(tox1),
tox_self_get_connection_status(tox2),
tox_self_get_connection_status(tox3),
tox_self_get_connection_status(tox4));
}
c_sleep(tox_iteration_interval(tox1));
}
return false;
}
int main(int argc, char **argv)
{
setvbuf(stdout, nullptr, _IONBF, 0);
if (argc >= 3) {
char *proxy_bin = argv[2];
pthread_t proxy_thread;
pthread_create(&proxy_thread, nullptr, proxy_routine, proxy_bin);
c_sleep(100);
}
const uint16_t tcp_port = 8082;
uint32_t index[] = { 1, 2, 3, 4 };
struct Tox_Options *tox_options = tox_options_new(nullptr);
ck_assert(tox_options != nullptr);
// tox1 is a TCP server and has UDP enabled.
tox_options_set_udp_enabled(tox_options, true);
tox_options_set_tcp_port(tox_options, tcp_port);
Tox *tox1 = tox_new_log(tox_options, nullptr, &index[0]);
ck_assert(tox1 != nullptr);
// Get tox1's DHT key and port.
uint8_t dht_pk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox1, dht_pk);
uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
ck_assert(dht_port != 0);
// tox2 is a regular DHT node bootstrapping against tox1.
tox_options_set_udp_enabled(tox_options, true);
tox_options_set_tcp_port(tox_options, 0);
Tox *tox2 = tox_new_log(tox_options, nullptr, &index[1]);
ck_assert(tox2 != nullptr);
// tox2 bootstraps against tox1 with UDP.
ck_assert(tox_bootstrap(tox2, "127.0.0.1", dht_port, dht_pk, nullptr));
// tox3 has UDP disabled and connects to tox1 via an HTTP proxy
tox_options_set_udp_enabled(tox_options, false);
tox_options_set_proxy_host(tox_options, "127.0.0.1");
tox_options_set_proxy_port(tox_options, 8080);
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_HTTP);
Tox *tox3 = tox_new_log(tox_options, nullptr, &index[2]);
ck_assert(tox3 != nullptr);
// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy
tox_options_set_udp_enabled(tox_options, false);
tox_options_set_proxy_host(tox_options, "127.0.0.1");
tox_options_set_proxy_port(tox_options, 8081);
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
Tox *tox4 = tox_new_log(tox_options, nullptr, &index[3]);
ck_assert(tox4 != nullptr);
// tox3 and tox4 bootstrap against tox1 and add it as a TCP relay
ck_assert(tox_bootstrap(tox3, "127.0.0.1", dht_port, dht_pk, nullptr));
ck_assert(tox_add_tcp_relay(tox3, "127.0.0.1", tcp_port, dht_pk, nullptr));
ck_assert(tox_bootstrap(tox4, "127.0.0.1", dht_port, dht_pk, nullptr));
ck_assert(tox_add_tcp_relay(tox4, "127.0.0.1", tcp_port, dht_pk, nullptr));
int ret = 1;
if (try_bootstrap(tox1, tox2, tox3, tox4)) {
ret = 0;
}
tox_options_free(tox_options);
tox_kill(tox4);
tox_kill(tox3);
tox_kill(tox2);
tox_kill(tox1);
return ret;
}