1 /* Tests that we can save and load Tox data.
2  */
3 
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 
13 #include "../testing/misc_tools.h"
14 #include "../toxcore/ccompat.h"
15 #include "../toxcore/tox.h"
16 #include "../toxcore/util.h"
17 #include "check_compat.h"
18 
19 /* The Travis-CI container responds poorly to ::1 as a localhost address
20  * You're encouraged to -D FORCE_TESTS_IPV6 on a local test  */
21 #ifdef TOX_LOCALHOST
22 #undef TOX_LOCALHOST
23 #endif
24 #ifdef FORCE_TESTS_IPV6
25 #define TOX_LOCALHOST "::1"
26 #else
27 #define TOX_LOCALHOST "127.0.0.1"
28 #endif
29 
30 #ifdef TCP_RELAY_PORT
31 #undef TCP_RELAY_PORT
32 #endif
33 #define TCP_RELAY_PORT 33430
34 
accept_friend_request(Tox * m,const uint8_t * public_key,const uint8_t * data,size_t length,void * userdata)35 static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
36 {
37     if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
38         tox_friend_add_norequest(m, public_key, nullptr);
39     }
40 }
41 
42 static unsigned int connected_t1;
tox_connection_status(Tox * tox,Tox_Connection connection_status,void * user_data)43 static void tox_connection_status(Tox *tox, Tox_Connection connection_status, void *user_data)
44 {
45     if (connected_t1 && !connection_status) {
46         ck_abort_msg("Tox went offline");
47     }
48 
49     ck_assert_msg(connection_status != TOX_CONNECTION_NONE, "wrong status %d", connection_status);
50 
51     connected_t1 = connection_status;
52 }
53 
54 /* validate that:
55  * a) saving stays within the confined space
56  * b) a saved state can be loaded back successfully
57  * c) a second save is of equal size
58  * d) the second save is of equal content */
reload_tox(Tox ** tox,struct Tox_Options * const in_opts,void * user_data)59 static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_data)
60 {
61     const size_t extra = 64;
62     const size_t save_size1 = tox_get_savedata_size(*tox);
63     ck_assert_msg(save_size1 != 0, "save is invalid size %u", (unsigned)save_size1);
64     printf("%u\n", (unsigned)save_size1);
65 
66     uint8_t *buffer = (uint8_t *)malloc(save_size1 + 2 * extra);
67     ck_assert_msg(buffer != nullptr, "malloc failed");
68     memset(buffer, 0xCD, extra);
69     memset(buffer + extra + save_size1, 0xCD, extra);
70     tox_get_savedata(*tox, buffer + extra);
71     tox_kill(*tox);
72 
73     for (size_t i = 0; i < extra; ++i) {
74         ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from tox_get_savedata() @%u", (unsigned)i);
75         ck_assert_msg(buffer[extra + save_size1 + i] == 0xCD, "Buffer overwritten from tox_get_savedata() @%u", (unsigned)i);
76     }
77 
78     struct Tox_Options *const options = (in_opts == nullptr) ? tox_options_new(nullptr) : in_opts;
79 
80     tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
81 
82     tox_options_set_savedata_data(options, buffer + extra, save_size1);
83 
84     *tox = tox_new_log(options, nullptr, user_data);
85 
86     if (in_opts == nullptr) {
87         tox_options_free(options);
88     }
89 
90     ck_assert_msg(*tox != nullptr, "Failed to load back stored buffer");
91 
92     const size_t save_size2 = tox_get_savedata_size(*tox);
93 
94     ck_assert_msg(save_size1 == save_size2, "Tox save data changed in size from a store/load cycle: %u -> %u",
95                   (unsigned)save_size1, (unsigned)save_size2);
96 
97     uint8_t *buffer2 = (uint8_t *)malloc(save_size2);
98 
99     ck_assert_msg(buffer2 != nullptr, "malloc failed");
100 
101     tox_get_savedata(*tox, buffer2);
102 
103     ck_assert_msg(!memcmp(buffer + extra, buffer2, save_size2), "Tox state changed by store/load/store cycle");
104 
105     free(buffer2);
106 
107     free(buffer);
108 }
109 
test_few_clients(void)110 static void test_few_clients(void)
111 {
112     uint32_t index[] = { 1, 2, 3 };
113     time_t con_time = 0, cur_time = time(nullptr);
114 
115     struct Tox_Options *opts1 = tox_options_new(nullptr);
116     tox_options_set_tcp_port(opts1, TCP_RELAY_PORT);
117     Tox *tox1 = tox_new_log(opts1, nullptr, &index[0]);
118     tox_options_free(opts1);
119 
120     struct Tox_Options *opts2 = tox_options_new(nullptr);
121     tox_options_set_udp_enabled(opts2, false);
122     tox_options_set_local_discovery_enabled(opts2, false);
123     Tox *tox2 = tox_new_log(opts2, nullptr, &index[1]);
124 
125     struct Tox_Options *opts3 = tox_options_new(nullptr);
126     tox_options_set_local_discovery_enabled(opts3, false);
127     Tox *tox3 = tox_new_log(opts3, nullptr, &index[2]);
128 
129     ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances");
130 
131     uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
132     tox_self_get_dht_id(tox1, dht_key);
133     const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
134 
135     printf("using tox1 as tcp relay for tox2\n");
136     tox_add_tcp_relay(tox2, TOX_LOCALHOST, TCP_RELAY_PORT, dht_key, nullptr);
137 
138     printf("bootstrapping toxes off tox1\n");
139     tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
140     tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr);
141 
142     connected_t1 = 0;
143     tox_callback_self_connection_status(tox1, tox_connection_status);
144     tox_callback_friend_request(tox2, accept_friend_request);
145     uint8_t address[TOX_ADDRESS_SIZE];
146     tox_self_get_address(tox2, address);
147     uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr);
148     ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
149 
150     uint8_t off = 1;
151 
152     while (true) {
153         tox_iterate(tox1, nullptr);
154         tox_iterate(tox2, nullptr);
155         tox_iterate(tox3, nullptr);
156 
157         if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2)
158                 && tox_self_get_connection_status(tox3)) {
159             if (off) {
160                 printf("Toxes are online, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time));
161                 con_time = time(nullptr);
162                 off = 0;
163             }
164 
165             if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_TCP
166                     && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_TCP) {
167                 break;
168             }
169         }
170 
171         c_sleep(ITERATION_INTERVAL);
172     }
173 
174     ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1);
175     printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time));
176 
177     reload_tox(&tox2, opts2, &index[1]);
178 
179     reload_tox(&tox3, opts3, &index[2]);
180 
181     cur_time = time(nullptr);
182 
183     off = 1;
184 
185     while (true) {
186         tox_iterate(tox1, nullptr);
187         tox_iterate(tox2, nullptr);
188         tox_iterate(tox3, nullptr);
189 
190         if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2)
191                 && tox_self_get_connection_status(tox3)) {
192             if (off) {
193                 printf("Toxes are online again after reloading, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time));
194                 con_time = time(nullptr);
195                 off = 0;
196             }
197 
198             if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_TCP
199                     && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_TCP) {
200                 break;
201             }
202         }
203 
204         c_sleep(ITERATION_INTERVAL);
205     }
206 
207     printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time));
208 
209     printf("test_few_clients succeeded, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time));
210 
211     tox_kill(tox1);
212     tox_kill(tox2);
213     tox_kill(tox3);
214 
215     tox_options_free(opts2);
216     tox_options_free(opts3);
217 }
218 
main(void)219 int main(void)
220 {
221     setvbuf(stdout, nullptr, _IONBF, 0);
222 
223     test_few_clients();
224     return 0;
225 }
226