bio_memleak_test.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. const char *str = "BIO test\n";
  20. char buf[100];
  21. bio = BIO_new(BIO_s_mem());
  22. if (bio == NULL)
  23. goto finish;
  24. bufmem.length = strlen(str) + 1;
  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 (BIO_read(bio, buf, sizeof(buf)) <= 0)
  30. goto finish;
  31. ok = strcmp(buf, str) == 0;
  32. finish:
  33. BIO_free(bio);
  34. return ok;
  35. }
  36. int global_init(void)
  37. {
  38. CRYPTO_set_mem_debug(1);
  39. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  40. return 1;
  41. }
  42. int setup_tests(void)
  43. {
  44. ADD_TEST(test_bio_memleak);
  45. return 1;
  46. }