paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / util / test_disk.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/test_disk.c
21  * @brief testcase for the storage module
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27 #define TESTSTRING "Hello World\0"
28
29
30 static int
31 testReadWrite ()
32 {
33   char tmp[100 + 1];
34   int ret;
35
36   if (strlen (TESTSTRING) !=
37       GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
38                             GNUNET_DISK_PERM_USER_READ |
39                             GNUNET_DISK_PERM_USER_WRITE))
40     return 1;
41   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
42     return 1;
43   ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
44   if (ret < 0)
45   {
46     FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
47     return 1;
48   }
49   tmp[ret] = '\0';
50   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
51   {
52     FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
53              TESTSTRING, ".testfile");
54     return 1;
55   }
56   GNUNET_DISK_file_copy (".testfile", ".testfile2");
57   memset (tmp, 0, sizeof (tmp));
58   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
59   if (ret < 0)
60   {
61     FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n",
62              ".testfile2");
63     return 1;
64   }
65   tmp[ret] = '\0';
66   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
67   {
68     FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
69              TESTSTRING, ".testfile2");
70     return 1;
71   }
72
73   GNUNET_break (0 == UNLINK (".testfile"));
74   GNUNET_break (0 == UNLINK (".testfile2"));
75   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
76     return 1;
77
78   return 0;
79 }
80
81
82 static int
83 testOpenClose ()
84 {
85   struct GNUNET_DISK_FileHandle *fh;
86   uint64_t size;
87
88   fh = GNUNET_DISK_file_open (".testfile",
89                               GNUNET_DISK_OPEN_READWRITE |
90                               GNUNET_DISK_OPEN_CREATE,
91                               GNUNET_DISK_PERM_USER_READ |
92                               GNUNET_DISK_PERM_USER_WRITE);
93   GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh));
94   GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
95   GNUNET_DISK_file_close (fh);
96   GNUNET_break (GNUNET_OK ==
97                 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, GNUNET_YES));
98   if (size != 5)
99     return 1;
100   GNUNET_break (0 == UNLINK (".testfile"));
101
102   return 0;
103 }
104
105 static int ok;
106
107
108 static int
109 scan_callback (void *want, const char *filename)
110 {
111   if (NULL != strstr (filename, want))
112     ok++;
113   return GNUNET_OK;
114 }
115
116
117 static int
118 testDirScan ()
119 {
120   if (GNUNET_OK !=
121       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
122   {
123     GNUNET_break (0);
124     return 1;
125   }
126   if (GNUNET_OK !=
127       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
128   {
129     GNUNET_break (0);
130     return 1;
131   }
132   GNUNET_DISK_directory_scan ("test", &scan_callback,
133                               "test" DIR_SEPARATOR_STR "entry");
134   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
135   {
136     GNUNET_break (0);
137     return 1;
138   }
139   if (ok < 2)
140   {
141     GNUNET_break (0);
142     return 1;
143   }
144   return 0;
145 }
146
147
148 static int
149 iter_callback (void *cls,
150                const char *filename)
151 {
152   int *i = cls;
153   
154   (*i)++;
155   return GNUNET_OK;
156 }
157
158
159 static int
160 testDirIter ()
161 {
162   int i;
163
164   i = 0;
165   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
166   {
167     GNUNET_break (0);
168     return 1;
169   }
170   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
171   {
172     GNUNET_break (0);
173     return 1;
174   }
175   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
176   {
177     GNUNET_break (0);
178     return 1;
179   }
180   GNUNET_DISK_directory_scan ("test",
181                               &iter_callback,
182                               &i);
183   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
184   {
185     GNUNET_break (0);
186     return 1;
187   }
188   if (i < 3)
189   {
190     GNUNET_break (0);
191     return 1;
192   }
193   return 0;
194 }
195
196
197 static int
198 testCanonicalize ()
199 {
200   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
201
202   GNUNET_DISK_filename_canonicalize (fn);
203   if (0 != strcmp (fn, "ab____cd_ef__g_"))
204   {
205     GNUNET_free (fn);
206     return 1;
207   }
208   GNUNET_free (fn);
209   return 0;
210 }
211
212
213 static int
214 testChangeOwner ()
215 {
216 #ifndef WINDOWS
217   GNUNET_log_skip (1, GNUNET_NO);
218   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
219     return 1;
220 #endif
221   return 0;
222 }
223
224
225 static int
226 testDirMani ()
227 {
228   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
229   {
230     GNUNET_break (0);
231     return 1;
232   }
233   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
234   {
235     GNUNET_break (0);
236     return 1;
237   }
238   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
239   {
240     GNUNET_break (0);
241     return 1;
242   }
243   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
244   {
245     GNUNET_break (0);
246     return 1;
247   }
248   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
249   {
250     GNUNET_break (0);
251     return 1;
252   }
253   if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
254   {
255     GNUNET_break (0);
256     return 1;
257   }
258   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
259   {
260     GNUNET_break (0);
261     return 1;
262   }
263   return 0;
264 }
265
266
267 int
268 main (int argc, char *argv[])
269 {
270   unsigned int failureCount = 0;
271
272   GNUNET_log_setup ("test-disk", "WARNING", NULL);
273   failureCount += testReadWrite ();
274   failureCount += testOpenClose ();
275   failureCount += testDirScan ();
276   failureCount += testDirIter ();
277   failureCount += testCanonicalize ();
278   failureCount += testChangeOwner ();
279   failureCount += testDirMani ();
280   if (0 != failureCount)
281   {
282     FPRINTF (stderr,
283              "\n%u TESTS FAILED!\n",
284              failureCount);
285     return -1;
286   }
287   return 0;
288 }                               /* end of main */