2
0

bio_callback_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #define OPENSSL_SUPPRESS_DEPRECATED
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/bio.h>
  13. #include "testutil.h"
  14. #define MAXCOUNT 5
  15. static int my_param_count;
  16. static BIO *my_param_b[MAXCOUNT];
  17. static int my_param_oper[MAXCOUNT];
  18. static const char *my_param_argp[MAXCOUNT];
  19. static int my_param_argi[MAXCOUNT];
  20. static long my_param_argl[MAXCOUNT];
  21. static long my_param_ret[MAXCOUNT];
  22. static size_t my_param_len[MAXCOUNT];
  23. static size_t my_param_processed[MAXCOUNT];
  24. static long my_bio_cb_ex(BIO *b, int oper, const char *argp, size_t len,
  25. int argi, long argl, int ret, size_t *processed)
  26. {
  27. if (my_param_count >= MAXCOUNT)
  28. return -1;
  29. my_param_b[my_param_count] = b;
  30. my_param_oper[my_param_count] = oper;
  31. my_param_argp[my_param_count] = argp;
  32. my_param_argi[my_param_count] = argi;
  33. my_param_argl[my_param_count] = argl;
  34. my_param_ret[my_param_count] = ret;
  35. my_param_len[my_param_count] = len;
  36. my_param_processed[my_param_count] = processed != NULL ? *processed : 0;
  37. my_param_count++;
  38. return ret;
  39. }
  40. static int test_bio_callback_ex(void)
  41. {
  42. int ok = 0;
  43. BIO *bio;
  44. int i;
  45. char test1[] = "test";
  46. const size_t test1len = sizeof(test1) - 1;
  47. char test2[] = "hello";
  48. const size_t test2len = sizeof(test2) - 1;
  49. char buf[16];
  50. my_param_count = 0;
  51. bio = BIO_new(BIO_s_mem());
  52. if (bio == NULL)
  53. goto err;
  54. BIO_set_callback_ex(bio, my_bio_cb_ex);
  55. i = BIO_write(bio, test1, test1len);
  56. if (!TEST_int_eq(i, test1len)
  57. || !TEST_int_eq(my_param_count, 2)
  58. || !TEST_ptr_eq(my_param_b[0], bio)
  59. || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
  60. || !TEST_ptr_eq(my_param_argp[0], test1)
  61. || !TEST_size_t_eq(my_param_len[0], test1len)
  62. || !TEST_long_eq(my_param_argl[0], 0L)
  63. || !TEST_int_eq((int)my_param_ret[0], 1)
  64. || !TEST_ptr_eq(my_param_b[1], bio)
  65. || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
  66. || !TEST_ptr_eq(my_param_argp[1], test1)
  67. || !TEST_size_t_eq(my_param_len[1], test1len)
  68. || !TEST_long_eq(my_param_argl[1], 0L)
  69. || !TEST_size_t_eq(my_param_processed[1], test1len)
  70. || !TEST_int_eq((int)my_param_ret[1], 1))
  71. goto err;
  72. my_param_count = 0;
  73. i = BIO_read(bio, buf, sizeof(buf));
  74. if (!TEST_mem_eq(buf, i, test1, test1len)
  75. || !TEST_int_eq(my_param_count, 2)
  76. || !TEST_ptr_eq(my_param_b[0], bio)
  77. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  78. || !TEST_ptr_eq(my_param_argp[0], buf)
  79. || !TEST_size_t_eq(my_param_len[0], sizeof(buf))
  80. || !TEST_long_eq(my_param_argl[0], 0L)
  81. || !TEST_int_eq((int)my_param_ret[0], 1)
  82. || !TEST_ptr_eq(my_param_b[1], bio)
  83. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  84. || !TEST_ptr_eq(my_param_argp[1], buf)
  85. || !TEST_size_t_eq(my_param_len[1], sizeof(buf))
  86. || !TEST_long_eq(my_param_argl[1], 0L)
  87. || !TEST_size_t_eq(my_param_processed[1], test1len)
  88. || !TEST_int_eq((int)my_param_ret[1], 1))
  89. goto err;
  90. /* By default a mem bio returns -1 if it has run out of data */
  91. my_param_count = 0;
  92. i = BIO_read(bio, buf, sizeof(buf));
  93. if (!TEST_int_eq(i, -1)
  94. || !TEST_int_eq(my_param_count, 2)
  95. || !TEST_ptr_eq(my_param_b[0], bio)
  96. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  97. || !TEST_ptr_eq(my_param_argp[0], buf)
  98. || !TEST_size_t_eq(my_param_len[0], sizeof(buf))
  99. || !TEST_long_eq(my_param_argl[0], 0L)
  100. || !TEST_int_eq((int)my_param_ret[0], 1)
  101. || !TEST_ptr_eq(my_param_b[1], bio)
  102. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  103. || !TEST_ptr_eq(my_param_argp[1], buf)
  104. || !TEST_size_t_eq(my_param_len[1], sizeof(buf))
  105. || !TEST_long_eq(my_param_argl[1], 0L)
  106. || !TEST_size_t_eq(my_param_processed[1], 0)
  107. || !TEST_int_eq((int)my_param_ret[1], -1))
  108. goto err;
  109. /* Force the mem bio to return 0 if it has run out of data */
  110. my_param_count = 0;
  111. i = BIO_set_mem_eof_return(bio, 0);
  112. if (!TEST_int_eq(i, 1)
  113. || !TEST_int_eq(my_param_count, 2)
  114. || !TEST_ptr_eq(my_param_b[0], bio)
  115. || !TEST_int_eq(my_param_oper[0], BIO_CB_CTRL)
  116. || !TEST_ptr_eq(my_param_argp[0], NULL)
  117. || !TEST_int_eq(my_param_argi[0], BIO_C_SET_BUF_MEM_EOF_RETURN)
  118. || !TEST_long_eq(my_param_argl[0], 0L)
  119. || !TEST_int_eq((int)my_param_ret[0], 1)
  120. || !TEST_ptr_eq(my_param_b[1], bio)
  121. || !TEST_int_eq(my_param_oper[1], BIO_CB_CTRL | BIO_CB_RETURN)
  122. || !TEST_ptr_eq(my_param_argp[1], NULL)
  123. || !TEST_int_eq(my_param_argi[1], BIO_C_SET_BUF_MEM_EOF_RETURN)
  124. || !TEST_long_eq(my_param_argl[1], 0L)
  125. || !TEST_int_eq((int)my_param_ret[1], 1))
  126. goto err;
  127. my_param_count = 0;
  128. i = BIO_read(bio, buf, sizeof(buf));
  129. if (!TEST_int_eq(i, 0)
  130. || !TEST_int_eq(my_param_count, 2)
  131. || !TEST_ptr_eq(my_param_b[0], bio)
  132. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  133. || !TEST_ptr_eq(my_param_argp[0], buf)
  134. || !TEST_size_t_eq(my_param_len[0], sizeof(buf))
  135. || !TEST_long_eq(my_param_argl[0], 0L)
  136. || !TEST_int_eq((int)my_param_ret[0], 1)
  137. || !TEST_ptr_eq(my_param_b[1], bio)
  138. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  139. || !TEST_ptr_eq(my_param_argp[1], buf)
  140. || !TEST_size_t_eq(my_param_len[1], sizeof(buf))
  141. || !TEST_long_eq(my_param_argl[1], 0L)
  142. || !TEST_size_t_eq(my_param_processed[1], 0)
  143. || !TEST_int_eq((int)my_param_ret[1], 0))
  144. goto err;
  145. my_param_count = 0;
  146. i = BIO_puts(bio, test2);
  147. if (!TEST_int_eq(i, 5)
  148. || !TEST_int_eq(my_param_count, 2)
  149. || !TEST_ptr_eq(my_param_b[0], bio)
  150. || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS)
  151. || !TEST_ptr_eq(my_param_argp[0], test2)
  152. || !TEST_int_eq(my_param_argi[0], 0)
  153. || !TEST_long_eq(my_param_argl[0], 0L)
  154. || !TEST_int_eq((int)my_param_ret[0], 1)
  155. || !TEST_ptr_eq(my_param_b[1], bio)
  156. || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN)
  157. || !TEST_ptr_eq(my_param_argp[1], test2)
  158. || !TEST_int_eq(my_param_argi[1], 0)
  159. || !TEST_long_eq(my_param_argl[1], 0L)
  160. || !TEST_size_t_eq(my_param_processed[1], test2len)
  161. || !TEST_int_eq((int)my_param_ret[1], 1))
  162. goto err;
  163. my_param_count = 0;
  164. i = BIO_free(bio);
  165. if (!TEST_int_eq(i, 1)
  166. || !TEST_int_eq(my_param_count, 1)
  167. || !TEST_ptr_eq(my_param_b[0], bio)
  168. || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE)
  169. || !TEST_ptr_eq(my_param_argp[0], NULL)
  170. || !TEST_int_eq(my_param_argi[0], 0)
  171. || !TEST_long_eq(my_param_argl[0], 0L)
  172. || !TEST_int_eq((int)my_param_ret[0], 1))
  173. goto finish;
  174. ok = 1;
  175. goto finish;
  176. err:
  177. BIO_free(bio);
  178. finish:
  179. /* This helps finding memory leaks with ASAN */
  180. memset(my_param_b, 0, sizeof(my_param_b));
  181. memset(my_param_argp, 0, sizeof(my_param_argp));
  182. return ok;
  183. }
  184. #ifndef OPENSSL_NO_DEPRECATED_3_0
  185. static long my_bio_callback(BIO *b, int oper, const char *argp, int argi,
  186. long argl, long ret)
  187. {
  188. if (my_param_count >= MAXCOUNT)
  189. return -1;
  190. my_param_b[my_param_count] = b;
  191. my_param_oper[my_param_count] = oper;
  192. my_param_argp[my_param_count] = argp;
  193. my_param_argi[my_param_count] = argi;
  194. my_param_argl[my_param_count] = argl;
  195. my_param_ret[my_param_count] = ret;
  196. my_param_count++;
  197. return ret;
  198. }
  199. static int test_bio_callback(void)
  200. {
  201. int ok = 0;
  202. BIO *bio;
  203. int i;
  204. char test1[] = "test";
  205. const int test1len = sizeof(test1) - 1;
  206. char test2[] = "hello";
  207. const int test2len = sizeof(test2) - 1;
  208. char buf[16];
  209. my_param_count = 0;
  210. bio = BIO_new(BIO_s_mem());
  211. if (bio == NULL)
  212. goto err;
  213. BIO_set_callback(bio, my_bio_callback);
  214. i = BIO_write(bio, test1, test1len);
  215. if (!TEST_int_eq(i, test1len)
  216. || !TEST_int_eq(my_param_count, 2)
  217. || !TEST_ptr_eq(my_param_b[0], bio)
  218. || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
  219. || !TEST_ptr_eq(my_param_argp[0], test1)
  220. || !TEST_int_eq(my_param_argi[0], test1len)
  221. || !TEST_long_eq(my_param_argl[0], 0L)
  222. || !TEST_long_eq(my_param_ret[0], 1L)
  223. || !TEST_ptr_eq(my_param_b[1], bio)
  224. || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
  225. || !TEST_ptr_eq(my_param_argp[1], test1)
  226. || !TEST_int_eq(my_param_argi[1], test1len)
  227. || !TEST_long_eq(my_param_argl[1], 0L)
  228. || !TEST_long_eq(my_param_ret[1], (long)test1len))
  229. goto err;
  230. my_param_count = 0;
  231. i = BIO_read(bio, buf, sizeof(buf));
  232. if (!TEST_mem_eq(buf, i, test1, test1len)
  233. || !TEST_int_eq(my_param_count, 2)
  234. || !TEST_ptr_eq(my_param_b[0], bio)
  235. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  236. || !TEST_ptr_eq(my_param_argp[0], buf)
  237. || !TEST_int_eq(my_param_argi[0], sizeof(buf))
  238. || !TEST_long_eq(my_param_argl[0], 0L)
  239. || !TEST_long_eq(my_param_ret[0], 1L)
  240. || !TEST_ptr_eq(my_param_b[1], bio)
  241. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  242. || !TEST_ptr_eq(my_param_argp[1], buf)
  243. || !TEST_int_eq(my_param_argi[1], sizeof(buf))
  244. || !TEST_long_eq(my_param_argl[1], 0L)
  245. || !TEST_long_eq(my_param_ret[1], (long)test1len))
  246. goto err;
  247. /* By default a mem bio returns -1 if it has run out of data */
  248. my_param_count = 0;
  249. i = BIO_read(bio, buf, sizeof(buf));
  250. if (!TEST_int_eq(i, -1)
  251. || !TEST_int_eq(my_param_count, 2)
  252. || !TEST_ptr_eq(my_param_b[0], bio)
  253. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  254. || !TEST_ptr_eq(my_param_argp[0], buf)
  255. || !TEST_int_eq(my_param_argi[0], sizeof(buf))
  256. || !TEST_long_eq(my_param_argl[0], 0L)
  257. || !TEST_long_eq(my_param_ret[0], 1L)
  258. || !TEST_ptr_eq(my_param_b[1], bio)
  259. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  260. || !TEST_ptr_eq(my_param_argp[1], buf)
  261. || !TEST_int_eq(my_param_argi[1], sizeof(buf))
  262. || !TEST_long_eq(my_param_argl[1], 0L)
  263. || !TEST_long_eq(my_param_ret[1], -1L))
  264. goto err;
  265. /* Force the mem bio to return 0 if it has run out of data */
  266. BIO_set_mem_eof_return(bio, 0);
  267. my_param_count = 0;
  268. i = BIO_read(bio, buf, sizeof(buf));
  269. if (!TEST_int_eq(i, 0)
  270. || !TEST_int_eq(my_param_count, 2)
  271. || !TEST_ptr_eq(my_param_b[0], bio)
  272. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  273. || !TEST_ptr_eq(my_param_argp[0], buf)
  274. || !TEST_int_eq(my_param_argi[0], sizeof(buf))
  275. || !TEST_long_eq(my_param_argl[0], 0L)
  276. || !TEST_long_eq(my_param_ret[0], 1L)
  277. || !TEST_ptr_eq(my_param_b[1], bio)
  278. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  279. || !TEST_ptr_eq(my_param_argp[1], buf)
  280. || !TEST_int_eq(my_param_argi[1], sizeof(buf))
  281. || !TEST_long_eq(my_param_argl[1], 0L)
  282. || !TEST_long_eq(my_param_ret[1], 0L))
  283. goto err;
  284. my_param_count = 0;
  285. i = BIO_puts(bio, test2);
  286. if (!TEST_int_eq(i, 5)
  287. || !TEST_int_eq(my_param_count, 2)
  288. || !TEST_ptr_eq(my_param_b[0], bio)
  289. || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS)
  290. || !TEST_ptr_eq(my_param_argp[0], test2)
  291. || !TEST_int_eq(my_param_argi[0], 0)
  292. || !TEST_long_eq(my_param_argl[0], 0L)
  293. || !TEST_long_eq(my_param_ret[0], 1L)
  294. || !TEST_ptr_eq(my_param_b[1], bio)
  295. || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN)
  296. || !TEST_ptr_eq(my_param_argp[1], test2)
  297. || !TEST_int_eq(my_param_argi[1], 0)
  298. || !TEST_long_eq(my_param_argl[1], 0L)
  299. || !TEST_long_eq(my_param_ret[1], (long)test2len))
  300. goto err;
  301. my_param_count = 0;
  302. i = BIO_free(bio);
  303. if (!TEST_int_eq(i, 1)
  304. || !TEST_int_eq(my_param_count, 1)
  305. || !TEST_ptr_eq(my_param_b[0], bio)
  306. || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE)
  307. || !TEST_ptr_eq(my_param_argp[0], NULL)
  308. || !TEST_int_eq(my_param_argi[0], 0)
  309. || !TEST_long_eq(my_param_argl[0], 0L)
  310. || !TEST_long_eq(my_param_ret[0], 1L))
  311. goto finish;
  312. ok = 1;
  313. goto finish;
  314. err:
  315. BIO_free(bio);
  316. finish:
  317. /* This helps finding memory leaks with ASAN */
  318. memset(my_param_b, 0, sizeof(my_param_b));
  319. memset(my_param_argp, 0, sizeof(my_param_argp));
  320. return ok;
  321. }
  322. #endif
  323. int setup_tests(void)
  324. {
  325. ADD_TEST(test_bio_callback_ex);
  326. #ifndef OPENSSL_NO_DEPRECATED_3_0
  327. ADD_TEST(test_bio_callback);
  328. #endif
  329. return 1;
  330. }