diff options
Diffstat (limited to 'src/util/test_disk.c')
-rw-r--r-- | src/util/test_disk.c | 291 |
1 files changed, 0 insertions, 291 deletions
diff --git a/src/util/test_disk.c b/src/util/test_disk.c deleted file mode 100644 index 35b4bd14a..000000000 --- a/src/util/test_disk.c +++ /dev/null | |||
@@ -1,291 +0,0 @@ | |||
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 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | |||
30 | #define TESTSTRING "Hello World\0" | ||
31 | |||
32 | |||
33 | static int | ||
34 | testReadWrite (void) | ||
35 | { | ||
36 | char tmp[100 + 1]; | ||
37 | int ret; | ||
38 | |||
39 | if (GNUNET_OK != | ||
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 | |||
85 | static int | ||
86 | testOpenClose () | ||
87 | { | ||
88 | struct GNUNET_DISK_FileHandle *fh; | ||
89 | uint64_t size; | ||
90 | |||
91 | fh = GNUNET_DISK_file_open (".testfile", | ||
92 | GNUNET_DISK_OPEN_READWRITE | ||
93 | | GNUNET_DISK_OPEN_CREATE, | ||
94 | GNUNET_DISK_PERM_USER_READ | ||
95 | | GNUNET_DISK_PERM_USER_WRITE); | ||
96 | GNUNET_assert (GNUNET_NO == GNUNET_DISK_handle_invalid (fh)); | ||
97 | GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5)); | ||
98 | GNUNET_DISK_file_close (fh); | ||
99 | GNUNET_break (GNUNET_OK == | ||
100 | GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, | ||
101 | GNUNET_YES)); | ||
102 | if (size != 5) | ||
103 | return 1; | ||
104 | GNUNET_break (0 == unlink (".testfile")); | ||
105 | |||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | |||
110 | static int ok; | ||
111 | |||
112 | |||
113 | static int | ||
114 | scan_callback (void *want, const char *filename) | ||
115 | { | ||
116 | if (NULL != strstr (filename, want)) | ||
117 | ok++; | ||
118 | return GNUNET_OK; | ||
119 | } | ||
120 | |||
121 | |||
122 | static int | ||
123 | testDirScan () | ||
124 | { | ||
125 | if (GNUNET_OK != | ||
126 | GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry")) | ||
127 | { | ||
128 | GNUNET_break (0); | ||
129 | return 1; | ||
130 | } | ||
131 | if (GNUNET_OK != | ||
132 | GNUNET_DISK_directory_create ("test" DIR_SEPARATOR_STR "entry_more")) | ||
133 | { | ||
134 | GNUNET_break (0); | ||
135 | return 1; | ||
136 | } | ||
137 | GNUNET_DISK_directory_scan ("test", &scan_callback, | ||
138 | "test" DIR_SEPARATOR_STR "entry"); | ||
139 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
140 | { | ||
141 | GNUNET_break (0); | ||
142 | return 1; | ||
143 | } | ||
144 | if (ok < 2) | ||
145 | { | ||
146 | GNUNET_break (0); | ||
147 | return 1; | ||
148 | } | ||
149 | return 0; | ||
150 | } | ||
151 | |||
152 | |||
153 | static int | ||
154 | iter_callback (void *cls, | ||
155 | const char *filename) | ||
156 | { | ||
157 | int *i = cls; | ||
158 | |||
159 | (*i)++; | ||
160 | return GNUNET_OK; | ||
161 | } | ||
162 | |||
163 | |||
164 | static int | ||
165 | testDirIter () | ||
166 | { | ||
167 | int i; | ||
168 | |||
169 | i = 0; | ||
170 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry")) | ||
171 | { | ||
172 | GNUNET_break (0); | ||
173 | return 1; | ||
174 | } | ||
175 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_many")) | ||
176 | { | ||
177 | GNUNET_break (0); | ||
178 | return 1; | ||
179 | } | ||
180 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test/entry_more")) | ||
181 | { | ||
182 | GNUNET_break (0); | ||
183 | return 1; | ||
184 | } | ||
185 | GNUNET_DISK_directory_scan ("test", | ||
186 | &iter_callback, | ||
187 | &i); | ||
188 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
189 | { | ||
190 | GNUNET_break (0); | ||
191 | return 1; | ||
192 | } | ||
193 | if (i < 3) | ||
194 | { | ||
195 | GNUNET_break (0); | ||
196 | return 1; | ||
197 | } | ||
198 | return 0; | ||
199 | } | ||
200 | |||
201 | |||
202 | static int | ||
203 | testCanonicalize () | ||
204 | { | ||
205 | char *fn = GNUNET_strdup ("ab?><|cd*ef:/g\""); | ||
206 | |||
207 | GNUNET_DISK_filename_canonicalize (fn); | ||
208 | if (0 != strcmp (fn, "ab____cd_ef__g_")) | ||
209 | { | ||
210 | GNUNET_free (fn); | ||
211 | return 1; | ||
212 | } | ||
213 | GNUNET_free (fn); | ||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | |||
218 | static int | ||
219 | testChangeOwner () | ||
220 | { | ||
221 | GNUNET_log_skip (1, GNUNET_NO); | ||
222 | if (GNUNET_OK == GNUNET_DISK_file_change_owner ("/dev/null", "unknownuser")) | ||
223 | return 1; | ||
224 | return 0; | ||
225 | } | ||
226 | |||
227 | |||
228 | static int | ||
229 | testDirMani () | ||
230 | { | ||
231 | if (GNUNET_OK != GNUNET_DISK_directory_create_for_file ("test/ing")) | ||
232 | { | ||
233 | GNUNET_break (0); | ||
234 | return 1; | ||
235 | } | ||
236 | if (GNUNET_NO != GNUNET_DISK_file_test ("test")) | ||
237 | { | ||
238 | GNUNET_break (0); | ||
239 | return 1; | ||
240 | } | ||
241 | if (GNUNET_NO != GNUNET_DISK_file_test ("test/ing")) | ||
242 | { | ||
243 | GNUNET_break (0); | ||
244 | return 1; | ||
245 | } | ||
246 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
247 | { | ||
248 | GNUNET_break (0); | ||
249 | return 1; | ||
250 | } | ||
251 | if (GNUNET_OK != GNUNET_DISK_directory_create ("test")) | ||
252 | { | ||
253 | GNUNET_break (0); | ||
254 | return 1; | ||
255 | } | ||
256 | if (GNUNET_YES != GNUNET_DISK_directory_test ("test", GNUNET_YES)) | ||
257 | { | ||
258 | GNUNET_break (0); | ||
259 | return 1; | ||
260 | } | ||
261 | if (GNUNET_OK != GNUNET_DISK_directory_remove ("test")) | ||
262 | { | ||
263 | GNUNET_break (0); | ||
264 | return 1; | ||
265 | } | ||
266 | return 0; | ||
267 | } | ||
268 | |||
269 | |||
270 | int | ||
271 | main (int argc, char *argv[]) | ||
272 | { | ||
273 | unsigned int failureCount = 0; | ||
274 | |||
275 | GNUNET_log_setup ("test-disk", "WARNING", NULL); | ||
276 | failureCount += testReadWrite (); | ||
277 | failureCount += testOpenClose (); | ||
278 | failureCount += testDirScan (); | ||
279 | failureCount += testDirIter (); | ||
280 | failureCount += testCanonicalize (); | ||
281 | failureCount += testChangeOwner (); | ||
282 | failureCount += testDirMani (); | ||
283 | if (0 != failureCount) | ||
284 | { | ||
285 | fprintf (stderr, | ||
286 | "\n%u TESTS FAILED!\n", | ||
287 | failureCount); | ||
288 | return -1; | ||
289 | } | ||
290 | return 0; | ||
291 | } /* end of main */ | ||