diff options
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/data/save.tox | bin | 0 -> 4347 bytes | |||
-rw-r--r-- | auto_tests/save_compatibility_test.c | 132 |
2 files changed, 132 insertions, 0 deletions
diff --git a/auto_tests/data/save.tox b/auto_tests/data/save.tox new file mode 100644 index 00000000..03430c8e --- /dev/null +++ b/auto_tests/data/save.tox | |||
Binary files differ | |||
diff --git a/auto_tests/save_compatibility_test.c b/auto_tests/save_compatibility_test.c new file mode 100644 index 00000000..460dea2a --- /dev/null +++ b/auto_tests/save_compatibility_test.c | |||
@@ -0,0 +1,132 @@ | |||
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 "check_compat.h" | ||
6 | |||
7 | #include <string.h> | ||
8 | |||
9 | #define SAVE_FILE "../auto_tests/data/save.tox" | ||
10 | |||
11 | // Information from the save file | ||
12 | #define NAME "name" | ||
13 | #define NAME_SIZE strlen(NAME) | ||
14 | #define STATUS_MESSAGE "Hello World" | ||
15 | #define STATUS_MESSAGE_SIZE strlen(STATUS_MESSAGE) | ||
16 | #define NUM_FRIENDS 1 | ||
17 | #define NOSPAM "4C762C7D" | ||
18 | #define TOX_ID "B70E97D41F69B7F4C42A5BC7BD7A76B95B8030BE1B7C0E9E6FC19FC4ABEB195B4C762C7D800B" | ||
19 | |||
20 | static size_t get_file_size(void) | ||
21 | { | ||
22 | size_t size = 0; | ||
23 | |||
24 | FILE *fp = fopen(SAVE_FILE, "r"); | ||
25 | |||
26 | if (fp == nullptr) { | ||
27 | return size; | ||
28 | } | ||
29 | |||
30 | fseek(fp, 0, SEEK_END); | ||
31 | size = ftell(fp); | ||
32 | fseek(fp, 0, SEEK_SET); | ||
33 | fclose(fp); | ||
34 | |||
35 | return size; | ||
36 | } | ||
37 | |||
38 | static uint8_t *read_save(size_t *length) | ||
39 | { | ||
40 | const size_t size = get_file_size(); | ||
41 | |||
42 | if (size == 0) { | ||
43 | return nullptr; | ||
44 | } | ||
45 | |||
46 | FILE *fp = fopen(SAVE_FILE, "r"); | ||
47 | |||
48 | if (!fp) { | ||
49 | return nullptr; | ||
50 | } | ||
51 | |||
52 | uint8_t *data = (uint8_t *)malloc(size); | ||
53 | |||
54 | if (!data) { | ||
55 | fclose(fp); | ||
56 | return nullptr; | ||
57 | } | ||
58 | |||
59 | if (fread(data, size, 1, fp) != 1) { | ||
60 | free(data); | ||
61 | fclose(fp); | ||
62 | return nullptr; | ||
63 | } | ||
64 | |||
65 | *length = size; | ||
66 | fclose(fp); | ||
67 | |||
68 | return data; | ||
69 | } | ||
70 | |||
71 | static void test_save_compatibility(void) | ||
72 | { | ||
73 | struct Tox_Options options = { 0 }; | ||
74 | tox_options_default(&options); | ||
75 | |||
76 | size_t size = 0; | ||
77 | uint8_t *save_data = read_save(&size); | ||
78 | ck_assert_msg(save_data != nullptr, "Error while reading save file."); | ||
79 | |||
80 | options.savedata_data = save_data; | ||
81 | options.savedata_length = size; | ||
82 | options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; | ||
83 | |||
84 | Tox *tox = tox_new(&options, nullptr); | ||
85 | |||
86 | free(save_data); | ||
87 | |||
88 | size_t name_size = tox_self_get_name_size(tox); | ||
89 | ck_assert_msg(name_size == NAME_SIZE, "name sizes do not match expected %zu got %zu", NAME_SIZE, name_size); | ||
90 | |||
91 | uint8_t name[TOX_MAX_NAME_LENGTH]; | ||
92 | tox_self_get_name(tox, name); | ||
93 | ck_assert_msg(strncmp((const char *)name, NAME, name_size) == 0, "names do not match, expected %s got %s", NAME, name); | ||
94 | |||
95 | size_t status_message_size = tox_self_get_status_message_size(tox); | ||
96 | ck_assert_msg(status_message_size == STATUS_MESSAGE_SIZE, "status message sizes do not match, expected %zu got %zu", | ||
97 | STATUS_MESSAGE_SIZE, status_message_size); | ||
98 | |||
99 | uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH]; | ||
100 | tox_self_get_status_message(tox, status_message); | ||
101 | ck_assert_msg(strncmp((const char *)status_message, STATUS_MESSAGE, status_message_size) == 0, | ||
102 | "status messages do not match, expected %s got %s", | ||
103 | STATUS_MESSAGE, status_message); | ||
104 | |||
105 | size_t num_friends = tox_self_get_friend_list_size(tox); | ||
106 | ck_assert_msg(num_friends == NUM_FRIENDS, "number of friends do not match, expected %d got %zu", NUM_FRIENDS, | ||
107 | num_friends); | ||
108 | |||
109 | uint32_t nospam = tox_self_get_nospam(tox); | ||
110 | char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; | ||
111 | size_t length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); | ||
112 | nospam_str[length] = '\0'; | ||
113 | ck_assert_msg(strcmp(nospam_str, NOSPAM) == 0, "nospam does not match, expected %s got %s", NOSPAM, nospam_str); | ||
114 | |||
115 | uint8_t tox_id[TOX_ADDRESS_SIZE]; | ||
116 | char tox_id_str[TOX_ADDRESS_SIZE * 2]; | ||
117 | tox_self_get_address(tox, tox_id); | ||
118 | to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); | ||
119 | ck_assert_msg(strncmp(tox_id_str, TOX_ID, TOX_ADDRESS_SIZE * 2) == 0, "tox ids do not match, expected %s got %s", | ||
120 | TOX_ID, tox_id_str); | ||
121 | |||
122 | tox_kill(tox); | ||
123 | } | ||
124 | |||
125 | int main(void) | ||
126 | { | ||
127 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
128 | |||
129 | test_save_compatibility(); | ||
130 | |||
131 | return 0; | ||
132 | } | ||