bio_callback_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/bio.h>
  12. #include "testutil.h"
  13. #define MAXCOUNT 5
  14. static int my_param_count;
  15. static BIO *my_param_b[MAXCOUNT];
  16. static int my_param_oper[MAXCOUNT];
  17. static const char *my_param_argp[MAXCOUNT];
  18. static int my_param_argi[MAXCOUNT];
  19. static long my_param_argl[MAXCOUNT];
  20. static long my_param_ret[MAXCOUNT];
  21. static long my_bio_callback(BIO *b, int oper, const char *argp, int argi,
  22. long argl, long ret)
  23. {
  24. if (my_param_count >= MAXCOUNT)
  25. return -1;
  26. my_param_b[my_param_count] = b;
  27. my_param_oper[my_param_count] = oper;
  28. my_param_argp[my_param_count] = argp;
  29. my_param_argi[my_param_count] = argi;
  30. my_param_argl[my_param_count] = argl;
  31. my_param_ret[my_param_count] = ret;
  32. my_param_count++;
  33. return ret;
  34. }
  35. static int test_bio_callback(void)
  36. {
  37. int ok = 0;
  38. BIO *bio;
  39. int i;
  40. char test1[] = "test";
  41. const int test1len = sizeof(test1) - 1;
  42. char test2[] = "hello";
  43. const int test2len = sizeof(test2) - 1;
  44. char buf[16];
  45. my_param_count = 0;
  46. bio = BIO_new(BIO_s_mem());
  47. if (bio == NULL)
  48. goto err;
  49. BIO_set_callback(bio, my_bio_callback);
  50. i = BIO_write(bio, test1, test1len);
  51. if (!TEST_int_eq(i, test1len)
  52. || !TEST_int_eq(my_param_count, 2)
  53. || !TEST_ptr_eq(my_param_b[0], bio)
  54. || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
  55. || !TEST_ptr_eq(my_param_argp[0], test1)
  56. || !TEST_int_eq(my_param_argi[0], test1len)
  57. || !TEST_long_eq(my_param_argl[0], 0L)
  58. || !TEST_long_eq(my_param_ret[0], 1L)
  59. || !TEST_ptr_eq(my_param_b[1], bio)
  60. || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
  61. || !TEST_ptr_eq(my_param_argp[1], test1)
  62. || !TEST_int_eq(my_param_argi[1], test1len)
  63. || !TEST_long_eq(my_param_argl[1], 0L)
  64. || !TEST_long_eq(my_param_ret[1], (long)test1len))
  65. goto err;
  66. my_param_count = 0;
  67. i = BIO_read(bio, buf, sizeof(buf));
  68. if (!TEST_mem_eq(buf, i, test1, test1len)
  69. || !TEST_int_eq(my_param_count, 2)
  70. || !TEST_ptr_eq(my_param_b[0], bio)
  71. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  72. || !TEST_ptr_eq(my_param_argp[0], buf)
  73. || !TEST_int_eq(my_param_argi[0], sizeof(buf))
  74. || !TEST_long_eq(my_param_argl[0], 0L)
  75. || !TEST_long_eq(my_param_ret[0], 1L)
  76. || !TEST_ptr_eq(my_param_b[1], bio)
  77. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  78. || !TEST_ptr_eq(my_param_argp[1], buf)
  79. || !TEST_int_eq(my_param_argi[1], sizeof(buf))
  80. || !TEST_long_eq(my_param_argl[1], 0L)
  81. || !TEST_long_eq(my_param_ret[1], (long)test1len))
  82. goto err;
  83. /* By default a mem bio returns -1 if it has run out of data */
  84. my_param_count = 0;
  85. i = BIO_read(bio, buf, sizeof(buf));
  86. if (!TEST_int_eq(i, -1)
  87. || !TEST_int_eq(my_param_count, 2)
  88. || !TEST_ptr_eq(my_param_b[0], bio)
  89. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  90. || !TEST_ptr_eq(my_param_argp[0], buf)
  91. || !TEST_int_eq(my_param_argi[0], sizeof(buf))
  92. || !TEST_long_eq(my_param_argl[0], 0L)
  93. || !TEST_long_eq(my_param_ret[0], 1L)
  94. || !TEST_ptr_eq(my_param_b[1], bio)
  95. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  96. || !TEST_ptr_eq(my_param_argp[1], buf)
  97. || !TEST_int_eq(my_param_argi[1], sizeof(buf))
  98. || !TEST_long_eq(my_param_argl[1], 0L)
  99. || !TEST_long_eq(my_param_ret[1], -1L))
  100. goto err;
  101. /* Force the mem bio to return 0 if it has run out of data */
  102. BIO_set_mem_eof_return(bio, 0);
  103. my_param_count = 0;
  104. i = BIO_read(bio, buf, sizeof(buf));
  105. if (!TEST_int_eq(i, 0)
  106. || !TEST_int_eq(my_param_count, 2)
  107. || !TEST_ptr_eq(my_param_b[0], bio)
  108. || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
  109. || !TEST_ptr_eq(my_param_argp[0], buf)
  110. || !TEST_int_eq(my_param_argi[0], sizeof(buf))
  111. || !TEST_long_eq(my_param_argl[0], 0L)
  112. || !TEST_long_eq(my_param_ret[0], 1L)
  113. || !TEST_ptr_eq(my_param_b[1], bio)
  114. || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
  115. || !TEST_ptr_eq(my_param_argp[1], buf)
  116. || !TEST_int_eq(my_param_argi[1], sizeof(buf))
  117. || !TEST_long_eq(my_param_argl[1], 0L)
  118. || !TEST_long_eq(my_param_ret[1], 0L))
  119. goto err;
  120. my_param_count = 0;
  121. i = BIO_puts(bio, test2);
  122. if (!TEST_int_eq(i, 5)
  123. || !TEST_int_eq(my_param_count, 2)
  124. || !TEST_ptr_eq(my_param_b[0], bio)
  125. || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS)
  126. || !TEST_ptr_eq(my_param_argp[0], test2)
  127. || !TEST_int_eq(my_param_argi[0], 0)
  128. || !TEST_long_eq(my_param_argl[0], 0L)
  129. || !TEST_long_eq(my_param_ret[0], 1L)
  130. || !TEST_ptr_eq(my_param_b[1], bio)
  131. || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN)
  132. || !TEST_ptr_eq(my_param_argp[1], test2)
  133. || !TEST_int_eq(my_param_argi[1], 0)
  134. || !TEST_long_eq(my_param_argl[1], 0L)
  135. || !TEST_long_eq(my_param_ret[1], (long)test2len))
  136. goto err;
  137. my_param_count = 0;
  138. i = BIO_free(bio);
  139. if (!TEST_int_eq(i, 1)
  140. || !TEST_int_eq(my_param_count, 1)
  141. || !TEST_ptr_eq(my_param_b[0], bio)
  142. || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE)
  143. || !TEST_ptr_eq(my_param_argp[0], NULL)
  144. || !TEST_int_eq(my_param_argi[0], 0)
  145. || !TEST_long_eq(my_param_argl[0], 0L)
  146. || !TEST_long_eq(my_param_ret[0], 1L))
  147. goto finish;
  148. ok = 1;
  149. goto finish;
  150. err:
  151. BIO_free(bio);
  152. finish:
  153. /* This helps finding memory leaks with ASAN */
  154. memset(my_param_b, 0, sizeof(my_param_b));
  155. memset(my_param_argp, 0, sizeof(my_param_argp));
  156. return ok;
  157. }
  158. int setup_tests(void)
  159. {
  160. ADD_TEST(test_bio_callback);
  161. return 1;
  162. }