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) auto_test(tox_strncasecmp) auto_test(typing) auto_test(version) +auto_test(save_compatibility) if(BUILD_TOXAV) auto_test(toxav_basic) @@ -525,3 +526,7 @@ target_link_modules(Messenger_test toxcore misc_tools) add_executable(random_testing ${CPUFEATURES} testing/random_testing.cc) target_link_modules(random_testing toxcore misc_tools) + +add_executable(save-generator + other/fun/save-generator.c) +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 Binary files /dev/null and b/auto_tests/data/save.tox 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 @@ +//Tests to make sure new save code is compatible with old save files + +#include "../testing/misc_tools.h" +#include "../toxcore/tox.h" +#include "check_compat.h" + +#include + +#define SAVE_FILE "../auto_tests/data/save.tox" + +// Information from the save file +#define NAME "name" +#define NAME_SIZE strlen(NAME) +#define STATUS_MESSAGE "Hello World" +#define STATUS_MESSAGE_SIZE strlen(STATUS_MESSAGE) +#define NUM_FRIENDS 1 +#define NOSPAM "4C762C7D" +#define TOX_ID "B70E97D41F69B7F4C42A5BC7BD7A76B95B8030BE1B7C0E9E6FC19FC4ABEB195B4C762C7D800B" + +static size_t get_file_size(void) +{ + size_t size = 0; + + FILE *fp = fopen(SAVE_FILE, "r"); + + if (fp == nullptr) { + return size; + } + + fseek(fp, 0, SEEK_END); + size = ftell(fp); + fseek(fp, 0, SEEK_SET); + fclose(fp); + + return size; +} + +static uint8_t *read_save(size_t *length) +{ + const size_t size = get_file_size(); + + if (size == 0) { + return nullptr; + } + + FILE *fp = fopen(SAVE_FILE, "r"); + + if (!fp) { + return nullptr; + } + + uint8_t *data = (uint8_t *)malloc(size); + + if (!data) { + fclose(fp); + return nullptr; + } + + if (fread(data, size, 1, fp) != 1) { + free(data); + fclose(fp); + return nullptr; + } + + *length = size; + fclose(fp); + + return data; +} + +static void test_save_compatibility(void) +{ + struct Tox_Options options = { 0 }; + tox_options_default(&options); + + size_t size = 0; + uint8_t *save_data = read_save(&size); + ck_assert_msg(save_data != nullptr, "Error while reading save file."); + + options.savedata_data = save_data; + options.savedata_length = size; + options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + + Tox *tox = tox_new(&options, nullptr); + + free(save_data); + + size_t name_size = tox_self_get_name_size(tox); + ck_assert_msg(name_size == NAME_SIZE, "name sizes do not match expected %zu got %zu", NAME_SIZE, name_size); + + uint8_t name[TOX_MAX_NAME_LENGTH]; + tox_self_get_name(tox, name); + ck_assert_msg(strncmp((const char *)name, NAME, name_size) == 0, "names do not match, expected %s got %s", NAME, name); + + size_t status_message_size = tox_self_get_status_message_size(tox); + ck_assert_msg(status_message_size == STATUS_MESSAGE_SIZE, "status message sizes do not match, expected %zu got %zu", + STATUS_MESSAGE_SIZE, status_message_size); + + uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH]; + tox_self_get_status_message(tox, status_message); + ck_assert_msg(strncmp((const char *)status_message, STATUS_MESSAGE, status_message_size) == 0, + "status messages do not match, expected %s got %s", + STATUS_MESSAGE, status_message); + + size_t num_friends = tox_self_get_friend_list_size(tox); + ck_assert_msg(num_friends == NUM_FRIENDS, "number of friends do not match, expected %d got %zu", NUM_FRIENDS, + num_friends); + + uint32_t nospam = tox_self_get_nospam(tox); + char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; + size_t length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); + nospam_str[length] = '\0'; + ck_assert_msg(strcmp(nospam_str, NOSPAM) == 0, "nospam does not match, expected %s got %s", NOSPAM, nospam_str); + + uint8_t tox_id[TOX_ADDRESS_SIZE]; + char tox_id_str[TOX_ADDRESS_SIZE * 2]; + tox_self_get_address(tox, tox_id); + to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); + ck_assert_msg(strncmp(tox_id_str, TOX_ID, TOX_ADDRESS_SIZE * 2) == 0, "tox ids do not match, expected %s got %s", + TOX_ID, tox_id_str); + + tox_kill(tox); +} + +int main(void) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + test_save_compatibility(); + + return 0; +} 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( "@libsodium", ], ) + +cc_binary( + name = "save-generator", + srcs = ["save-generator.c"], + deps = [ + "//c-toxcore/testing:misc_tools", + "//c-toxcore/toxcore", + ], +) 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 @@ +#include +#include +#include +#include + +#include "../../testing/misc_tools.h" +#include "../../toxcore/ccompat.h" +#include "../../toxcore/tox.h" + +#define SAVE_FILE "save.tox" +#define STATUS_MESSAGE "Hello World" +#define REQUEST_MESSAGE "Add me." +#define BOOTSTRAP_IP "185.14.30.213" +#define BOOTSTRAP_ADDRESS "2555763C8C460495B14157D234DD56B86300A2395554BCAE4621AC345B8C1B1B" +#define UDP_PORT 443 + +static bool write_save(const uint8_t *data, size_t length) +{ + FILE *fp = fopen(SAVE_FILE, "w"); + + if (!fp) { + return false; + } + + if (fwrite(data, length, 1, fp) != 1) { + fclose(fp); + return false; + } + + fclose(fp); + return true; +} + +static bool bootstrap_tox(Tox *tox) +{ + uint8_t *key = hex_string_to_bin(BOOTSTRAP_ADDRESS); + + if (!key) { + printf("Could not allocate memory for tox address\n"); + return false; + } + + Tox_Err_Bootstrap err; + tox_bootstrap(tox, BOOTSTRAP_IP, UDP_PORT, key, &err); + free(key); + + if (err != TOX_ERR_BOOTSTRAP_OK) { + printf("Failed to bootstrap. Error number: %d", err); + return false; + } + + return true; +} + +static void tox_connection_callback(Tox *tox, Tox_Connection connection, void *userdata) +{ + if (connection == TOX_CONNECTION_UDP) { + printf("Connected to the tox network.\n"); + *(bool *)userdata = true; + } +} + +static void print_information(Tox *tox) +{ + uint8_t tox_id[TOX_ADDRESS_SIZE]; + char tox_id_str[TOX_ADDRESS_SIZE * 2]; + tox_self_get_address(tox, tox_id); + to_hex(tox_id_str, tox_id, TOX_ADDRESS_SIZE); + + char nospam_str[(TOX_NOSPAM_SIZE * 2) + 1]; + uint32_t nospam = tox_self_get_nospam(tox); + int length = snprintf(nospam_str, sizeof(nospam_str), "%08X", nospam); + nospam_str[length] = '\0'; + + uint8_t name[TOX_MAX_NAME_LENGTH]; + tox_self_get_name(tox, name); + + printf("INFORMATION\n"); + printf("----------------------------------\n"); + printf("Tox ID: %.*s.\n", (int)TOX_ADDRESS_SIZE * 2, tox_id_str); + printf("Nospam: %s.\n", nospam_str); + printf("Name: %s.\n", name); + printf("Status message: %s.\n", STATUS_MESSAGE); + printf("Number of friends: %zu.\n", tox_self_get_friend_list_size(tox)); + printf("----------------------------------\n"); +} + +int main(int argc, char *argv[]) +{ + if (argc < 3) { + printf("Usage: ./save-generator ...\n"); + return -1; + } + + Tox *tox = tox_new(nullptr, nullptr); + + if (!tox) { + printf("Failed to create tox.\n"); + return -1; + } + + if (!bootstrap_tox(tox)) { + tox_kill(tox); + return -1; + } + + tox_callback_self_connection_status(tox, tox_connection_callback); + + bool connected = false; + + while (!connected) { + tox_iterate(tox, &connected); + c_sleep(tox_iteration_interval(tox)); + } + + Tox_Err_Set_Info err; + const uint8_t *name = (uint8_t *)argv[1]; + tox_self_set_name(tox, name, strlen((char *)name), &err); + + if (err != TOX_ERR_SET_INFO_OK) { + printf("Failed to set name. Error number %d\n", err); + } + + tox_self_set_status_message(tox, (const uint8_t *)STATUS_MESSAGE, strlen(STATUS_MESSAGE), &err); + + if (err != TOX_ERR_SET_INFO_OK) { + printf("Failed to set status. Error number: %d\n", err); + } + + for (unsigned int i = 2; i < argc; i++) { //start at 2 because that is where the tox ids are + uint8_t *address = hex_string_to_bin(argv[i]); + Tox_Err_Friend_Add friend_err; + tox_friend_add(tox, address, (const uint8_t *)REQUEST_MESSAGE, strlen(REQUEST_MESSAGE), &friend_err); + free(address); + + if (friend_err != TOX_ERR_FRIEND_ADD_OK) { + printf("Failed to add friend number %u. Error number: %d\n", i - 1, friend_err); + } + } + + const size_t length = tox_get_savedata_size(tox); + uint8_t *savedata = (uint8_t *)malloc(length); + + if (!savedata) { + printf("Could not allocate memory for savedata.\n"); + tox_kill(tox); + return -1; + } + + tox_get_savedata(tox, savedata); + + bool ret = write_save(savedata, length); + free(savedata); + + if (!ret) { + printf("Failed to write save.\n"); + tox_kill(tox); + return -1; + } + + printf("Wrote tox save to %s\n", SAVE_FILE); + + print_information(tox); + + tox_kill(tox); + + return 0; +} 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) return ret; } +void to_hex(char *out, uint8_t *in, int size) +{ + while (size--) { + if (*in >> 4 < 0xA) { + *out++ = '0' + (*in >> 4); + } else { + *out++ = 'A' + (*in >> 4) - 0xA; + } + + if ((*in & 0xf) < 0xA) { + *out++ = '0' + (*in & 0xF); + } else { + *out++ = 'A' + (*in & 0xF) - 0xA; + } + + in++; + } +} + /* Reimplementation of strncasecmp() function from strings.h, as strings.h is * POSIX and not portable. Specifically it doesn't exist on MSVC. */ 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" { void c_sleep(uint32_t x); uint8_t *hex_string_to_bin(const char *hex_string); +void to_hex(char *out, uint8_t *in, int size); int tox_strncasecmp(const char *s1, const char *s2, size_t n); int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled);