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, GNUNET_YES));
102 GNUNET_break (0 == UNLINK (".testfile"));
111 scan_callback (void *want, const char *filename)
113 if (NULL != strstr (filename, want))
123 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
129 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
134 GNUNET_DISK_directory_scan ("test", &scan_callback,
135 "test" DIR_SEPARATOR_STR "entry");
136 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
151 iter_callback (void *cls,
152 const char *filename)
167 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
172 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
177 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
182 GNUNET_DISK_directory_scan ("test",
185 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
202 char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
204 GNUNET_DISK_filename_canonicalize (fn);
205 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"))
230 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
235 if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
240 if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
245 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
250 if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
255 if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
260 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
270 main (int argc, char *argv[])
272 unsigned int failureCount = 0;
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)
285 "\n%u TESTS FAILED!\n",