]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-test: Add test_assert_memcmp*()
authorStephan Bosch <stephan.bosch@open-xchange.com>
Fri, 25 Jul 2025 01:31:44 +0000 (03:31 +0200)
committerStephan Bosch <stephan.bosch@open-xchange.com>
Fri, 1 Aug 2025 01:11:19 +0000 (03:11 +0200)
src/lib-test/test-common.c
src/lib-test/test-common.h

index 2a29eb02de10f12fd52e6d83196d57b1b8d148ba..e556bf0ad25384d67c36b3756e316f098ab33c69 100644 (file)
@@ -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,
index 5e3e5e43fa489765ceb5dc1e681f52f11d1c112d..2f06d67b13a60094daafcc7e4221d426a1a9e3bd 100644 (file)
@@ -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,