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