diff options
author | Yu-Ping Wu <yupingso@chromium.org> | 2024-08-08 17:20:05 +0800 |
---|---|---|
committer | Yu-Ping Wu <yupingso@google.com> | 2024-08-14 03:09:03 +0000 |
commit | 0dcdc0347c8d0405c1c6b444d23483dd9bf31d34 (patch) | |
tree | d5a585c35646db88b97be81a25ce756dfe5e759b /tests/commonlib/bsd/string-test.c | |
parent | 4ea4d82cec7ca3e66a10b2cd5bffe82d6854ff5d (diff) |
commonlib/bsd: Add strlen() and strnlen() functions
Add strlen() and strnlen() to commonlib/bsd by rewriting them from
scratch, and remove the same functions from coreboot and libpayload.
Note that in the existing libpayload implementation, these functions
return 0 for NULL strings. Given that POSIX doesn't require the NULL
check and that other major libc implementations (e.g. glibc [1]) don't
seem to do that, the new functions also don't perform the NULL check.
[1] https://github.com/bminor/glibc/blob/master/sysdeps/i386/strlen.c
Change-Id: I1203ec9affabe493bd14b46662d212b08240cced
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/83830
Reviewed-by: Maximilian Brune <maximilian.brune@9elements.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'tests/commonlib/bsd/string-test.c')
-rw-r--r-- | tests/commonlib/bsd/string-test.c | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/tests/commonlib/bsd/string-test.c b/tests/commonlib/bsd/string-test.c index d94b82e18c..29e3977675 100644 --- a/tests/commonlib/bsd/string-test.c +++ b/tests/commonlib/bsd/string-test.c @@ -1,24 +1,57 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include <commonlib/bsd/string.h> +#include <stddef.h> #include <tests/test.h> -/* Used to test skip_atoi */ -struct str_with_u_val_t { - char *str; - uint32_t value; - uint32_t offset; -} str_with_u_val[] = { - {"42aa", 42, 2}, - {"a", 0, 0}, - {"0", 0, 1}, - {"4a2", 4, 1}, -}; +static void test_strlen(void **state) +{ + const char *str; + + str = "coreboot"; + assert_int_equal(__builtin_strlen(str), strlen(str)); + + str = "is\0very"; + assert_int_equal(__builtin_strlen(str), strlen(str)); + + str = "nice\n"; + assert_int_equal(__builtin_strlen(str), strlen(str)); + + assert_int_equal(0, strlen("")); +} + +static void test_strnlen(void **state) +{ + /* maxlen larger than string len */ + assert_int_equal(8, strnlen("coreboot", 100)); + + /* maxlen equal to string len */ + assert_int_equal(8, strnlen("coreboot", 8)); + + /* maxlen smaller than string len */ + assert_int_equal(5, strnlen("coreboot", 5)); + + /* maxlen is 0 */ + assert_int_equal(0, strnlen("coreboot", 0)); + + /* Empty string */ + assert_int_equal(0, strnlen("", 3)); +} static void test_skip_atoi(void **state) { int i; char *ptr, *copy; + const struct str_with_u_val_t { + char *str; + uint32_t value; + uint32_t offset; + } str_with_u_val[] = { + {"42aa", 42, 2}, + {"a", 0, 0}, + {"0", 0, 1}, + {"4a2", 4, 1}, + }; for (i = 0; i < ARRAY_SIZE(str_with_u_val); i++) { ptr = str_with_u_val[i].str; @@ -31,6 +64,8 @@ static void test_skip_atoi(void **state) int main(void) { const struct CMUnitTest tests[] = { + cmocka_unit_test(test_strlen), + cmocka_unit_test(test_strnlen), cmocka_unit_test(test_skip_atoi), }; |