diff options
author | Alejandro Colomar <alx@kernel.org> | 2023-10-20 14:58:29 +0200 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2023-11-13 12:45:55 +0100 |
commit | 96d4d5c6fd874b7b55c3ddf94b1d552f6896a75c (patch) | |
tree | e726015a148c19feef22ab4425e86c9b4bb6db0f | |
parent | ee3a79c6952f8ca649c286c7f76639d9d1dedaad (diff) |
tests/unit/test_strlcpy.c: Test strlcpy_() and STRLCPY()
This test fails now, due to a bug: the return type of strlcpy_() is
size_t, but it should be ssize_t. The next commit will pass the test,
by fixing the bug.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | tests/unit/Makefile.am | 16 | ||||
-rw-r--r-- | tests/unit/test_strlcpy.c | 67 |
2 files changed, 83 insertions, 0 deletions
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 2ee14497..e209f041 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -4,6 +4,7 @@ if HAVE_CMOCKA TESTS = $(check_PROGRAMS) check_PROGRAMS = \ + test_strlcpy \ test_xasprintf if ENABLE_LOGIND @@ -31,6 +32,21 @@ test_logind_LDADD = \ $(LIBSYSTEMD) \ $(NULL) +test_strlcpy_SOURCES = \ + ../../lib/strlcpy.c \ + test_strlcpy.c \ + $(NULL) +test_strlcpy_CFLAGS = \ + $(AM_FLAGS) \ + $(LIBBSD_CFLAGS) \ + $(NULL) +test_strlcpy_LDFLAGS = \ + $(NULL) +test_strlcpy_LDADD = \ + $(CMOCKA_LIBS) \ + $(LIBBSD_LIBS) \ + $(NULL) + test_xasprintf_SOURCES = \ ../../lib/sprintf.c \ test_xasprintf.c \ diff --git a/tests/unit/test_strlcpy.c b/tests/unit/test_strlcpy.c new file mode 100644 index 00000000..1c8aa8f6 --- /dev/null +++ b/tests/unit/test_strlcpy.c @@ -0,0 +1,67 @@ +/* + * SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org> + * SPDX-License-Identifier: BSD-3-Clause + */ + + +#include <config.h> + +#include <string.h> + +#include <stdarg.h> // Required by <cmocka.h> +#include <stddef.h> // Required by <cmocka.h> +#include <setjmp.h> // Required by <cmocka.h> +#include <stdint.h> // Required by <cmocka.h> +#include <cmocka.h> + +#include "sizeof.h" +#include "strlcpy.h" + + +static void test_STRLCPY_trunc(void **state); +static void test_STRLCPY_ok(void **state); + + +int +main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_STRLCPY_trunc), + cmocka_unit_test(test_STRLCPY_ok), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} + + +static void +test_STRLCPY_trunc(void **state) +{ + char buf[NITEMS("foo")]; + + // Test that we're not returning SIZE_MAX + assert_true(STRLCPY(buf, "fooo") < 0); + assert_string_equal(buf, "foo"); + + assert_int_equal(STRLCPY(buf, "barbaz"), -1); + assert_string_equal(buf, "bar"); +} + + +static void +test_STRLCPY_ok(void **state) +{ + char buf[NITEMS("foo")]; + + assert_int_equal(STRLCPY(buf, "foo"), strlen("foo")); + assert_string_equal(buf, "foo"); + + assert_int_equal(STRLCPY(buf, "fo"), strlen("fo")); + assert_string_equal(buf, "fo"); + + assert_int_equal(STRLCPY(buf, "f"), strlen("f")); + assert_string_equal(buf, "f"); + + assert_int_equal(STRLCPY(buf, ""), strlen("")); + assert_string_equal(buf, ""); +} |