diff options
-rw-r--r-- | auto_tests/BUILD.bazel | 2 | ||||
-rw-r--r-- | auto_tests/save_compatibility_test.c | 49 |
2 files changed, 31 insertions, 20 deletions
diff --git a/auto_tests/BUILD.bazel b/auto_tests/BUILD.bazel index a5ef7645..eb9acb06 100644 --- a/auto_tests/BUILD.bazel +++ b/auto_tests/BUILD.bazel | |||
@@ -21,7 +21,9 @@ test_sizes = { | |||
21 | "small", | 21 | "small", |
22 | ), | 22 | ), |
23 | srcs = [src], | 23 | srcs = [src], |
24 | args = ["$(location %s)" % src], | ||
24 | copts = ["-Wno-sign-compare"], | 25 | copts = ["-Wno-sign-compare"], |
26 | data = glob(["data/*"]), | ||
25 | deps = [ | 27 | deps = [ |
26 | ":check_compat", | 28 | ":check_compat", |
27 | ":run_auto_test", | 29 | ":run_auto_test", |
diff --git a/auto_tests/save_compatibility_test.c b/auto_tests/save_compatibility_test.c index 460dea2a..08ac9e17 100644 --- a/auto_tests/save_compatibility_test.c +++ b/auto_tests/save_compatibility_test.c | |||
@@ -17,39 +17,36 @@ | |||
17 | #define NOSPAM "4C762C7D" | 17 | #define NOSPAM "4C762C7D" |
18 | #define TOX_ID "B70E97D41F69B7F4C42A5BC7BD7A76B95B8030BE1B7C0E9E6FC19FC4ABEB195B4C762C7D800B" | 18 | #define TOX_ID "B70E97D41F69B7F4C42A5BC7BD7A76B95B8030BE1B7C0E9E6FC19FC4ABEB195B4C762C7D800B" |
19 | 19 | ||
20 | static size_t get_file_size(void) | 20 | static size_t get_file_size(const char *save_path) |
21 | { | 21 | { |
22 | size_t size = 0; | 22 | FILE *const fp = fopen(save_path, "r"); |
23 | |||
24 | FILE *fp = fopen(SAVE_FILE, "r"); | ||
25 | 23 | ||
26 | if (fp == nullptr) { | 24 | if (fp == nullptr) { |
27 | return size; | 25 | return 0; |
28 | } | 26 | } |
29 | 27 | ||
30 | fseek(fp, 0, SEEK_END); | 28 | fseek(fp, 0, SEEK_END); |
31 | size = ftell(fp); | 29 | const size_t size = ftell(fp); |
32 | fseek(fp, 0, SEEK_SET); | ||
33 | fclose(fp); | 30 | fclose(fp); |
34 | 31 | ||
35 | return size; | 32 | return size; |
36 | } | 33 | } |
37 | 34 | ||
38 | static uint8_t *read_save(size_t *length) | 35 | static uint8_t *read_save(const char *save_path, size_t *length) |
39 | { | 36 | { |
40 | const size_t size = get_file_size(); | 37 | const size_t size = get_file_size(save_path); |
41 | 38 | ||
42 | if (size == 0) { | 39 | if (size == 0) { |
43 | return nullptr; | 40 | return nullptr; |
44 | } | 41 | } |
45 | 42 | ||
46 | FILE *fp = fopen(SAVE_FILE, "r"); | 43 | FILE *const fp = fopen(save_path, "r"); |
47 | 44 | ||
48 | if (!fp) { | 45 | if (!fp) { |
49 | return nullptr; | 46 | return nullptr; |
50 | } | 47 | } |
51 | 48 | ||
52 | uint8_t *data = (uint8_t *)malloc(size); | 49 | uint8_t *const data = (uint8_t *)malloc(size); |
53 | 50 | ||
54 | if (!data) { | 51 | if (!data) { |
55 | fclose(fp); | 52 | fclose(fp); |
@@ -68,13 +65,13 @@ static uint8_t *read_save(size_t *length) | |||
68 | return data; | 65 | return data; |
69 | } | 66 | } |
70 | 67 | ||
71 | static void test_save_compatibility(void) | 68 | static void test_save_compatibility(const char *save_path) |
72 | { | 69 | { |
73 | struct Tox_Options options = { 0 }; | 70 | struct Tox_Options options = { 0 }; |
74 | tox_options_default(&options); | 71 | tox_options_default(&options); |
75 | 72 | ||
76 | size_t size = 0; | 73 | size_t size = 0; |
77 | uint8_t *save_data = read_save(&size); | 74 | uint8_t *save_data = read_save(save_path, &size); |
78 | ck_assert_msg(save_data != nullptr, "Error while reading save file."); | 75 | ck_assert_msg(save_data != nullptr, "Error while reading save file."); |
79 | 76 | ||
80 | options.savedata_data = save_data; | 77 | options.savedata_data = save_data; |
@@ -85,14 +82,14 @@ static void test_save_compatibility(void) | |||
85 | 82 | ||
86 | free(save_data); | 83 | free(save_data); |
87 | 84 | ||
88 | size_t name_size = tox_self_get_name_size(tox); | 85 | const 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); | 86 | ck_assert_msg(name_size == NAME_SIZE, "name sizes do not match expected %zu got %zu", NAME_SIZE, name_size); |
90 | 87 | ||
91 | uint8_t name[TOX_MAX_NAME_LENGTH]; | 88 | uint8_t name[TOX_MAX_NAME_LENGTH]; |
92 | tox_self_get_name(tox, name); | 89 | 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); | 90 | ck_assert_msg(strncmp((const char *)name, NAME, name_size) == 0, "names do not match, expected %s got %s", NAME, name); |
94 | 91 | ||
95 | size_t status_message_size = tox_self_get_status_message_size(tox); | 92 | const 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", | 93 | 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); | 94 | STATUS_MESSAGE_SIZE, status_message_size); |
98 | 95 | ||
@@ -102,13 +99,13 @@ static void test_save_compatibility(void) | |||
102 | "status messages do not match, expected %s got %s", | 99 | "status messages do not match, expected %s got %s", |
103 | STATUS_MESSAGE, status_message); | 100 | STATUS_MESSAGE, status_message); |
104 | 101 | ||
105 | size_t num_friends = tox_self_get_friend_list_size(tox); | 102 | const 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, | 103 | ck_assert_msg(num_friends == NUM_FRIENDS, "number of friends do not match, expected %d got %zu", NUM_FRIENDS, |
107 | num_friends); | 104 | num_friends); |
108 | 105 | ||
109 | uint32_t nospam = tox_self_get_nospam(tox); | 106 | const uint32_t nospam = tox_self_get_nospam(tox); |
110 | char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; | 107 | char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; |
111 | size_t length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); | 108 | const size_t length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); |
112 | nospam_str[length] = '\0'; | 109 | nospam_str[length] = '\0'; |
113 | ck_assert_msg(strcmp(nospam_str, NOSPAM) == 0, "nospam does not match, expected %s got %s", NOSPAM, nospam_str); | 110 | ck_assert_msg(strcmp(nospam_str, NOSPAM) == 0, "nospam does not match, expected %s got %s", NOSPAM, nospam_str); |
114 | 111 | ||
@@ -122,11 +119,23 @@ static void test_save_compatibility(void) | |||
122 | tox_kill(tox); | 119 | tox_kill(tox); |
123 | } | 120 | } |
124 | 121 | ||
125 | int main(void) | 122 | int main(int argc, char *argv[]) |
126 | { | 123 | { |
127 | setvbuf(stdout, nullptr, _IONBF, 0); | 124 | setvbuf(stdout, nullptr, _IONBF, 0); |
128 | 125 | ||
129 | test_save_compatibility(); | 126 | char base_path[4096]; |
127 | |||
128 | if (argc <= 1) { | ||
129 | strcpy(base_path, "."); | ||
130 | } else { | ||
131 | strcpy(base_path, argv[1]); | ||
132 | base_path[strrchr(base_path, '/') - base_path] = 0; | ||
133 | } | ||
134 | |||
135 | char save_path[4096]; | ||
136 | snprintf(save_path, sizeof(save_path), "%s/%s", base_path, SAVE_FILE); | ||
137 | |||
138 | test_save_compatibility(save_path); | ||
130 | 139 | ||
131 | return 0; | 140 | return 0; |
132 | } | 141 | } |