summaryrefslogtreecommitdiff
path: root/auto_tests/file_saving_test.c
diff options
context:
space:
mode:
authorpranomostro <pranomestro@gmail.com>2016-12-15 13:35:47 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-12-16 04:35:25 +0000
commit1a1529c2c555bb5d6cee9804d3166e50adc5c222 (patch)
treeeab04c207b4c52968d53706e7dadbee4191bd60e /auto_tests/file_saving_test.c
parentbf6db329ac94267117fc74d9fdd90069a5145331 (diff)
Add test for obtaining savedata, writing it to a file, reading it again and checking if it was read correctly.
Diffstat (limited to 'auto_tests/file_saving_test.c')
-rw-r--r--auto_tests/file_saving_test.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/auto_tests/file_saving_test.c b/auto_tests/file_saving_test.c
new file mode 100644
index 00000000..05841e0f
--- /dev/null
+++ b/auto_tests/file_saving_test.c
@@ -0,0 +1,133 @@
1/* file_saving_test.c
2 *
3 * Small test for checking if obtaining savedata, saving it to disk and using
4 * works correctly.
5 *
6 * Copyright (C) 2016 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "../toxcore/tox.h"
30#include "../toxencryptsave/toxencryptsave.h"
31
32static const char *pphrase = "bar", *name = "foo";
33
34static void tse(void)
35{
36 struct Tox_Options options;
37 tox_options_default(&options);
38 Tox *t = tox_new(&options, NULL);
39
40 tox_self_set_name(t, (const uint8_t *)name, strlen(name), NULL);
41
42 FILE *f = fopen("save", "w");
43
44 off_t sz = tox_get_savedata_size(t);
45 uint8_t *clear = (uint8_t *)malloc(sz);
46
47 /*this function does not write any data at all*/
48 tox_get_savedata(t, clear);
49
50 sz += TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
51 uint8_t *cipher = (uint8_t *)malloc(sz);
52
53 TOX_ERR_ENCRYPTION eerr;
54
55 if (!tox_pass_encrypt(clear, sz - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)pphrase, strlen(pphrase), cipher,
56 &eerr)) {
57 fprintf(stderr, "error: could not encrypt, error code %d\n", eerr);
58 exit(4);
59 }
60
61 size_t wv = fwrite(cipher, sizeof(*cipher), sz, f);
62 printf("written wv = %li of %li\n", wv, sz);
63
64 free(cipher);
65 free(clear);
66 fclose(f);
67 tox_kill(t);
68}
69
70static void tsd(void)
71{
72 FILE *f = fopen("save", "r");
73 fseek(f, 0, SEEK_END);
74 long sz = ftell(f);
75 fseek(f, 0, SEEK_SET);
76
77 uint8_t *cipher = (uint8_t *)malloc(sz);
78 uint8_t *clear = (uint8_t *)malloc(sz - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
79 size_t rv = fread(cipher, sizeof(*cipher), sz, f);
80 printf("read rv = %li of %li\n", rv, sz);
81
82 TOX_ERR_DECRYPTION derr;
83
84 if (!tox_pass_decrypt(cipher, sz, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr)) {
85 fprintf(stderr, "error: could not decrypt, error code %d\n", derr);
86 exit(3);
87 }
88
89 struct Tox_Options options;
90
91 tox_options_default(&options);
92
93 tox_options_set_savedata_type(&options, TOX_SAVEDATA_TYPE_TOX_SAVE);
94
95 tox_options_set_savedata_data(&options, clear, sz);
96
97 TOX_ERR_NEW err;
98
99 Tox *t = tox_new(&options, &err);
100
101 if (t == NULL) {
102 fprintf(stderr, "error: tox_new returned the error value %d\n", err);
103 return;
104 }
105
106 uint8_t readname[TOX_MAX_NAME_LENGTH];
107 tox_self_get_name(t, readname);
108 readname[tox_self_get_name_size(t)] = '\0';
109
110 if (strcmp((const char *)readname, name)) {
111 fprintf(stderr, "error: name returned by tox_self_get_name does not match expected result\n");
112 exit(2);
113 }
114
115 free(cipher);
116 free(clear);
117 fclose(f);
118 tox_kill(t);
119}
120
121int main(void)
122{
123 tse();
124 tsd();
125
126 int ret = remove("./save");
127
128 if (ret != 0) {
129 fprintf(stderr, "error: could not remove savefile\n");
130 }
131
132 return 0;
133}