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