2
0

bio_memleak_test.c 7.5 KB

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