/work/auto_tests/announce_test.c
Line | Count | Source |
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 | 3 | { |
16 | 3 | const Random *rng = os_random(); |
17 | 3 | ck_assert(rng != nullptr); |
18 | 3 | uint8_t key1[CRYPTO_PUBLIC_KEY_SIZE], key2[CRYPTO_PUBLIC_KEY_SIZE]; |
19 | 3 | random_bytes(rng, key1, sizeof(key1)); |
20 | 3 | memcpy(key2, key1, CRYPTO_PUBLIC_KEY_SIZE); |
21 | | |
22 | 3 | ck_assert_msg(announce_get_bucketnum(key1, key2) == 0, "Bad bucketnum"); |
23 | | |
24 | 3 | key2[4] ^= 0x09; |
25 | 3 | key2[5] ^= 0xc5; |
26 | | |
27 | 3 | ck_assert_msg(announce_get_bucketnum(key1, key2) == 7, "Bad bucketnum"); |
28 | | |
29 | 3 | key2[4] ^= 0x09; |
30 | | |
31 | 3 | ck_assert_msg(announce_get_bucketnum(key1, key2) == 17, "Bad bucketnum"); |
32 | | |
33 | 3 | key2[5] ^= 0xc5; |
34 | 3 | key2[31] ^= 0x09; |
35 | | |
36 | 3 | ck_assert_msg(announce_get_bucketnum(key1, key2) == 4, "Bad bucketnum"); |
37 | 3 | } |
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 | 3 | { |
47 | 3 | Announce_Test_Data *test_data = (Announce_Test_Data *) object; |
48 | 3 | test_data->passed = test_data->length == length && memcmp(test_data->data, data, length) == 0; |
49 | 3 | } |
50 | | |
51 | | static void test_store_data(void) |
52 | 3 | { |
53 | 3 | const Random *rng = os_random(); |
54 | 3 | ck_assert(rng != nullptr); |
55 | 3 | const Network *ns = os_network(); |
56 | 3 | ck_assert(ns != nullptr); |
57 | 3 | const Memory *mem = os_memory(); |
58 | 3 | ck_assert(mem != nullptr); |
59 | | |
60 | 3 | Logger *log = logger_new(); |
61 | 3 | ck_assert(log != nullptr); |
62 | 3 | logger_callback_log(log, print_debug_logger, nullptr, nullptr); |
63 | 3 | Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); |
64 | 3 | ck_assert(mono_time != nullptr); |
65 | 3 | Networking_Core *net = new_networking_no_udp(log, mem, ns); |
66 | 3 | ck_assert(net != nullptr); |
67 | 3 | DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); |
68 | 3 | ck_assert(dht != nullptr); |
69 | 3 | Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht); |
70 | 3 | ck_assert(forwarding != nullptr); |
71 | 3 | Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding); |
72 | 3 | ck_assert(announce != nullptr); |
73 | | |
74 | | /* Just to prevent CI from complaining that set_synch_offset is unused: */ |
75 | 3 | announce_set_synch_offset(announce, 0); |
76 | | |
77 | 3 | Announce_Test_Data test_data; |
78 | 3 | random_bytes(rng, test_data.data, sizeof(test_data.data)); |
79 | 3 | test_data.length = sizeof(test_data.data); |
80 | | |
81 | 3 | uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]; |
82 | 3 | random_bytes(rng, key, sizeof(key)); |
83 | | |
84 | 3 | ck_assert_msg(!announce_on_stored(announce, key, nullptr, nullptr), "Unstored announcement exists"); |
85 | | |
86 | 3 | ck_assert_msg(announce_store_data(announce, key, test_data.data, sizeof(test_data.data), |
87 | 3 | MAX_MAX_ANNOUNCEMENT_TIMEOUT), "Failed to store announcement"); |
88 | | |
89 | 3 | ck_assert_msg(announce_on_stored(announce, key, test_announce_data, &test_data), "Failed to get stored announcement"); |
90 | | |
91 | 3 | ck_assert_msg(test_data.passed, "Bad stored announcement data"); |
92 | | |
93 | 3 | const uint8_t *const base = dht_get_self_public_key(dht); |
94 | 3 | ck_assert_msg(announce_store_data(announce, base, test_data.data, sizeof(test_data.data), 1), "failed to store base"); |
95 | | |
96 | 3 | uint8_t test_keys[ANNOUNCE_BUCKET_SIZE + 1][CRYPTO_PUBLIC_KEY_SIZE]; |
97 | | |
98 | 30 | for (uint8_t i = 0; i < ANNOUNCE_BUCKET_SIZE + 1; ++i) { |
99 | 27 | memcpy(test_keys[i], base, CRYPTO_PUBLIC_KEY_SIZE); |
100 | 27 | test_keys[i][i] ^= 1; |
101 | 27 | ck_assert_msg(announce_store_data(announce, test_keys[i], test_data.data, sizeof(test_data.data), 1), |
102 | 27 | "Failed to store announcement %d", i); |
103 | 27 | } |
104 | | |
105 | 3 | ck_assert_msg(announce_on_stored(announce, base, nullptr, nullptr), "base was evicted"); |
106 | 3 | ck_assert_msg(!announce_on_stored(announce, test_keys[0], nullptr, nullptr), "furthest was not evicted"); |
107 | 3 | ck_assert_msg(!announce_store_data(announce, test_keys[0], nullptr, 0, 1), "furthest evicted closer"); |
108 | | |
109 | 3 | kill_announcements(announce); |
110 | 3 | kill_forwarding(forwarding); |
111 | 3 | kill_dht(dht); |
112 | 3 | kill_networking(net); |
113 | 3 | mono_time_free(mem, mono_time); |
114 | 3 | logger_kill(log); |
115 | 3 | } |
116 | | |
117 | | static void basic_announce_tests(void) |
118 | 3 | { |
119 | 3 | test_bucketnum(); |
120 | 3 | test_store_data(); |
121 | 3 | } |
122 | | |
123 | | |
124 | | int main(void) |
125 | 721 | { |
126 | 721 | setvbuf(stdout, nullptr, _IONBF, 0); |
127 | | |
128 | 721 | basic_announce_tests(); |
129 | 721 | return 0; |
130 | 721 | } |