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
|
|
|
|
2016-12-15 20:35:47 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
#include "helpers.h"
|
2016-12-15 20:35:47 +08:00
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
#include "../toxencryptsave/toxencryptsave.h"
|
2018-01-29 05:30:39 +08:00
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
static const char *pphrase = "bar", *name = "foo", *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;
|
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
if (!tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase, strlen(pphrase), cipher,
|
2016-12-15 20:35:47 +08:00
|
|
|
&eerr)) {
|
|
|
|
fprintf(stderr, "error: could not encrypt, error code %d\n", eerr);
|
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
size_t written_value = fwrite(cipher, sizeof(*cipher), size, f);
|
2018-02-23 02:29:20 +08:00
|
|
|
printf("written written_value = %zu of %zu\n", written_value, 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);
|
2017-05-11 19:55:43 +08:00
|
|
|
long size = ftell(f);
|
2016-12-15 20:35:47 +08:00
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
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-02-23 02:29:20 +08:00
|
|
|
printf("read read_vavue = %zu of %ld\n", read_value, size);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
TOX_ERR_DECRYPTION derr;
|
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
if (!tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr)) {
|
2016-12-15 20:35:47 +08:00
|
|
|
fprintf(stderr, "error: could not decrypt, error code %d\n", derr);
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
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-01-29 05:30:39 +08:00
|
|
|
if (t == nullptr) {
|
2016-12-15 20:35:47 +08:00
|
|
|
fprintf(stderr, "error: tox_new returned the error value %d\n", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t readname[TOX_MAX_NAME_LENGTH];
|
|
|
|
tox_self_get_name(t, readname);
|
|
|
|
readname[tox_self_get_name_size(t)] = '\0';
|
|
|
|
|
|
|
|
if (strcmp((const char *)readname, name)) {
|
|
|
|
fprintf(stderr, "error: name returned by tox_self_get_name does not match expected result\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-05-11 19:55:43 +08:00
|
|
|
int ret = remove(savefile);
|
2016-12-15 20:35:47 +08:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "error: could not remove savefile\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|