2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
|
|
|
* Copyright © 2016 Tox project.
|
2017-01-14 23:46:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2020-03-12 09:10:33 +08:00
|
|
|
* Small test for checking if obtaining savedata, saving it to disk and using
|
|
|
|
* works correctly.
|
2016-12-15 20:35:47 +08:00
|
|
|
*/
|
2017-06-05 04:58:28 +08:00
|
|
|
|
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"
|
2022-02-22 01:34:02 +08:00
|
|
|
#include "auto_test_support.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
|
|
|
|
2020-10-11 04:07:03 +08:00
|
|
|
FILE *f = fopen(savefile, "wb");
|
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
|
|
|
|
2018-10-09 05:05:14 +08:00
|
|
|
Tox_Err_Encryption eerr;
|
2016-12-15 20:35:47 +08:00
|
|
|
|
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
|
|
|
{
|
2020-10-11 04:07:03 +08:00
|
|
|
FILE *f = fopen(savefile, "rb");
|
2020-05-03 08:09:06 +08:00
|
|
|
ck_assert(f != nullptr);
|
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);
|
2020-05-03 08:09:06 +08:00
|
|
|
ck_assert(cipher != nullptr);
|
2017-05-11 19:55:43 +08:00
|
|
|
uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
|
2020-05-03 08:09:06 +08:00
|
|
|
ck_assert(clear != nullptr);
|
2017-05-11 19:55:43 +08:00
|
|
|
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
|
|
|
|
2018-10-09 05:05:14 +08:00
|
|
|
Tox_Err_Decryption derr;
|
2016-12-15 20:35:47 +08:00
|
|
|
|
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);
|
2020-05-03 08:09:06 +08:00
|
|
|
ck_assert(options != 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
|
|
|
|
2018-10-09 05:05:14 +08:00
|
|
|
Tox_Err_New err;
|
2016-12-15 20:35:47 +08:00
|
|
|
|
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
|
|
|
|
2022-03-31 04:04:01 +08:00
|
|
|
uint8_t *readname = (uint8_t *)malloc(tox_self_get_name_size(t));
|
|
|
|
ck_assert(readname != nullptr);
|
2016-12-15 20:35:47 +08:00
|
|
|
tox_self_get_name(t, readname);
|
|
|
|
|
2022-03-31 04:04:01 +08:00
|
|
|
ck_assert_msg(memcmp(readname, name, tox_self_get_name_size(t)) == 0,
|
2018-07-16 11:55:45 +08:00
|
|
|
"name returned by tox_self_get_name does not match expected result");
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2020-05-03 08:09:06 +08:00
|
|
|
tox_kill(t);
|
2016-12-15 20:35:47 +08:00
|
|
|
free(clear);
|
2020-05-03 08:09:06 +08:00
|
|
|
free(cipher);
|
2022-03-31 04:04:01 +08:00
|
|
|
free(readname);
|
2016-12-15 20:35:47 +08:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|