summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/messenger_test.c89
-rw-r--r--toxcore/DHT.c14
-rw-r--r--toxcore/Messenger.c4
-rw-r--r--toxcore/util.c4
4 files changed, 98 insertions, 13 deletions
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c
index 1b72a92f..dc1625a3 100644
--- a/auto_tests/messenger_test.c
+++ b/auto_tests/messenger_test.c
@@ -43,10 +43,9 @@ Messenger *m;
43 43
44unsigned char *hex_string_to_bin(char hex_string[]) 44unsigned char *hex_string_to_bin(char hex_string[])
45{ 45{
46 size_t len = strlen(hex_string); 46 size_t i, len = strlen(hex_string);
47 unsigned char *val = calloc(1, len); 47 unsigned char *val = calloc(1, len);
48 char *pos = hex_string; 48 char *pos = hex_string;
49 int i = 0;
50 49
51 for (i = 0; i < len; ++i, pos += 2) 50 for (i = 0; i < len; ++i, pos += 2)
52 sscanf(pos, "%2hhx", &val[i]); 51 sscanf(pos, "%2hhx", &val[i]);
@@ -227,6 +226,86 @@ START_TEST(test_getname)
227} 226}
228END_TEST 227END_TEST
229 228
229START_TEST(test_dht_state_saveloadsave)
230{
231 /* validate that:
232 * a) saving stays within the confined space
233 * b) a save()d state can be load()ed back successfully
234 * c) a second save() is of equal size
235 * d) the second save() is of equal content */
236 size_t i, extra = 64;
237 size_t size = DHT_size(m->dht);
238 uint8_t buffer[size + 2 * extra];
239 memset(buffer, 0xCD, extra);
240 memset(buffer + extra + size, 0xCD, extra);
241 DHT_save(m->dht, buffer + extra);
242 for(i = 0; i < extra; i++) {
243 ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from DHT_save() @%u", i);
244 ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from DHT_save() @%u", i);
245 }
246
247 int res = DHT_load_new(m->dht, buffer + extra, size);
248 if (res == -1)
249 ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1");
250 else {
251 char msg[128];
252 size_t offset = res >> 4;
253 uint8_t *ptr = buffer + extra + offset;
254 sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%u/%u, code %d",
255 ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F);
256 ck_assert_msg(res == 0, msg);
257 }
258
259 size_t size2 = DHT_size(m->dht);
260 ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);
261
262 uint8_t buffer2[size2];
263 DHT_save(m->dht, buffer2);
264
265 ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "DHT state changed by store/load/store cycle");
266}
267END_TEST
268
269START_TEST(test_messenger_state_saveloadsave)
270{
271 /* validate that:
272 * a) saving stays within the confined space
273 * b) a save()d state can be load()ed back successfully
274 * c) a second save() is of equal size
275 * d) the second save() is of equal content */
276 size_t i, extra = 64;
277 size_t size = Messenger_size(m);
278 uint8_t buffer[size + 2 * extra];
279 memset(buffer, 0xCD, extra);
280 memset(buffer + extra + size, 0xCD, extra);
281 Messenger_save(m, buffer + extra);
282 for(i = 0; i < extra; i++) {
283 ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from Messenger_save() @%u", i);
284 ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from Messenger_save() @%u", i);
285 }
286
287 int res = Messenger_load(m, buffer + extra, size);
288 if (res == -1)
289 ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1");
290 else {
291 char msg[128];
292 size_t offset = res >> 4;
293 uint8_t *ptr = buffer + extra + offset;
294 sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%u/%u, code %d",
295 ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F);
296 ck_assert_msg(res == 0, msg);
297 }
298
299 size_t size2 = Messenger_size(m);
300 ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);
301
302 uint8_t buffer2[size2];
303 Messenger_save(m, buffer2);
304
305 ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "Messenger state changed by store/load/store cycle");
306}
307END_TEST
308
230Suite *messenger_suite(void) 309Suite *messenger_suite(void)
231{ 310{
232 Suite *s = suite_create("Messenger"); 311 Suite *s = suite_create("Messenger");
@@ -241,6 +320,8 @@ Suite *messenger_suite(void)
241 //TCase *addfriend = tcase_create("addfriend"); 320 //TCase *addfriend = tcase_create("addfriend");
242 TCase *setname = tcase_create("setname"); 321 TCase *setname = tcase_create("setname");
243 TCase *getname = tcase_create("getname"); 322 TCase *getname = tcase_create("getname");
323 TCase *dht_state_saveloadsave = tcase_create("dht_state_saveloadsave");
324 TCase *messenger_state_saveloadsave = tcase_create("messenger_state_saveloadsave");
244 325
245 tcase_add_test(userstatus_size, test_m_get_userstatus_size); 326 tcase_add_test(userstatus_size, test_m_get_userstatus_size);
246 tcase_add_test(set_userstatus, test_m_set_userstatus); 327 tcase_add_test(set_userstatus, test_m_set_userstatus);
@@ -252,6 +333,8 @@ Suite *messenger_suite(void)
252 //tcase_add_test(addfriend, test_m_addfriend); 333 //tcase_add_test(addfriend, test_m_addfriend);
253 tcase_add_test(setname, test_getname); 334 tcase_add_test(setname, test_getname);
254 tcase_add_test(setname, test_setname); 335 tcase_add_test(setname, test_setname);
336 tcase_add_test(dht_state_saveloadsave, test_dht_state_saveloadsave);
337 tcase_add_test(messenger_state_saveloadsave, test_messenger_state_saveloadsave);
255 338
256 suite_add_tcase(s, userstatus_size); 339 suite_add_tcase(s, userstatus_size);
257 suite_add_tcase(s, set_userstatus); 340 suite_add_tcase(s, set_userstatus);
@@ -263,6 +346,8 @@ Suite *messenger_suite(void)
263 //suite_add_tcase(s, addfriend); 346 //suite_add_tcase(s, addfriend);
264 suite_add_tcase(s, getname); 347 suite_add_tcase(s, getname);
265 suite_add_tcase(s, setname); 348 suite_add_tcase(s, setname);
349 suite_add_tcase(s, messenger_state_saveloadsave);
350 suite_add_tcase(s, dht_state_saveloadsave);
266 351
267 return s; 352 return s;
268} 353}
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index ddd1e1e0..f38ce3a5 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -1576,18 +1576,16 @@ void DHT_save(DHT *dht, uint8_t *data)
1576 if (dht->close_clientlist[i].timestamp != 0) 1576 if (dht->close_clientlist[i].timestamp != 0)
1577 num++; 1577 num++;
1578 1578
1579 if (!num)
1580 return;
1581
1582 len = num * sizeof(Client_data); 1579 len = num * sizeof(Client_data);
1583 type = DHT_STATE_TYPE_CLIENTS; 1580 type = DHT_STATE_TYPE_CLIENTS;
1584 data = z_state_save_subheader(data, len, type); 1581 data = z_state_save_subheader(data, len, type);
1585 Client_data *clients = (Client_data *)data; 1582 if (num) {
1586 1583 Client_data *clients = (Client_data *)data;
1587 for (num = 0, i = 0; i < LCLIENT_LIST; ++i)
1588 if (dht->close_clientlist[i].timestamp != 0)
1589 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data));
1590 1584
1585 for (num = 0, i = 0; i < LCLIENT_LIST; ++i)
1586 if (dht->close_clientlist[i].timestamp != 0)
1587 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data));
1588 }
1591 data += len; 1589 data += len;
1592} 1590}
1593 1591
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index e4d32b8a..a27a44dc 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1483,7 +1483,7 @@ uint32_t Messenger_size(Messenger *m)
1483{ 1483{
1484 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 1484 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
1485 return size32 * 2 // global cookie 1485 return size32 * 2 // global cookie
1486 + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 1486 + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
1487 + sizesubhead + DHT_size(m->dht) // DHT 1487 + sizesubhead + DHT_size(m->dht) // DHT
1488 + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself. 1488 + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself.
1489 + sizesubhead + m->name_length // Own nickname. 1489 + sizesubhead + m->name_length // Own nickname.
@@ -1589,10 +1589,12 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
1589 } 1589 }
1590 1590
1591 break; 1591 break;
1592
1592#ifdef DEBUG 1593#ifdef DEBUG
1593 default: 1594 default:
1594 fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", 1595 fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n",
1595 length, type); 1596 length, type);
1597 break;
1596#endif 1598#endif
1597 } 1599 }
1598 1600
diff --git a/toxcore/util.c b/toxcore/util.c
index 51b31e7b..b3263d05 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -58,7 +58,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
58 uint32_t length_sub, cookie_type; 58 uint32_t length_sub, cookie_type;
59 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2; 59 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2;
60 60
61 while (length > size_head) { 61 while (length >= size_head) {
62 length_sub = *(uint32_t *)data; 62 length_sub = *(uint32_t *)data;
63 cookie_type = *(uint32_t *)(data + size32); 63 cookie_type = *(uint32_t *)(data + size32);
64 data += size_head; 64 data += size_head;
@@ -89,7 +89,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
89 length -= length_sub; 89 length -= length_sub;
90 } 90 }
91 91
92 return length == 0 ? 0 : - 1; 92 return length == 0 ? 0 : -1;
93}; 93};
94 94
95#ifdef LOGGING 95#ifdef LOGGING