2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_disk.c
23 * @brief testcase for the storage module
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
29 #define TESTSTRING "Hello World\0"
38 if (strlen (TESTSTRING) !=
39 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
40 GNUNET_DISK_PERM_USER_READ
41 | GNUNET_DISK_PERM_USER_WRITE))
43 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
45 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof(tmp) - 1);
48 fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
52 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
54 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
55 TESTSTRING, ".testfile");
58 GNUNET_DISK_file_copy (".testfile", ".testfile2");
59 memset (tmp, 0, sizeof(tmp));
60 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof(tmp) - 1);
63 fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
68 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
70 fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
71 TESTSTRING, ".testfile2");
75 GNUNET_break (0 == unlink (".testfile"));
76 GNUNET_break (0 == unlink (".testfile2"));
77 if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
87 struct GNUNET_DISK_FileHandle *fh;
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,
103 GNUNET_break (0 == unlink (".testfile"));
112 scan_callback (void *want, const char *filename)
114 if (NULL != strstr (filename, want))
124 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
130 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
135 GNUNET_DISK_directory_scan ("test", &scan_callback,
136 "test" DIR_SEPARATOR_STR "entry");
137 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
152 iter_callback (void *cls,
153 const char *filename)
168 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
173 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
178 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
183 GNUNET_DISK_directory_scan ("test",
186 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
203 char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
205 GNUNET_DISK_filename_canonicalize (fn);
206 if (0 != strcmp (fn, "ab____cd_ef__g_"))
219 GNUNET_log_skip (1, GNUNET_NO);
220 if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
229 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
234 if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
239 if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
244 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
249 if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
254 if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
259 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
269 main (int argc, char *argv[])
271 unsigned int failureCount = 0;
273 GNUNET_log_setup ("test-disk", "WARNING", NULL);
274 failureCount += testReadWrite ();
275 failureCount += testOpenClose ();
276 failureCount += testDirScan ();
277 failureCount += testDirIter ();
278 failureCount += testCanonicalize ();
279 failureCount += testChangeOwner ();
280 failureCount += testDirMani ();
281 if (0 != failureCount)
284 "\n%u TESTS FAILED!\n",