diff options
author | endoffile78 <endoffile78@yahoo.com> | 2018-08-04 15:56:01 -0500 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-08-19 10:38:51 +0000 |
commit | 30960dcc7e9661f431d58081d79dc88692bdb50d (patch) | |
tree | 56724c62ce9605ec49768b57b16e7333b118310f | |
parent | fc7a5dc4d80ae065a11fa483e619ead02a88e213 (diff) |
Add save file generator, save compatibility test, and generate a save file
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | auto_tests/data/save.tox | bin | 0 -> 4347 bytes | |||
-rw-r--r-- | auto_tests/save_compatibility_test.c | 132 | ||||
-rw-r--r-- | other/fun/BUILD.bazel | 9 | ||||
-rw-r--r-- | other/fun/save-generator.c | 168 | ||||
-rw-r--r-- | testing/misc_tools.c | 19 | ||||
-rw-r--r-- | testing/misc_tools.h | 1 |
7 files changed, 334 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e5654429..450c224a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -439,6 +439,7 @@ auto_test(tox_one) | |||
439 | auto_test(tox_strncasecmp) | 439 | auto_test(tox_strncasecmp) |
440 | auto_test(typing) | 440 | auto_test(typing) |
441 | auto_test(version) | 441 | auto_test(version) |
442 | auto_test(save_compatibility) | ||
442 | 443 | ||
443 | if(BUILD_TOXAV) | 444 | if(BUILD_TOXAV) |
444 | auto_test(toxav_basic) | 445 | auto_test(toxav_basic) |
@@ -525,3 +526,7 @@ target_link_modules(Messenger_test toxcore misc_tools) | |||
525 | add_executable(random_testing ${CPUFEATURES} | 526 | add_executable(random_testing ${CPUFEATURES} |
526 | testing/random_testing.cc) | 527 | testing/random_testing.cc) |
527 | target_link_modules(random_testing toxcore misc_tools) | 528 | target_link_modules(random_testing toxcore misc_tools) |
529 | |||
530 | add_executable(save-generator | ||
531 | other/fun/save-generator.c) | ||
532 | target_link_modules(save-generator toxcore misc_tools) | ||
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 | } | ||
diff --git a/other/fun/BUILD.bazel b/other/fun/BUILD.bazel index 93009543..f66aab25 100644 --- a/other/fun/BUILD.bazel +++ b/other/fun/BUILD.bazel | |||
@@ -25,3 +25,12 @@ cc_binary( | |||
25 | "@libsodium", | 25 | "@libsodium", |
26 | ], | 26 | ], |
27 | ) | 27 | ) |
28 | |||
29 | cc_binary( | ||
30 | name = "save-generator", | ||
31 | srcs = ["save-generator.c"], | ||
32 | deps = [ | ||
33 | "//c-toxcore/testing:misc_tools", | ||
34 | "//c-toxcore/toxcore", | ||
35 | ], | ||
36 | ) | ||
diff --git a/other/fun/save-generator.c b/other/fun/save-generator.c new file mode 100644 index 00000000..82b83e72 --- /dev/null +++ b/other/fun/save-generator.c | |||
@@ -0,0 +1,168 @@ | |||
1 | #include <stdint.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | |||
6 | #include "../../testing/misc_tools.h" | ||
7 | #include "../../toxcore/ccompat.h" | ||
8 | #include "../../toxcore/tox.h" | ||
9 | |||
10 | #define SAVE_FILE "save.tox" | ||
11 | #define STATUS_MESSAGE "Hello World" | ||
12 | #define REQUEST_MESSAGE "Add me." | ||
13 | #define BOOTSTRAP_IP "185.14.30.213" | ||
14 | #define BOOTSTRAP_ADDRESS "2555763C8C460495B14157D234DD56B86300A2395554BCAE4621AC345B8C1B1B" | ||
15 | #define UDP_PORT 443 | ||
16 | |||
17 | static bool write_save(const uint8_t *data, size_t length) | ||
18 | { | ||
19 | FILE *fp = fopen(SAVE_FILE, "w"); | ||
20 | |||
21 | if (!fp) { | ||
22 | return false; | ||
23 | } | ||
24 | |||
25 | if (fwrite(data, length, 1, fp) != 1) { | ||
26 | fclose(fp); | ||
27 | return false; | ||
28 | } | ||
29 | |||
30 | fclose(fp); | ||
31 | return true; | ||
32 | } | ||
33 | |||
34 | static bool bootstrap_tox(Tox *tox) | ||
35 | { | ||
36 | uint8_t *key = hex_string_to_bin(BOOTSTRAP_ADDRESS); | ||
37 | |||
38 | if (!key) { | ||
39 | printf("Could not allocate memory for tox address\n"); | ||
40 | return false; | ||
41 | } | ||
42 | |||
43 | Tox_Err_Bootstrap err; | ||
44 | tox_bootstrap(tox, BOOTSTRAP_IP, UDP_PORT, key, &err); | ||
45 | free(key); | ||
46 | |||
47 | if (err != TOX_ERR_BOOTSTRAP_OK) { | ||
48 | printf("Failed to bootstrap. Error number: %d", err); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | return true; | ||
53 | } | ||
54 | |||
55 | static void tox_connection_callback(Tox *tox, Tox_Connection connection, void *userdata) | ||
56 | { | ||
57 | if (connection == TOX_CONNECTION_UDP) { | ||
58 | printf("Connected to the tox network.\n"); | ||
59 | *(bool *)userdata = true; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | static void print_information(Tox *tox) | ||
64 | { | ||
65 | uint8_t tox_id[TOX_ADDRESS_SIZE]; | ||
66 | char tox_id_str[TOX_ADDRESS_SIZE * 2]; | ||
67 | tox_self_get_address(tox, tox_id); | ||
68 | to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); | ||
69 | |||
70 | char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; | ||
71 | uint32_t nospam = tox_self_get_nospam(tox); | ||
72 | int length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); | ||
73 | nospam_str[length] = '\0'; | ||
74 | |||
75 | uint8_t name[TOX_MAX_NAME_LENGTH]; | ||
76 | tox_self_get_name(tox, name); | ||
77 | |||
78 | printf("INFORMATION\n"); | ||
79 | printf("----------------------------------\n"); | ||
80 | printf("Tox ID: %.*s.\n", (int)TOX_ADDRESS_SIZE * 2, tox_id_str); | ||
81 | printf("Nospam: %s.\n", nospam_str); | ||
82 | printf("Name: %s.\n", name); | ||
83 | printf("Status message: %s.\n", STATUS_MESSAGE); | ||
84 | printf("Number of friends: %zu.\n", tox_self_get_friend_list_size(tox)); | ||
85 | printf("----------------------------------\n"); | ||
86 | } | ||
87 | |||
88 | int main(int argc, char *argv[]) | ||
89 | { | ||
90 | if (argc < 3) { | ||
91 | printf("Usage: ./save-generator <name> <friend id> ...\n"); | ||
92 | return -1; | ||
93 | } | ||
94 | |||
95 | Tox *tox = tox_new(nullptr, nullptr); | ||
96 | |||
97 | if (!tox) { | ||
98 | printf("Failed to create tox.\n"); | ||
99 | return -1; | ||
100 | } | ||
101 | |||
102 | if (!bootstrap_tox(tox)) { | ||
103 | tox_kill(tox); | ||
104 | return -1; | ||
105 | } | ||
106 | |||
107 | tox_callback_self_connection_status(tox, tox_connection_callback); | ||
108 | |||
109 | bool connected = false; | ||
110 | |||
111 | while (!connected) { | ||
112 | tox_iterate(tox, &connected); | ||
113 | c_sleep(tox_iteration_interval(tox)); | ||
114 | } | ||
115 | |||
116 | Tox_Err_Set_Info err; | ||
117 | const uint8_t *name = (uint8_t *)argv[1]; | ||
118 | tox_self_set_name(tox, name, strlen((char *)name), &err); | ||
119 | |||
120 | if (err != TOX_ERR_SET_INFO_OK) { | ||
121 | printf("Failed to set name. Error number %d\n", err); | ||
122 | } | ||
123 | |||
124 | tox_self_set_status_message(tox, (const uint8_t *)STATUS_MESSAGE, strlen(STATUS_MESSAGE), &err); | ||
125 | |||
126 | if (err != TOX_ERR_SET_INFO_OK) { | ||
127 | printf("Failed to set status. Error number: %d\n", err); | ||
128 | } | ||
129 | |||
130 | for (unsigned int i = 2; i < argc; i++) { //start at 2 because that is where the tox ids are | ||
131 | uint8_t *address = hex_string_to_bin(argv[i]); | ||
132 | Tox_Err_Friend_Add friend_err; | ||
133 | tox_friend_add(tox, address, (const uint8_t *)REQUEST_MESSAGE, strlen(REQUEST_MESSAGE), &friend_err); | ||
134 | free(address); | ||
135 | |||
136 | if (friend_err != TOX_ERR_FRIEND_ADD_OK) { | ||
137 | printf("Failed to add friend number %u. Error number: %d\n", i - 1, friend_err); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | const size_t length = tox_get_savedata_size(tox); | ||
142 | uint8_t *savedata = (uint8_t *)malloc(length); | ||
143 | |||
144 | if (!savedata) { | ||
145 | printf("Could not allocate memory for savedata.\n"); | ||
146 | tox_kill(tox); | ||
147 | return -1; | ||
148 | } | ||
149 | |||
150 | tox_get_savedata(tox, savedata); | ||
151 | |||
152 | bool ret = write_save(savedata, length); | ||
153 | free(savedata); | ||
154 | |||
155 | if (!ret) { | ||
156 | printf("Failed to write save.\n"); | ||
157 | tox_kill(tox); | ||
158 | return -1; | ||
159 | } | ||
160 | |||
161 | printf("Wrote tox save to %s\n", SAVE_FILE); | ||
162 | |||
163 | print_information(tox); | ||
164 | |||
165 | tox_kill(tox); | ||
166 | |||
167 | return 0; | ||
168 | } | ||
diff --git a/testing/misc_tools.c b/testing/misc_tools.c index af6f6872..c9a73b3e 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c | |||
@@ -85,6 +85,25 @@ uint8_t *hex_string_to_bin(const char *hex_string) | |||
85 | return ret; | 85 | return ret; |
86 | } | 86 | } |
87 | 87 | ||
88 | void to_hex(char *out, uint8_t *in, int size) | ||
89 | { | ||
90 | while (size--) { | ||
91 | if (*in >> 4 < 0xA) { | ||
92 | *out++ = '0' + (*in >> 4); | ||
93 | } else { | ||
94 | *out++ = 'A' + (*in >> 4) - 0xA; | ||
95 | } | ||
96 | |||
97 | if ((*in & 0xf) < 0xA) { | ||
98 | *out++ = '0' + (*in & 0xF); | ||
99 | } else { | ||
100 | *out++ = 'A' + (*in & 0xF) - 0xA; | ||
101 | } | ||
102 | |||
103 | in++; | ||
104 | } | ||
105 | } | ||
106 | |||
88 | /* Reimplementation of strncasecmp() function from strings.h, as strings.h is | 107 | /* Reimplementation of strncasecmp() function from strings.h, as strings.h is |
89 | * POSIX and not portable. Specifically it doesn't exist on MSVC. | 108 | * POSIX and not portable. Specifically it doesn't exist on MSVC. |
90 | */ | 109 | */ |
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index b715c8cc..259525a5 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -13,6 +13,7 @@ extern "C" { | |||
13 | void c_sleep(uint32_t x); | 13 | void c_sleep(uint32_t x); |
14 | 14 | ||
15 | uint8_t *hex_string_to_bin(const char *hex_string); | 15 | uint8_t *hex_string_to_bin(const char *hex_string); |
16 | void to_hex(char *out, uint8_t *in, int size); | ||
16 | int tox_strncasecmp(const char *s1, const char *s2, size_t n); | 17 | int tox_strncasecmp(const char *s1, const char *s2, size_t n); |
17 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); | 18 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); |
18 | 19 | ||