2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/test_bio.c
23 * @brief testcase for the buffered IO module
29 #include "gnunet_util_lib.h"
30 #define TESTSTRING "testString"
31 #define TESTNUMBER64 ((int64_t)100000L)
38 char *readResultString;
39 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
40 struct GNUNET_BIO_WriteHandle *fileW;
41 struct GNUNET_BIO_ReadHandle *fileR;
42 struct GNUNET_CONTAINER_MetaData *metaDataW;
43 struct GNUNET_CONTAINER_MetaData *metaDataR;
45 metaDataW = GNUNET_CONTAINER_meta_data_create ();
47 GNUNET_CONTAINER_meta_data_add_publication_date (metaDataW);
49 fileW = GNUNET_BIO_write_open (fileName);
50 GNUNET_assert (NULL != fileW);
51 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, TESTSTRING));
52 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_meta_data (fileW, metaDataW));
53 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int64 (fileW, TESTNUMBER64));
54 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
56 fileR = GNUNET_BIO_read_open (fileName);
57 GNUNET_assert (NULL != fileR);
58 readResultString = NULL;
59 GNUNET_assert (GNUNET_OK ==
60 GNUNET_BIO_read_string (fileR, "Read string error",
61 &readResultString, 200));
62 GNUNET_assert (NULL != readResultString);
63 GNUNET_assert (0 == strcmp (TESTSTRING, readResultString));
64 GNUNET_free (readResultString);
65 GNUNET_assert (GNUNET_OK ==
66 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
68 GNUNET_assert (GNUNET_YES ==
69 GNUNET_CONTAINER_meta_data_test_equal (metaDataR, metaDataW));
70 GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_int64 (fileR, &testNum));
71 GNUNET_BIO_read_close (fileR, &msg);
72 GNUNET_CONTAINER_meta_data_destroy (metaDataW);
73 GNUNET_CONTAINER_meta_data_destroy (metaDataR);
74 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
75 GNUNET_free (fileName);
83 char *readResultString = (char *) "not null";
84 struct GNUNET_BIO_WriteHandle *fileW;
85 struct GNUNET_BIO_ReadHandle *fileR;
86 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
88 fileW = GNUNET_BIO_write_open (fileName);
89 GNUNET_assert (NULL != fileW);
90 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, NULL));
91 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
93 fileR = GNUNET_BIO_read_open (fileName);
94 GNUNET_assert (NULL != fileR);
95 GNUNET_assert (GNUNET_OK ==
96 GNUNET_BIO_read_string (fileR, "Read string error",
97 &readResultString, 200));
98 GNUNET_assert (NULL == readResultString);
99 GNUNET_BIO_read_close (fileR, &msg);
100 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
101 GNUNET_free (fileName);
107 test_emptystring_rw ()
110 char *readResultString;
111 struct GNUNET_BIO_WriteHandle *fileW;
112 struct GNUNET_BIO_ReadHandle *fileR;
113 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
115 fileW = GNUNET_BIO_write_open (fileName);
116 GNUNET_assert (NULL != fileW);
117 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, ""));
118 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
120 fileR = GNUNET_BIO_read_open (fileName);
121 GNUNET_assert (NULL != fileR);
122 readResultString = NULL;
123 GNUNET_assert (GNUNET_OK ==
124 GNUNET_BIO_read_string (fileR, "Read string error",
125 &readResultString, 200));
126 GNUNET_free (readResultString);
127 GNUNET_BIO_read_close (fileR, &msg);
128 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
129 GNUNET_free (fileName);
137 char *readResultString;
138 struct GNUNET_BIO_WriteHandle *fileW;
139 struct GNUNET_BIO_ReadHandle *fileR;
140 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
142 fileW = GNUNET_BIO_write_open (fileName);
143 GNUNET_assert (NULL != fileW);
144 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (fileW, TESTSTRING));
145 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
147 fileR = GNUNET_BIO_read_open (fileName);
148 GNUNET_assert (NULL != fileR);
149 readResultString = NULL;
150 GNUNET_assert (GNUNET_SYSERR ==
151 GNUNET_BIO_read_string (fileR, "Read string error",
152 &readResultString, 1));
153 GNUNET_assert (NULL == readResultString);
155 GNUNET_BIO_read_close (fileR, &msg);
157 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
158 GNUNET_free (fileName);
166 static char meta[1024 * 1024 * 10];
167 struct GNUNET_BIO_WriteHandle *fileW;
168 struct GNUNET_BIO_ReadHandle *fileR;
169 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
170 struct GNUNET_CONTAINER_MetaData *metaDataR;
172 memset (meta, 'b', sizeof (meta));
173 meta[sizeof (meta) - 1] = '\0';
174 fileW = GNUNET_BIO_write_open (fileName);
175 GNUNET_assert (NULL != fileW);
176 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, sizeof (meta)));
177 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write (fileW, meta, sizeof (meta)));
178 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
180 fileR = GNUNET_BIO_read_open (fileName);
181 GNUNET_assert (NULL != fileR);
183 GNUNET_assert (GNUNET_SYSERR ==
184 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
187 GNUNET_BIO_read_close (fileR, &msg);
189 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
190 GNUNET_assert (NULL == metaDataR);
191 GNUNET_free (fileName);
200 char readResult[200];
201 struct GNUNET_BIO_ReadHandle *fileR;
203 fileR = GNUNET_BIO_read_open ("/dev");
204 GNUNET_assert (NULL != fileR);
205 GNUNET_assert (GNUNET_SYSERR ==
206 GNUNET_BIO_read (fileR, "Read error", readResult,
207 sizeof (readResult)));
209 GNUNET_BIO_read_close (fileR, &msg);
218 static char fileNameNO[102401];
219 struct GNUNET_BIO_WriteHandle *fileWNO;
220 struct GNUNET_BIO_ReadHandle *fileRNO;
222 memset (fileNameNO, 'a', sizeof (fileNameNO));
223 fileNameNO[sizeof (fileNameNO) - 1] = '\0';
225 GNUNET_log_skip (1, GNUNET_NO);
226 fileWNO = GNUNET_BIO_write_open (fileNameNO);
227 GNUNET_log_skip (0, GNUNET_YES);
228 GNUNET_assert (NULL == fileWNO);
230 GNUNET_log_skip (1, GNUNET_NO);
231 fileRNO = GNUNET_BIO_read_open (fileNameNO);
232 GNUNET_log_skip (0, GNUNET_YES);
233 GNUNET_assert (NULL == fileRNO);
242 /* /dev/full only seems to exist on Linux */
245 char *readResultString;
246 char readResult[200];
247 struct GNUNET_BIO_WriteHandle *fileW;
248 struct GNUNET_BIO_ReadHandle *fileR;
249 struct GNUNET_CONTAINER_MetaData *metaDataW;
250 struct GNUNET_CONTAINER_MetaData *metaDataR;
252 metaDataW = GNUNET_CONTAINER_meta_data_create ();
253 GNUNET_CONTAINER_meta_data_add_publication_date (metaDataW);
255 fileW = GNUNET_BIO_write_open ("/dev/full");
256 GNUNET_assert (NULL != fileW);
257 (void) GNUNET_BIO_write (fileW, TESTSTRING, strlen (TESTSTRING));
258 (void) GNUNET_BIO_write_string (fileW, TESTSTRING);
259 (void) GNUNET_BIO_write_meta_data (fileW, metaDataW);
260 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_close (fileW));
261 GNUNET_CONTAINER_meta_data_destroy (metaDataW);
263 fileW = GNUNET_BIO_write_open ("/dev/full");
264 GNUNET_assert (NULL != fileW);
265 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_close (fileW));
267 fileR = GNUNET_BIO_read_open ("/dev/null");
268 GNUNET_assert (NULL != fileR);
269 GNUNET_assert (GNUNET_SYSERR ==
270 GNUNET_BIO_read (fileR, "Read error", readResult,
271 sizeof (readResult)));
272 readResultString = NULL;
273 GNUNET_assert (GNUNET_SYSERR ==
274 GNUNET_BIO_read_string (fileR, "Read string error",
275 &readResultString, 200));
276 GNUNET_assert (NULL == readResultString);
277 GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_int64 (fileR, &testNum));
279 GNUNET_assert (GNUNET_SYSERR ==
280 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
283 GNUNET_BIO_read_close (fileR, &msg);
285 GNUNET_assert (NULL == metaDataR);
291 test_fakestring_rw ()
296 struct GNUNET_BIO_WriteHandle *fileW;
297 struct GNUNET_BIO_ReadHandle *fileR;
298 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
300 fileW = GNUNET_BIO_write_open (fileName);
301 GNUNET_assert (NULL != fileW);
302 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, tmpInt));
303 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
305 fileR = GNUNET_BIO_read_open (fileName);
306 GNUNET_assert (NULL != fileR);
307 GNUNET_assert (GNUNET_SYSERR ==
308 GNUNET_BIO_read_string (fileR, "Read string error",
311 GNUNET_BIO_read_close (fileR, &msg);
313 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
314 GNUNET_free (fileName);
323 struct GNUNET_BIO_WriteHandle *fileW;
324 struct GNUNET_BIO_ReadHandle *fileR;
325 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
326 struct GNUNET_CONTAINER_MetaData *metaDataR;
328 fileW = GNUNET_BIO_write_open (fileName);
329 GNUNET_assert (NULL != fileW);
330 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, tmpInt));
331 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
333 fileR = GNUNET_BIO_read_open (fileName);
334 GNUNET_assert (NULL != fileR);
336 GNUNET_assert (GNUNET_SYSERR ==
337 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
339 GNUNET_assert (NULL == metaDataR);
341 GNUNET_BIO_read_close (fileR, &msg);
343 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
344 GNUNET_free (fileName);
349 test_fakebigmeta_rw ()
352 int32_t tmpInt = 1024 * 1024 * 10;
353 struct GNUNET_BIO_WriteHandle *fileW;
354 struct GNUNET_BIO_ReadHandle *fileR;
355 char *fileName = GNUNET_DISK_mktemp ("gnunet_bio");
356 struct GNUNET_CONTAINER_MetaData *metaDataR;
358 fileW = GNUNET_BIO_write_open (fileName);
359 GNUNET_assert (NULL != fileW);
360 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (fileW, tmpInt));
361 GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (fileW));
363 fileR = GNUNET_BIO_read_open (fileName);
364 GNUNET_assert (NULL != fileR);
366 GNUNET_assert (GNUNET_SYSERR ==
367 GNUNET_BIO_read_meta_data (fileR, "Read meta error",
370 GNUNET_BIO_read_close (fileR, &msg);
372 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (fileName));
373 GNUNET_assert (NULL == metaDataR);
374 GNUNET_free (fileName);
381 GNUNET_assert (0 == test_nullstring_rw ());
382 GNUNET_assert (0 == test_emptystring_rw ());
383 GNUNET_assert (0 == test_bigstring_rw ());
384 GNUNET_assert (0 == test_fakestring_rw ());
391 GNUNET_assert (0 == test_fakebigmeta_rw ());
392 GNUNET_assert (0 == test_fakemeta_rw ());
393 GNUNET_assert (0 == test_bigmeta_rw ());
400 GNUNET_assert (0 == test_normal_rw ());
401 GNUNET_assert (0 == test_nullfile_rw ());
402 GNUNET_assert (0 == test_fullfile_rw ());
403 GNUNET_assert (0 == test_directory_r ());
408 main (int argc, char *argv[])
410 GNUNET_log_setup ("test-bio", "WARNING", NULL);
411 GNUNET_assert (0 == check_file_rw ());
412 GNUNET_assert (0 == check_metadata_rw ());
413 GNUNET_assert (0 == check_string_rw ());
417 /* end of test_bio.c */