2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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"
37 if (strlen (TESTSTRING) !=
38 GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
39 GNUNET_DISK_PERM_USER_READ |
40 GNUNET_DISK_PERM_USER_WRITE))
42 if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
44 ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
47 FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
51 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
53 FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
54 TESTSTRING, ".testfile");
57 GNUNET_DISK_file_copy (".testfile", ".testfile2");
58 memset (tmp, 0, sizeof (tmp));
59 ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
62 FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n",
67 if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
69 FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
70 TESTSTRING, ".testfile2");
74 GNUNET_break (0 == UNLINK (".testfile"));
75 GNUNET_break (0 == UNLINK (".testfile2"));
76 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"));
108 scan_callback (void *want, const char *filename)
110 if (NULL != strstr (filename, want))
119 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
122 GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
124 GNUNET_DISK_directory_scan ("test", &scan_callback,
125 "test" DIR_SEPARATOR_STR "entry");
126 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
134 iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
135 const char *filename, const char *dirname)
140 GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
144 iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
147 "test", &iter_callback, cls);
156 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
158 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
160 if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
162 GNUNET_SCHEDULER_run (&iter_task, &i);
163 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
174 struct GNUNET_CONFIGURATION_Handle *cfg;
178 cfg = GNUNET_CONFIGURATION_create ();
179 GNUNET_assert (cfg != NULL);
180 GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
181 "/tmp/test-gnunet-disk-a/b/c");
182 fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
183 GNUNET_assert (fn != NULL);
184 GNUNET_CONFIGURATION_destroy (cfg);
185 ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
187 GNUNET_break (GNUNET_OK ==
188 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a"));
195 char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
197 GNUNET_DISK_filename_canonicalize (fn);
198 if (0 != strcmp (fn, "ab____cd_ef__g_"))
210 GNUNET_log_skip (1, GNUNET_NO);
211 if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
219 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
221 if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
223 if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
225 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
227 if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
229 if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
231 if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
240 main (int argc, char *argv[])
242 unsigned int failureCount = 0;
244 GNUNET_log_setup ("test-disk", "WARNING", NULL);
245 failureCount += testReadWrite ();
246 failureCount += testOpenClose ();
247 failureCount += testDirScan ();
248 failureCount += testDirIter ();
249 failureCount += testGetHome ();
250 failureCount += testCanonicalize ();
251 failureCount += testChangeOwner ();
252 failureCount += testDirMani ();
253 if (failureCount != 0)
255 FPRINTF (stderr, "\n%u TESTS FAILED!\n", failureCount);