e8ec88c9fc3346c9768d4fac67d94d4b9e6dcb1e
[oweals/gnunet.git] / src / util / test_disk.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
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_common.h"
28 #include "gnunet_disk_lib.h"
29 #include "gnunet_scheduler_lib.h"
30
31 #define TESTSTRING "Hello World\0"
32
33 static int
34 testReadWrite ()
35 {
36   char tmp[100 + 1];
37   int ret;
38
39   if (strlen (TESTSTRING) !=
40       GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
41                             GNUNET_DISK_PERM_USER_READ |
42                             GNUNET_DISK_PERM_USER_WRITE))
43     return 1;
44   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
45     return 1;
46   ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
47   if (ret < 0)
48     {
49       fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
50                ".testfile");
51       return 1;
52     }
53   tmp[ret] = '\0';
54   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
55     {
56       fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n",
57                tmp, TESTSTRING, ".testfile");
58       return 1;
59     }
60   GNUNET_DISK_file_copy (".testfile", ".testfile2");
61   memset (tmp, 0, sizeof (tmp));
62   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
63   if (ret < 0)
64     {
65       fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
66                ".testfile2");
67       return 1;
68     }
69   tmp[ret] = '\0';
70   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
71     {
72       fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n",
73                tmp, TESTSTRING, ".testfile2");
74       return 1;
75     }
76
77   GNUNET_break (0 == UNLINK (".testfile"));
78   GNUNET_break (0 == UNLINK (".testfile2"));
79   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
80     return 1;
81
82   return 0;
83 }
84
85 static int
86 testOpenClose ()
87 {
88   struct GNUNET_DISK_FileHandle *fh;
89   uint64_t size;
90   long avail;
91
92   fh = GNUNET_DISK_file_open (".testfile",
93                               GNUNET_DISK_OPEN_READWRITE |
94                               GNUNET_DISK_OPEN_CREATE,
95                               GNUNET_DISK_PERM_USER_READ |
96                               GNUNET_DISK_PERM_USER_WRITE);
97   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
98   GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
99   GNUNET_DISK_file_close (fh);
100   GNUNET_break (GNUNET_OK ==
101                 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
102   if (size != 5)
103     return 1;
104   GNUNET_break (0 == UNLINK (".testfile"));
105
106   /* test that avail goes down as we fill the disk... */
107   GNUNET_log_skip (1, GNUNET_NO);
108   avail = GNUNET_DISK_get_blocks_available (".testfile");
109   GNUNET_log_skip (0, GNUNET_NO);
110   fh = GNUNET_DISK_file_open (".testfile",
111                               GNUNET_DISK_OPEN_READWRITE |
112                               GNUNET_DISK_OPEN_CREATE,
113                               GNUNET_DISK_PERM_USER_WRITE |
114                               GNUNET_DISK_PERM_USER_READ);
115   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
116   while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
117          (avail != -1))
118     if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
119       {
120         GNUNET_DISK_file_close (fh);
121         GNUNET_break (0 == UNLINK (".testfile"));
122         return 1;
123       }
124   GNUNET_DISK_file_close (fh);
125   GNUNET_break (0 == UNLINK (".testfile"));
126
127   return 0;
128 }
129
130 static int ok;
131
132 static int
133 scan_callback (void *want, const char *filename)
134 {
135   if (NULL != strstr (filename, want))
136     ok++;
137   return GNUNET_OK;
138 }
139
140 static int
141 testDirScan ()
142 {
143   if (GNUNET_OK !=
144       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
145     return 1;
146   if (GNUNET_OK !=
147       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
148     return 1;
149   GNUNET_DISK_directory_scan ("test", &scan_callback,
150                               "test" DIR_SEPARATOR_STR "entry");
151   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
152     return 1;
153   if (ok < 2)
154     return 1;
155   return 0;
156 }
157
158 static void
159 iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
160                const char *filename, const char *dirname)
161 {
162   int *i = cls;
163
164   (*i)++;
165   GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
166 }
167
168 static void
169 iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171   GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
172                                         "test", &iter_callback, cls);
173 }
174
175 static int
176 testDirIter ()
177 {
178   int i;
179
180   i = 0;
181   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
182     return 1;
183   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
184     return 1;
185   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
186     return 1;
187   GNUNET_SCHEDULER_run (&iter_task, &i);
188   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
189     return 1;
190   if (i < 3)
191     return 1;
192   return 0;
193 }
194
195
196 static int
197 testGetHome ()
198 {
199   struct GNUNET_CONFIGURATION_Handle *cfg;
200   char *fn;
201   int ret;
202
203   cfg = GNUNET_CONFIGURATION_create ();
204   GNUNET_assert (cfg != NULL);
205   GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
206                                          "/tmp/test-gnunet-disk-a/b/c");
207   fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
208   GNUNET_assert (fn != NULL);
209   GNUNET_CONFIGURATION_destroy (cfg);
210   ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
211   GNUNET_free (fn);
212   GNUNET_break (GNUNET_OK ==
213                 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a"));
214   return ret;
215 }
216
217 static int
218 testCanonicalize ()
219 {
220   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
221
222   GNUNET_DISK_filename_canonicalize (fn);
223   if (0 != strcmp (fn, "ab____cd_ef__g_"))
224     {
225       GNUNET_free (fn);
226       return 1;
227     }
228   GNUNET_free (fn);
229   return 0;
230 }
231
232 static int
233 testChangeOwner ()
234 {
235   GNUNET_log_skip (1, GNUNET_NO);
236   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
237     return 1;
238   return 0;
239 }
240
241 static int
242 testDirMani ()
243 {
244   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
245     return 1;
246   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
247     return 1;
248   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
249     return 1;
250   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
251     return 1;
252   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
253     return 1;
254   if (GNUNET_YES != GNUNET_DISK_directory_test ("test"))
255     return 1;
256   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
257     return 1;
258
259
260   return 0;
261 }
262
263
264 int
265 main (int argc, char *argv[])
266 {
267   unsigned int failureCount = 0;
268
269   GNUNET_log_setup ("test-disk", "WARNING", NULL);
270   failureCount += testReadWrite ();
271   failureCount += testOpenClose ();
272   failureCount += testDirScan ();
273   failureCount += testDirIter ();
274   failureCount += testGetHome ();
275   failureCount += testCanonicalize ();
276   failureCount += testChangeOwner ();
277   failureCount += testDirMani ();
278   if (failureCount != 0)
279     {
280       fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount);
281       return -1;
282     }
283   return 0;
284 }                               /* end of main */