aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_disk.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_disk.c')
-rw-r--r--src/util/test_disk.c290
1 files changed, 0 insertions, 290 deletions
diff --git a/src/util/test_disk.c b/src/util/test_disk.c
deleted file mode 100644
index 12bde8107..000000000
--- a/src/util/test_disk.c
+++ /dev/null
@@ -1,290 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/test_disk.c
23 * @brief testcase for the storage module
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29#define TESTSTRING "Hello World\0"
30
31
32static int
33testReadWrite (void)
34{
35 char tmp[100 + 1];
36 int ret;
37
38 if (GNUNET_OK !=
39 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
40 GNUNET_DISK_PERM_USER_READ
41 | GNUNET_DISK_PERM_USER_WRITE))
42 return 1;
43 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
44 return 1;
45 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof(tmp) - 1);
46 if (ret < 0)
47 {
48 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
49 return 1;
50 }
51 tmp[ret] = '\0';
52 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
53 {
54 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
55 TESTSTRING, ".testfile");
56 return 1;
57 }
58 GNUNET_DISK_file_copy (".testfile", ".testfile2");
59 memset (tmp, 0, sizeof(tmp));
60 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof(tmp) - 1);
61 if (ret < 0)
62 {
63 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
64 ".testfile2");
65 return 1;
66 }
67 tmp[ret] = '\0';
68 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
69 {
70 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
71 TESTSTRING, ".testfile2");
72 return 1;
73 }
74
75 GNUNET_break (0 == unlink (".testfile"));
76 GNUNET_break (0 == unlink (".testfile2"));
77 if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
78 return 1;
79
80 return 0;
81}
82
83
84static int
85testOpenClose ()
86{
87 struct GNUNET_DISK_FileHandle *fh;
88 uint64_t size;
89
90 fh = GNUNET_DISK_file_open (".testfile",
91 GNUNET_DISK_OPEN_READWRITE
92 | GNUNET_DISK_OPEN_CREATE,
93 GNUNET_DISK_PERM_USER_READ
94 | GNUNET_DISK_PERM_USER_WRITE);
95 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
96 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
97 GNUNET_DISK_file_close (fh);
98 GNUNET_break (GNUNET_OK ==
99 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO,
100 GNUNET_YES));
101 if (size != 5)
102 return 1;
103 GNUNET_break (0 == unlink (".testfile"));
104
105 return 0;
106}
107
108
109static int ok;
110
111
112static int
113scan_callback (void *want, const char *filename)
114{
115 if (NULL != strstr (filename, want))
116 ok++;
117 return GNUNET_OK;
118}
119
120
121static int
122testDirScan ()
123{
124 if (GNUNET_OK !=
125 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
126 {
127 GNUNET_break (0);
128 return 1;
129 }
130 if (GNUNET_OK !=
131 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
132 {
133 GNUNET_break (0);
134 return 1;
135 }
136 GNUNET_DISK_directory_scan ("test", &scan_callback,
137 "test" DIR_SEPARATOR_STR "entry");
138 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
139 {
140 GNUNET_break (0);
141 return 1;
142 }
143 if (ok < 2)
144 {
145 GNUNET_break (0);
146 return 1;
147 }
148 return 0;
149}
150
151
152static int
153iter_callback (void *cls,
154 const char *filename)
155{
156 int *i = cls;
157
158 (*i)++;
159 return GNUNET_OK;
160}
161
162
163static int
164testDirIter ()
165{
166 int i;
167
168 i = 0;
169 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
170 {
171 GNUNET_break (0);
172 return 1;
173 }
174 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
175 {
176 GNUNET_break (0);
177 return 1;
178 }
179 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
180 {
181 GNUNET_break (0);
182 return 1;
183 }
184 GNUNET_DISK_directory_scan ("test",
185 &iter_callback,
186 &i);
187 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
188 {
189 GNUNET_break (0);
190 return 1;
191 }
192 if (i < 3)
193 {
194 GNUNET_break (0);
195 return 1;
196 }
197 return 0;
198}
199
200
201static int
202testCanonicalize ()
203{
204 char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
205
206 GNUNET_DISK_filename_canonicalize (fn);
207 if (0 != strcmp (fn, "ab____cd_ef__g_"))
208 {
209 GNUNET_free (fn);
210 return 1;
211 }
212 GNUNET_free (fn);
213 return 0;
214}
215
216
217static int
218testChangeOwner ()
219{
220 GNUNET_log_skip (1, GNUNET_NO);
221 if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
222 return 1;
223 return 0;
224}
225
226
227static int
228testDirMani ()
229{
230 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
231 {
232 GNUNET_break (0);
233 return 1;
234 }
235 if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
236 {
237 GNUNET_break (0);
238 return 1;
239 }
240 if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
241 {
242 GNUNET_break (0);
243 return 1;
244 }
245 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
246 {
247 GNUNET_break (0);
248 return 1;
249 }
250 if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
251 {
252 GNUNET_break (0);
253 return 1;
254 }
255 if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
256 {
257 GNUNET_break (0);
258 return 1;
259 }
260 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
261 {
262 GNUNET_break (0);
263 return 1;
264 }
265 return 0;
266}
267
268
269int
270main (int argc, char *argv[])
271{
272 unsigned int failureCount = 0;
273
274 GNUNET_log_setup ("test-disk", "WARNING", NULL);
275 failureCount += testReadWrite ();
276 failureCount += testOpenClose ();
277 failureCount += testDirScan ();
278 failureCount += testDirIter ();
279 failureCount += testCanonicalize ();
280 failureCount += testChangeOwner ();
281 failureCount += testDirMani ();
282 if (0 != failureCount)
283 {
284 fprintf (stderr,
285 "\n%u TESTS FAILED!\n",
286 failureCount);
287 return -1;
288 }
289 return 0;
290} /* end of main */