Coverage Report

Created: 2024-01-26 01:52

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