Commit e364c3b2 authored by Emilia Kasper's avatar Emilia Kasper
Browse files

Add main() test methods to reduce test boilerplate.



Simple tests only need to implement register_tests().
Tests that need a custom main() should implement test_main(). This will
be wrapped in a main() that performs common setup/teardown (currently
crypto-mdebug).

Note that for normal development, enable-asan is usually
sufficient for detecting leaks, and more versatile.

enable-crypto-mdebug is stricter as it will also
insist that all static variables be freed. This is useful for debugging
library init/deinit; however, it also means that test_main() must free
everything it allocates.

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
Reviewed-by: default avatarRich Salz <rsalz@openssl.org>
parent 7380737d
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include <openssl/asn1.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include "test_main.h"
#include "testutil.h"
#include "e_os.h"

@@ -90,10 +91,8 @@ static int test_standard_methods()
    return 0;
}

int main(int argc, char **argv)
void register_tests(void)
{
    ADD_TEST(test_tbl_standard);
    ADD_TEST(test_standard_methods);

    return run_tests(argv[0]);
}
+13 −13
Original line number Diff line number Diff line
@@ -226,7 +226,7 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[dtlsv1listentest]=.. ../include
  DEPEND[dtlsv1listentest]=../libssl

  SOURCE[ct_test]=ct_test.c testutil.c
  SOURCE[ct_test]=ct_test.c testutil.c test_main_custom.c
  INCLUDE[ct_test]=../crypto/include ../include
  DEPEND[ct_test]=../libcrypto

@@ -238,19 +238,19 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[afalgtest]=.. ../include
  DEPEND[afalgtest]=../libcrypto

  SOURCE[d2i_test]=d2i_test.c testutil.c
  SOURCE[d2i_test]=d2i_test.c testutil.c test_main_custom.c
  INCLUDE[d2i_test]=.. ../include
  DEPEND[d2i_test]=../libcrypto

  SOURCE[ssl_test_ctx_test]=ssl_test_ctx_test.c ssl_test_ctx.c testutil.c
  SOURCE[ssl_test_ctx_test]=ssl_test_ctx_test.c ssl_test_ctx.c testutil.c test_main_custom.c
  INCLUDE[ssl_test_ctx_test]=.. ../include
  DEPEND[ssl_test_ctx_test]=../libcrypto

  SOURCE[ssl_test]=ssl_test.c ssl_test_ctx.c testutil.c handshake_helper.c
  SOURCE[ssl_test]=ssl_test.c ssl_test_ctx.c testutil.c handshake_helper.c test_main_custom.c
  INCLUDE[ssl_test]=.. ../include
  DEPEND[ssl_test]=../libcrypto ../libssl

  SOURCE[cipherlist_test]=cipherlist_test.c testutil.c
  SOURCE[cipherlist_test]=cipherlist_test.c testutil.c test_main.c
  INCLUDE[cipherlist_test]=.. ../include
  DEPEND[cipherlist_test]=../libcrypto ../libssl

@@ -271,11 +271,11 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[bioprinttest]=../include
  DEPEND[bioprinttest]=../libcrypto

  SOURCE[sslapitest]=sslapitest.c ssltestlib.c testutil.c
  SOURCE[sslapitest]=sslapitest.c ssltestlib.c testutil.c test_main_custom.c
  INCLUDE[sslapitest]=../include
  DEPEND[sslapitest]=../libcrypto ../libssl

  SOURCE[dtlstest]=dtlstest.c ssltestlib.c testutil.c
  SOURCE[dtlstest]=dtlstest.c ssltestlib.c testutil.c test_main_custom.c
  INCLUDE[dtlstest]=../include .
  DEPEND[dtlstest]=../libcrypto ../libssl

@@ -298,7 +298,7 @@ IF[{- !$disabled{tests} -}]

  IF[{- $disabled{shared} -}]
    PROGRAMS_NO_INST=wpackettest cipher_overhead_test
    SOURCE[wpackettest]=wpackettest.c testutil.c
    SOURCE[wpackettest]=wpackettest.c testutil.c test_main_custom.c
    INCLUDE[wpackettest]=../include
    DEPEND[wpackettest]=../libcrypto ../libssl

@@ -330,7 +330,7 @@ IF[{- !$disabled{tests} -}]
    PROGRAMS_NO_INST=poly1305_internal_test
  ENDIF

  SOURCE[poly1305_internal_test]=poly1305_internal_test.c testutil.c
  SOURCE[poly1305_internal_test]=poly1305_internal_test.c testutil.c test_main_custom.c
  IF[{- !$disabled{shared} -}]
    SOURCE[poly1305_internal_test]= ../crypto/poly1305/poly1305.c \
        {- rebase_files("../crypto/poly1305", $target{poly1305_asm_src}) -} \
@@ -340,7 +340,7 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[poly1305_internal_test]=.. ../include ../crypto/include
  DEPEND[poly1305_internal_test]=../libcrypto

  SOURCE[asn1_internal_test]=asn1_internal_test.c testutil.c
  SOURCE[asn1_internal_test]=asn1_internal_test.c testutil.c test_main.c
  IF[{- !$disabled{shared} -}]
    SOURCE[asn1_internal_test]= ../crypto/asn1/a_strnid.c \
        ../crypto/rsa/rsa_ameth.c ../crypto/dsa/dsa_ameth.c \
