Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/file_saving_test.c
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2026 The TokTok team.
3
 * Copyright © 2016 Tox project.
4
 */
5
6
/*
7
 * Small test for checking if obtaining savedata, saving it to disk and using
8
 * works correctly.
9
 */
10
11
#include <limits.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <string.h>
15
16
#include "../toxcore/ccompat.h"
17
#include "../toxcore/tox.h"
18
#include "auto_test_support.h"
19
#include "check_compat.h"
20
21
#include "../toxcore/ccompat.h"
22
#include "../toxcore/tox.h"
23
#include "../toxencryptsave/toxencryptsave.h"
24
25
static const char *pphrase = "bar";
26
static const char *name = "foo";
27
static const char *savefile = "./save";
28
29
static void save_data_encrypted(void)
30
1
{
31
1
    struct Tox_Options *options = tox_options_new(nullptr);
32
1
    ck_assert(options != nullptr);
33
1
    Tox *t = tox_new_log(options, nullptr, nullptr);
34
1
    ck_assert(t != nullptr);
35
1
    tox_options_free(options);
36
37
1
    tox_self_set_name(t, (const uint8_t *)name, strlen(name), nullptr);
38
39
1
    FILE *f = fopen(savefile, "wb");
40
1
    ck_assert(f != nullptr);
41
42
1
    size_t size = tox_get_savedata_size(t);
43
1
    uint8_t *clear = (uint8_t *)malloc(size);
44
1
    ck_assert(clear != nullptr);
45
46
    /*this function does not write any data at all*/
47
1
    tox_get_savedata(t, clear);
48
49
1
    size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
50
1
    uint8_t *cipher = (uint8_t *)malloc(size);
51
1
    ck_assert(cipher != nullptr);
52
53
1
    Tox_Err_Encryption eerr;
54
55
1
    ck_assert_msg(tox_pass_encrypt(clear, size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase,
56
1
                                   strlen(pphrase), cipher,
57
1
                                   &eerr), "Could not encrypt, error code %u.", eerr);
58
59
1
    size_t written_value = fwrite(cipher, sizeof(*cipher), size, f);
60
1
    printf("written written_value = %u of %u\n", (unsigned)written_value, (unsigned)size);
61
62
1
    free(cipher);
63
1
    free(clear);
64
1
    fclose(f);
65
1
    tox_kill(t);
66
1
}
67
68
static void load_data_decrypted(void)
69
1
{
70
1
    FILE *f = fopen(savefile, "rb");
71
1
    ck_assert(f != nullptr);
72
1
    fseek(f, 0, SEEK_END);
73
1
    int64_t size = ftell(f);
74
1
    fseek(f, 0, SEEK_SET);
75
76
1
    ck_assert_msg(TOX_PASS_ENCRYPTION_EXTRA_LENGTH <= size && size <= UINT_MAX, "file size out of range");
77
78
1
    uint8_t *cipher = (uint8_t *)malloc(size);
79
1
    ck_assert(cipher != nullptr);
80
1
    const size_t clear_size = size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
81
1
    uint8_t *clear = (uint8_t *)malloc(clear_size);
82
1
    ck_assert(clear != nullptr);
83
1
    size_t read_value = fread(cipher, sizeof(*cipher), size, f);
84
1
    printf("Read read_value = %u of %u\n", (unsigned)read_value, (unsigned)size);
85
86
1
    Tox_Err_Decryption derr;
87
88
1
    ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr),
89
1
                  "Could not decrypt, error code %s.", tox_err_decryption_to_string(derr));
90
91
1
    struct Tox_Options *options = tox_options_new(nullptr);
92
1
    ck_assert(options != nullptr);
93
94
1
    tox_options_set_experimental_owned_data(options, true);
95
1
    tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
96
1
    ck_assert(tox_options_set_savedata_data(options, clear, clear_size));
97
1
    free(clear);
98
99
1
    Tox_Err_New err;
100
101
1
    Tox *t = tox_new_log(options, &err, nullptr);
102
1
    ck_assert_msg(t != nullptr, "tox_new returned the error value %s", tox_err_new_to_string(err));
103
104
1
    tox_options_free(options);
105
106
1
    uint8_t *readname = (uint8_t *)malloc(tox_self_get_name_size(t));
107
1
    ck_assert(readname != nullptr);
108
1
    tox_self_get_name(t, readname);
109
110
1
    ck_assert_msg(memcmp(readname, name, tox_self_get_name_size(t)) == 0,
111
1
                  "name returned by tox_self_get_name does not match expected result");
112
113
1
    tox_kill(t);
114
1
    free(cipher);
115
1
    free(readname);
116
1
    fclose(f);
117
1
}
118
119
int main(void)
120
1
{
121
1
    setvbuf(stdout, nullptr, _IONBF, 0);
122
1
    save_data_encrypted();
123
1
    load_data_decrypted();
124
125
1
    ck_assert_msg(remove(savefile) == 0, "Could not remove the savefile.");
126
127
1
    return 0;
128
1
}