2017-01-14 23:46:31 +08:00
|
|
|
/*
|
2016-12-15 20:35:47 +08:00
|
|
|
* Small test for checking if obtaining savedata, saving it to disk and using
|
|
|
|
* works correctly.
|
2017-01-14 23:46:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright © 2016-2017 The TokTok team.
|
|
|
|
* Copyright © 2016 Tox project.
|
2016-12-15 20:35:47 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* This file is part of Tox, the free peer to peer instant messenger.
|
2016-12-15 20:35:47 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-12-15 20:35:47 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2016-12-15 20:35:47 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-15 20:35:47 +08:00
|
|
|
*/
|
2017-06-05 04:58:28 +08:00
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-16 05:31:40 +08:00
|
|
|
#include <limits.h>
|
2016-12-15 20:35:47 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
|
|
|
#include "../toxcore/ccompat.h"
|
2018-07-16 11:55:45 +08:00
|
|
|
#include "check_compat.h"
|
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
#include "../toxencryptsave/toxencryptsave.h"
|
2018-01-29 05:30:39 +08:00
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
static const char *pphrase = "bar";
|
|
|
|
static const char *name = "foo";
|
|
|
|
static const char *savefile = "./save";
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
static void save_data_encrypted(void)
|
2016-12-15 20:35:47 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
struct Tox_Options *options = tox_options_new(nullptr);
|
2018-02-19 01:50:50 +08:00
|
|
|
Tox *t = tox_new_log(options, nullptr, nullptr);
|
2018-01-21 03:47:46 +08:00
|
|
|
tox_options_free(options);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
tox_self_set_name(t, (const uint8_t *)name, strlen(name), nullptr);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
FILE *f = fopen(savefile, "w");
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-01-19 05:51:45 +08:00
|
|
|
size_t size = tox_get_savedata_size(t);
|
2017-05-11 19:55:43 +08:00
|
|
|
uint8_t *clear = (uint8_t *)malloc(size);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
/*this function does not write any data at all*/
|
|
|
|
tox_get_savedata(t, clear);
|
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
|
|
|
uint8_t *cipher = (uint8_t *)malloc(size);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
TOX_ERR_ENCRYPTION eerr;
|
|
|
|
|
2018-07-16 11:55:45 +08:00
|
|
|
ck_assert_msg(tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase,
|
|
|
|
strlen(pphrase), cipher,
|
|
|
|
&eerr), "Could not encrypt, error code %d.", eerr);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
size_t written_value = fwrite(cipher, sizeof(*cipher), size, f);
|
2018-02-24 01:11:00 +08:00
|
|
|
printf("written written_value = %u of %u\n", (unsigned)written_value, (unsigned)size);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
free(cipher);
|
|
|
|
free(clear);
|
|
|
|
fclose(f);
|
|
|
|
tox_kill(t);
|
|
|
|
}
|
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
static void load_data_decrypted(void)
|
2016-12-15 20:35:47 +08:00
|
|
|
{
|
2017-05-11 19:55:43 +08:00
|
|
|
FILE *f = fopen(savefile, "r");
|
2016-12-15 20:35:47 +08:00
|
|
|
fseek(f, 0, SEEK_END);
|
2018-07-16 11:55:45 +08:00
|
|
|
int64_t size = ftell(f);
|
2016-12-15 20:35:47 +08:00
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
2018-08-16 05:31:40 +08:00
|
|
|
ck_assert_msg(0 <= size && size <= UINT_MAX, "file size out of range");
|
2018-08-11 21:01:09 +08:00
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
uint8_t *cipher = (uint8_t *)malloc(size);
|
|
|
|
uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
|
|
|
|
size_t read_value = fread(cipher, sizeof(*cipher), size, f);
|
2018-08-16 05:31:40 +08:00
|
|
|
printf("Read read_value = %u of %u\n", (unsigned)read_value, (unsigned)size);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
TOX_ERR_DECRYPTION derr;
|
|
|
|
|
2018-07-16 11:55:45 +08:00
|
|
|
ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr),
|
|
|
|
"Could not decrypt, error code %d.", derr);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
struct Tox_Options *options = tox_options_new(nullptr);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-01-21 03:47:46 +08:00
|
|
|
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-01-21 03:47:46 +08:00
|
|
|
tox_options_set_savedata_data(options, clear, size);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
TOX_ERR_NEW err;
|
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
Tox *t = tox_new_log(options, &err, nullptr);
|
2018-01-21 03:47:46 +08:00
|
|
|
|
|
|
|
tox_options_free(options);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-07-16 11:55:45 +08:00
|
|
|
ck_assert_msg(t != nullptr, "tox_new returned the error value %d", err);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
uint8_t readname[TOX_MAX_NAME_LENGTH];
|
|
|
|
tox_self_get_name(t, readname);
|
|
|
|
readname[tox_self_get_name_size(t)] = '\0';
|
|
|
|
|
2018-07-16 11:55:45 +08:00
|
|
|
ck_assert_msg(strcmp((const char *)readname, name) == 0,
|
|
|
|
"name returned by tox_self_get_name does not match expected result");
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
free(cipher);
|
|
|
|
free(clear);
|
|
|
|
fclose(f);
|
|
|
|
tox_kill(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2018-02-19 01:50:50 +08:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2017-05-11 19:55:43 +08:00
|
|
|
save_data_encrypted();
|
|
|
|
load_data_decrypted();
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-07-16 11:55:45 +08:00
|
|
|
ck_assert_msg(remove(savefile) == 0, "Could not remove the savefile.");
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|