|
| 1 | +#include <stdint.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +#include "../toxcore/announce.h" |
| 5 | +#include "../toxcore/tox.h" |
| 6 | +#include "../testing/misc_tools.h" |
| 7 | +#include "../toxcore/mono_time.h" |
| 8 | +#include "../toxcore/forwarding.h" |
| 9 | +#include "../toxcore/net_crypto.h" |
| 10 | +#include "../toxcore/util.h" |
| 11 | +#include "auto_test_support.h" |
| 12 | +#include "check_compat.h" |
| 13 | + |
| 14 | +static void test_bucketnum(void) |
| 15 | +{ |
| 16 | + const Random *rng = system_random(); |
| 17 | + ck_assert(rng != nullptr); |
| 18 | + uint8_t key1[CRYPTO_PUBLIC_KEY_SIZE], key2[CRYPTO_PUBLIC_KEY_SIZE]; |
| 19 | + random_bytes(rng, key1, sizeof(key1)); |
| 20 | + memcpy(key2, key1, CRYPTO_PUBLIC_KEY_SIZE); |
| 21 | + |
| 22 | + ck_assert_msg(get_bucketnum(key1, key2) == 0, "Bad bucketnum"); |
| 23 | + |
| 24 | + key2[4] ^= 0x09; |
| 25 | + key2[5] ^= 0xc5; |
| 26 | + |
| 27 | + ck_assert_msg(get_bucketnum(key1, key2) == 7, "Bad bucketnum"); |
| 28 | + |
| 29 | + key2[4] ^= 0x09; |
| 30 | + |
| 31 | + ck_assert_msg(get_bucketnum(key1, key2) == 17, "Bad bucketnum"); |
| 32 | + |
| 33 | + key2[5] ^= 0xc5; |
| 34 | + key2[31] ^= 0x09; |
| 35 | + |
| 36 | + ck_assert_msg(get_bucketnum(key1, key2) == 4, "Bad bucketnum"); |
| 37 | +} |
| 38 | + |
| 39 | +typedef struct Announce_Test_Data { |
| 40 | + uint8_t data[MAX_ANNOUNCEMENT_SIZE]; |
| 41 | + uint16_t length; |
| 42 | + bool passed; |
| 43 | +} Announce_Test_Data; |
| 44 | + |
| 45 | +static void test_announce_data(void *object, const uint8_t *data, uint16_t length) |
| 46 | +{ |
| 47 | + Announce_Test_Data *test_data = (Announce_Test_Data *) object; |
| 48 | + test_data->passed = test_data->length == length && memcmp(test_data->data, data, length) == 0; |
| 49 | +} |
| 50 | + |
| 51 | +static void test_store_data(void) |
| 52 | +{ |
| 53 | + const Random *rng = system_random(); |
| 54 | + ck_assert(rng != nullptr); |
| 55 | + const Network *ns = system_network(); |
| 56 | + ck_assert(ns != nullptr); |
| 57 | + Logger *log = logger_new(); |
| 58 | + ck_assert(log != nullptr); |
| 59 | + logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, nullptr); |
| 60 | + Mono_Time *mono_time = mono_time_new(); |
| 61 | + Networking_Core *net = new_networking_no_udp(log, ns); |
| 62 | + DHT *dht = new_dht(log, rng, ns, mono_time, net, true, true); |
| 63 | + Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht); |
| 64 | + Announcements *announce = new_announcements(log, rng, mono_time, forwarding); |
| 65 | + ck_assert(announce != nullptr); |
| 66 | + |
| 67 | + /* Just to prevent CI from complaining that set_synch_offset is unused: */ |
| 68 | + set_synch_offset(announce, 0); |
| 69 | + |
| 70 | + Announce_Test_Data test_data; |
| 71 | + random_bytes(rng, test_data.data, sizeof(test_data.data)); |
| 72 | + test_data.length = sizeof(test_data.data); |
| 73 | + |
| 74 | + uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]; |
| 75 | + random_bytes(rng, key, sizeof(key)); |
| 76 | + |
| 77 | + ck_assert_msg(!on_stored(announce, key, nullptr, nullptr), "Unstored announcement exists"); |
| 78 | + |
| 79 | + ck_assert_msg(store_data(announce, key, test_data.data, sizeof(test_data.data), |
| 80 | + MAX_MAX_ANNOUNCEMENT_TIMEOUT), "Failed to store announcement"); |
| 81 | + |
| 82 | + ck_assert_msg(on_stored(announce, key, test_announce_data, &test_data), "Failed to get stored announcement"); |
| 83 | + |
| 84 | + ck_assert_msg(test_data.passed, "Bad stored announcement data"); |
| 85 | + |
| 86 | + const uint8_t *const base = dht_get_self_public_key(dht); |
| 87 | + ck_assert_msg(store_data(announce, base, test_data.data, sizeof(test_data.data), 1), "failed to store base"); |
| 88 | + |
| 89 | + uint8_t test_keys[ANNOUNCE_BUCKET_SIZE + 1][CRYPTO_PUBLIC_KEY_SIZE]; |
| 90 | + |
| 91 | + for (uint8_t i = 0; i < ANNOUNCE_BUCKET_SIZE + 1; ++i) { |
| 92 | + memcpy(test_keys[i], base, CRYPTO_PUBLIC_KEY_SIZE); |
| 93 | + test_keys[i][i] ^= 1; |
| 94 | + ck_assert_msg(store_data(announce, test_keys[i], test_data.data, sizeof(test_data.data), 1), |
| 95 | + "Failed to store announcement %d", i); |
| 96 | + } |
| 97 | + |
| 98 | + ck_assert_msg(on_stored(announce, base, nullptr, nullptr), "base was evicted"); |
| 99 | + ck_assert_msg(!on_stored(announce, test_keys[0], nullptr, nullptr), "furthest was not evicted"); |
| 100 | + ck_assert_msg(!store_data(announce, test_keys[0], nullptr, 0, 1), "furthest evicted closer"); |
| 101 | + |
| 102 | + kill_announcements(announce); |
| 103 | + kill_forwarding(forwarding); |
| 104 | + kill_dht(dht); |
| 105 | + kill_networking(net); |
| 106 | + mono_time_free(mono_time); |
| 107 | + logger_kill(log); |
| 108 | +} |
| 109 | + |
| 110 | +static void basic_announce_tests(void) |
| 111 | +{ |
| 112 | + test_bucketnum(); |
| 113 | + test_store_data(); |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +int main(void) |
| 118 | +{ |
| 119 | + setvbuf(stdout, nullptr, _IONBF, 0); |
| 120 | + |
| 121 | + basic_announce_tests(); |
| 122 | + return 0; |
| 123 | +} |
0 commit comments