From: Stephan Bosch Date: Fri, 25 Jul 2025 01:31:44 +0000 (+0200) Subject: lib-test: Add test_assert_memcmp*() X-Git-Tag: 2.4.2~630 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4677492d038f5187363790b5a93bd5fccc989a98;p=thirdparty%2Fdovecot%2Fcore.git lib-test: Add test_assert_memcmp*() --- diff --git a/src/lib-test/test-common.c b/src/lib-test/test-common.c index 2a29eb02de..e556bf0ad2 100644 --- a/src/lib-test/test-common.c +++ b/src/lib-test/test-common.c @@ -84,6 +84,39 @@ void test_assert_failed_strcmp_idx(const char *code, const char *file, unsigned #endif } +static void print_memcmp_data(const void *_data, size_t len) +{ + const unsigned char *data = _data; + size_t i; + + for (i = 0; i < len; i++) { + if (data[i] >= 0x20 && data[i] < 0x7f) + printf("%c", data[i]); + else + printf("\\x%02X", data[i]); + } +} + +void test_assert_failed_memcmp_idx(const char *code, const char *file, unsigned int line, + const void *src, const void *dst, size_t len, long long i) +{ + printf("%s:%u: Assert", file, line); + if (i == LLONG_MIN) + printf(" failed: %s\n", code); + else + printf("(#%lld) failed: %s\n", i, code); + printf(" \""); + print_memcmp_data(src, len); + printf("\" != \""); + print_memcmp_data(dst, len); + printf("\" (len == %zu)", len); + fflush(stdout); + test_success = FALSE; +#ifdef STATIC_CHECKER + i_unreached(); +#endif +} + void test_assert_failed_cmp_intmax_idx(const char *code, const char *file, unsigned int line, intmax_t src, intmax_t dst, diff --git a/src/lib-test/test-common.h b/src/lib-test/test-common.h index 5e3e5e43fa..2f06d67b13 100644 --- a/src/lib-test/test-common.h +++ b/src/lib-test/test-common.h @@ -40,7 +40,14 @@ void test_begin(const char *name); test_assert_strcmp_idx(s1, s2, LLONG_MIN); \ } STMT_END -/* Same as test_assert_strcmp expect that it takes an additional i as input. +/* Additional parameters are m1 (source) and m2 (destination) memory and len + * in memcmp(). + */ +#define test_assert_memcmp(m1, m2, len) STMT_START { \ + test_assert_memcmp_idx(m1, m2, len, LLONG_MIN); \ + } STMT_END + +/* Same as test_assert_strcmp except that it takes an additional i as input. * When i is greater than or equals 0 it is used to identify the barrage of * tests failed like in test_assert_idx. */ @@ -52,6 +59,19 @@ void test_begin(const char *name); __FILE__, __LINE__, _temp_s1, _temp_s2, i); \ } STMT_END +/* Same as test_assert_memcmp except that it takes an additional i as input. + * When i is greater than or equals 0 it is used to identify the barrage of + * tests failed like in test_assert_idx. +*/ +#define test_assert_memcmp_idx(_m1, _m2, _len, i) STMT_START { \ + const void *_temp_m1 = (_m1); \ + const void *_temp_m2 = (_m2); \ + const size_t _temp_len = (_len); \ + if ((memcmp(_temp_m1,_temp_m2, _temp_len) != 0)) \ + test_assert_failed_memcmp_idx("memcmp(" #_m1 "," #_m2 "," #_len ")", \ + __FILE__, __LINE__, _temp_m1, _temp_m2, _temp_len, i); \ + } STMT_END + #define test_assert_cmp_bool(_bool_value1, _op, _value2) \ test_assert_cmp((unsigned int) _bool_value1, _op, (unsigned int _bool_value2)) @@ -92,6 +112,9 @@ void test_assert_failed_idx(const char *code, const char *file, unsigned int lin void test_assert_failed_strcmp_idx(const char *code, const char *file, unsigned int line, const char *src, const char *dst, long long i) ATTR_STATIC_CHECKER_NORETURN; +void test_assert_failed_memcmp_idx(const char *code, const char *file, unsigned int line, + const void *src, const void *dst, size_t len, long long i) + ATTR_STATIC_CHECKER_NORETURN; void test_assert_failed_cmp_intmax_idx(const char *code, const char *file, unsigned int line, intmax_t src, intmax_t dst,