summaryrefslogtreecommitdiff
path: root/auto_tests/file_saving_test.c
blob: cdcce95fa3791d869b776b4e8c26bfe561435ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* SPDX-License-Identifier: GPL-3.0-or-later
 * Copyright © 2016-2018 The TokTok team.
 * Copyright © 2016 Tox project.
 */

/*
 * Small test for checking if obtaining savedata, saving it to disk and using
 * works correctly.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h"
#include "check_compat.h"

#include "../toxencryptsave/toxencryptsave.h"

static const char *pphrase = "bar";
static const char *name = "foo";
static const char *savefile = "./save";

static void save_data_encrypted(void)
{
    struct Tox_Options *options = tox_options_new(nullptr);
    Tox *t = tox_new_log(options, nullptr, nullptr);
    tox_options_free(options);

    tox_self_set_name(t, (const uint8_t *)name, strlen(name), nullptr);

    FILE *f = fopen(savefile, "w");

    size_t size = tox_get_savedata_size(t);
    uint8_t *clear = (uint8_t *)malloc(size);

    /*this function does not write any data at all*/
    tox_get_savedata(t, clear);

    size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
    uint8_t *cipher = (uint8_t *)malloc(size);

    Tox_Err_Encryption eerr;

    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);

    size_t written_value = fwrite(cipher, sizeof(*cipher), size, f);
    printf("written written_value = %u of %u\n", (unsigned)written_value, (unsigned)size);

    free(cipher);
    free(clear);
    fclose(f);
    tox_kill(t);
}

static void load_data_decrypted(void)
{
    FILE *f = fopen(savefile, "r");
    ck_assert(f != nullptr);
    fseek(f, 0, SEEK_END);
    int64_t size = ftell(f);
    fseek(f, 0, SEEK_SET);

    ck_assert_msg(0 <= size && size <= UINT_MAX, "file size out of range");

    uint8_t *cipher = (uint8_t *)malloc(size);
    ck_assert(cipher != nullptr);
    uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
    ck_assert(clear != nullptr);
    size_t read_value = fread(cipher, sizeof(*cipher), size, f);
    printf("Read read_value = %u of %u\n", (unsigned)read_value, (unsigned)size);

    Tox_Err_Decryption derr;

    ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr),
                  "Could not decrypt, error code %d.", derr);

    struct Tox_Options *options = tox_options_new(nullptr);
    ck_assert(options != nullptr);

    tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);

    tox_options_set_savedata_data(options, clear, size);

    Tox_Err_New err;

    Tox *t = tox_new_log(options, &err, nullptr);

    tox_options_free(options);

    ck_assert_msg(t != nullptr, "tox_new returned the error value %d", err);

    uint8_t readname[TOX_MAX_NAME_LENGTH];
    tox_self_get_name(t, readname);
    readname[tox_self_get_name_size(t)] = '\0';

    ck_assert_msg(strcmp((const char *)readname, name) == 0,
                  "name returned by tox_self_get_name does not match expected result");

    tox_kill(t);
    free(clear);
    free(cipher);
    fclose(f);
}

int main(void)
{
    setvbuf(stdout, nullptr, _IONBF, 0);
    save_data_encrypted();
    load_data_decrypted();

    ck_assert_msg(remove(savefile) == 0, "Could not remove the savefile.");

    return 0;
}