/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 | 3 | #define LOADED_SAVE_FILE "../auto_tests/data/save.tox" |
13 | | |
14 | | // Information from the save file |
15 | | #define EXPECTED_NAME "name" |
16 | | #define EXPECTED_NAME_SIZE strlen(EXPECTED_NAME) |
17 | | #define EXPECTED_STATUS_MESSAGE "Hello World" |
18 | | #define EXPECTED_STATUS_MESSAGE_SIZE strlen(EXPECTED_STATUS_MESSAGE) |
19 | | #define EXPECTED_NUM_FRIENDS 1 |
20 | | #define EXPECTED_NOSPAM "4C762C7D" |
21 | | #define EXPECTED_TOX_ID "B70E97D41F69B7F4C42A5BC7BD7A76B95B8030BE1B7C0E9E6FC19FC4ABEB195B4C762C7D800B" |
22 | | |
23 | | static size_t get_file_size(const char *save_path) |
24 | 3 | { |
25 | 3 | FILE *const fp = fopen(save_path, "rb"); |
26 | | |
27 | 3 | if (fp == nullptr) { |
28 | 2 | return 0; |
29 | 2 | } |
30 | | |
31 | 1 | fseek(fp, 0, SEEK_END); |
32 | 1 | const size_t size = ftell(fp); |
33 | 1 | fclose(fp); |
34 | | |
35 | 1 | return size; |
36 | 3 | } |
37 | | |
38 | | static uint8_t *read_save(const char *save_path, size_t *length) |
39 | 3 | { |
40 | 3 | const size_t size = get_file_size(save_path); |
41 | | |
42 | 3 | if (size == 0) { |
43 | 2 | return nullptr; |
44 | 2 | } |
45 | | |
46 | 1 | FILE *const fp = fopen(save_path, "rb"); |
47 | | |
48 | 1 | if (!fp) { |
49 | 0 | return nullptr; |
50 | 0 | } |
51 | | |
52 | 1 | uint8_t *const data = (uint8_t *)malloc(size); |
53 | | |
54 | 1 | if (!data) { |
55 | 0 | fclose(fp); |
56 | 0 | return nullptr; |
57 | 0 | } |
58 | | |
59 | 1 | if (fread(data, size, 1, fp) != 1) { |
60 | 0 | free(data); |
61 | 0 | fclose(fp); |
62 | 0 | return nullptr; |
63 | 0 | } |
64 | | |
65 | 1 | *length = size; |
66 | 1 | fclose(fp); |
67 | | |
68 | 1 | return data; |
69 | 1 | } |
70 | | |
71 | | static void test_save_compatibility(const char *save_path) |
72 | 3 | { |
73 | 3 | struct Tox_Options options = {0}; |
74 | 3 | tox_options_default(&options); |
75 | | |
76 | 3 | size_t size = 0; |
77 | 3 | uint8_t *save_data = read_save(save_path, &size); |
78 | 3 | ck_assert_msg(save_data != nullptr, "error while reading save file '%s'", save_path); |
79 | | |
80 | 1 | options.savedata_data = save_data; |
81 | 1 | options.savedata_length = size; |
82 | 1 | options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; |
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: %d", err); |
88 | | |
89 | 1 | free(save_data); |
90 | | |
91 | 1 | const size_t name_size = tox_self_get_name_size(tox); |
92 | 1 | ck_assert_msg(name_size == EXPECTED_NAME_SIZE, "name sizes do not match expected %zu got %zu", EXPECTED_NAME_SIZE, |
93 | 1 | name_size); |
94 | | |
95 | 1 | uint8_t *name = (uint8_t *)malloc(tox_self_get_name_size(tox)); |
96 | 1 | ck_assert(name != nullptr); |
97 | 1 | tox_self_get_name(tox, name); |
98 | 1 | ck_assert_msg(strncmp((const char *)name, EXPECTED_NAME, name_size) == 0, |
99 | 1 | "names do not match, expected %s got %s", EXPECTED_NAME, name); |
100 | | |
101 | 1 | const size_t status_message_size = tox_self_get_status_message_size(tox); |
102 | 1 | ck_assert_msg(status_message_size == EXPECTED_STATUS_MESSAGE_SIZE, |
103 | 1 | "status message sizes do not match, expected %zu got %zu", EXPECTED_STATUS_MESSAGE_SIZE, status_message_size); |
104 | | |
105 | 1 | uint8_t *status_message = (uint8_t *)malloc(tox_self_get_status_message_size(tox)); |
106 | 1 | ck_assert(status_message != nullptr); |
107 | 1 | tox_self_get_status_message(tox, status_message); |
108 | 1 | ck_assert_msg(strncmp((const char *)status_message, EXPECTED_STATUS_MESSAGE, status_message_size) == 0, |
109 | 1 | "status messages do not match, expected %s got %s", |
110 | 1 | EXPECTED_STATUS_MESSAGE, status_message); |
111 | | |
112 | 1 | const size_t num_friends = tox_self_get_friend_list_size(tox); |
113 | 1 | ck_assert_msg(num_friends == EXPECTED_NUM_FRIENDS, |
114 | 1 | "number of friends do not match, expected %d got %zu", EXPECTED_NUM_FRIENDS, num_friends); |
115 | | |
116 | 1 | const uint32_t nospam = tox_self_get_nospam(tox); |
117 | 1 | char nospam_str[TOX_NOSPAM_SIZE * 2 + 1]; |
118 | 1 | const size_t length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); |
119 | 1 | nospam_str[length] = '\0'; |
120 | 1 | ck_assert_msg(strcmp(nospam_str, EXPECTED_NOSPAM) == 0, |
121 | 1 | "nospam does not match, expected %s got %s", EXPECTED_NOSPAM, nospam_str); |
122 | | |
123 | 1 | uint8_t tox_id[TOX_ADDRESS_SIZE]; |
124 | 1 | char tox_id_str[TOX_ADDRESS_SIZE * 2 + 1] = {0}; |
125 | 1 | tox_self_get_address(tox, tox_id); |
126 | 1 | to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); |
127 | 1 | ck_assert_msg(strncmp(tox_id_str, EXPECTED_TOX_ID, TOX_ADDRESS_SIZE * 2) == 0, |
128 | 1 | "tox ids do not match, expected %s got %s", EXPECTED_TOX_ID, tox_id_str); |
129 | | |
130 | | /* Giving the tox a chance to error on iterate due to corrupted loaded structures */ |
131 | 1 | tox_iterate(tox, nullptr); |
132 | | |
133 | 1 | free(status_message); |
134 | 1 | free(name); |
135 | 1 | tox_kill(tox); |
136 | 1 | } |
137 | | |
138 | | int main(int argc, char *argv[]) |
139 | 3 | { |
140 | 3 | char base_path[4096] = {0}; |
141 | | |
142 | 3 | if (argc <= 1) { |
143 | 3 | const char *srcdir = getenv("srcdir"); |
144 | | |
145 | 3 | if (srcdir == nullptr) { |
146 | 2 | srcdir = "."; |
147 | 2 | } |
148 | | |
149 | 3 | snprintf(base_path, sizeof(base_path), "%s", srcdir); |
150 | 3 | } else { |
151 | 0 | snprintf(base_path, sizeof(base_path), "%s", argv[1]); |
152 | 0 | base_path[strrchr(base_path, '/') - base_path] = '\0'; |
153 | 0 | } |
154 | | |
155 | 3 | char save_path[4096 + sizeof(LOADED_SAVE_FILE)]; |
156 | 3 | snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE); |
157 | | |
158 | 3 | test_save_compatibility(save_path); |
159 | | |
160 | 3 | return 0; |
161 | 3 | } |