summaryrefslogtreecommitdiff
path: root/auto_tests/file_saving_test.c
blob: 05841e0fe13dcdeb9d53ee26f3a8638e5a26d17f (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
124
125
126
127
128
129
130
131
132
133
/* file_saving_test.c
 *
 * Small test for checking if obtaining savedata, saving it to disk and using
 * works correctly.
 *
 *  Copyright (C) 2016 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  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.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

#include "../toxcore/tox.h"
#include "../toxencryptsave/toxencryptsave.h"

static const char *pphrase = "bar", *name = "foo";

static void tse(void)
{
    struct Tox_Options options;
    tox_options_default(&options);
    Tox *t = tox_new(&options, NULL);

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

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

    off_t sz = tox_get_savedata_size(t);
    uint8_t *clear = (uint8_t *)malloc(sz);

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

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

    TOX_ERR_ENCRYPTION eerr;

    if (!tox_pass_encrypt(clear, sz - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase, strlen(pphrase), cipher,
                          &eerr)) {
        fprintf(stderr, "error: could not encrypt, error code %d\n", eerr);
        exit(4);
    }

    size_t wv = fwrite(cipher, sizeof(*cipher), sz, f);
    printf("written wv = %li of %li\n", wv, sz);

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

static void tsd(void)
{
    FILE *f = fopen("save", "r");
    fseek(f, 0, SEEK_END);
    long sz = ftell(f);
    fseek(f, 0, SEEK_SET);

    uint8_t *cipher = (uint8_t *)malloc(sz);
    uint8_t *clear = (uint8_t *)malloc(sz - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
    size_t rv = fread(cipher, sizeof(*cipher), sz, f);
    printf("read rv = %li of %li\n", rv, sz);

    TOX_ERR_DECRYPTION derr;

    if (!tox_pass_decrypt(cipher, sz, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr)) {
        fprintf(stderr, "error: could not decrypt, error code %d\n", derr);
        exit(3);
    }

    struct Tox_Options options;

    tox_options_default(&options);

    tox_options_set_savedata_type(&options, TOX_SAVEDATA_TYPE_TOX_SAVE);

    tox_options_set_savedata_data(&options, clear, sz);

    TOX_ERR_NEW err;

    Tox *t = tox_new(&options, &err);

    if (t == NULL) {
        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)
{
    tse();
    tsd();

    int ret = remove("./save");

    if (ret != 0) {
        fprintf(stderr, "error: could not remove savefile\n");
    }

    return 0;
}