summaryrefslogtreecommitdiff
path: root/auto_tests/save_load_test.c
diff options
context:
space:
mode:
authorRobin Lindén <dev@robinlinden.eu>2018-02-18 01:57:45 +0100
committeriphydf <iphydf@users.noreply.github.com>2018-02-18 14:50:18 +0000
commitc3515c49e012f76c5d2ca104476e372d924a79f9 (patch)
tree56512d94a79ad08d768ea51afa735c56833338b1 /auto_tests/save_load_test.c
parent13706de14bce8f8bfd1cb7db226d12cbd927b1b4 (diff)
Split up tox_test into multiple smaller tests
Diffstat (limited to 'auto_tests/save_load_test.c')
-rw-r--r--auto_tests/save_load_test.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/auto_tests/save_load_test.c b/auto_tests/save_load_test.c
new file mode 100644
index 00000000..ab4e0a8c
--- /dev/null
+++ b/auto_tests/save_load_test.c
@@ -0,0 +1,149 @@
1/* Tests that we can save and load Tox data.
2 */
3
4#ifndef _XOPEN_SOURCE
5#define _XOPEN_SOURCE 600
6#endif
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "check_compat.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <time.h>
17
18#include "../toxcore/ccompat.h"
19#include "../toxcore/tox.h"
20#include "../toxcore/util.h"
21
22#include "helpers.h"
23
24static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
25{
26 if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
27 tox_friend_add_norequest(m, public_key, nullptr);
28 }
29}
30
31static unsigned int connected_t1;
32static void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data)
33{
34 if (connected_t1 && !connection_status) {
35 ck_abort_msg("Tox went offline");
36 }
37
38 ck_assert_msg(connection_status == TOX_CONNECTION_UDP, "wrong status %u", connection_status);
39
40 connected_t1 = connection_status;
41}
42
43static void test_few_clients(void)
44{
45 uint32_t index[] = { 1, 2, 3 };
46 time_t con_time = 0, cur_time = time(nullptr);
47 Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]);
48 Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]);
49 Tox *tox3 = tox_new_log(nullptr, nullptr, &index[2]);
50
51 ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances");
52
53 printf("bootstrapping tox2 and tox3 off tox1\n");
54 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
55 tox_self_get_dht_id(tox1, dht_key);
56 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
57
58 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
59 tox_bootstrap(tox3, "localhost", dht_port, dht_key, nullptr);
60
61 connected_t1 = 0;
62 tox_callback_self_connection_status(tox1, tox_connection_status);
63 tox_callback_friend_request(tox2, accept_friend_request);
64 uint8_t address[TOX_ADDRESS_SIZE];
65 tox_self_get_address(tox2, address);
66 uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr);
67 ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
68
69 uint8_t off = 1;
70
71 while (1) {
72 tox_iterate(tox1, nullptr);
73 tox_iterate(tox2, nullptr);
74 tox_iterate(tox3, nullptr);
75
76 if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2)
77 && tox_self_get_connection_status(tox3)) {
78 if (off) {
79 printf("Toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
80 con_time = time(nullptr);
81 off = 0;
82 }
83
84 if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP
85 && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) {
86 break;
87 }
88 }
89
90 c_sleep(ITERATION_INTERVAL);
91 }
92
93 ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1);
94 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
95
96 const size_t save_size1 = tox_get_savedata_size(tox2);
97 ck_assert_msg(save_size1 != 0, "save is invalid size %u", (unsigned)save_size1);
98 printf("%u\n", (unsigned)save_size1);
99 VLA(uint8_t, save1, save_size1);
100 tox_get_savedata(tox2, save1);
101 tox_kill(tox2);
102
103 struct Tox_Options *const options = tox_options_new(nullptr);
104 tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
105 tox_options_set_savedata_data(options, save1, save_size1);
106 tox_options_set_local_discovery_enabled(options, false);
107 tox2 = tox_new_log(options, nullptr, &index[1]);
108 cur_time = time(nullptr);
109 off = 1;
110
111 while (1) {
112 tox_iterate(tox1, nullptr);
113 tox_iterate(tox2, nullptr);
114 tox_iterate(tox3, nullptr);
115
116 if (tox_self_get_connection_status(tox1) && tox_self_get_connection_status(tox2)
117 && tox_self_get_connection_status(tox3)) {
118 if (off) {
119 printf("Toxes are online again after reloading, took %ld seconds\n", time(nullptr) - cur_time);
120 con_time = time(nullptr);
121 off = 0;
122 }
123
124 if (tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_UDP
125 && tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_UDP) {
126 break;
127 }
128 }
129
130 c_sleep(ITERATION_INTERVAL);
131 }
132
133 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
134
135 printf("test_few_clients succeeded, took %ld seconds\n", time(nullptr) - cur_time);
136
137 tox_options_free(options);
138 tox_kill(tox1);
139 tox_kill(tox2);
140 tox_kill(tox3);
141}
142
143int main(int argc, char *argv[])
144{
145 setvbuf(stdout, nullptr, _IONBF, 0);
146
147 test_few_clients();
148 return 0;
149}