summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2024-02-04 10:31:05 +0100
committerAndreas Schneider <asn@cryptomilk.org>2024-02-05 09:20:41 +0100
commite48a664e7570691f16b79bb3b70abb21e88ec91e (patch)
tree08d33b418129ac44b2b233ac7fa81da206d283e9
parent371497b419a940283c239f9160d89067172a016a (diff)
downloadcmocka-e48a664e7570691f16b79bb3b70abb21e88ec91e.zip
cmocka-e48a664e7570691f16b79bb3b70abb21e88ec91e.tar.gz
cmocka-e48a664e7570691f16b79bb3b70abb21e88ec91e.tar.bz2
cmocka: Add all_zero() function and test
Reviewed-by: Joseph Sutton <jsutton@samba.org>
-rw-r--r--src/cmocka.c8
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/test_buffer.c48
3 files changed, 57 insertions, 0 deletions
diff --git a/src/cmocka.c b/src/cmocka.c
index 0cbc342..ca67fd5 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -1636,6 +1636,14 @@ static int string_not_equal_display_error(
return 0;
}
+static bool all_zero(const uint8_t *buf, size_t len)
+{
+ if (buf == NULL || len == 0) {
+ return true;
+ }
+
+ return buf[0] == '\0' && memcmp(buf, buf + 1, len - 1) == 0;
+}
/*
* Determine whether the specified areas of memory are equal. If they're equal
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b048637..01ebf31 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -17,6 +17,7 @@ endif()
set(CMOCKA_TESTS
test_alloc
+ test_buffer
test_expect_check
test_expect_u_int_in_set
test_expect_check_fail
diff --git a/tests/test_buffer.c b/tests/test_buffer.c
new file mode 100644
index 0000000..00627bd
--- /dev/null
+++ b/tests/test_buffer.c
@@ -0,0 +1,48 @@
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <cmocka.h>
+#include <cmocka_private.h>
+
+#include "../src/cmocka.c"
+
+static const uint8_t buf0[] = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+
+static const uint8_t buf1[] = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+
+static void test_all_zero(void **state)
+{
+ (void)state;
+
+ assert_true(all_zero(buf0, sizeof(buf0)));
+
+ assert_true(all_zero(NULL, 0));
+
+ assert_true(all_zero(buf0, 0));
+
+ assert_false(all_zero(buf1, sizeof(buf1)));
+}
+
+int main(int argc, char **argv) {
+ const struct CMUnitTest buffer_tests[] = {
+ cmocka_unit_test(test_all_zero),
+ };
+
+ (void)argc;
+ (void)argv;
+
+ return cmocka_run_group_tests(buffer_tests, NULL, NULL);
+}