Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 2016-2017 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 
     10 #include <string.h>
     11 #include <openssl/bio.h>
     12 #include <openssl/crypto.h>
     13 
     14 #include "testutil.h"
     15 
     16 /*
     17  * We use a proper main function here instead of the custom main from the
     18  * test framework because the CRYPTO_mem_leaks_fp function cannot be called
     19  * a second time without trying to use a null pointer.  The test framework
     20  * calls this function as part of its close down.
     21  *
     22  * A work around is to call putenv("OPENSSL_DEBUG_MEMORY=0"); before exiting
     23  * but that is worse than avoiding the test framework's main.
     24  */
     25 
     26 int main(int argc, char *argv[])
     27 {
     28 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
     29     char *p;
     30     char *lost;
     31     int noleak;
     32 
     33     p = getenv("OPENSSL_DEBUG_MEMORY");
     34     if (p != NULL && strcmp(p, "on") == 0)
     35         CRYPTO_set_mem_debug(1);
     36     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
     37 
     38     lost = OPENSSL_malloc(3);
     39     if (!TEST_ptr(lost))
     40         return EXIT_FAILURE;
     41 
     42     if (argv[1] && strcmp(argv[1], "freeit") == 0) {
     43         OPENSSL_free(lost);
     44         lost = NULL;
     45     }
     46 
     47     noleak = CRYPTO_mem_leaks_fp(stderr);
     48     /* If -1 return value something bad happened */
     49     if (!TEST_int_ne(noleak, -1))
     50         return EXIT_FAILURE;
     51 
     52     return TEST_int_eq(lost != NULL, noleak == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
     53 #else
     54     return EXIT_SUCCESS;
     55 #endif
     56 }
     57