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/>.
20 * @file util/test_disk.c
21 * @brief testcase for the storage module
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
27 #define TESTSTRING "Hello World\0"
36 if (strlen (TESTSTRING) !=
37 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
38 GNUNET_DISK_PERM_USER_READ |
39 GNUNET_DISK_PERM_USER_WRITE))
41 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
43 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
46 FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
50 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
52 FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
53 TESTSTRING, ".testfile");
56 GNUNET_DISK_file_copy (".testfile", ".testfile2");
57 memset (tmp, 0, sizeof (tmp));
58 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
61 FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n",
66 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
68 FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
69 TESTSTRING, ".testfile2");
73 GNUNET_break (0 == UNLINK (".testfile"));
74 GNUNET_break (0 == UNLINK (".testfile2"));
75 if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
85 struct GNUNET_DISK_FileHandle *fh;
88 fh = GNUNET_DISK_file_open (".testfile",
89 GNUNET_DISK_OPEN_READWRITE |
90 GNUNET_DISK_OPEN_CREATE,
91 GNUNET_DISK_PERM_USER_READ |
92 GNUNET_DISK_PERM_USER_WRITE);
93 GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
94 GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
95 GNUNET_DISK_file_close (fh);
96 GNUNET_break (GNUNET_OK ==
97 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, GNUNET_YES));
100 GNUNET_break (0 == UNLINK (".testfile"));
109 scan_callback (void *want, const char *filename)
111 if (NULL != strstr (filename, want))
121 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
127 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
132 GNUNET_DISK_directory_scan ("test", &scan_callback,
133 "test" DIR_SEPARATOR_STR "entry");
134 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
149 iter_callback (void *cls,
150 const char *filename)
165 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
170 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
175 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
180 GNUNET_DISK_directory_scan ("test",
183 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
200 char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
202 GNUNET_DISK_filename_canonicalize (fn);
203 if (0 != strcmp (fn, "ab____cd_ef__g_"))
217 GNUNET_log_skip (1, GNUNET_NO);
218 if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
228 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
233 if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
238 if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
243 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
248 if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
253 if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
258 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
268 main (int argc, char *argv[])
270 unsigned int failureCount = 0;
272 GNUNET_log_setup ("test-disk", "WARNING", NULL);
273 failureCount += testReadWrite ();
274 failureCount += testOpenClose ();
275 failureCount += testDirScan ();
276 failureCount += testDirIter ();
277 failureCount += testCanonicalize ();
278 failureCount += testChangeOwner ();
279 failureCount += testDirMani ();
280 if (0 != failureCount)
283 "\n%u TESTS FAILED!\n",