add missing files
[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,
100                                        GNUNET_YES));
101   if (size != 5)
102     return 1;
103   GNUNET_break (0 == unlink (".testfile"));
104
105   return 0;
106 }
107
108 static int ok;
109
110
111 static int
112 scan_callback (void *want, const char *filename)
113 {
114   if (NULL != strstr (filename, want))
115     ok++;
116   return GNUNET_OK;
117 }
118
119
120 static int
121 testDirScan ()
122 {
123   if (GNUNET_OK !=
124       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry"))
125   {
126     GNUNET_break (0);
127     return 1;
128   }
129   if (GNUNET_OK !=
130       GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more"))
131   {
132     GNUNET_break (0);
133     return 1;
134   }
135   GNUNET_DISK_directory_scan ("test", &scan_callback,
136                               "test" DIR_SEPARATOR_STR "entry");
137   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
138   {
139     GNUNET_break (0);
140     return 1;
141   }
142   if (ok < 2)
143   {
144     GNUNET_break (0);
145     return 1;
146   }
147   return 0;
148 }
149
150
151 static int
152 iter_callback (void *cls,
153                const char *filename)
154 {
155   int *i = cls;
156
157   (*i)++;
158   return GNUNET_OK;
159 }
160
161
162 static int
163 testDirIter ()
164 {
165   int i;
166
167   i = 0;
168   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry"))
169   {
170     GNUNET_break (0);
171     return 1;
172   }
173   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many"))
174   {
175     GNUNET_break (0);
176     return 1;
177   }
178   if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more"))
179   {
180     GNUNET_break (0);
181     return 1;
182   }
183   GNUNET_DISK_directory_scan ("test",
184                               &iter_callback,
185                               &i);
186   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
187   {
188     GNUNET_break (0);
189     return 1;
190   }
191   if (i < 3)
192   {
193     GNUNET_break (0);
194     return 1;
195   }
196   return 0;
197 }
198
199
200 static int
201 testCanonicalize ()
202 {
203   char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\"");
204
205   GNUNET_DISK_filename_canonicalize (fn);
206   if (0 != strcmp (fn, "ab____cd_ef__g_"))
207   {
208     GNUNET_free (fn);
209     return 1;
210   }
211   GNUNET_free (fn);
212   return 0;
213 }
214
215
216 static int
217 testChangeOwner ()
218 {
219   GNUNET_log_skip (1, GNUNET_NO);
220   if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser"))
221     return 1;
222   return 0;
223 }
224
225
226 static int
227 testDirMani ()
228 {
229   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing"))
230   {
231     GNUNET_break (0);
232     return 1;
233   }
234   if (GNUNET_NO != GNUNET_DISK_file_test ("test"))
235   {
236     GNUNET_break (0);
237     return 1;
238   }
239   if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing"))
240   {
241     GNUNET_break (0);
242     return 1;
243   }
244   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
245   {
246     GNUNET_break (0);
247     return 1;
248   }
249   if (GNUNET_OK != GNUNET_DISK_directory_create ("test"))
250   {
251     GNUNET_break (0);
252     return 1;
253   }
254   if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES))
255   {
256     GNUNET_break (0);
257     return 1;
258   }
259   if (GNUNET_OK != GNUNET_DISK_directory_remove ("test"))
260   {
261     GNUNET_break (0);
262     return 1;
263   }
264   return 0;
265 }
266
267
268 int
269 main (int argc, char *argv[])
270 {
271   unsigned int failureCount = 0;
272
273   GNUNET_log_setup ("test-disk", "WARNING", NULL);
274   failureCount += testReadWrite ();
275   failureCount += testOpenClose ();
276   failureCount += testDirScan ();
277   failureCount += testDirIter ();
278   failureCount += testCanonicalize ();
279   failureCount += testChangeOwner ();
280   failureCount += testDirMani ();
281   if (0 != failureCount)
282   {
283     fprintf (stderr,
284              "\n%u TESTS FAILED!\n",
285              failureCount);
286     return -1;
287   }
288   return 0;
289 }                               /* end of main */