indentation
[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, "Error reading file `%s' in testReadWrite\n", ".testfile");
51     return 1;
52   }
53   tmp[ret] = '\0';
54   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
55   {
56     fprintf (stderr,
57              "Error in testReadWrite: *%s* != *%s* for file %s\n",
58              tmp, TESTSTRING, ".testfile");
59     return 1;
60   }
61   GNUNET_DISK_file_copy (".testfile", ".testfile2");
62   memset (tmp, 0, sizeof (tmp));
63   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
64   if (ret < 0)
65   {
66     fprintf (stderr,
67              "Error reading file `%s' in testReadWrite\n", ".testfile2");
68     return 1;
69   }
70   tmp[ret] = '\0';
71   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
72   {
73     fprintf (stderr,
74              "Error in testReadWrite: *%s* != *%s* for file %s\n",
75              tmp, TESTSTRING, ".testfile2");
76     return 1;
77   }
78
79   GNUNET_break (0 == UNLINK (".testfile"));
80   GNUNET_break (0 == UNLINK (".testfile2"));
81   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
82     return 1;
83
84   return 0;
85 }
86
87 static int
88 testOpenClose ()
89 {
90   struct GNUNET_DISK_FileHandle *fh;
91   uint64_t size;
92   long avail;
93
94   fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
95                               | GNUNET_DISK_OPEN_CREATE,
96                               GNUNET_DISK_PERM_USER_READ |
97                               GNUNET_DISK_PERM_USER_WRITE);
98   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
99   GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
100   GNUNET_DISK_file_close (fh);
101   GNUNET_break (GNUNET_OK ==
102                 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
103   if (size != 5)
104     return 1;
105   GNUNET_break (0 == UNLINK (".testfile"));
106
107   /* test that avail goes down as we fill the disk... */
108   GNUNET_log_skip (1, GNUNET_NO);
109   avail = GNUNET_DISK_get_blocks_available (".testfile");
110   GNUNET_log_skip (0, GNUNET_NO);
111   fh = GNUNET_DISK_file_open (".testfile", 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,
160                struct GNUNET_DISK_DirectoryIterator *di,
161                const char *filename, const char *dirname)
162 {
163   int *i = cls;
164
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 (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
173                                         "test", &iter_callback, cls);
174 }
175
176 static int
177 testDirIter ()
178 {
179   int i;
180
181   i = 0;
182   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
183     return 1;
184   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
185     return 1;
186   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
187     return 1;
188   GNUNET_SCHEDULER_run (&iter_task, &i);
189   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
190     return 1;
191   if (i < 3)
192     return 1;
193   return 0;
194 }
195
196
197 static int
198 testGetHome ()
199 {
200   struct GNUNET_CONFIGURATION_Handle *cfg;
201   char *fn;
202   int ret;
203
204   cfg = GNUNET_CONFIGURATION_create ();
205   GNUNET_assert (cfg != NULL);
206   GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
207                                          "/tmp/test-gnunet-disk-a/b/c");
208   fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
209   GNUNET_assert (fn != NULL);
210   GNUNET_CONFIGURATION_destroy (cfg);
211   ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
212   GNUNET_free (fn);
213   GNUNET_break (GNUNET_OK ==
214                 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
223   GNUNET_DISK_filename_canonicalize (fn);
224   if (0 != strcmp (fn, "ab____cd_ef__g_"))
225   {
226     GNUNET_free (fn);
227     return 1;
228   }
229   GNUNET_free (fn);
230   return 0;
231 }
232
233 static int
234 testChangeOwner ()
235 {
236   GNUNET_log_skip (1, GNUNET_NO);
237   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
238     return 1;
239   return 0;
240 }
241
242 static int
243 testDirMani ()
244 {
245   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
246     return 1;
247   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
248     return 1;
249   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
250     return 1;
251   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
252     return 1;
253   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
254     return 1;
255   if (GNUNET_YES != GNUNET_DISK_directory_test ("test"))
256     return 1;
257   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
258     return 1;
259
260
261   return 0;
262 }
263
264
265 int
266 main (int argc, char *argv[])
267 {
268   unsigned int failureCount = 0;
269
270   GNUNET_log_setup ("test-disk", "WARNING", NULL);
271   failureCount += testReadWrite ();
272   failureCount += testOpenClose ();
273   failureCount += testDirScan ();
274   failureCount += testDirIter ();
275   failureCount += testGetHome ();
276   failureCount += testCanonicalize ();
277   failureCount += testChangeOwner ();
278   failureCount += testDirMani ();
279   if (failureCount != 0)
280   {
281     fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount);
282     return -1;
283   }
284   return 0;
285 }                               /* end of main */