X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Ftestutil%2Ftests.c;h=057ad22b3749ce26110b40f515c894be90847d3c;hp=6dfe2424ef45169c79c47f02e5e0136eba2036d1;hb=ee6b68ce4c67870f9323d2a380eb949f447c56ee;hpb=603ddbdb7527710c293a762aa5eed51ad05646b3;ds=inline diff --git a/test/testutil/tests.c b/test/testutil/tests.c index 6dfe2424ef..057ad22b37 100644 --- a/test/testutil/tests.c +++ b/test/testutil/tests.c @@ -15,7 +15,7 @@ #include "../../e_os.h" /* The size of memory buffers to display on failure */ -#define MEM_BUFFER_SIZE (21) +#define MEM_BUFFER_SIZE (33) /* * A common routine to output test failure messages. Generally this should not @@ -49,19 +49,17 @@ static void test_fail_message(const char *prefix, const char *file, int line, static void test_fail_message_va(const char *prefix, const char *file, int line, const char *type, const char *fmt, va_list ap) { - test_printf_stderr("%*s# ", subtest_level(), ""); - test_puts_stderr(prefix != NULL ? prefix : "ERROR"); - test_puts_stderr(":"); + test_printf_stderr("%*s# %s: ", subtest_level(), "", + prefix != NULL ? prefix : "ERROR"); if (type) - test_printf_stderr(" (%s)", type); + test_printf_stderr("(%s)", type); if (fmt != NULL) { - test_puts_stderr(" "); test_vprintf_stderr(fmt, ap); } if (file != NULL) { test_printf_stderr(" @ %s:%d", file, line); } - test_puts_stderr("\n"); + test_printf_stderr("\n"); test_flush_stderr(); } @@ -111,6 +109,11 @@ void test_error(const char *file, int line, const char *desc, ...) va_end(ap); } +void test_openssl_errors(void) +{ + ERR_print_errors_cb(openssl_error_cb, NULL); +} + /* * Define some comparisons between pairs of various types. * These functions return 1 if the test is true. @@ -264,23 +267,35 @@ int test_strn_ne(const char *file, int line, const char *st1, const char *st2, /* * We could use OPENSSL_buf2hexstr() to do this but trying to allocate memory - * in a failure state isn't generally a great idea. + * in a failure state isn't generally a great idea and if it fails, we want a + * fall back position using caller supplied buffers. + * + * If the return value is different from the buffer supplied, it needs to be + * freed by the caller. */ -static const char *print_mem_maybe_null(const void *s, size_t n, - char out[MEM_BUFFER_SIZE]) +static char *print_mem_maybe_null(const void *s, size_t n, + char outbuf[MEM_BUFFER_SIZE]) { size_t i; const unsigned char *p = (const unsigned char *)s; - int pad = 2*n >= MEM_BUFFER_SIZE; + char *out = outbuf; + int pad = 2 * n >= MEM_BUFFER_SIZE; if (s == NULL) - return "(NULL)"; - if (pad) - n = MEM_BUFFER_SIZE-4; - - for (i=0; i<2*n; i++) { - unsigned char c = (i & 1) != 0 ? p[i / 2] & 15 : p[i / 2] >> 4; - out[i] = "0123456789abcdef"[c]; + return strcpy(outbuf, "(NULL)"); + if (pad) { + if ((out = OPENSSL_malloc(2 * n + 1)) == NULL) { + out = outbuf; + n = (MEM_BUFFER_SIZE - 4) / 2; + } else { + pad = 0; + } + } + + for (i = 0; i < 2 * n; ) { + const unsigned char c = *p++; + out[i++] = "0123456789abcdef"[c >> 4]; + out[i++] = "0123456789abcdef"[c & 15]; } if (pad) { out[i++] = '.'; @@ -288,7 +303,7 @@ static const char *print_mem_maybe_null(const void *s, size_t n, out[i++] = '.'; } out[i] = '\0'; - + return out; } @@ -299,18 +314,17 @@ int test_mem_eq(const char *file, int line, const char *st1, const char *st2, if (s1 == NULL && s2 == NULL) return 1; - if (n1 != n2) { - test_fail_message(NULL, file, line, "memory", - "size mismatch %s %s [%zu] != %s %s [%zu]", - st1, print_mem_maybe_null(s1, n1, b1), n1, - st2, print_mem_maybe_null(s2, n2, b2), n2); - return 0; - } - if (s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) { + if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) { + char *m1 = print_mem_maybe_null(s1, n1, b1); + char *m2 = print_mem_maybe_null(s2, n2, b2); + test_fail_message(NULL, file, line, "memory", - "%s %s [%zu] != %s %s [%zu]", - st1, print_mem_maybe_null(s1, n1, b1), n1, - st2, print_mem_maybe_null(s2, n2, b2), n2); + "%s %s [%zu] == %s %s [%zu]", + st1, m1, n1, st2, m2, n2); + if (m1 != b1) + OPENSSL_free(m1); + if (m2 != b2) + OPENSSL_free(m2); return 0; } return 1; @@ -322,14 +336,20 @@ int test_mem_ne(const char *file, int line, const char *st1, const char *st2, char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE]; if ((s1 == NULL) ^ (s2 == NULL)) - return 1; + return 1; if (n1 != n2) return 1; if (s1 == NULL || memcmp(s1, s2, n1) == 0) { + char *m1 = print_mem_maybe_null(s1, n1, b1); + char *m2 = print_mem_maybe_null(s2, n2, b2); + test_fail_message(NULL, file, line, "memory", "%s %s [%zu] != %s %s [%zu]", - st1, print_mem_maybe_null(s1, n1, b1), n1, - st2, print_mem_maybe_null(s2, n2, b2), n2); + st1, m1, n1, st2, m2, n2); + if (m1 != b1) + OPENSSL_free(m1); + if (m2 != b2) + OPENSSL_free(m2); return 0; } return 1;