aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_bio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_bio.c')
-rw-r--r--src/util/test_bio.c476
1 files changed, 0 insertions, 476 deletions
diff --git a/src/util/test_bio.c b/src/util/test_bio.c
deleted file mode 100644
index f18014719..000000000
--- a/src/util/test_bio.c
+++ /dev/null
@@ -1,476 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 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_bio.c
23 * @brief testcase for the buffered IO module
24 * @author Ji Lu
25 */
26
27
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#define TESTSTRING "testString"
31#define TESTNUMBER64 ((int64_t) 100000L)
32
33
34static int
35test_normal_rw (void)
36{
37 struct GNUNET_BIO_WriteHandle *wh;
38 struct GNUNET_BIO_ReadHandle *rh;
39 void *buffer;
40 size_t buffer_size = 0;
41 char *filename = GNUNET_DISK_mktemp ("gnunet-bio");
42 struct GNUNET_CONTAINER_MetaData *mdW;
43 struct GNUNET_CONTAINER_MetaData *mdR = NULL;
44 char *rString = NULL;
45 int64_t wNum = TESTNUMBER64;
46 int64_t rNum = 0;
47
48 mdW = GNUNET_CONTAINER_meta_data_create ();
49 GNUNET_CONTAINER_meta_data_add_publication_date (mdW);
50
51 struct GNUNET_BIO_WriteSpec ws[] = {
52 GNUNET_BIO_write_spec_string ("test-normal-rw-string", TESTSTRING),
53 GNUNET_BIO_write_spec_meta_data ("test-normal-rw-metadata", mdW),
54 GNUNET_BIO_write_spec_int64 ("test-normal-rw-int64", &wNum),
55 GNUNET_BIO_write_spec_end (),
56 };
57
58 struct GNUNET_BIO_ReadSpec rs[] = {
59 GNUNET_BIO_read_spec_string ("test-normal-rw-string", &rString, 200),
60 GNUNET_BIO_read_spec_meta_data ("test-normal-rw-metadata", &mdR),
61 GNUNET_BIO_read_spec_int64 ("test-normal-rw-int64", &rNum),
62 GNUNET_BIO_read_spec_end (),
63 };
64
65 /* I/O on file */
66 wh = GNUNET_BIO_write_open_file (filename);
67 GNUNET_assert (NULL != wh);
68 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_spec_commit (wh, ws));
69 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
70
71 rh = GNUNET_BIO_read_open_file (filename);
72 GNUNET_assert (NULL != rh);
73 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_spec_commit (rh, rs));
74 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
75 GNUNET_assert (0 == strcmp (TESTSTRING, rString));
76 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_meta_data_test_equal (mdR,
77 mdW));
78 GNUNET_assert (wNum == rNum);
79
80 GNUNET_CONTAINER_meta_data_destroy (mdR);
81 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
82 GNUNET_free (filename);
83
84 /* I/O on buffer */
85 wh = GNUNET_BIO_write_open_buffer ();
86 GNUNET_assert (NULL != wh);
87 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_spec_commit (wh, 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 == GNUNET_BIO_write_close (wh, NULL));
94
95 rh = GNUNET_BIO_read_open_buffer (buffer, buffer_size);
96 GNUNET_assert (NULL != rh);
97 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_spec_commit (rh, rs));
98 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
99 GNUNET_assert (0 == strcmp (TESTSTRING, rString));
100 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_meta_data_test_equal (mdR,
101 mdW));
102 GNUNET_assert (wNum == rNum);
103
104 GNUNET_free (buffer);
105
106 GNUNET_CONTAINER_meta_data_destroy (mdW);
107 GNUNET_CONTAINER_meta_data_destroy (mdR);
108 return 0;
109}
110
111
112static int
113test_nullstring_rw (void)
114{
115 struct GNUNET_BIO_WriteHandle *wh;
116 struct GNUNET_BIO_ReadHandle *rh;
117 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
118 char *rString = "not null";
119
120 wh = GNUNET_BIO_write_open_file (filename);
121 GNUNET_assert (NULL != wh);
122 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (wh,
123 "test-nullstring-rw",
124 NULL));
125 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
126
127 rh = GNUNET_BIO_read_open_file (filename);
128 GNUNET_assert (NULL != rh);
129 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_string (rh,
130 "test-nullstring-rw",
131 &rString, 200));
132 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
133
134 GNUNET_assert (NULL == rString);
135
136 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
137 GNUNET_free (filename);
138 return 0;
139}
140
141
142static int
143test_emptystring_rw (void)
144{
145 struct GNUNET_BIO_WriteHandle *wh;
146 struct GNUNET_BIO_ReadHandle *rh;
147 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
148 char *rString = NULL;
149
150 wh = GNUNET_BIO_write_open_file (filename);
151 GNUNET_assert (NULL != wh);
152 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (wh,
153 "test-emptystring-rw",
154 ""));
155 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
156
157 rh = GNUNET_BIO_read_open_file (filename);
158 GNUNET_assert (NULL != rh);
159 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_string (rh,
160 "test-emptystring-rw",
161 &rString, 200));
162 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
163
164 GNUNET_free (rString);
165
166 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
167 GNUNET_free (filename);
168 return 0;
169}
170
171
172static int
173test_bigstring_rw (void)
174{
175 struct GNUNET_BIO_WriteHandle *wh;
176 struct GNUNET_BIO_ReadHandle *rh;
177 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
178 char *rString = NULL;
179
180 wh = GNUNET_BIO_write_open_file (filename);
181 GNUNET_assert (NULL != wh);
182 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (wh,
183 "test-bigstring-rw",
184 TESTSTRING));
185 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
186
187 rh = GNUNET_BIO_read_open_file (filename);
188 GNUNET_assert (NULL != rh);
189 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_string (rh,
190 "test-bigstring-rw",
191 &rString, 1));
192 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
193
194 GNUNET_assert (NULL == rString);
195
196 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
197 GNUNET_free (filename);
198 return 0;
199}
200
201
202static int
203test_bigmeta_rw (void)
204{
205 static char meta[1024 * 1024 * 10];
206 struct GNUNET_BIO_WriteHandle *wh;
207 struct GNUNET_BIO_ReadHandle *rh;
208 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
209 struct GNUNET_CONTAINER_MetaData *mdR = NULL;
210
211 memset (meta, 'b', sizeof (meta));
212 meta[sizeof (meta) - 1] = '\0';
213
214 wh = GNUNET_BIO_write_open_file (filename);
215 GNUNET_assert (NULL != wh);
216 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
217 "test-bigmeta-rw-int32",
218 sizeof (meta)));
219 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write (wh,
220 "test-bigmeta-rw-bytes",
221 meta,
222 sizeof (meta)));
223 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
224
225 rh = GNUNET_BIO_read_open_file (filename);
226 GNUNET_assert (NULL != rh);
227 GNUNET_assert (GNUNET_SYSERR ==
228 GNUNET_BIO_read_meta_data (rh,
229 "test-bigmeta-rw-metadata",
230 &mdR));
231 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
232
233 GNUNET_assert (NULL == mdR);
234
235 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
236 GNUNET_free (filename);
237 return 0;
238}
239
240
241static int
242test_directory_r (void)
243{
244#ifdef LINUX
245 struct GNUNET_BIO_ReadHandle *rh;
246 char rString[200];
247
248 rh = GNUNET_BIO_read_open_file ("/dev");
249 GNUNET_assert (NULL != rh);
250 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read (rh,
251 "test-directory-r",
252 rString,
253 sizeof (rString)));
254 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
255#endif
256 return 0;
257}
258
259
260static int
261test_nullfile_rw (void)
262{
263 static char filename[102401];
264 struct GNUNET_BIO_WriteHandle *wh;
265 struct GNUNET_BIO_ReadHandle *rh;
266
267 memset (filename, 'a', sizeof (filename));
268 filename[sizeof (filename) - 1] = '\0';
269
270 GNUNET_log_skip (2, GNUNET_NO);
271 wh = GNUNET_BIO_write_open_file (filename);
272 GNUNET_log_skip (0, GNUNET_YES);
273 GNUNET_assert (NULL == wh);
274
275 GNUNET_log_skip (2, GNUNET_NO);
276 rh = GNUNET_BIO_read_open_file (filename);
277 GNUNET_log_skip (0, GNUNET_YES);
278 GNUNET_assert (NULL == rh);
279
280 return 0;
281}
282
283
284static int
285test_fullfile_rw (void)
286{
287#ifdef LINUX
288 /* /dev/full doesn't exist on every platform */
289 struct GNUNET_BIO_WriteHandle *wh;
290 struct GNUNET_BIO_ReadHandle *rh;
291 char *rString = NULL;
292 char rResult[200];
293 struct GNUNET_CONTAINER_MetaData *mdW;
294 struct GNUNET_CONTAINER_MetaData *mdR = NULL;
295
296 mdW = GNUNET_CONTAINER_meta_data_create ();
297 GNUNET_CONTAINER_meta_data_add_publication_date (mdW);
298
299 struct GNUNET_BIO_WriteSpec ws[] = {
300 GNUNET_BIO_write_spec_object ("test-fullfile-rw-bytes",
301 TESTSTRING,
302 strlen (TESTSTRING)),
303 GNUNET_BIO_write_spec_string ("test-fullfile-rw-string",
304 TESTSTRING),
305 GNUNET_BIO_write_spec_meta_data ("test-fullfile-rw-metadata",
306 mdW),
307 GNUNET_BIO_write_spec_end (),
308 };
309
310 struct GNUNET_BIO_ReadSpec rs[] = {
311 GNUNET_BIO_read_spec_object ("test-fullfile-rw-bytes",
312 rResult,
313 sizeof (rResult)),
314 GNUNET_BIO_read_spec_string ("test-fullfile-rw-string",
315 &rString,
316 200),
317 GNUNET_BIO_read_spec_meta_data ("test-fullfile-rw-metadata",
318 &mdR),
319 GNUNET_BIO_read_spec_end (),
320 };
321
322 wh = GNUNET_BIO_write_open_file ("/dev/full");
323 GNUNET_assert (NULL != wh);
324 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_spec_commit (wh, ws));
325 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_close (wh, NULL));
326
327 rh = GNUNET_BIO_read_open_file ("/dev/null");
328 GNUNET_assert (NULL != rh);
329 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_spec_commit (rh, rs));
330 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
331
332 GNUNET_assert (NULL == rString);
333 GNUNET_assert (NULL == mdR);
334#endif
335 return 0;
336}
337
338
339static int
340test_fakestring_rw (void)
341{
342 struct GNUNET_BIO_WriteHandle *wh;
343 struct GNUNET_BIO_ReadHandle *rh;
344 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
345 char *rString = NULL;
346
347 wh = GNUNET_BIO_write_open_file (filename);
348 GNUNET_assert (NULL != wh);
349 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
350 "test-fakestring-rw-int32",
351 2));
352 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
353
354 rh = GNUNET_BIO_read_open_file (filename);
355 GNUNET_assert (NULL != rh);
356 GNUNET_assert (GNUNET_SYSERR ==
357 GNUNET_BIO_read_string (rh,
358 "test-fakestring-rw-string",
359 &rString, 200));
360 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
361
362 GNUNET_assert (NULL == rString);
363
364 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
365 GNUNET_free (filename);
366 return 0;
367}
368
369
370static int
371test_fakemeta_rw (void)
372{
373 struct GNUNET_BIO_WriteHandle *wh;
374 struct GNUNET_BIO_ReadHandle *rh;
375 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
376 struct GNUNET_CONTAINER_MetaData *mdR = NULL;
377
378 wh = GNUNET_BIO_write_open_file (filename);
379 GNUNET_assert (NULL != wh);
380 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
381 "test-fakestring-rw-int32",
382 2));
383 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
384
385 rh = GNUNET_BIO_read_open_file (filename);
386 GNUNET_assert (NULL != rh);
387 GNUNET_assert (GNUNET_SYSERR ==
388 GNUNET_BIO_read_meta_data (rh,
389 "test-fakestring-rw-metadata",
390 &mdR));
391 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
392
393 GNUNET_assert (NULL == mdR);
394
395 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
396 GNUNET_free (filename);
397 return 0;
398}
399
400
401static int
402test_fakebigmeta_rw (void)
403{
404 struct GNUNET_BIO_WriteHandle *wh;
405 struct GNUNET_BIO_ReadHandle *rh;
406 char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
407 struct GNUNET_CONTAINER_MetaData *mdR = NULL;
408 int32_t wNum = 1024 * 1024 * 10;
409
410 wh = GNUNET_BIO_write_open_file (filename);
411 GNUNET_assert (NULL != wh);
412 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
413 "test-fakebigmeta-rw-int32",
414 wNum));
415 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
416
417 rh = GNUNET_BIO_read_open_file (filename);
418 GNUNET_assert (NULL != rh);
419 GNUNET_assert (GNUNET_SYSERR ==
420 GNUNET_BIO_read_meta_data (rh,
421 "test-fakebigmeta-rw-metadata",
422 &mdR));
423 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
424
425 GNUNET_assert (NULL == mdR);
426
427 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
428 GNUNET_free (filename);
429 return 0;
430}
431
432
433static int
434check_string_rw (void)
435{
436 GNUNET_assert (0 == test_nullstring_rw ());
437 GNUNET_assert (0 == test_emptystring_rw ());
438 GNUNET_assert (0 == test_bigstring_rw ());
439 GNUNET_assert (0 == test_fakestring_rw ());
440 return 0;
441}
442
443
444static int
445check_metadata_rw (void)
446{
447 GNUNET_assert (0 == test_fakebigmeta_rw ());
448 GNUNET_assert (0 == test_fakemeta_rw ());
449 GNUNET_assert (0 == test_bigmeta_rw ());
450 return 0;
451}
452
453
454static int
455check_file_rw (void)
456{
457 GNUNET_assert (0 == test_normal_rw ());
458 GNUNET_assert (0 == test_nullfile_rw ());
459 GNUNET_assert (0 == test_fullfile_rw ());
460 GNUNET_assert (0 == test_directory_r ());
461 return 0;
462}
463
464
465int
466main (int argc, char *argv[])
467{
468 GNUNET_log_setup ("test-bio", "WARNING", NULL);
469 GNUNET_assert (0 == check_file_rw ());
470 GNUNET_assert (0 == check_metadata_rw ());
471 GNUNET_assert (0 == check_string_rw ());
472 return 0;
473}
474
475
476/* end of test_bio.c */