bio_memleak_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/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. int global_init(void)
  193. {
  194. CRYPTO_set_mem_debug(1);
  195. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  196. return 1;
  197. }
  198. int setup_tests(void)
  199. {
  200. ADD_TEST(test_bio_memleak);
  201. ADD_TEST(test_bio_get_mem);
  202. ADD_TEST(test_bio_new_mem_buf);
  203. ADD_TEST(test_bio_rdonly_mem_buf);
  204. ADD_TEST(test_bio_rdwr_rdonly);
  205. ADD_TEST(test_bio_nonclear_rst);
  206. return 1;
  207. }