@@ -351,7 +351,7 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[asn1_internal_test]=.. ../include ../crypto/include
  DEPEND[asn1_internal_test]=../libcrypto

  SOURCE[modes_internal_test]=modes_internal_test.c testutil.c
  SOURCE[modes_internal_test]=modes_internal_test.c testutil.c test_main_custom.c
  IF[{- !$disabled{shared} -}]
    SOURCE[modes_internal_test]= \
        {- rebase_files("../crypto", $target{cpuid_asm_src}); -} \
@@ -363,7 +363,7 @@ IF[{- !$disabled{tests} -}]
  # The reason for the huge amount of directly included x509v3 files
  # is that a table that is checked by x509_internal_test refers to
  # structures that are spread all over those files.
  SOURCE[x509_internal_test]=x509_internal_test.c testutil.c
  SOURCE[x509_internal_test]=x509_internal_test.c testutil.c test_main.c
  IF[{- !$disabled{shared} -}]
    SOURCE[x509_internal_test]= ../crypto/x509v3/v3_bitst.c \
        ../crypto/x509v3/v3_ia5.c ../crypto/x509v3/v3_skey.c \
@@ -382,7 +382,7 @@ IF[{- !$disabled{tests} -}]
  INCLUDE[x509_internal_test]=.. ../include
  DEPEND[x509_internal_test]=../libcrypto

  SOURCE[mdc2_internal_test]=mdc2_internal_test.c testutil.c
  SOURCE[mdc2_internal_test]=mdc2_internal_test.c testutil.c test_main.c
  INCLUDE[mdc2_internal_test]=.. ../include
  DEPEND[mdc2_internal_test]=../libcrypto

+2 −7
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#include <openssl/tls1.h>

#include "e_os.h"
#include "test_main.h"
#include "testutil.h"

typedef struct cipherlist_test_fixture {
@@ -189,14 +190,8 @@ static int test_default_cipherlist_explicit()
    EXECUTE_CIPHERLIST_TEST();
}

int main(int argc, char **argv)
void register_tests()
{
    int result = 0;

    ADD_TEST(test_default_cipherlist_implicit);
    ADD_TEST(test_default_cipherlist_explicit);

    result = run_tests(argv[0]);

    return result;
}
+4 −23
Original line number Diff line number Diff line
@@ -18,9 +18,9 @@
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "testutil.h"
#include "test_main_custom.h"

#ifndef OPENSSL_NO_CT

/* Used when declaring buffers to read text files into */
#define CT_TEST_MAX_FILE_SIZE 8096

@@ -530,15 +530,10 @@ static int test_encode_tls_sct()
    EXECUTE_CT_TEST();
}

int main(int argc, char *argv[])
int test_main(int argc, char *argv[])
{
    int result = 0;
    char *tmp_env = NULL;

    tmp_env = getenv("OPENSSL_DEBUG_MEMORY");
    if (tmp_env != NULL && strcmp(tmp_env, "on") == 0)
        CRYPTO_set_mem_debug(1);
    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
    char *tmp_env;

    tmp_env = getenv("CT_DIR");
    ct_dir = OPENSSL_strdup(tmp_env != NULL ? tmp_env : "ct");
@@ -554,24 +549,10 @@ int main(int argc, char *argv[])
    ADD_TEST(test_encode_tls_sct);

    result = run_tests(argv[0]);
    ERR_print_errors_fp(stderr);

    OPENSSL_free(ct_dir);
    OPENSSL_free(certs_dir);

#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (CRYPTO_mem_leaks_fp(stderr) <= 0)
        result = 1;
#endif

    return result;
}

#else /* OPENSSL_NO_CT */

int main(int argc, char* argv[])
{
    return EXIT_SUCCESS;
}

#endif /* OPENSSL_NO_CT */
#endif
+3 −15
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#include <string.h>

#include "testutil.h"
#include "test_main_custom.h"

#include <openssl/asn1.h>
#include <openssl/asn1t.h>
@@ -109,12 +110,10 @@ static int test_bad_asn1()
 * Usage: d2i_test <type> <file>, e.g.
 * d2i_test generalname bad_generalname.der
 */
int main(int argc, char **argv)
int test_main(int argc, char *argv[])
{
    int result = 0;
    const char *test_type_name;
    const char *expected_error_string;
    const char *p = getenv("OPENSSL_DEBUG_MEMORY");

    size_t i;

@@ -126,10 +125,6 @@ int main(int argc, char **argv)
        {"compare", ASN1_COMPARE}
    };

    if (p != NULL && strcmp(p, "on") == 0)
        CRYPTO_set_mem_debug(1);
    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);

    if (argc != 4) {
        fprintf(stderr,
                "Usage: d2i_test item_name expected_error file.der\n");
@@ -169,12 +164,5 @@ int main(int argc, char **argv)

    ADD_TEST(test_bad_asn1);

    result = run_tests(argv[0]);

#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (CRYPTO_mem_leaks_fp(stderr) <= 0)
        result = 1;
#endif

    return result;
    return run_tests(argv[0]);
}
Loading