summaryrefslogtreecommitdiff
path: root/auto_tests
diff options
context:
space:
mode:
authorzugz (tox) <mbays+tox@sdf.org>2018-09-25 21:37:21 +0200
committerzugz (tox) <mbays+tox@sdf.org>2018-10-11 00:21:39 +0200
commit4dd86f1f29886772cf19d12acbd8cd7b35fb1d07 (patch)
treed7e909b448a7b24c45b6b3a5b32405473fd59ec2 /auto_tests
parent605dfe882c52bfad4dee9678a69e4b816d93431d (diff)
ensure save data unchanged after save and load
Diffstat (limited to 'auto_tests')
-rw-r--r--auto_tests/save_load_test.c121
1 files changed, 101 insertions, 20 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
19static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) 35static 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 */
59static 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
38static void test_few_clients(void) 110static 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
138int main(void) 219int main(void)