Update plibc header
[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_util_lib.h"
28
29 #define TESTSTRING "Hello World\0"
30
31 static int
32 testReadWrite ()
33 {
34   char tmp[100 + 1];
35   int ret;
36
37   if (strlen (TESTSTRING) !=
38       GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
39                             GNUNET_DISK_PERM_USER_READ |
40                             GNUNET_DISK_PERM_USER_WRITE))
41     return 1;
42   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
43     return 1;
44   ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
45   if (ret < 0)
46   {
47     FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
48     return 1;
49   }
50   tmp[ret] = '\0';
51   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
52   {
53     FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
54              TESTSTRING, ".testfile");
55     return 1;
56   }
57   GNUNET_DISK_file_copy (".testfile", ".testfile2");
58   memset (tmp, 0, sizeof (tmp));
59   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
60   if (ret < 0)
61   {
62     FPRINTF (stderr, "Error reading file `%s' in testReadWrite\n",
63              ".testfile2");
64     return 1;
65   }
66   tmp[ret] = '\0';
67   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
68   {
69     FPRINTF (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
70              TESTSTRING, ".testfile2");
71     return 1;
72   }
73
74   GNUNET_break (0 == UNLINK (".testfile"));
75   GNUNET_break (0 == UNLINK (".testfile2"));
76   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
77     return 1;
78
79   return 0;
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 static int
108 scan_callback (void *want, const char *filename)
109 {
110   if (NULL != strstr (filename, want))
111     ok++;
112   return GNUNET_OK;
113 }
114
115 static int
116 testDirScan ()
117 {
118   if (GNUNET_OK !=
119       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
120     return 1;
121   if (GNUNET_OK !=
122       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
123     return 1;
124   GNUNET_DISK_directory_scan ("test", &scan_callback,
125                               "test" DIR_SEPARATOR_STR "entry");
126   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
127     return 1;
128   if (ok < 2)
129     return 1;
130   return 0;
131 }
132
133 static void
134 iter_callback (void *cls, struct GNUNET_DISK_DirectoryIterator *di,
135                const char *filename, const char *dirname)
136 {
137   int *i = cls;
138
139   (*i)++;
140   GNUNET_DISK_directory_iterator_next (di, GNUNET_NO);
141 }
142
143 static void
144 iter_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 {
146   GNUNET_DISK_directory_iterator_start (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
147                                         "test", &iter_callback, cls);
148 }
149
150 static int
151 testDirIter ()
152 {
153   int i;
154
155   i = 0;
156   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
157     return 1;
158   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
159     return 1;
160   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
161     return 1;
162   GNUNET_SCHEDULER_run (&iter_task, &i);
163   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
164     return 1;
165   if (i < 3)
166     return 1;
167   return 0;
168 }
169
170
171 static int
172 testGetHome ()
173 {
174   struct GNUNET_CONFIGURATION_Handle *cfg;
175   char *fn;
176   int ret;
177
178   cfg = GNUNET_CONFIGURATION_create ();
179   GNUNET_assert (cfg != NULL);
180   GNUNET_CONFIGURATION_set_value_string (cfg, "service", "HOME",
181                                          "/tmp/test-gnunet-disk-a/b/c");
182   fn = GNUNET_DISK_get_home_filename (cfg, "service", "d", "e", NULL);
183   GNUNET_assert (fn != NULL);
184   GNUNET_CONFIGURATION_destroy (cfg);
185   ret = strcmp ("/tmp/test-gnunet-disk-a/b/c/d/e", fn);
186   GNUNET_free (fn);
187   GNUNET_break (GNUNET_OK ==
188                 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-disk-a"));
189   return ret;
190 }
191
192 static int
193 testCanonicalize ()
194 {
195   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
196
197   GNUNET_DISK_filename_canonicalize (fn);
198   if (0 != strcmp (fn, "ab____cd_ef__g_"))
199   {
200     GNUNET_free (fn);
201     return 1;
202   }
203   GNUNET_free (fn);
204   return 0;
205 }
206
207 static int
208 testChangeOwner ()
209 {
210   GNUNET_log_skip (1, GNUNET_NO);
211   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
212     return 1;
213   return 0;
214 }
215
216 static int
217 testDirMani ()
218 {
219   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
220     return 1;
221   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
222     return 1;
223   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
224     return 1;
225   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
226     return 1;
227   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
228     return 1;
229   if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
230     return 1;
231   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
232     return 1;
233
234
235   return 0;
236 }
237
238
239 int
240 main (int argc, char *argv[])
241 {
242   unsigned int failureCount = 0;
243
244   GNUNET_log_setup ("test-disk", "WARNING", NULL);
245   failureCount += testReadWrite ();
246   failureCount += testOpenClose ();
247   failureCount += testDirScan ();
248   failureCount += testDirIter ();
249   failureCount += testGetHome ();
250   failureCount += testCanonicalize ();
251   failureCount += testChangeOwner ();
252   failureCount += testDirMani ();
253   if (failureCount != 0)
254   {
255     FPRINTF (stderr, "\n%u TESTS FAILED!\n", failureCount);
256     return -1;
257   }
258   return 0;
259 }                               /* end of main */