8b485edc51adc181c022b956c62a4c50b91ce029
[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 2, 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 | 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,
50                "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, 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", GNUNET_DISK_OPEN_READWRITE
111       | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_WRITE
112       | GNUNET_DISK_PERM_USER_READ);
113   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
114   while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
115          (avail != -1))
116     if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
117       {
118         GNUNET_DISK_file_close (fh);
119         GNUNET_break (0 == UNLINK (".testfile"));
120         return 1;
121       }
122   GNUNET_DISK_file_close (fh);
123   GNUNET_break (0 == UNLINK (".testfile"));
124
125   return 0;
126 }
127
128 static int ok;
129
130 static int
131 scan_callback (void *want, const char *filename)
132 {
133   if (NULL != strstr (filename, want))
134     ok++;
135   return GNUNET_OK;
136 }
137
138 static int
139 testDirScan ()
140 {
141   if (GNUNET_OK != GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
142     return 1;
143   if (GNUNET_OK != GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
144     return 1;
145   GNUNET_DISK_directory_scan ("test", &scan_callback, "test" DIR_SEPARATOR_STR "entry");
146   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
147     return 1;
148   if (ok < 2)
149     return 1;
150   return 0;
151 }
152
153 static void
154 iter_callback (void *cls,
155                struct GNUNET_DISK_DirectoryIterator *di,
156                const char *filename, const char *dirname)
157 {
158   int *i = cls;
159   (*i)++;
160   GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
161 }
162
163 static void
164 iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 {
166   GNUNET_DISK_directory_iterator_start (tc->sched,
167                                         GNUNET_SCHEDULER_PRIORITY_DEFAULT,
168                                         "test", &iter_callback, cls);
169 }
170
171 static int
172 testDirIter ()
173 {
174   int i;
175
176   i = 0;
177   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
178     return 1;
179   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
180     return 1;
181   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
182     return 1;
183   GNUNET_SCHEDULER_run (&iter_task, &i);
184   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
185     return 1;
186   if (i < 3)
187     return 1;
188   return 0;
189 }
190
191
192 static int
193 testGetHome ()
194 {
195   struct GNUNET_CONFIGURATION_Handle *cfg;
196   char *fn;
197   int ret;
198
199   cfg = GNUNET_CONFIGURATION_create ();
200   GNUNET_assert (cfg != NULL);
201   GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
202                                          "/tmp/test-gnunet-disk-a/b/c");
203   fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
204   GNUNET_assert (fn != NULL);
205   GNUNET_CONFIGURATION_destroy (cfg);
206   ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
207   GNUNET_free (fn);
208   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a");
209   return ret;
210 }
211
212 static int
213 testCanonicalize ()
214 {
215   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
216   GNUNET_DISK_filename_canonicalize (fn);
217   if (0 != strcmp (fn, "ab____cd_ef__g_"))
218     {
219       GNUNET_free (fn);
220       return 1;
221     }
222   GNUNET_free (fn);
223   return 0;
224 }
225
226 static int
227 testChangeOwner ()
228 {
229   GNUNET_log_skip (1, GNUNET_NO);
230   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
231     return 1;
232   return 0;
233 }
234
235 static int
236 testDirMani ()
237 {
238   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
239     return 1;
240   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
241     return 1;
242   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
243     return 1;
244   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
245     return 1;
246   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
247     return 1;
248   if (GNUNET_YES != GNUNET_DISK_directory_test ("test"))
249     return 1;
250   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
251     return 1;
252
253
254   return 0;
255 }
256
257
258 int
259 main (int argc, char *argv[])
260 {
261   unsigned int failureCount = 0;
262
263   GNUNET_log_setup ("test-disk", "WARNING", NULL);
264   failureCount += testReadWrite ();
265   failureCount += testOpenClose ();
266   failureCount += testDirScan ();
267   failureCount += testDirIter ();
268   failureCount += testGetHome ();
269   failureCount += testCanonicalize ();
270   failureCount += testChangeOwner ();
271   failureCount += testDirMani ();
272   if (failureCount != 0)
273     {
274       fprintf (stderr, "\n%u TESTS FAILED!\n", failureCount);
275       return -1;
276     }
277   return 0;
278 }                               /* end of main */