diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-26 16:18:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-26 16:18:36 +0000 |
commit | 6c3ea4f47ea280811a7fe53a22f7832e4533c9ec (patch) | |
tree | 3d7ed5da23b5dbf6f9e450dfb61642832249c31e /tests/unit/test_strtcpy.c | |
parent | Adding upstream version 1:4.13+dfsg1. (diff) | |
download | shadow-d2499e3c1ba992b56cb2e482d5518e927c589d46.tar.xz shadow-d2499e3c1ba992b56cb2e482d5518e927c589d46.zip |
Adding upstream version 1:4.15.2.upstream/1%4.15.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/unit/test_strtcpy.c')
-rw-r--r-- | tests/unit/test_strtcpy.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/unit/test_strtcpy.c b/tests/unit/test_strtcpy.c new file mode 100644 index 0000000..12351a5 --- /dev/null +++ b/tests/unit/test_strtcpy.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 "string/strtcpy.h" + + +static void test_STRTCPY_trunc(void **state); +static void test_STRTCPY_ok(void **state); + + +int +main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_STRTCPY_trunc), + cmocka_unit_test(test_STRTCPY_ok), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} + + +static void +test_STRTCPY_trunc(void **state) +{ + char buf[NITEMS("foo")]; + + // Test that we're not returning SIZE_MAX + assert_true(STRTCPY(buf, "fooo") < 0); + assert_string_equal(buf, "foo"); + + assert_int_equal(STRTCPY(buf, "barbaz"), -1); + assert_string_equal(buf, "bar"); +} + + +static void +test_STRTCPY_ok(void **state) +{ + char buf[NITEMS("foo")]; + + assert_int_equal(STRTCPY(buf, "foo"), strlen("foo")); + assert_string_equal(buf, "foo"); + + assert_int_equal(STRTCPY(buf, "fo"), strlen("fo")); + assert_string_equal(buf, "fo"); + + assert_int_equal(STRTCPY(buf, "f"), strlen("f")); + assert_string_equal(buf, "f"); + + assert_int_equal(STRTCPY(buf, ""), strlen("")); + assert_string_equal(buf, ""); +} |