From 401465d8ea07ca3be6c9e829df034feb14f4538e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Tue, 15 Mar 2016 14:29:44 +0100 Subject: [PATCH] Move main() from libcryb-test to the test program. Instead of having libcryb-test provide main() and assume that the test program defines t_prepare() and t_cleanup(), have libcryb-test provide a t_main() function which the test program calls with pointers to its prepare and cleanup functions. --- include/cryb/test.h | 6 ++++-- lib/test/t_main.c | 11 +++++++---- t/t__string.c | 8 +++++--- t/t_adler.c | 8 +++++--- t/t_aes.c | 8 +++++--- t/t_ctype.c | 8 +++++--- t/t_endian.c | 8 +++++--- t/t_fletcher.c | 8 +++++--- t/t_hmac_sha1.c | 8 +++++--- t/t_hmac_sha224.c | 8 +++++--- t/t_hmac_sha256.c | 8 +++++--- t/t_hmac_sha384.c | 8 +++++--- t/t_hmac_sha512.c | 8 +++++--- t/t_md2.c | 8 +++++--- t/t_md4.c | 8 +++++--- t/t_md5.c | 8 +++++--- t/t_memset_s.c | 8 +++++--- t/t_mpi.c | 11 +++++++++-- t/t_murmur3_32.c | 8 +++++--- t/t_pearson.c | 8 +++++--- t/t_rc4.c | 8 +++++--- t/t_rfc3986.c | 17 +++++++++-------- t/t_rfc4648.c | 17 +++++++++-------- t/t_sha1.c | 8 +++++--- t/t_sha224.c | 8 +++++--- t/t_sha256.c | 8 +++++--- t/t_sha384.c | 8 +++++--- t/t_sha512.c | 8 +++++--- t/t_strlcat.c | 8 +++++--- t/t_strlcmp.c | 8 +++++--- t/t_strlcpy.c | 8 +++++--- 31 files changed, 168 insertions(+), 102 deletions(-) diff --git a/include/cryb/test.h b/include/cryb/test.h index 97ec0f3..9e3aac1 100644 --- a/include/cryb/test.h +++ b/include/cryb/test.h @@ -53,8 +53,10 @@ extern const char *t_progname; void t_add_test(t_func *, void *, const char *, ...); void t_add_tests(struct t_test *, int); -int t_prepare(int, char **); -void t_cleanup(void); +typedef int (*t_prepare_func)(int, char **); +typedef void (*t_cleanup_func)(void); +void t_main(t_prepare_func, t_cleanup_func, int, char **) + CRYB_NORETURN; void t_verbose_hex(const uint8_t *, size_t); void t_verbose(const char *, ...) diff --git a/lib/test/t_main.c b/lib/test/t_main.c index a27e8ce..dae1658 100644 --- a/lib/test/t_main.c +++ b/lib/test/t_main.c @@ -221,8 +221,9 @@ usage(void) exit(1); } -int -main(int argc, char *argv[]) +void +t_main(t_prepare_func t_prepare, t_cleanup_func t_cleanup, + int argc, char *argv[]) { unsigned int n, pass, fail; size_t nt; @@ -270,7 +271,8 @@ main(int argc, char *argv[]) argv += optind; /* prepare the test plan */ - t_prepare(argc, argv); + if (t_prepare != NULL) + t_prepare(argc, argv); if (t_plan_len == 0) errx(1, "no plan\n"); @@ -281,7 +283,8 @@ main(int argc, char *argv[]) t_run_test(t_plan[n], n + 1) ? ++pass : ++fail; /* clean up as much as possible before we exit */ - t_cleanup(); + if (t_cleanup != NULL) + t_cleanup(); t_fcloseall(); for (n = 0; n < t_plan_len; ++n) { free(t_plan[n]->desc); diff --git a/t/t__string.c b/t/t__string.c index f15b672..1592865 100644 --- a/t/t__string.c +++ b/t/t__string.c @@ -271,7 +271,7 @@ t_string_trunc(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { unsigned int i; @@ -299,7 +299,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_adler.c b/t/t_adler.c index 5f8f6e2..3642d95 100644 --- a/t/t_adler.c +++ b/t/t_adler.c @@ -132,7 +132,7 @@ t_adler32(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { unsigned int i, n; @@ -147,7 +147,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_aes.c b/t/t_aes.c index b81c21f..876f0a7 100644 --- a/t/t_aes.c +++ b/t/t_aes.c @@ -139,7 +139,7 @@ t_aes_dec(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { unsigned int i, n; @@ -154,7 +154,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_ctype.c b/t/t_ctype.c index ec706ac..2b6f1c8 100644 --- a/t/t_ctype.c +++ b/t/t_ctype.c @@ -95,7 +95,7 @@ T_OC(pfcs) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { @@ -113,7 +113,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_endian.c b/t/t_endian.c index bf93dc6..c6896b1 100644 --- a/t/t_endian.c +++ b/t/t_endian.c @@ -91,7 +91,7 @@ T_ENC(le, 64) #define T_ENC_ADD(e, w) \ t_add_test(t_##e##w##enc, 0, #e #w "enc"); -int +static int t_prepare(int argc, char *argv[]) { @@ -112,7 +112,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_fletcher.c b/t/t_fletcher.c index 725e8be..b3b2de5 100644 --- a/t/t_fletcher.c +++ b/t/t_fletcher.c @@ -279,7 +279,7 @@ t_fletcher64(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { unsigned int i, n; @@ -307,7 +307,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_hmac_sha1.c b/t/t_hmac_sha1.c index 12f30ba..fe8d2d2 100644 --- a/t/t_hmac_sha1.c +++ b/t/t_hmac_sha1.c @@ -157,7 +157,7 @@ t_hmac_sha1_vector(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -171,7 +171,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_hmac_sha224.c b/t/t_hmac_sha224.c index a732386..7640c3e 100644 --- a/t/t_hmac_sha224.c +++ b/t/t_hmac_sha224.c @@ -162,7 +162,7 @@ t_hmac_sha224_vector(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -176,7 +176,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_hmac_sha256.c b/t/t_hmac_sha256.c index ff43ad7..7f72758 100644 --- a/t/t_hmac_sha256.c +++ b/t/t_hmac_sha256.c @@ -162,7 +162,7 @@ t_hmac_sha256_vector(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -176,7 +176,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_hmac_sha384.c b/t/t_hmac_sha384.c index 10a7204..3d70eb9 100644 --- a/t/t_hmac_sha384.c +++ b/t/t_hmac_sha384.c @@ -172,7 +172,7 @@ t_hmac_sha384_vector(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -186,7 +186,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_hmac_sha512.c b/t/t_hmac_sha512.c index 027bb58..f9514b3 100644 --- a/t/t_hmac_sha512.c +++ b/t/t_hmac_sha512.c @@ -182,7 +182,7 @@ t_hmac_sha512_vector(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -196,7 +196,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_md2.c b/t/t_md2.c index 2d87520..c55b9e3 100644 --- a/t/t_md2.c +++ b/t/t_md2.c @@ -192,7 +192,7 @@ t_md2_perf(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -229,7 +229,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_md4.c b/t/t_md4.c index 9bc892d..c001361 100644 --- a/t/t_md4.c +++ b/t/t_md4.c @@ -208,7 +208,7 @@ t_md4_perf(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -245,7 +245,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_md5.c b/t/t_md5.c index a4a5972..e926e51 100644 --- a/t/t_md5.c +++ b/t/t_md5.c @@ -208,7 +208,7 @@ t_md5_perf(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -245,7 +245,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_memset_s.c b/t/t_memset_s.c index de39197..ed856ce 100644 --- a/t/t_memset_s.c +++ b/t/t_memset_s.c @@ -128,7 +128,7 @@ t_memset_s_null(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -142,7 +142,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_mpi.c b/t/t_mpi.c index b63f5fb..aedb6e2 100644 --- a/t/t_mpi.c +++ b/t/t_mpi.c @@ -1554,7 +1554,7 @@ t_mpi_sub_b_from_zero(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { unsigned int i; @@ -1651,9 +1651,16 @@ t_prepare(int argc, char *argv[]) return (0); } -void +static void t_cleanup(void) { assert(memcmp(&z, t_zero, sizeof z) == 0); } + +int +main(int argc, char *argv[]) +{ + + t_main(t_prepare, t_cleanup, argc, argv); +} diff --git a/t/t_murmur3_32.c b/t/t_murmur3_32.c index 5e924b3..fedd000 100644 --- a/t/t_murmur3_32.c +++ b/t/t_murmur3_32.c @@ -195,7 +195,7 @@ t_murmur3_32(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -208,7 +208,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_pearson.c b/t/t_pearson.c index 21313ca..ee6a979 100644 --- a/t/t_pearson.c +++ b/t/t_pearson.c @@ -92,7 +92,7 @@ t_pearson(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -105,7 +105,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_rc4.c b/t/t_rc4.c index 406e047..22f5c65 100644 --- a/t/t_rc4.c +++ b/t/t_rc4.c @@ -1987,7 +1987,7 @@ t_rc4(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { unsigned int i, n; @@ -2000,7 +2000,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_rfc3986.c b/t/t_rfc3986.c index e275fae..e887a5a 100644 --- a/t/t_rfc3986.c +++ b/t/t_rfc3986.c @@ -323,10 +323,12 @@ t_rfc3986(char **desc CRYB_UNUSED, void *arg) return (ret); } -/* - * Generate the test plan + +/*************************************************************************** + * Boilerplate */ -int + +static int t_prepare(int argc, char *argv[]) { struct t_case *t; @@ -354,10 +356,9 @@ t_prepare(int argc, char *argv[]) return (0); } -/* - * Cleanup - */ -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_rfc4648.c b/t/t_rfc4648.c index 3a5b3e3..e5bde13 100644 --- a/t/t_rfc4648.c +++ b/t/t_rfc4648.c @@ -299,10 +299,12 @@ t_rfc4648(char **desc CRYB_UNUSED, void *arg) return (1); } -/* - * Generate the test plan + +/*************************************************************************** + * Boilerplate */ -int + +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -315,10 +317,9 @@ t_prepare(int argc, char *argv[]) return (0); } -/* - * Cleanup - */ -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_sha1.c b/t/t_sha1.c index 602993d..7dbc8ae 100644 --- a/t/t_sha1.c +++ b/t/t_sha1.c @@ -201,7 +201,7 @@ t_sha1_perf(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -238,7 +238,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_sha224.c b/t/t_sha224.c index 66c16d2..3134917 100644 --- a/t/t_sha224.c +++ b/t/t_sha224.c @@ -210,7 +210,7 @@ t_sha224_perf(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -247,7 +247,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_sha256.c b/t/t_sha256.c index 1c098ff..a00f9fa 100644 --- a/t/t_sha256.c +++ b/t/t_sha256.c @@ -206,7 +206,7 @@ t_sha256_perf(char **desc, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -243,7 +243,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_sha384.c b/t/t_sha384.c index dd72aff..a0c9cf8 100644 --- a/t/t_sha384.c +++ b/t/t_sha384.c @@ -254,7 +254,7 @@ t_sha384_carry(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -292,7 +292,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_sha512.c b/t/t_sha512.c index 716fd69..7fd27a7 100644 --- a/t/t_sha512.c +++ b/t/t_sha512.c @@ -266,7 +266,7 @@ t_sha512_carry(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -304,7 +304,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_strlcat.c b/t/t_strlcat.c index f0450de..12734ca 100644 --- a/t/t_strlcat.c +++ b/t/t_strlcat.c @@ -149,7 +149,7 @@ t_strlcat(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -162,7 +162,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_strlcmp.c b/t/t_strlcmp.c index a5fde0d..a5d1797 100644 --- a/t/t_strlcmp.c +++ b/t/t_strlcmp.c @@ -131,7 +131,7 @@ t_strlcmp(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -144,7 +144,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); } diff --git a/t/t_strlcpy.c b/t/t_strlcpy.c index defe9f6..f1f9c15 100644 --- a/t/t_strlcpy.c +++ b/t/t_strlcpy.c @@ -104,7 +104,7 @@ t_strlcpy(char **desc CRYB_UNUSED, void *arg) * Boilerplate */ -int +static int t_prepare(int argc, char *argv[]) { int i, n; @@ -117,7 +117,9 @@ t_prepare(int argc, char *argv[]) return (0); } -void -t_cleanup(void) +int +main(int argc, char *argv[]) { + + t_main(t_prepare, NULL, argc, argv); }