bio_memleak_test.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/buffer.h>
  12. #include <openssl/bio.h>
  13. #include "testutil.h"
  14. static int test_bio_memleak(void)
  15. {
  16. int ok = 0;
  17. BIO *bio;
  18. BUF_MEM bufmem;
  19. static const char str[] = "BIO test\n";
  20. char buf[100];
  21. bio = BIO_new(BIO_s_mem());
  22. if (!TEST_ptr(bio))
  23. goto finish;
  24. bufmem.length = sizeof(str);
  25. bufmem.data = (char *) str;
  26. bufmem.max = bufmem.length;
  27. BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE);
  28. BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
  29. if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str)))
  30. goto finish;
  31. if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str)))
  32. goto finish;
  33. ok = 1;
  34. finish:
  35. BIO_free(bio);
  36. return ok;
  37. }
  38. static int test_bio_get_mem(void)
  39. {
  40. int ok = 0;
  41. BIO *bio = NULL;
  42. BUF_MEM *bufmem = NULL;
  43. bio = BIO_new(BIO_s_mem());
  44. if (!TEST_ptr(bio))
  45. goto finish;
  46. if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
  47. goto finish;
  48. BIO_get_mem_ptr(bio, &bufmem);
  49. if (!TEST_ptr(bufmem))
  50. goto finish;
  51. if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0))
  52. goto finish;
  53. BIO_free(bio);
  54. bio = NULL;
  55. if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12))
  56. goto finish;
  57. ok = 1;
  58. finish:
  59. BIO_free(bio);
  60. BUF_MEM_free(bufmem);
  61. return ok;
  62. }
  63. static int test_bio_new_mem_buf(void)
  64. {
  65. int ok = 0;
  66. BIO *bio;
  67. BUF_MEM *bufmem;
  68. char data[16];
  69. bio = BIO_new_mem_buf("Hello World\n", 12);
  70. if (!TEST_ptr(bio))
  71. goto finish;
  72. if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
  73. goto finish;
  74. if (!TEST_mem_eq(data, 5, "Hello", 5))
  75. goto finish;
  76. if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
  77. goto finish;
  78. if (!TEST_int_lt(BIO_write(bio, "test", 4), 0))
  79. goto finish;
  80. if (!TEST_int_eq(BIO_read(bio, data, 16), 7))
  81. goto finish;
  82. if (!TEST_mem_eq(data, 7, " World\n", 7))
  83. goto finish;
  84. if (!TEST_int_gt(BIO_reset(bio), 0))
  85. goto finish;
  86. if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
  87. goto finish;
  88. if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
  89. goto finish;
  90. ok = 1;
  91. finish:
  92. BIO_free(bio);
  93. return ok;
  94. }
  95. static int test_bio_rdonly_mem_buf(void)
  96. {
  97. int ok = 0;
  98. BIO *bio, *bio2 = NULL;
  99. BUF_MEM *bufmem;
  100. char data[16];
  101. bio = BIO_new_mem_buf("Hello World\n", 12);
  102. if (!TEST_ptr(bio))
  103. goto finish;
  104. if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
  105. goto finish;
  106. if (!TEST_mem_eq(data, 5, "Hello", 5))
  107. goto finish;
  108. if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
  109. goto finish;
  110. (void)BIO_set_close(bio, BIO_NOCLOSE);
  111. bio2 = BIO_new(BIO_s_mem());
  112. if (!TEST_ptr(bio2))
  113. goto finish;
  114. BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE);
  115. BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY);
  116. if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
  117. goto finish;
  118. if (!TEST_mem_eq(data, 7, " World\n", 7))
  119. goto finish;
  120. if (!TEST_int_gt(BIO_reset(bio2), 0))
  121. goto finish;
  122. if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
  123. goto finish;
  124. if (!TEST_mem_eq(data, 7, " World\n", 7))
  125. goto finish;
  126. ok = 1;
  127. finish:
  128. BIO_free(bio);
  129. BIO_free(bio2);
  130. return ok;
  131. }
  132. static int test_bio_rdwr_rdonly(void)
  133. {
  134. int ok = 0;
  135. BIO *bio = NULL;
  136. char data[16];
  137. bio = BIO_new(BIO_s_mem());
  138. if (!TEST_ptr(bio))
  139. goto finish;
  140. if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
  141. goto finish;
  142. BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
  143. if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
  144. goto finish;
  145. if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
  146. goto finish;
  147. if (!TEST_int_gt(BIO_reset(bio), 0))
  148. goto finish;
  149. BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY);
  150. if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4))
  151. goto finish;
  152. if (!TEST_int_eq(BIO_read(bio, data, 16), 16))
  153. goto finish;
  154. if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16))
  155. goto finish;
  156. ok = 1;
  157. finish:
  158. BIO_free(bio);
  159. return ok;
  160. }
  161. static int test_bio_nonclear_rst(void)
  162. {
  163. int ok = 0;
  164. BIO *bio = NULL;
  165. char data[16];
  166. bio = BIO_new(BIO_s_mem());
  167. if (!TEST_ptr(bio))
  168. goto finish;
  169. if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
  170. goto finish;
  171. BIO_set_flags(bio, BIO_FLAGS_NONCLEAR_RST);
  172. if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
  173. goto finish;
  174. if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
  175. goto finish;
  176. if (!TEST_int_gt(BIO_reset(bio), 0))
  177. goto finish;
  178. if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
  179. goto finish;
  180. if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
  181. goto finish;
  182. BIO_clear_flags(bio, BIO_FLAGS_NONCLEAR_RST);
  183. if (!TEST_int_gt(BIO_reset(bio), 0))
  184. goto finish;
  185. if (!TEST_int_lt(BIO_read(bio, data, 16), 1))
  186. goto finish;
  187. ok = 1;
  188. finish:
  189. BIO_free(bio);
  190. return ok;
  191. }
  192. static int error_callback_fired;
  193. static long BIO_error_callback(BIO *bio, int cmd, const char *argp,
  194. size_t len, int argi,
  195. long argl, int ret, size_t *processed)
  196. {
  197. if ((cmd & (BIO_CB_READ | BIO_CB_RETURN)) != 0) {
  198. error_callback_fired = 1;
  199. ret = 0; /* fail for read operations to simulate error in input BIO */
  200. }
  201. return ret;
  202. }
  203. /* Checks i2d_ASN1_bio_stream() is freeing all memory when input BIO ends unexpectedly. */
  204. static int test_bio_i2d_ASN1_mime(void)
  205. {
  206. int ok = 0;
  207. BIO *bio = NULL, *out = NULL;
  208. BUF_MEM bufmem;
  209. static const char str[] = "BIO mime test\n";
  210. PKCS7 *p7 = NULL;
  211. if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
  212. goto finish;
  213. bufmem.length = sizeof(str);
  214. bufmem.data = (char *) str;
  215. bufmem.max = bufmem.length;
  216. BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE);
  217. BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
  218. BIO_set_callback_ex(bio, BIO_error_callback);
  219. if (!TEST_ptr(out = BIO_new(BIO_s_mem())))
  220. goto finish;
  221. if (!TEST_ptr(p7 = PKCS7_new()))
  222. goto finish;
  223. if (!TEST_true(PKCS7_set_type(p7, NID_pkcs7_data)))
  224. goto finish;
  225. error_callback_fired = 0;
  226. /*
  227. * The call succeeds even if the input stream ends unexpectedly as
  228. * there is no handling for this case in SMIME_crlf_copy().
  229. */
  230. if (!TEST_true(i2d_ASN1_bio_stream(out, (ASN1_VALUE*) p7, bio,
  231. SMIME_STREAM | SMIME_BINARY,
  232. ASN1_ITEM_rptr(PKCS7))))
  233. goto finish;
  234. if (!TEST_int_eq(error_callback_fired, 1))
  235. goto finish;
  236. ok = 1;
  237. finish:
  238. BIO_free(bio);
  239. BIO_free(out);
  240. PKCS7_free(p7);
  241. return ok;
  242. }
  243. int setup_tests(void)
  244. {
  245. ADD_TEST(test_bio_memleak);
  246. ADD_TEST(test_bio_get_mem);
  247. ADD_TEST(test_bio_new_mem_buf);
  248. ADD_TEST(test_bio_rdonly_mem_buf);
  249. ADD_TEST(test_bio_rdwr_rdonly);
  250. ADD_TEST(test_bio_nonclear_rst);
  251. ADD_TEST(test_bio_i2d_ASN1_mime);
  252. return 1;
  253. }