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