test_bio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_bio.c
  18. * @brief testcase for the buffered IO module
  19. * @author Ji Lu
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #define TESTSTRING "testString"
  24. #define TESTNUMBER64 ((int64_t) 100000L)
  25. static int
  26. test_normal_rw (void)
  27. {
  28. struct GNUNET_BIO_WriteHandle *wh;
  29. struct GNUNET_BIO_ReadHandle *rh;
  30. void *buffer;
  31. size_t buffer_size = 0;
  32. char *filename = GNUNET_DISK_mktemp ("gnunet-bio");
  33. struct GNUNET_CONTAINER_MetaData *mdW;
  34. struct GNUNET_CONTAINER_MetaData *mdR = NULL;
  35. char *rString = NULL;
  36. int64_t wNum = TESTNUMBER64;
  37. int64_t rNum = 0;
  38. mdW = GNUNET_CONTAINER_meta_data_create ();
  39. GNUNET_CONTAINER_meta_data_add_publication_date (mdW);
  40. struct GNUNET_BIO_WriteSpec ws[] = {
  41. GNUNET_BIO_write_spec_string ("test-normal-rw-string", TESTSTRING),
  42. GNUNET_BIO_write_spec_meta_data ("test-normal-rw-metadata", mdW),
  43. GNUNET_BIO_write_spec_int64 ("test-normal-rw-int64", &wNum),
  44. GNUNET_BIO_write_spec_end (),
  45. };
  46. struct GNUNET_BIO_ReadSpec rs[] = {
  47. GNUNET_BIO_read_spec_string ("test-normal-rw-string", &rString, 200),
  48. GNUNET_BIO_read_spec_meta_data ("test-normal-rw-metadata", &mdR),
  49. GNUNET_BIO_read_spec_int64 ("test-normal-rw-int64", &rNum),
  50. GNUNET_BIO_read_spec_end (),
  51. };
  52. /* I/O on file */
  53. wh = GNUNET_BIO_write_open_file (filename);
  54. GNUNET_assert (NULL != wh);
  55. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_spec_commit (wh, ws));
  56. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  57. rh = GNUNET_BIO_read_open_file (filename);
  58. GNUNET_assert (NULL != rh);
  59. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_spec_commit (rh, rs));
  60. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
  61. GNUNET_assert (0 == strcmp (TESTSTRING, rString));
  62. GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_meta_data_test_equal (mdR,
  63. mdW));
  64. GNUNET_assert (wNum == rNum);
  65. GNUNET_CONTAINER_meta_data_destroy (mdR);
  66. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  67. GNUNET_free (filename);
  68. /* I/O on buffer */
  69. wh = GNUNET_BIO_write_open_buffer ();
  70. GNUNET_assert (NULL != wh);
  71. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_spec_commit (wh, ws));
  72. GNUNET_assert (GNUNET_OK ==
  73. GNUNET_BIO_get_buffer_contents (wh,
  74. NULL,
  75. &buffer,
  76. &buffer_size));
  77. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  78. rh = GNUNET_BIO_read_open_buffer (buffer, buffer_size);
  79. GNUNET_assert (NULL != rh);
  80. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_spec_commit (rh, rs));
  81. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
  82. GNUNET_assert (0 == strcmp (TESTSTRING, rString));
  83. GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_meta_data_test_equal (mdR,
  84. mdW));
  85. GNUNET_assert (wNum == rNum);
  86. GNUNET_free (buffer);
  87. GNUNET_CONTAINER_meta_data_destroy (mdW);
  88. GNUNET_CONTAINER_meta_data_destroy (mdR);
  89. return 0;
  90. }
  91. static int
  92. test_nullstring_rw (void)
  93. {
  94. struct GNUNET_BIO_WriteHandle *wh;
  95. struct GNUNET_BIO_ReadHandle *rh;
  96. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  97. char *rString = "not null";
  98. wh = GNUNET_BIO_write_open_file (filename);
  99. GNUNET_assert (NULL != wh);
  100. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (wh,
  101. "test-nullstring-rw",
  102. NULL));
  103. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  104. rh = GNUNET_BIO_read_open_file (filename);
  105. GNUNET_assert (NULL != rh);
  106. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_string (rh,
  107. "test-nullstring-rw",
  108. &rString, 200));
  109. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
  110. GNUNET_assert (NULL == rString);
  111. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  112. GNUNET_free (filename);
  113. return 0;
  114. }
  115. static int
  116. test_emptystring_rw (void)
  117. {
  118. struct GNUNET_BIO_WriteHandle *wh;
  119. struct GNUNET_BIO_ReadHandle *rh;
  120. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  121. char *rString = NULL;
  122. wh = GNUNET_BIO_write_open_file (filename);
  123. GNUNET_assert (NULL != wh);
  124. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (wh,
  125. "test-emptystring-rw",
  126. ""));
  127. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  128. rh = GNUNET_BIO_read_open_file (filename);
  129. GNUNET_assert (NULL != rh);
  130. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_string (rh,
  131. "test-emptystring-rw",
  132. &rString, 200));
  133. GNUNET_assert (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
  134. GNUNET_free (rString);
  135. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  136. GNUNET_free (filename);
  137. return 0;
  138. }
  139. static int
  140. test_bigstring_rw (void)
  141. {
  142. struct GNUNET_BIO_WriteHandle *wh;
  143. struct GNUNET_BIO_ReadHandle *rh;
  144. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  145. char *rString = NULL;
  146. wh = GNUNET_BIO_write_open_file (filename);
  147. GNUNET_assert (NULL != wh);
  148. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_string (wh,
  149. "test-bigstring-rw",
  150. TESTSTRING));
  151. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  152. rh = GNUNET_BIO_read_open_file (filename);
  153. GNUNET_assert (NULL != rh);
  154. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_string (rh,
  155. "test-bigstring-rw",
  156. &rString, 1));
  157. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  158. GNUNET_assert (NULL == rString);
  159. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  160. GNUNET_free (filename);
  161. return 0;
  162. }
  163. static int
  164. test_bigmeta_rw (void)
  165. {
  166. static char meta[1024 * 1024 * 10];
  167. struct GNUNET_BIO_WriteHandle *wh;
  168. struct GNUNET_BIO_ReadHandle *rh;
  169. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  170. struct GNUNET_CONTAINER_MetaData *mdR = NULL;
  171. memset (meta, 'b', sizeof (meta));
  172. meta[sizeof (meta) - 1] = '\0';
  173. wh = GNUNET_BIO_write_open_file (filename);
  174. GNUNET_assert (NULL != wh);
  175. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
  176. "test-bigmeta-rw-int32",
  177. sizeof (meta)));
  178. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write (wh,
  179. "test-bigmeta-rw-bytes",
  180. meta,
  181. sizeof (meta)));
  182. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  183. rh = GNUNET_BIO_read_open_file (filename);
  184. GNUNET_assert (NULL != rh);
  185. GNUNET_assert (GNUNET_SYSERR ==
  186. GNUNET_BIO_read_meta_data (rh,
  187. "test-bigmeta-rw-metadata",
  188. &mdR));
  189. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  190. GNUNET_assert (NULL == mdR);
  191. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  192. GNUNET_free (filename);
  193. return 0;
  194. }
  195. static int
  196. test_directory_r (void)
  197. {
  198. #ifdef LINUX
  199. struct GNUNET_BIO_ReadHandle *rh;
  200. char rString[200];
  201. rh = GNUNET_BIO_read_open_file ("/dev");
  202. GNUNET_assert (NULL != rh);
  203. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read (rh,
  204. "test-directory-r",
  205. rString,
  206. sizeof (rString)));
  207. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  208. #endif
  209. return 0;
  210. }
  211. static int
  212. test_nullfile_rw (void)
  213. {
  214. static char filename[102401];
  215. struct GNUNET_BIO_WriteHandle *wh;
  216. struct GNUNET_BIO_ReadHandle *rh;
  217. memset (filename, 'a', sizeof (filename));
  218. filename[sizeof (filename) - 1] = '\0';
  219. GNUNET_log_skip (2, GNUNET_NO);
  220. wh = GNUNET_BIO_write_open_file (filename);
  221. GNUNET_log_skip (0, GNUNET_YES);
  222. GNUNET_assert (NULL == wh);
  223. GNUNET_log_skip (2, GNUNET_NO);
  224. rh = GNUNET_BIO_read_open_file (filename);
  225. GNUNET_log_skip (0, GNUNET_YES);
  226. GNUNET_assert (NULL == rh);
  227. return 0;
  228. }
  229. static int
  230. test_fullfile_rw (void)
  231. {
  232. #ifdef LINUX
  233. /* /dev/full doesn't exist on every platform */
  234. struct GNUNET_BIO_WriteHandle *wh;
  235. struct GNUNET_BIO_ReadHandle *rh;
  236. char *rString = NULL;
  237. char rResult[200];
  238. struct GNUNET_CONTAINER_MetaData *mdW;
  239. struct GNUNET_CONTAINER_MetaData *mdR = NULL;
  240. mdW = GNUNET_CONTAINER_meta_data_create ();
  241. GNUNET_CONTAINER_meta_data_add_publication_date (mdW);
  242. struct GNUNET_BIO_WriteSpec ws[] = {
  243. GNUNET_BIO_write_spec_object ("test-fullfile-rw-bytes",
  244. TESTSTRING,
  245. strlen (TESTSTRING)),
  246. GNUNET_BIO_write_spec_string ("test-fullfile-rw-string",
  247. TESTSTRING),
  248. GNUNET_BIO_write_spec_meta_data ("test-fullfile-rw-metadata",
  249. mdW),
  250. GNUNET_BIO_write_spec_end (),
  251. };
  252. struct GNUNET_BIO_ReadSpec rs[] = {
  253. GNUNET_BIO_read_spec_object ("test-fullfile-rw-bytes",
  254. rResult,
  255. sizeof (rResult)),
  256. GNUNET_BIO_read_spec_string ("test-fullfile-rw-string",
  257. &rString,
  258. 200),
  259. GNUNET_BIO_read_spec_meta_data ("test-fullfile-rw-metadata",
  260. &mdR),
  261. GNUNET_BIO_read_spec_end (),
  262. };
  263. wh = GNUNET_BIO_write_open_file ("/dev/full");
  264. GNUNET_assert (NULL != wh);
  265. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_spec_commit (wh, ws));
  266. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_write_close (wh, NULL));
  267. rh = GNUNET_BIO_read_open_file ("/dev/null");
  268. GNUNET_assert (NULL != rh);
  269. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_spec_commit (rh, rs));
  270. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  271. GNUNET_assert (NULL == rString);
  272. GNUNET_assert (NULL == mdR);
  273. #endif
  274. return 0;
  275. }
  276. static int
  277. test_fakestring_rw (void)
  278. {
  279. struct GNUNET_BIO_WriteHandle *wh;
  280. struct GNUNET_BIO_ReadHandle *rh;
  281. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  282. char *rString = NULL;
  283. wh = GNUNET_BIO_write_open_file (filename);
  284. GNUNET_assert (NULL != wh);
  285. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
  286. "test-fakestring-rw-int32",
  287. 2));
  288. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  289. rh = GNUNET_BIO_read_open_file (filename);
  290. GNUNET_assert (NULL != rh);
  291. GNUNET_assert (GNUNET_SYSERR ==
  292. GNUNET_BIO_read_string (rh,
  293. "test-fakestring-rw-string",
  294. &rString, 200));
  295. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  296. GNUNET_assert (NULL == rString);
  297. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  298. GNUNET_free (filename);
  299. return 0;
  300. }
  301. static int
  302. test_fakemeta_rw (void)
  303. {
  304. struct GNUNET_BIO_WriteHandle *wh;
  305. struct GNUNET_BIO_ReadHandle *rh;
  306. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  307. struct GNUNET_CONTAINER_MetaData *mdR = NULL;
  308. wh = GNUNET_BIO_write_open_file (filename);
  309. GNUNET_assert (NULL != wh);
  310. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
  311. "test-fakestring-rw-int32",
  312. 2));
  313. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  314. rh = GNUNET_BIO_read_open_file (filename);
  315. GNUNET_assert (NULL != rh);
  316. GNUNET_assert (GNUNET_SYSERR ==
  317. GNUNET_BIO_read_meta_data (rh,
  318. "test-fakestring-rw-metadata",
  319. &mdR));
  320. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  321. GNUNET_assert (NULL == mdR);
  322. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  323. GNUNET_free (filename);
  324. return 0;
  325. }
  326. static int
  327. test_fakebigmeta_rw (void)
  328. {
  329. struct GNUNET_BIO_WriteHandle *wh;
  330. struct GNUNET_BIO_ReadHandle *rh;
  331. char *filename = GNUNET_DISK_mktemp ("gnunet_bio");
  332. struct GNUNET_CONTAINER_MetaData *mdR = NULL;
  333. int32_t wNum = 1024 * 1024 * 10;
  334. wh = GNUNET_BIO_write_open_file (filename);
  335. GNUNET_assert (NULL != wh);
  336. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_int32 (wh,
  337. "test-fakebigmeta-rw-int32",
  338. wNum));
  339. GNUNET_assert (GNUNET_OK == GNUNET_BIO_write_close (wh, NULL));
  340. rh = GNUNET_BIO_read_open_file (filename);
  341. GNUNET_assert (NULL != rh);
  342. GNUNET_assert (GNUNET_SYSERR ==
  343. GNUNET_BIO_read_meta_data (rh,
  344. "test-fakebigmeta-rw-metadata",
  345. &mdR));
  346. GNUNET_assert (GNUNET_SYSERR == GNUNET_BIO_read_close (rh, NULL));
  347. GNUNET_assert (NULL == mdR);
  348. GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (filename));
  349. GNUNET_free (filename);
  350. return 0;
  351. }
  352. static int
  353. check_string_rw (void)
  354. {
  355. GNUNET_assert (0 == test_nullstring_rw ());
  356. GNUNET_assert (0 == test_emptystring_rw ());
  357. GNUNET_assert (0 == test_bigstring_rw ());
  358. GNUNET_assert (0 == test_fakestring_rw ());
  359. return 0;
  360. }
  361. static int
  362. check_metadata_rw (void)
  363. {
  364. GNUNET_assert (0 == test_fakebigmeta_rw ());
  365. GNUNET_assert (0 == test_fakemeta_rw ());
  366. GNUNET_assert (0 == test_bigmeta_rw ());
  367. return 0;
  368. }
  369. static int
  370. check_file_rw (void)
  371. {
  372. GNUNET_assert (0 == test_normal_rw ());
  373. GNUNET_assert (0 == test_nullfile_rw ());
  374. GNUNET_assert (0 == test_fullfile_rw ());
  375. GNUNET_assert (0 == test_directory_r ());
  376. return 0;
  377. }
  378. int
  379. main (int argc, char *argv[])
  380. {
  381. GNUNET_log_setup ("test-bio", "WARNING", NULL);
  382. GNUNET_assert (0 == check_file_rw ());
  383. GNUNET_assert (0 == check_metadata_rw ());
  384. GNUNET_assert (0 == check_string_rw ());
  385. return 0;
  386. }
  387. /* end of test_bio.c */