diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
commit | 9ef4abad615bea12d13be542b8ae5fbeb2dfee32 (patch) | |
tree | 8875a687e004d331c9ea6a1d511a328c72b88113 /src/util/test_bio.c | |
parent | e95236b3ed78cd597c15f34b89385295702b627f (diff) | |
download | gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.tar.gz gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.zip |
NEWS: Refactoring components under src/ into lib/, plugin/, cli/ and service/
This also includes a necessary API refactoring of crypto from IDENTITY
to UTIL.
Diffstat (limited to 'src/util/test_bio.c')
-rw-r--r-- | src/util/test_bio.c | 383 |
1 files changed, 0 insertions, 383 deletions
diff --git a/src/util/test_bio.c b/src/util/test_bio.c deleted file mode 100644 index b407eccfe..000000000 --- a/src/util/test_bio.c +++ /dev/null | |||
@@ -1,383 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009, 2023 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 | * @file util/test_bio.c | ||
22 | * @brief testcase for the buffered IO module | ||
23 | * @author Ji Lu | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #define TESTSTRING "testString" | ||
28 | #define TESTNUMBER64 ((int64_t) 100000L) | ||
29 | |||
30 | |||
31 | static int | ||
32 | test_normal_rw (void) | ||
33 | { | ||
34 | struct GNUNET_BIO_WriteHandle *wh; | ||
35 | struct GNUNET_BIO_ReadHandle *rh; | ||
36 | void *buffer; | ||
37 | size_t buffer_size = 0; | ||
38 | char *filename = GNUNET_DISK_mktemp ("gnunet-bio"); | ||
39 | char *rString = NULL; | ||
40 | int64_t wNum = TESTNUMBER64; | ||
41 | int64_t rNum = 0; | ||
42 | struct GNUNET_BIO_WriteSpec ws[] = { | ||
43 | GNUNET_BIO_write_spec_string ("test-normal-rw-string", | ||
44 | TESTSTRING), | ||
45 | GNUNET_BIO_write_spec_int64 ("test-normal-rw-int64", | ||
46 | &wNum), | ||
47 | GNUNET_BIO_write_spec_end (), | ||
48 | }; | ||
49 | struct GNUNET_BIO_ReadSpec rs[] = { | ||
50 | GNUNET_BIO_read_spec_string ("test-normal-rw-string", | ||
51 | &rString, | ||
52 | 200), | ||
53 | GNUNET_BIO_read_spec_int64 ("test-normal-rw-int64", | ||
54 | &rNum), | ||
55 | GNUNET_BIO_read_spec_end (), | ||
56 | }; | ||
57 | |||
58 | /* I/O on file */ | ||
59 | wh = GNUNET_BIO_write_open_file (filename); | ||
60 | GNUNET_assert (NULL != wh); | ||
61 | GNUNET_assert (GNUNET_OK == | ||
62 | GNUNET_BIO_write_spec_commit (wh, ws)); | ||
63 | GNUNET_assert (GNUNET_OK == | ||
64 | GNUNET_BIO_write_close (wh, NULL)); | ||
65 | |||
66 | rh = GNUNET_BIO_read_open_file (filename); | ||
67 | GNUNET_assert (NULL != rh); | ||
68 | GNUNET_assert (GNUNET_OK == | ||
69 | GNUNET_BIO_read_spec_commit (rh, | ||
70 | rs)); | ||
71 | GNUNET_assert (GNUNET_OK == | ||
72 | GNUNET_BIO_read_close (rh, | ||
73 | NULL)); | ||
74 | GNUNET_assert (0 == strcmp (TESTSTRING, | ||
75 | rString)); | ||
76 | GNUNET_assert (wNum == rNum); | ||
77 | GNUNET_free (rString); | ||
78 | GNUNET_assert (GNUNET_OK == | ||
79 | GNUNET_DISK_directory_remove (filename)); | ||
80 | GNUNET_free (filename); | ||
81 | |||
82 | /* I/O on buffer */ | ||
83 | wh = GNUNET_BIO_write_open_buffer (); | ||
84 | GNUNET_assert (NULL != wh); | ||
85 | GNUNET_assert (GNUNET_OK == | ||
86 | GNUNET_BIO_write_spec_commit (wh, | ||
87 | ws)); | ||
88 | GNUNET_assert (GNUNET_OK == | ||
89 | GNUNET_BIO_get_buffer_contents (wh, | ||
90 | NULL, | ||
91 | &buffer, | ||
92 | &buffer_size)); | ||
93 | GNUNET_assert (GNUNET_OK == | ||
94 | GNUNET_BIO_write_close (wh, | ||
95 | NULL)); | ||
96 | |||
97 | rh = GNUNET_BIO_read_open_buffer (buffer, | ||
98 | buffer_size); | ||
99 | GNUNET_assert (NULL != rh); | ||
100 | GNUNET_assert (GNUNET_OK == | ||
101 | GNUNET_BIO_read_spec_commit (rh, | ||
102 | rs)); | ||
103 | GNUNET_assert (GNUNET_OK == | ||
104 | GNUNET_BIO_read_close (rh, | ||
105 | NULL)); | ||
106 | GNUNET_assert (0 == strcmp (TESTSTRING, | ||
107 | rString)); | ||
108 | GNUNET_assert (wNum == rNum); | ||
109 | GNUNET_free (rString); | ||
110 | GNUNET_free (buffer); | ||
111 | |||
112 | return 0; | ||
113 | } | ||
114 | |||
115 | |||
116 | static int | ||
117 | test_nullstring_rw (void) | ||
118 | { | ||
119 | struct GNUNET_BIO_WriteHandle *wh; | ||
120 | struct GNUNET_BIO_ReadHandle *rh; | ||
121 | char *filename = GNUNET_DISK_mktemp ("gnunet_bio"); | ||
122 | char *rString = "not null"; | ||
123 | |||
124 | wh = GNUNET_BIO_write_open_file (filename); | ||
125 | GNUNET_assert (NULL != wh); | ||
126 | GNUNET_assert (GNUNET_OK == | ||
127 | GNUNET_BIO_write_string (wh, | ||
128 | "test-nullstring-rw", | ||
129 | NULL)); | ||
130 | GNUNET_assert (GNUNET_OK == | ||
131 | GNUNET_BIO_write_close (wh, | ||
132 | NULL)); | ||
133 | rh = GNUNET_BIO_read_open_file (filename); | ||
134 | GNUNET_assert (NULL != rh); | ||
135 | GNUNET_assert (GNUNET_OK == | ||
136 | GNUNET_BIO_read_string (rh, | ||
137 | "test-nullstring-rw", | ||
138 | &rString, | ||
139 | 200)); | ||
140 | GNUNET_assert (GNUNET_OK == | ||
141 | GNUNET_BIO_read_close (rh, | ||
142 | NULL)); | ||
143 | GNUNET_assert (NULL == rString); | ||
144 | GNUNET_assert (GNUNET_OK == | ||
145 | GNUNET_DISK_directory_remove (filename)); | ||
146 | GNUNET_free (filename); | ||
147 | return 0; | ||
148 | } | ||
149 | |||
150 | |||
151 | static int | ||
152 | test_emptystring_rw (void) | ||
153 | { | ||
154 | struct GNUNET_BIO_WriteHandle *wh; | ||
155 | struct GNUNET_BIO_ReadHandle *rh; | ||
156 | char *filename = GNUNET_DISK_mktemp ("gnunet_bio"); | ||
157 | char *rString = NULL; | ||
158 | |||
159 | wh = GNUNET_BIO_write_open_file (filename); | ||
160 | GNUNET_assert (NULL != wh); | ||
161 | GNUNET_assert (GNUNET_OK == | ||
162 | GNUNET_BIO_write_string (wh, | ||
163 | "test-emptystring-rw", | ||
164 | "")); | ||
165 | GNUNET_assert (GNUNET_OK == | ||
166 | GNUNET_BIO_write_close (wh, | ||
167 | NULL)); | ||
168 | |||
169 | rh = GNUNET_BIO_read_open_file (filename); | ||
170 | GNUNET_assert (NULL != rh); | ||
171 | GNUNET_assert (GNUNET_OK == | ||
172 | GNUNET_BIO_read_string (rh, | ||
173 | "test-emptystring-rw", | ||
174 | &rString, 200)); | ||
175 | GNUNET_assert (GNUNET_OK == | ||
176 | GNUNET_BIO_read_close (rh, | ||
177 | NULL)); | ||
178 | GNUNET_free (rString); | ||
179 | GNUNET_assert (GNUNET_OK == | ||
180 | GNUNET_DISK_directory_remove (filename)); | ||
181 | GNUNET_free (filename); | ||
182 | return 0; | ||
183 | } | ||
184 | |||
185 | |||
186 | static int | ||
187 | test_bigstring_rw (void) | ||
188 | { | ||
189 | struct GNUNET_BIO_WriteHandle *wh; | ||
190 | struct GNUNET_BIO_ReadHandle *rh; | ||
191 | char *filename = GNUNET_DISK_mktemp ("gnunet_bio"); | ||
192 | char *rString = NULL; | ||
193 | |||
194 | wh = GNUNET_BIO_write_open_file (filename); | ||
195 | GNUNET_assert (NULL != wh); | ||
196 | GNUNET_assert (GNUNET_OK == | ||
197 | GNUNET_BIO_write_string (wh, | ||
198 | "test-bigstring-rw", | ||
199 | TESTSTRING)); | ||
200 | GNUNET_assert (GNUNET_OK == | ||
201 | GNUNET_BIO_write_close (wh, | ||
202 | NULL)); | ||
203 | rh = GNUNET_BIO_read_open_file (filename); | ||
204 | GNUNET_assert (NULL != rh); | ||
205 | GNUNET_assert (GNUNET_SYSERR == | ||
206 | GNUNET_BIO_read_string (rh, | ||
207 | "test-bigstring-rw", | ||
208 | &rString, 1)); | ||
209 | GNUNET_assert (GNUNET_SYSERR == | ||
210 | GNUNET_BIO_read_close (rh, | ||
211 | NULL)); | ||
212 | GNUNET_assert (NULL == rString); | ||
213 | GNUNET_assert (GNUNET_OK == | ||
214 | GNUNET_DISK_directory_remove (filename)); | ||
215 | GNUNET_free (filename); | ||
216 | return 0; | ||
217 | } | ||
218 | |||
219 | |||
220 | static int | ||
221 | test_directory_r (void) | ||
222 | { | ||
223 | #ifdef LINUX | ||
224 | struct GNUNET_BIO_ReadHandle *rh; | ||
225 | char rString[200]; | ||
226 | |||
227 | rh = GNUNET_BIO_read_open_file ("/dev"); | ||
228 | GNUNET_assert (NULL != rh); | ||
229 | GNUNET_assert (GNUNET_SYSERR == | ||
230 | GNUNET_BIO_read (rh, | ||
231 | "test-directory-r", | ||
232 | rString, | ||
233 | sizeof (rString))); | ||
234 | GNUNET_assert (GNUNET_SYSERR == | ||
235 | GNUNET_BIO_read_close (rh, | ||
236 | NULL)); | ||
237 | #endif | ||
238 | return 0; | ||
239 | } | ||
240 | |||
241 | |||
242 | static int | ||
243 | test_nullfile_rw (void) | ||
244 | { | ||
245 | static char filename[102401]; | ||
246 | struct GNUNET_BIO_WriteHandle *wh; | ||
247 | struct GNUNET_BIO_ReadHandle *rh; | ||
248 | |||
249 | memset (filename, 'a', sizeof (filename)); | ||
250 | filename[sizeof (filename) - 1] = '\0'; | ||
251 | |||
252 | GNUNET_log_skip (1, GNUNET_NO); | ||
253 | wh = GNUNET_BIO_write_open_file (filename); | ||
254 | GNUNET_log_skip (0, GNUNET_YES); | ||
255 | GNUNET_assert (NULL == wh); | ||
256 | |||
257 | GNUNET_log_skip (1, GNUNET_NO); | ||
258 | rh = GNUNET_BIO_read_open_file (filename); | ||
259 | GNUNET_log_skip (0, GNUNET_YES); | ||
260 | GNUNET_assert (NULL == rh); | ||
261 | |||
262 | return 0; | ||
263 | } | ||
264 | |||
265 | |||
266 | static int | ||
267 | test_fullfile_rw (void) | ||
268 | { | ||
269 | #ifdef LINUX | ||
270 | /* /dev/full doesn't exist on every platform */ | ||
271 | struct GNUNET_BIO_WriteHandle *wh; | ||
272 | struct GNUNET_BIO_ReadHandle *rh; | ||
273 | char *rString = NULL; | ||
274 | char rResult[200]; | ||
275 | |||
276 | struct GNUNET_BIO_WriteSpec ws[] = { | ||
277 | GNUNET_BIO_write_spec_object ("test-fullfile-rw-bytes", | ||
278 | TESTSTRING, | ||
279 | strlen (TESTSTRING)), | ||
280 | GNUNET_BIO_write_spec_string ("test-fullfile-rw-string", | ||
281 | TESTSTRING), | ||
282 | GNUNET_BIO_write_spec_end (), | ||
283 | }; | ||
284 | |||
285 | struct GNUNET_BIO_ReadSpec rs[] = { | ||
286 | GNUNET_BIO_read_spec_object ("test-fullfile-rw-bytes", | ||
287 | rResult, | ||
288 | sizeof (rResult)), | ||
289 | GNUNET_BIO_read_spec_string ("test-fullfile-rw-string", | ||
290 | &rString, | ||
291 | 200), | ||
292 | GNUNET_BIO_read_spec_end (), | ||
293 | }; | ||
294 | |||
295 | wh = GNUNET_BIO_write_open_file ("/dev/full"); | ||
296 | GNUNET_assert (NULL != wh); | ||
297 | GNUNET_assert (GNUNET_SYSERR == | ||
298 | GNUNET_BIO_write_spec_commit (wh, ws)); | ||
299 | GNUNET_assert (GNUNET_SYSERR == | ||
300 | GNUNET_BIO_write_close (wh, NULL)); | ||
301 | |||
302 | rh = GNUNET_BIO_read_open_file ("/dev/null"); | ||
303 | GNUNET_assert (NULL != rh); | ||
304 | GNUNET_assert (GNUNET_SYSERR == | ||
305 | GNUNET_BIO_read_spec_commit (rh, rs)); | ||
306 | GNUNET_assert (GNUNET_SYSERR == | ||
307 | GNUNET_BIO_read_close (rh, NULL)); | ||
308 | |||
309 | GNUNET_assert (NULL == rString); | ||
310 | #endif | ||
311 | return 0; | ||
312 | } | ||
313 | |||
314 | |||
315 | static int | ||
316 | test_fakestring_rw (void) | ||
317 | { | ||
318 | struct GNUNET_BIO_WriteHandle *wh; | ||
319 | struct GNUNET_BIO_ReadHandle *rh; | ||
320 | char *filename = GNUNET_DISK_mktemp ("gnunet_bio"); | ||
321 | char *rString = NULL; | ||
322 | |||
323 | wh = GNUNET_BIO_write_open_file (filename); | ||
324 | GNUNET_assert (NULL != wh); | ||
325 | GNUNET_assert (GNUNET_OK == | ||
326 | GNUNET_BIO_write_int32 (wh, | ||
327 | "test-fakestring-rw-int32", | ||
328 | 2)); | ||
329 | GNUNET_assert (GNUNET_OK == | ||
330 | GNUNET_BIO_write_close (wh, | ||
331 | NULL)); | ||
332 | |||
333 | rh = GNUNET_BIO_read_open_file (filename); | ||
334 | GNUNET_assert (NULL != rh); | ||
335 | GNUNET_assert (GNUNET_SYSERR == | ||
336 | GNUNET_BIO_read_string (rh, | ||
337 | "test-fakestring-rw-string", | ||
338 | &rString, | ||
339 | 200)); | ||
340 | GNUNET_assert (GNUNET_SYSERR == | ||
341 | GNUNET_BIO_read_close (rh, | ||
342 | NULL)); | ||
343 | GNUNET_assert (NULL == rString); | ||
344 | GNUNET_assert (GNUNET_OK == | ||
345 | GNUNET_DISK_directory_remove (filename)); | ||
346 | GNUNET_free (filename); | ||
347 | return 0; | ||
348 | } | ||
349 | |||
350 | |||
351 | static int | ||
352 | check_string_rw (void) | ||
353 | { | ||
354 | GNUNET_assert (0 == test_nullstring_rw ()); | ||
355 | GNUNET_assert (0 == test_emptystring_rw ()); | ||
356 | GNUNET_assert (0 == test_bigstring_rw ()); | ||
357 | GNUNET_assert (0 == test_fakestring_rw ()); | ||
358 | return 0; | ||
359 | } | ||
360 | |||
361 | |||
362 | static int | ||
363 | check_file_rw (void) | ||
364 | { | ||
365 | GNUNET_assert (0 == test_normal_rw ()); | ||
366 | GNUNET_assert (0 == test_nullfile_rw ()); | ||
367 | GNUNET_assert (0 == test_fullfile_rw ()); | ||
368 | GNUNET_assert (0 == test_directory_r ()); | ||
369 | return 0; | ||
370 | } | ||
371 | |||
372 | |||
373 | int | ||
374 | main (int argc, char *argv[]) | ||
375 | { | ||
376 | GNUNET_log_setup ("test-bio", "WARNING", NULL); | ||
377 | GNUNET_assert (0 == check_file_rw ()); | ||
378 | GNUNET_assert (0 == check_string_rw ()); | ||
379 | return 0; | ||
380 | } | ||
381 | |||
382 | |||
383 | /* end of test_bio.c */ | ||