fix
[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,
41                             strlen (TESTSTRING),
42                             GNUNET_DISK_PERM_USER_READ |
43                             GNUNET_DISK_PERM_USER_WRITE))
44     return 1;
45   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
46     return 1;
47   ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
48   if (ret < 0)
49     {
50       fprintf (stderr,
51                "Error reading file `%s' in testReadWrite\n", ".testfile");
52       return 1;
53     }
54   tmp[ret] = '\0';
55   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
56     {
57       fprintf (stderr,
58                "Error in testReadWrite: *%s* != *%s* for file %s\n",
59                tmp, TESTSTRING, ".testfile");
60       return 1;
61     }
62   GNUNET_DISK_file_copy (".testfile", ".testfile2");
63   memset (tmp, 0, sizeof (tmp));
64   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
65   if (ret < 0)
66     {
67       fprintf (stderr,
68                "Error reading file `%s' in testReadWrite\n", ".testfile2");
69       return 1;
70     }
71   tmp[ret] = '\0';
72   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
73     {
74       fprintf (stderr,
75                "Error in testReadWrite: *%s* != *%s* for file %s\n",
76                tmp, TESTSTRING, ".testfile2");
77       return 1;
78     }
79
80   GNUNET_break (0 == UNLINK (".testfile"));
81   GNUNET_break (0 == UNLINK (".testfile2"));
82   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
83     return 1;
84
85   return 0;
86 }
87
88 static int
89 testOpenClose ()
90 {
91   struct GNUNET_DISK_FileHandle *fh;
92   uint64_t size;
93   long avail;
94
95   fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
96                               | GNUNET_DISK_OPEN_CREATE,
97                               GNUNET_DISK_PERM_USER_READ |
98                               GNUNET_DISK_PERM_USER_WRITE);
99   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
100   GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
101   GNUNET_DISK_file_close (fh);
102   GNUNET_break (GNUNET_OK ==
103                 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
104   if (size != 5)
105     return 1;
106   GNUNET_break (0 == UNLINK (".testfile"));
107
108   /* test that avail goes down as we fill the disk... */
109   GNUNET_log_skip (1, GNUNET_NO);
110   avail = GNUNET_DISK_get_blocks_available (".testfile");
111   GNUNET_log_skip (0, GNUNET_NO);
112   fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
113                               | GNUNET_DISK_OPEN_CREATE,
114                               GNUNET_DISK_PERM_USER_WRITE |
115                               GNUNET_DISK_PERM_USER_READ);
116   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
117   while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
118          (avail != -1))
119     if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
120       {
121         GNUNET_DISK_file_close (fh);
122         GNUNET_break (0 == UNLINK (".testfile"));
123         return 1;
124       }
125   GNUNET_DISK_file_close (fh);
126   GNUNET_break (0 == UNLINK (".testfile"));
127
128   return 0;
129 }
130
131 static int ok;
132
133 static int
134 scan_callback (void *want, const char *filename)
135 {
136   if (NULL != strstr (filename, want))
137     ok++;
138   return GNUNET_OK;
139 }
140
141 static int
142 testDirScan ()
143 {
144   if (GNUNET_OK !=
145       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
146     return 1;
147   if (GNUNET_OK !=
148       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
149     return 1;
150   GNUNET_DISK_directory_scan ("test", &scan_callback,
151                               "test" DIR_SEPARATOR_STR "entry");
152   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
153     return 1;
154   if (ok < 2)
155     return 1;
156   return 0;
157 }
158
159 static void
160 iter_callback (void *cls,
161                struct GNUNET_DISK_DirectoryIterator *di,
162                const char *filename, const char *dirname)
163 {
164   int *i = cls;
165   (*i)++;
166   GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
167 }
168
169 static void
170 iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   GNUNET_DISK_directory_iterator_start (tc->sched,
173                                         GNUNET_SCHEDULER_PRIORITY_DEFAULT,
174                                         "test", &iter_callback, cls);
175 }
176
177 static int
178 testDirIter ()
179 {
180   int i;
181
182   i = 0;
183   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
184     return 1;
185   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
186     return 1;
187   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
188     return 1;
189   GNUNET_SCHEDULER_run (&iter_task, &i);
190   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
191     return 1;
192   if (i < 3)
193     return 1;
194   return 0;
195 }
196
197
198 static int
199 testGetHome ()
200 {
201   struct GNUNET_CONFIGURATION_Handle *cfg;
202   char *fn;
203   int ret;
204
205   cfg = GNUNET_CONFIGURATION_create ();
206   GNUNET_assert (cfg != NULL);
207   GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
208                                          "/tmp/test-gnunet-disk-a/b/c");
209   fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
210   GNUNET_assert (fn != NULL);
211   GNUNET_CONFIGURATION_destroy (cfg);
212   ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
213   GNUNET_free (fn);
214   GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a"));
215   return ret;
216 }
217
218 static int
219 testCanonicalize ()
220 {
221   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
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 */