/work/auto_tests/save_compatibility_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Tests to make sure new save code is compatible with old save files |
2 | | |
3 | | #include "../testing/misc_tools.h" |
4 | | #include "../toxcore/tox.h" |
5 | | #include "auto_test_support.h" |
6 | | #include "check_compat.h" |
7 | | |
8 | | #include <stdio.h> |
9 | | #include <stdlib.h> |
10 | | #include <string.h> |
11 | | |
12 | 0 | #define LOADED_SAVE_FILE_BIG "../auto_tests/data/save.tox.big" |
13 | 6 | #define LOADED_SAVE_FILE_LITTLE "../auto_tests/data/save.tox.little" |
14 | | |
15 | | // Information from the save file |
16 | | #define EXPECTED_NAME "name" |
17 | | #define EXPECTED_NAME_SIZE strlen(EXPECTED_NAME) |
18 | | #define EXPECTED_STATUS_MESSAGE "Hello World" |
19 | | #define EXPECTED_STATUS_MESSAGE_SIZE strlen(EXPECTED_STATUS_MESSAGE) |
20 | | #define EXPECTED_NUM_FRIENDS 1 |
21 | | #define EXPECTED_NOSPAM "4C762C7D" |
22 | | #define EXPECTED_TOX_ID "E776E9A993EE3CAE04F5946D9AC66FBBAA8C63A95A94B41942353C6DC1739B4B4C762C7DA7B9" |
23 | | |
24 | | static size_t get_file_size(const char *save_path) |
25 | 3 | { |
26 | 3 | FILE *const fp = fopen(save_path, "rb"); |
27 | | |
28 | 3 | if (fp == nullptr) { |
29 | 2 | return 0; |
30 | 2 | } |
31 | | |
32 | 1 | fseek(fp, 0, SEEK_END); |
33 | 1 | const size_t size = ftell(fp); |
34 | 1 | fclose(fp); |
35 | | |
36 | 1 | return size; |
37 | 3 | } |
38 | | |
39 | | static uint8_t *read_save(const char *save_path, size_t *length) |
40 | 3 | { |
41 | 3 | const size_t size = get_file_size(save_path); |
42 | | |
43 | 3 | if (size == 0) { |
44 | 2 | return nullptr; |
45 | 2 | } |
46 | | |
47 | 1 | FILE *const fp = fopen(save_path, "rb"); |
48 | | |
49 | 1 | if (!fp) { |
50 | 0 | return nullptr; |
51 | 0 | } |
52 | | |
53 | 1 | uint8_t *const data = (uint8_t *)malloc(size); |
54 | | |
55 | 1 | if (!data) { |
56 | 0 | fclose(fp); |
57 | 0 | return nullptr; |
58 | 0 | } |
59 | | |
60 | 1 | if (fread(data, size, 1, fp) != 1) { |
61 | 0 | free(data); |
62 | 0 | fclose(fp); |
63 | 0 | return nullptr; |
64 | 0 | } |
65 | | |
66 | 1 | *length = size; |
67 | 1 | fclose(fp); |
68 | | |
69 | 1 | return data; |
70 | 1 | } |
71 | | |
72 | | static void test_save_compatibility(const char *save_path) |
73 | 3 | { |
74 | 3 | Tox_Options *options = tox_options_new(nullptr); |
75 | 3 | ck_assert(options != nullptr); |
76 | | |
77 | 3 | size_t size = 0; |
78 | 3 | uint8_t *save_data = read_save(save_path, &size); |
79 | 3 | ck_assert_msg(save_data != nullptr, "error while reading save file '%s'", save_path); |
80 | | |
81 | 1 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); |
82 | 1 | tox_options_set_savedata_data(options, save_data, size); |
83 | | |
84 | 1 | size_t index = 0; |
85 | 1 | Tox_Err_New err; |
86 | 1 | Tox *tox = tox_new_log(options, &err, &index); |
87 | 1 | ck_assert_msg(tox, "failed to create tox, error number: %u", err); |
88 | | |
89 | 1 | tox_options_free(options); |
90 | 1 | free(save_data); |
91 | | |
92 | 1 | const size_t name_size = tox_self_get_name_size(tox); |
93 | 1 | ck_assert_msg(name_size == EXPECTED_NAME_SIZE, "name sizes do not match expected %zu got %zu", EXPECTED_NAME_SIZE, |
94 | 1 | name_size); |
95 | | |
96 | 1 | uint8_t *name = (uint8_t *)malloc(tox_self_get_name_size(tox)); |
97 | 1 | ck_assert(name != nullptr); |
98 | 1 | tox_self_get_name(tox, name); |
99 | 1 | ck_assert_msg(strncmp((const char *)name, EXPECTED_NAME, name_size) == 0, |
100 | 1 | "names do not match, expected %s got %s", EXPECTED_NAME, name); |
101 | | |
102 | 1 | const size_t status_message_size = tox_self_get_status_message_size(tox); |
103 | 1 | ck_assert_msg(status_message_size == EXPECTED_STATUS_MESSAGE_SIZE, |
104 | 1 | "status message sizes do not match, expected %zu got %zu", EXPECTED_STATUS_MESSAGE_SIZE, status_message_size); |
105 | | |
106 | 1 | uint8_t *status_message = (uint8_t *)malloc(tox_self_get_status_message_size(tox)); |
107 | 1 | ck_assert(status_message != nullptr); |
108 | 1 | tox_self_get_status_message(tox, status_message); |
109 | 1 | ck_assert_msg(strncmp((const char *)status_message, EXPECTED_STATUS_MESSAGE, status_message_size) == 0, |
110 | 1 | "status messages do not match, expected %s got %s", |
111 | 1 | EXPECTED_STATUS_MESSAGE, status_message); |
112 | | |
113 | 1 | const size_t num_friends = tox_self_get_friend_list_size(tox); |
114 | 1 | ck_assert_msg(num_friends == EXPECTED_NUM_FRIENDS, |
115 | 1 | "number of friends do not match, expected %d got %zu", EXPECTED_NUM_FRIENDS, num_friends); |
116 | | |
117 | 1 | const uint32_t nospam = tox_self_get_nospam(tox); |
118 | 1 | char nospam_str[TOX_NOSPAM_SIZE * 2 + 1]; |
119 | 1 | const size_t length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); |
120 | 1 | nospam_str[length] = '\0'; |
121 | 1 | ck_assert_msg(strcmp(nospam_str, EXPECTED_NOSPAM) == 0, |
122 | 1 | "nospam does not match, expected %s got %s", EXPECTED_NOSPAM, nospam_str); |
123 | | |
124 | 1 | uint8_t tox_id[TOX_ADDRESS_SIZE]; |
125 | 1 | char tox_id_str[TOX_ADDRESS_SIZE * 2 + 1] = {0}; |
126 | 1 | tox_self_get_address(tox, tox_id); |
127 | 1 | to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); |
128 | 1 | ck_assert_msg(strncmp(tox_id_str, EXPECTED_TOX_ID, TOX_ADDRESS_SIZE * 2) == 0, |
129 | 1 | "tox ids do not match, expected %s got %s", EXPECTED_TOX_ID, tox_id_str); |
130 | | |
131 | | /* Giving the tox a chance to error on iterate due to corrupted loaded structures */ |
132 | 1 | tox_iterate(tox, nullptr); |
133 | | |
134 | 1 | free(status_message); |
135 | 1 | free(name); |
136 | 1 | tox_kill(tox); |
137 | 1 | } |
138 | | |
139 | | static bool is_little_endian(void) |
140 | 3 | { |
141 | 3 | uint16_t x = 1; |
142 | 3 | return ((uint8_t *)&x)[0] == 1; |
143 | 3 | } |
144 | | |
145 | | // cppcheck-suppress constParameter |
146 | | int main(int argc, char *argv[]) |
147 | 3 | { |
148 | 3 | const size_t base_path_size = 4096; |
149 | 3 | char *base_path = (char *)malloc(base_path_size); |
150 | 3 | ck_assert(base_path != nullptr); |
151 | 3 | memset(base_path, 0, 4096); |
152 | | |
153 | 3 | if (argc <= 1) { |
154 | 3 | const char *srcdir = getenv("srcdir"); |
155 | | |
156 | 3 | if (srcdir == nullptr) { |
157 | 2 | srcdir = "."; |
158 | 2 | } |
159 | | |
160 | 3 | snprintf(base_path, base_path_size, "%s", srcdir); |
161 | 3 | } else { |
162 | 0 | snprintf(base_path, base_path_size, "%s", argv[1]); |
163 | 0 | base_path[strrchr(base_path, '/') - base_path] = '\0'; |
164 | 0 | } |
165 | | |
166 | 3 | if (is_little_endian()) { |
167 | 3 | const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_LITTLE); |
168 | 3 | char *save_path = (char *)malloc(save_path_size); |
169 | 3 | ck_assert(save_path != nullptr); |
170 | 3 | snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE); |
171 | 3 | test_save_compatibility(save_path); |
172 | 3 | free(save_path); |
173 | 3 | } else { |
174 | 0 | const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_BIG); |
175 | 0 | char *save_path = (char *)malloc(save_path_size); |
176 | 0 | ck_assert(save_path != nullptr); |
177 | 0 | snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_BIG); |
178 | 0 | test_save_compatibility(save_path); |
179 | 0 | free(save_path); |
180 | 0 | } |
181 | | |
182 | 3 | free(base_path); |
183 | | |
184 | 3 | return 0; |
185 | 3 | } |