tomato/auto_tests/announce_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.3 KiB
C

#include <stdint.h>
#include <string.h>
#include "../toxcore/announce.h"
#include "../toxcore/tox.h"
#include "../testing/misc_tools.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"
static void test_bucketnum(void)
{
const Random *rng = os_random();
ck_assert(rng != nullptr);
uint8_t key1[CRYPTO_PUBLIC_KEY_SIZE], key2[CRYPTO_PUBLIC_KEY_SIZE];
random_bytes(rng, key1, sizeof(key1));
memcpy(key2, key1, CRYPTO_PUBLIC_KEY_SIZE);
ck_assert_msg(announce_get_bucketnum(key1, key2) == 0, "Bad bucketnum");
key2[4] ^= 0x09;
key2[5] ^= 0xc5;
ck_assert_msg(announce_get_bucketnum(key1, key2) == 7, "Bad bucketnum");
key2[4] ^= 0x09;
ck_assert_msg(announce_get_bucketnum(key1, key2) == 17, "Bad bucketnum");
key2[5] ^= 0xc5;
key2[31] ^= 0x09;
ck_assert_msg(announce_get_bucketnum(key1, key2) == 4, "Bad bucketnum");
}
typedef struct Announce_Test_Data {
uint8_t data[MAX_ANNOUNCEMENT_SIZE];
uint16_t length;
bool passed;
} Announce_Test_Data;
static void test_announce_data(void *object, const uint8_t *data, uint16_t length)
{
Announce_Test_Data *test_data = (Announce_Test_Data *) object;
test_data->passed = test_data->length == length && memcmp(test_data->data, data, length) == 0;
}
static void test_store_data(void)
{
const Random *rng = os_random();
ck_assert(rng != nullptr);
const Network *ns = os_network();
ck_assert(ns != nullptr);
const Memory *mem = os_memory();
ck_assert(mem != nullptr);
Logger *log = logger_new(mem);
ck_assert(log != nullptr);
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
ck_assert(mono_time != nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns);
ck_assert(net != nullptr);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
ck_assert(dht != nullptr);
Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht);
ck_assert(forwarding != nullptr);
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
ck_assert(announce != nullptr);
/* Just to prevent CI from complaining that set_synch_offset is unused: */
announce_set_synch_offset(announce, 0);
Announce_Test_Data test_data;
random_bytes(rng, test_data.data, sizeof(test_data.data));
test_data.length = sizeof(test_data.data);
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE];
random_bytes(rng, key, sizeof(key));
ck_assert_msg(!announce_on_stored(announce, key, nullptr, nullptr), "Unstored announcement exists");
ck_assert_msg(announce_store_data(announce, key, test_data.data, sizeof(test_data.data),
MAX_MAX_ANNOUNCEMENT_TIMEOUT), "Failed to store announcement");
ck_assert_msg(announce_on_stored(announce, key, test_announce_data, &test_data), "Failed to get stored announcement");
ck_assert_msg(test_data.passed, "Bad stored announcement data");
const uint8_t *const base = dht_get_self_public_key(dht);
ck_assert_msg(announce_store_data(announce, base, test_data.data, sizeof(test_data.data), 1), "failed to store base");
uint8_t test_keys[ANNOUNCE_BUCKET_SIZE + 1][CRYPTO_PUBLIC_KEY_SIZE];
for (uint8_t i = 0; i < ANNOUNCE_BUCKET_SIZE + 1; ++i) {
memcpy(test_keys[i], base, CRYPTO_PUBLIC_KEY_SIZE);
test_keys[i][i] ^= 1;
ck_assert_msg(announce_store_data(announce, test_keys[i], test_data.data, sizeof(test_data.data), 1),
"Failed to store announcement %d", i);
}
ck_assert_msg(announce_on_stored(announce, base, nullptr, nullptr), "base was evicted");
ck_assert_msg(!announce_on_stored(announce, test_keys[0], nullptr, nullptr), "furthest was not evicted");
ck_assert_msg(!announce_store_data(announce, test_keys[0], nullptr, 0, 1), "furthest evicted closer");
kill_announcements(announce);
kill_forwarding(forwarding);
kill_dht(dht);
kill_networking(net);
mono_time_free(mem, mono_time);
logger_kill(log);
}
static void basic_announce_tests(void)
{
test_bucketnum();
test_store_data();
}
int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);
basic_announce_tests();
return 0;
}