cmocka: Add all_zero() function and test

Reviewed-by: Joseph Sutton <jsutton@samba.org>
This commit is contained in:
Andreas Schneider 2024-02-04 10:31:05 +01:00
parent 371497b419
commit e48a664e75
3 changed files with 57 additions and 0 deletions

View File

@ -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

View File

@ -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

48
tests/test_buffer.c Normal file
View File

@ -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);
}