diff options
author | zugz (tox) <mbays+tox@sdf.org> | 2018-09-25 21:37:21 +0200 |
---|---|---|
committer | zugz (tox) <mbays+tox@sdf.org> | 2018-10-11 00:21:39 +0200 |
commit | 4dd86f1f29886772cf19d12acbd8cd7b35fb1d07 (patch) | |
tree | d7e909b448a7b24c45b6b3a5b32405473fd59ec2 | |
parent | 605dfe882c52bfad4dee9678a69e4b816d93431d (diff) |
ensure save data unchanged after save and load
-rw-r--r-- | auto_tests/save_load_test.c | 121 | ||||
-rw-r--r-- | toxcore/DHT.c | 10 | ||||
-rw-r--r-- | toxcore/Messenger.c | 17 | ||||
-rw-r--r-- | toxcore/Messenger.h | 2 | ||||
-rw-r--r-- | toxcore/onion_client.c | 30 |
5 files changed, 144 insertions, 36 deletions
diff --git a/auto_tests/save_load_test.c b/auto_tests/save_load_test.c index cdaf3d3d..1e360b1b 100644 --- a/auto_tests/save_load_test.c +++ b/auto_tests/save_load_test.c | |||
@@ -16,6 +16,22 @@ | |||
16 | #include "../toxcore/util.h" | 16 | #include "../toxcore/util.h" |
17 | #include "check_compat.h" | 17 | #include "check_compat.h" |
18 | 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 | |||
19 | static void 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) |
20 | { | 36 | { |
21 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { | 37 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { |
@@ -35,21 +51,91 @@ static void tox_connection_status(Tox *tox, Tox_Connection connection_status, vo | |||
35 | connected_t1 = connection_status; | 51 | connected_t1 = connection_status; |
36 | } | 52 | } |
37 | 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 */ | ||
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 | |||
38 | static void test_few_clients(void) | 110 | static void test_few_clients(void) |
39 | { | 111 | { |
40 | uint32_t index[] = { 1, 2, 3 }; | 112 | uint32_t index[] = { 1, 2, 3 }; |
41 | time_t con_time = 0, cur_time = time(nullptr); | 113 | time_t con_time = 0, cur_time = time(nullptr); |
42 | Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); | 114 | |
43 | Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); | 115 | struct Tox_Options *opts1 = tox_options_new(nullptr); |
44 | Tox *tox3 = tox_new_log(nullptr, nullptr, &index[2]); | 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]); | ||
45 | 128 | ||
46 | ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); | 129 | ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); |
47 | 130 | ||
48 | printf("bootstrapping tox2 and tox3 off tox1\n"); | ||
49 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | 131 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; |
50 | tox_self_get_dht_id(tox1, dht_key); | 132 | tox_self_get_dht_id(tox1, dht_key); |
51 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | 133 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); |
52 | 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"); | ||
53 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | 139 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); |
54 | tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr); | 140 | tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr); |
55 | 141 | ||
@@ -76,8 +162,8 @@ static void test_few_clients(void) | |||
76 | off = 0; | 162 | off = 0; |
77 | } | 163 | } |
78 | 164 | ||
79 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP | 165 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_TCP |
80 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { | 166 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_TCP) { |
81 | break; | 167 | break; |
82 | } | 168 | } |
83 | } | 169 | } |
@@ -88,19 +174,12 @@ static void test_few_clients(void) | |||
88 | ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); | 174 | ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1); |
89 | printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time)); | 175 | printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time)); |
90 | 176 | ||
91 | const size_t save_size1 = tox_get_savedata_size(tox2); | 177 | reload_tox(&tox2, opts2, &index[1]); |
92 | ck_assert_msg(save_size1 != 0, "save is invalid size %u", (unsigned)save_size1); | 178 | |
93 | printf("%u\n", (unsigned)save_size1); | 179 | reload_tox(&tox3, opts3, &index[2]); |
94 | VLA(uint8_t, save1, save_size1); | ||
95 | tox_get_savedata(tox2, save1); | ||
96 | tox_kill(tox2); | ||
97 | 180 | ||
98 | struct Tox_Options *const options = tox_options_new(nullptr); | ||
99 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); | ||
100 | tox_options_set_savedata_data(options, save1, save_size1); | ||
101 | tox_options_set_local_discovery_enabled(options, false); | ||
102 | tox2 = tox_new_log(options, nullptr, &index[1]); | ||
103 | cur_time = time(nullptr); | 181 | cur_time = time(nullptr); |
182 | |||
104 | off = 1; | 183 | off = 1; |
105 | 184 | ||
106 | while (true) { | 185 | while (true) { |
@@ -116,8 +195,8 @@ static void test_few_clients(void) | |||
116 | off = 0; | 195 | off = 0; |
117 | } | 196 | } |
118 | 197 | ||
119 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP | 198 | if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_TCP |
120 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) { | 199 | && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_TCP) { |
121 | break; | 200 | break; |
122 | } | 201 | } |
123 | } | 202 | } |
@@ -129,10 +208,12 @@ static void test_few_clients(void) | |||
129 | 208 | ||
130 | printf("test_few_clients succeeded, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time)); | 209 | printf("test_few_clients succeeded, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time)); |
131 | 210 | ||
132 | tox_options_free(options); | ||
133 | tox_kill(tox1); | 211 | tox_kill(tox1); |
134 | tox_kill(tox2); | 212 | tox_kill(tox2); |
135 | tox_kill(tox3); | 213 | tox_kill(tox3); |
214 | |||
215 | tox_options_free(opts2); | ||
216 | tox_options_free(opts3); | ||
136 | } | 217 | } |
137 | 218 | ||
138 | int main(void) | 219 | int main(void) |
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 1eebb0c7..8208052d 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -2803,6 +2803,11 @@ uint32_t dht_size(const DHT *dht) | |||
2803 | uint32_t numv4 = 0; | 2803 | uint32_t numv4 = 0; |
2804 | uint32_t numv6 = 0; | 2804 | uint32_t numv6 = 0; |
2805 | 2805 | ||
2806 | for (uint32_t i = 0; i < dht->loaded_num_nodes; ++i) { | ||
2807 | numv4 += net_family_is_ipv4(dht->loaded_nodes_list[i].ip_port.ip.family); | ||
2808 | numv6 += net_family_is_ipv6(dht->loaded_nodes_list[i].ip_port.ip.family); | ||
2809 | } | ||
2810 | |||
2806 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 2811 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
2807 | numv4 += (dht->close_clientlist[i].assoc4.timestamp != 0); | 2812 | numv4 += (dht->close_clientlist[i].assoc4.timestamp != 0); |
2808 | numv6 += (dht->close_clientlist[i].assoc6.timestamp != 0); | 2813 | numv6 += (dht->close_clientlist[i].assoc6.timestamp != 0); |
@@ -2838,6 +2843,11 @@ void dht_save(const DHT *dht, uint8_t *data) | |||
2838 | 2843 | ||
2839 | uint32_t num = 0; | 2844 | uint32_t num = 0; |
2840 | 2845 | ||
2846 | if (dht->loaded_num_nodes > 0) { | ||
2847 | memcpy(clients, dht->loaded_nodes_list, sizeof(Node_format) * dht->loaded_num_nodes); | ||
2848 | num += dht->loaded_num_nodes; | ||
2849 | } | ||
2850 | |||
2841 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 2851 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
2842 | if (dht->close_clientlist[i].assoc4.timestamp != 0) { | 2852 | if (dht->close_clientlist[i].assoc4.timestamp != 0) { |
2843 | memcpy(clients[num].public_key, dht->close_clientlist[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); | 2853 | memcpy(clients[num].public_key, dht->close_clientlist[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 594fc79a..e092a9f2 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2593,12 +2593,12 @@ void do_messenger(Messenger *m, void *userdata) | |||
2593 | if (!m->has_added_relays) { | 2593 | if (!m->has_added_relays) { |
2594 | m->has_added_relays = true; | 2594 | m->has_added_relays = true; |
2595 | 2595 | ||
2596 | int i; | 2596 | for (uint16_t i = 0; i < m->num_loaded_relays; ++i) { |
2597 | |||
2598 | for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { | ||
2599 | add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].public_key); | 2597 | add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].public_key); |
2600 | } | 2598 | } |
2601 | 2599 | ||
2600 | m->num_loaded_relays = 0; | ||
2601 | |||
2602 | if (m->tcp_server) { | 2602 | if (m->tcp_server) { |
2603 | /* Add self tcp server. */ | 2603 | /* Add self tcp server. */ |
2604 | IP_Port local_ip_port; | 2604 | IP_Port local_ip_port; |
@@ -3035,6 +3035,7 @@ static uint8_t *friends_list_save(const Messenger *m, uint8_t *data) | |||
3035 | temp.info_size = net_htons(m->friendlist[i].info_size); | 3035 | temp.info_size = net_htons(m->friendlist[i].info_size); |
3036 | temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam; | 3036 | temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam; |
3037 | } else { | 3037 | } else { |
3038 | temp.status = 3; | ||
3038 | memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length); | 3039 | memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length); |
3039 | temp.name_length = net_htons(m->friendlist[i].name_length); | 3040 | temp.name_length = net_htons(m->friendlist[i].name_length); |
3040 | memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length); | 3041 | memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length); |
@@ -3193,7 +3194,13 @@ static uint8_t *save_tcp_relays(const Messenger *m, uint8_t *data) | |||
3193 | Node_format relays[NUM_SAVED_TCP_RELAYS]; | 3194 | Node_format relays[NUM_SAVED_TCP_RELAYS]; |
3194 | uint8_t *temp_data = data; | 3195 | uint8_t *temp_data = data; |
3195 | data = state_write_section_header(temp_data, MESSENGER_STATE_COOKIE_TYPE, 0, MESSENGER_STATE_TYPE_TCP_RELAY); | 3196 | data = state_write_section_header(temp_data, MESSENGER_STATE_COOKIE_TYPE, 0, MESSENGER_STATE_TYPE_TCP_RELAY); |
3196 | unsigned int num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS); | 3197 | uint32_t num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS); |
3198 | |||
3199 | if (m->num_loaded_relays > 0) { | ||
3200 | memcpy(relays, m->loaded_relays, sizeof(Node_format) * m->num_loaded_relays); | ||
3201 | num = min_u32(num + m->num_loaded_relays, NUM_SAVED_TCP_RELAYS); | ||
3202 | } | ||
3203 | |||
3197 | int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6), relays, num); | 3204 | int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6), relays, num); |
3198 | 3205 | ||
3199 | if (l > 0) { | 3206 | if (l > 0) { |
@@ -3208,7 +3215,7 @@ static uint8_t *save_tcp_relays(const Messenger *m, uint8_t *data) | |||
3208 | static State_Load_Status load_tcp_relays(Messenger *m, const uint8_t *data, uint32_t length) | 3215 | static State_Load_Status load_tcp_relays(Messenger *m, const uint8_t *data, uint32_t length) |
3209 | { | 3216 | { |
3210 | if (length != 0) { | 3217 | if (length != 0) { |
3211 | unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, nullptr, data, length, 1); | 3218 | m->num_loaded_relays = unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, nullptr, data, length, 1); |
3212 | m->has_added_relays = false; | 3219 | m->has_added_relays = false; |
3213 | } | 3220 | } |
3214 | 3221 | ||
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index ab9d7256..0d80bd0d 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -293,6 +293,8 @@ struct Messenger { | |||
293 | time_t lastdump; | 293 | time_t lastdump; |
294 | 294 | ||
295 | bool has_added_relays; // If the first connection has occurred in do_messenger | 295 | bool has_added_relays; // If the first connection has occurred in do_messenger |
296 | |||
297 | uint16_t num_loaded_relays; | ||
296 | Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config | 298 | Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config |
297 | 299 | ||
298 | m_friend_message_cb *friend_message; | 300 | m_friend_message_cb *friend_message; |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 1c3b34da..9528a041 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -232,27 +232,35 @@ static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uin | |||
232 | */ | 232 | */ |
233 | uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num) | 233 | uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num) |
234 | { | 234 | { |
235 | unsigned int i; | ||
236 | |||
237 | if (!max_num) { | 235 | if (!max_num) { |
238 | return 0; | 236 | return 0; |
239 | } | 237 | } |
240 | 238 | ||
241 | unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 239 | const uint16_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; |
240 | uint16_t i = 0; | ||
242 | 241 | ||
243 | if (num_nodes == 0) { | 242 | while (i < max_num && i < num_nodes) { |
244 | return 0; | 243 | nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes]; |
244 | ++i; | ||
245 | } | 245 | } |
246 | 246 | ||
247 | if (num_nodes < max_num) { | 247 | for (uint16_t j = 0; i < max_num && j < MAX_PATH_NODES && j < onion_c->path_nodes_index_bs; ++j) { |
248 | max_num = num_nodes; | 248 | bool already_saved = false; |
249 | } | ||
250 | 249 | ||
251 | for (i = 0; i < max_num; ++i) { | 250 | for (uint16_t k = 0; k < num_nodes; ++k) { |
252 | nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes]; | 251 | if (public_key_cmp(nodes[k].public_key, onion_c->path_nodes_bs[j].public_key) == 0) { |
252 | already_saved = true; | ||
253 | break; | ||
254 | } | ||
255 | } | ||
256 | |||
257 | if (!already_saved) { | ||
258 | nodes[i] = onion_c->path_nodes_bs[j]; | ||
259 | ++i; | ||
260 | } | ||
253 | } | 261 | } |
254 | 262 | ||
255 | return max_num; | 263 | return i; |
256 | } | 264 | } |
257 | 265 | ||
258 | /* Put up to max_num random nodes in nodes. | 266 | /* Put up to max_num random nodes in nodes. |