END_TEST START_TEST(test_messenger_state_saveloadsave) { /* validate that: * a) saving stays within the confined space * b) a save()d state can be load()ed back successfully * c) a second save() is of equal size * d) the second save() is of equal content */ size_t i, extra = 64; size_t size = messenger_size(m); uint8_t buffer[size + 2 * extra]; memset(buffer, 0xCD, extra); memset(buffer + extra + size, 0xCD, extra); messenger_save(m, buffer + extra); for (i = 0; i < extra; i++) { ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from messenger_save() @%u", i); ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from messenger_save() @%u", i); } int res = messenger_load(m, buffer + extra, size); if (res == -1) ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); else { char msg[128]; size_t offset = res >> 4; uint8_t *ptr = buffer + extra + offset; sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%zu/%zu, code %d", ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F); ck_assert_msg(res == 0, msg); } size_t size2 = messenger_size(m); ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2); uint8_t buffer2[size2]; messenger_save(m, buffer2); ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "Messenger state changed by store/load/store cycle"); }
/* return size of the messenger data (for saving). */ uint32_t tox_size(Tox *tox) { Messenger *m = tox; return messenger_size(m); }
/* return size of the messenger data (for saving). */ uint32_t tox_size(const Tox *tox) { const Messenger *m = tox; return messenger_size(m); }
int main(int argc, char *argv[]) { /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) exit(1); /* with optional --ipvx, now it can be 1-4 arguments... */ if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]); printf("or\n"); printf(" %s [--ipv4|--ipv6] Save.bak (to read Save.bak as state file)\n", argv[0]); exit(0); } m = new_messenger(ipv6enabled); if ( !m ) { fputs("Failed to allocate messenger datastructure\n", stderr); exit(0); } if (argc == argvoffset + 4) { uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(m->dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } } else { FILE *file = fopen(argv[argvoffset + 1], "rb"); if ( file == NULL ) { printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]); return 1; } int read; uint8_t buffer[128000]; read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", messenger_load(m, buffer, read)); fclose(file); } m_callback_friendrequest(m, print_request, NULL); m_callback_friendmessage(m, print_message, NULL); printf("OUR ID: "); uint32_t i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { if (address[i] < 16) printf("0"); printf("%hhX", address[i]); } setname(m, (uint8_t *)"Anon", 5); char temp_hex_id[128]; printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); if (!fgets(temp_hex_id, sizeof(temp_hex_id), stdin)) exit(0); if ((strlen(temp_hex_id) > 0) && (temp_hex_id[strlen(temp_hex_id) - 1] == '\n')) temp_hex_id[strlen(temp_hex_id) - 1] = '\0'; uint8_t *bin_id = hex_string_to_bin(temp_hex_id); int num = m_addfriend(m, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); free(bin_id); perror("Initialization"); while (1) { uint8_t name[128]; getname(m, num, name); printf("%s\n", name); m_sendmessage(m, num, (uint8_t *)"Test", 5); do_messenger(m); c_sleep(30); FILE *file = fopen("Save.bak", "wb"); if ( file == NULL ) { return 1; } uint8_t *buffer = malloc(messenger_size(m)); messenger_save(m, buffer); size_t write_result = fwrite(buffer, 1, messenger_size(m), file); if (write_result < messenger_size(m)) { return 1; } free(buffer); fclose(file); } kill_messenger(m); }