/work/auto_tests/save_friend_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Auto Tests: Save and load friends. |
2 | | */ |
3 | | |
4 | | #include <stdio.h> |
5 | | #include <stdlib.h> |
6 | | #include <string.h> |
7 | | |
8 | | #include "../testing/misc_tools.h" |
9 | | #include "../toxcore/ccompat.h" |
10 | | #include "../toxcore/crypto_core.h" |
11 | | #include "../toxcore/os_random.h" |
12 | | #include "../toxcore/tox.h" |
13 | | #include "auto_test_support.h" |
14 | | #include "check_compat.h" |
15 | | |
16 | | struct test_data { |
17 | | uint8_t *name; |
18 | | uint8_t *status_message; |
19 | | bool received_name; |
20 | | bool received_status_message; |
21 | | }; |
22 | | |
23 | | static void set_random(Tox *m, const Random *rng, bool (*setter)(Tox *, const uint8_t *, size_t, Tox_Err_Set_Info *), size_t length) |
24 | 4 | { |
25 | 4 | VLA(uint8_t, text, length); |
26 | | |
27 | 2.27k | for (uint32_t i = 0; i < length; ++i) { |
28 | 2.27k | text[i] = random_u08(rng); |
29 | 2.27k | } |
30 | | |
31 | 4 | setter(m, text, length, nullptr); |
32 | 4 | } |
33 | | |
34 | | static void alloc_string(uint8_t **to, size_t length) |
35 | 4 | { |
36 | 4 | free(*to); |
37 | 4 | *to = (uint8_t *)malloc(length); |
38 | 4 | ck_assert(*to != nullptr); |
39 | 4 | } |
40 | | |
41 | | static void set_string(uint8_t **to, const uint8_t *from, size_t length) |
42 | 2 | { |
43 | 2 | alloc_string(to, length); |
44 | 2 | memcpy(*to, from, length); |
45 | 2 | } |
46 | | |
47 | | static void namechange_callback(const Tox_Event_Friend_Name *event, void *user_data) |
48 | 1 | { |
49 | | //const uint32_t friend_number = tox_event_friend_name_get_friend_number(event); |
50 | 1 | const uint8_t *name = tox_event_friend_name_get_name(event); |
51 | 1 | const uint32_t name_length = tox_event_friend_name_get_name_length(event); |
52 | | |
53 | 1 | struct test_data *to_compare = (struct test_data *)user_data; |
54 | 1 | set_string(&to_compare->name, name, name_length); |
55 | 1 | to_compare->received_name = true; |
56 | 1 | } |
57 | | |
58 | | static void statuschange_callback(const Tox_Event_Friend_Status_Message *event, void *user_data) |
59 | 1 | { |
60 | | //const uint32_t friend_number = tox_event_friend_status_message_get_friend_number(event); |
61 | 1 | const uint8_t *message = tox_event_friend_status_message_get_message(event); |
62 | 1 | const uint32_t message_length = tox_event_friend_status_message_get_message_length(event); |
63 | | |
64 | 1 | struct test_data *to_compare = (struct test_data *)user_data; |
65 | 1 | set_string(&to_compare->status_message, message, message_length); |
66 | 1 | to_compare->received_status_message = true; |
67 | 1 | } |
68 | | |
69 | | int main(void) |
70 | 1 | { |
71 | 1 | setvbuf(stdout, nullptr, _IONBF, 0); |
72 | | |
73 | 1 | Tox *const tox1 = tox_new_log(nullptr, nullptr, nullptr); |
74 | 1 | Tox *const tox2 = tox_new_log(nullptr, nullptr, nullptr); |
75 | 1 | ck_assert(tox1 != nullptr); |
76 | 1 | ck_assert(tox2 != nullptr); |
77 | | |
78 | 1 | tox_events_init(tox1); |
79 | 1 | Tox_Dispatch *dispatch1 = tox_dispatch_new(nullptr); |
80 | 1 | ck_assert(dispatch1 != nullptr); |
81 | | |
82 | 1 | printf("bootstrapping tox2 off tox1\n"); |
83 | 1 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; |
84 | 1 | tox_self_get_dht_id(tox1, dht_key); |
85 | 1 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); |
86 | | |
87 | 1 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); |
88 | | |
89 | 1 | struct test_data to_compare = {nullptr}; |
90 | | |
91 | 1 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; |
92 | 1 | tox_self_get_public_key(tox1, public_key); |
93 | 1 | tox_friend_add_norequest(tox2, public_key, nullptr); |
94 | 1 | tox_self_get_public_key(tox2, public_key); |
95 | 1 | tox_friend_add_norequest(tox1, public_key, nullptr); |
96 | | |
97 | 1 | uint8_t *reference_name = (uint8_t *)malloc(tox_max_name_length()); |
98 | 1 | uint8_t *reference_status = (uint8_t *)malloc(tox_max_status_message_length()); |
99 | 1 | ck_assert(reference_name != nullptr); |
100 | 1 | ck_assert(reference_status != nullptr); |
101 | | |
102 | 1 | const Random *rng = os_random(); |
103 | 1 | ck_assert(rng != nullptr); |
104 | 1 | set_random(tox1, rng, tox_self_set_name, tox_max_name_length()); |
105 | 1 | set_random(tox2, rng, tox_self_set_name, tox_max_name_length()); |
106 | 1 | set_random(tox1, rng, tox_self_set_status_message, tox_max_status_message_length()); |
107 | 1 | set_random(tox2, rng, tox_self_set_status_message, tox_max_status_message_length()); |
108 | | |
109 | 1 | tox_self_get_name(tox2, reference_name); |
110 | 1 | tox_self_get_status_message(tox2, reference_status); |
111 | | |
112 | 1 | tox_events_callback_friend_name(dispatch1, namechange_callback); |
113 | 1 | tox_events_callback_friend_status_message(dispatch1, statuschange_callback); |
114 | | |
115 | 200 | while (true) { |
116 | 200 | if (tox_self_get_connection_status(tox1) && |
117 | 200 | tox_self_get_connection_status(tox2) && |
118 | 200 | tox_friend_get_connection_status(tox1, 0, nullptr) == TOX_CONNECTION_UDP) { |
119 | 1 | printf("Connected.\n"); |
120 | 1 | break; |
121 | 1 | } |
122 | | |
123 | 199 | Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK; |
124 | 199 | Tox_Events *events = tox_events_iterate(tox1, true, &err); |
125 | 199 | ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK); |
126 | 199 | tox_dispatch_invoke(dispatch1, events, &to_compare); |
127 | 199 | tox_events_free(events); |
128 | | |
129 | 199 | tox_iterate(tox2, nullptr); |
130 | | |
131 | 199 | c_sleep(tox_iteration_interval(tox1)); |
132 | 199 | } |
133 | | |
134 | 1 | while (true) { |
135 | 1 | if (to_compare.received_name && to_compare.received_status_message) { |
136 | 1 | printf("Exchanged names and status messages.\n"); |
137 | 1 | break; |
138 | 1 | } |
139 | | |
140 | 0 | Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK; |
141 | 0 | Tox_Events *events = tox_events_iterate(tox1, true, &err); |
142 | 0 | ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK); |
143 | 0 | tox_dispatch_invoke(dispatch1, events, &to_compare); |
144 | 0 | tox_events_free(events); |
145 | |
|
146 | 0 | tox_iterate(tox2, nullptr); |
147 | |
|
148 | 0 | c_sleep(tox_iteration_interval(tox1)); |
149 | 0 | } |
150 | | |
151 | 1 | size_t save_size = tox_get_savedata_size(tox1); |
152 | 1 | uint8_t *savedata = (uint8_t *)malloc(save_size); |
153 | 1 | tox_get_savedata(tox1, savedata); |
154 | | |
155 | 1 | struct Tox_Options *const options = tox_options_new(nullptr); |
156 | 1 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); |
157 | 1 | tox_options_set_savedata_data(options, savedata, save_size); |
158 | | |
159 | 1 | Tox *const tox_to_compare = tox_new_log(options, nullptr, nullptr); |
160 | | |
161 | 1 | alloc_string(&to_compare.name, tox_friend_get_name_size(tox_to_compare, 0, nullptr)); |
162 | 1 | tox_friend_get_name(tox_to_compare, 0, to_compare.name, nullptr); |
163 | 1 | alloc_string(&to_compare.status_message, tox_friend_get_status_message_size(tox_to_compare, 0, nullptr)); |
164 | 1 | tox_friend_get_status_message(tox_to_compare, 0, to_compare.status_message, nullptr); |
165 | | |
166 | 1 | ck_assert_msg(memcmp(reference_name, to_compare.name, tox_max_name_length()) == 0, |
167 | 1 | "incorrect name: should be all zeroes"); |
168 | 1 | ck_assert_msg(memcmp(reference_status, to_compare.status_message, tox_max_status_message_length()) == 0, |
169 | 1 | "incorrect status message: should be all zeroes"); |
170 | | |
171 | 1 | tox_options_free(options); |
172 | 1 | tox_dispatch_free(dispatch1); |
173 | 1 | tox_kill(tox1); |
174 | 1 | tox_kill(tox2); |
175 | 1 | tox_kill(tox_to_compare); |
176 | 1 | free(savedata); |
177 | 1 | free(to_compare.name); |
178 | 1 | free(to_compare.status_message); |
179 | 1 | free(reference_status); |
180 | 1 | free(reference_name); |
181 | | |
182 | 1 | return 0; |
183 | 1 | } |