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