first step to remove plibc
[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      SPDX-License-Identifier: AGPL3.0-or-later
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
32 static int
33 testReadWrite ()
34 {
35   char tmp[100 + 1];
36   int ret;
37
38   if (strlen (TESTSTRING) !=
39       GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (TESTSTRING),
40                             GNUNET_DISK_PERM_USER_READ |
41                             GNUNET_DISK_PERM_USER_WRITE))
42     return 1;
43   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
44     return 1;
45   ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
46   if (ret < 0)
47   {
48     fprintf (stderr, "Error reading file `%s' in testReadWrite\n", ".testfile");
49     return 1;
50   }
51   tmp[ret] = '\0';
52   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
53   {
54     fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
55              TESTSTRING, ".testfile");
56     return 1;
57   }
58   GNUNET_DISK_file_copy (".testfile", ".testfile2");
59   memset (tmp, 0, sizeof (tmp));
60   ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
61   if (ret < 0)
62   {
63     fprintf (stderr, "Error reading file `%s' in testReadWrite\n",
64              ".testfile2");
65     return 1;
66   }
67   tmp[ret] = '\0';
68   if (0 != memcmp (tmp, TESTSTRING, strlen (TESTSTRING) + 1))
69   {
70     fprintf (stderr, "Error in testReadWrite: *%s* != *%s* for file %s\n", tmp,
71              TESTSTRING, ".testfile2");
72     return 1;
73   }
74
75   GNUNET_break (0 == unlink (".testfile"));
76   GNUNET_break (0 == unlink (".testfile2"));
77   if (GNUNET_NO != GNUNET_DISK_file_test (".testfile"))
78     return 1;
79
80   return 0;
81 }
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
110 static int
111 scan_callback (void *want, const char *filename)
112 {
113   if (NULL != strstr (filename, want))
114     ok++;
115   return GNUNET_OK;
116 }
117
118
119 static int
120 testDirScan ()
121 {
122   if (GNUNET_OK !=
123       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
124   {
125     GNUNET_break (0);
126     return 1;
127   }
128   if (GNUNET_OK !=
129       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
130   {
131     GNUNET_break (0);
132     return 1;
133   }
134   GNUNET_DISK_directory_scan ("test", &scan_callback,
135                               "test" DIR_SEPARATOR_STR "entry");
136   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
137   {
138     GNUNET_break (0);
139     return 1;
140   }
141   if (ok < 2)
142   {
143     GNUNET_break (0);
144     return 1;
145   }
146   return 0;
147 }
148
149
150 static int
151 iter_callback (void *cls,
152                const char *filename)
153 {
154   int *i = cls;
155   
156   (*i)++;
157   return GNUNET_OK;
158 }
159
160
161 static int
162 testDirIter ()
163 {
164   int i;
165
166   i = 0;
167   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
168   {
169     GNUNET_break (0);
170     return 1;
171   }
172   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
173   {
174     GNUNET_break (0);
175     return 1;
176   }
177   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
178   {
179     GNUNET_break (0);
180     return 1;
181   }
182   GNUNET_DISK_directory_scan ("test",
183                               &iter_callback,
184                               &i);
185   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
186   {
187     GNUNET_break (0);
188     return 1;
189   }
190   if (i < 3)
191   {
192     GNUNET_break (0);
193     return 1;
194   }
195   return 0;
196 }
197
198
199 static int
200 testCanonicalize ()
201 {
202   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
203
204   GNUNET_DISK_filename_canonicalize (fn);
205   if (0 != strcmp (fn, "ab____cd_ef__g_"))
206   {
207     GNUNET_free (fn);
208     return 1;
209   }
210   GNUNET_free (fn);
211   return 0;
212 }
213
214
215 static int
216 testChangeOwner ()
217 {
218 #ifndef WINDOWS
219   GNUNET_log_skip (1, GNUNET_NO);
220   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
221     return 1;
222 #endif
223   return 0;
224 }
225
226
227 static int
228 testDirMani ()
229 {
230   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
231   {
232     GNUNET_break (0);
233     return 1;
234   }
235   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
236   {
237     GNUNET_break (0);
238     return 1;
239   }
240   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
241   {
242     GNUNET_break (0);
243     return 1;
244   }
245   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
246   {
247     GNUNET_break (0);
248     return 1;
249   }
250   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
251   {
252     GNUNET_break (0);
253     return 1;
254   }
255   if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
256   {
257     GNUNET_break (0);
258     return 1;
259   }
260   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
261   {
262     GNUNET_break (0);
263     return 1;
264   }
265   return 0;
266 }
267
268
269 int
270 main (int argc, char *argv[])
271 {
272   unsigned int failureCount = 0;
273
274   GNUNET_log_setup ("test-disk", "WARNING", NULL);
275   failureCount += testReadWrite ();
276   failureCount += testOpenClose ();
277   failureCount += testDirScan ();
278   failureCount += testDirIter ();
279   failureCount += testCanonicalize ();
280   failureCount += testChangeOwner ();
281   failureCount += testDirMani ();
282   if (0 != failureCount)
283   {
284     fprintf (stderr,
285              "\n%u TESTS FAILED!\n",
286              failureCount);
287     return -1;
288   }
289   return 0;
290 }                               /* end of main */