diff options
Diffstat (limited to 'test/lib')
-rw-r--r-- | test/lib/Makefile | 1 | ||||
-rw-r--r-- | test/lib/abuf.c | 105 | ||||
-rw-r--r-- | test/lib/slre.c | 58 | ||||
-rw-r--r-- | test/lib/string.c | 37 |
4 files changed, 201 insertions, 0 deletions
diff --git a/test/lib/Makefile b/test/lib/Makefile index d620510f998..ff4ff63270d 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -29,6 +29,7 @@ obj-$(CONFIG_SHA256) += test_sha256_hmac.o obj-$(CONFIG_HKDF_MBEDTLS) += test_sha256_hkdf.o obj-$(CONFIG_GETOPT) += getopt.o obj-$(CONFIG_CRC8) += test_crc8.o +obj-$(CONFIG_REGEX) += slre.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o obj-$(CONFIG_UT_TIME) += time.o obj-$(CONFIG_$(PHASE_)UT_UNICODE) += unicode.o diff --git a/test/lib/abuf.c b/test/lib/abuf.c index b38690fe1a9..97b128c01c0 100644 --- a/test/lib/abuf.c +++ b/test/lib/abuf.c @@ -419,3 +419,108 @@ static int lib_test_abuf_init(struct unit_test_state *uts) return 0; } LIB_TEST(lib_test_abuf_init, 0); + +/* Test abuf_copy() */ +static int lib_test_abuf_copy(struct unit_test_state *uts) +{ + struct abuf buf, copy; + ulong start; + + start = ut_check_free(); + + abuf_init_set(&buf, test_data, TEST_DATA_LEN); + ut_assert(abuf_copy(&buf, ©)); + ut_asserteq(buf.size, copy.size); + ut_assert(buf.data != copy.data); + ut_assert(copy.alloced); + abuf_uninit(©); + abuf_uninit(&buf); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_abuf_copy, 0); + +/* Test abuf_init_size() */ +static int lib_test_abuf_init_size(struct unit_test_state *uts) +{ + struct abuf buf; + ulong start; + + start = ut_check_free(); + + ut_assert(abuf_init_size(&buf, TEST_DATA_LEN)); + ut_assertnonnull(buf.data); + ut_asserteq(TEST_DATA_LEN, buf.size); + ut_asserteq(true, buf.alloced); + abuf_uninit(&buf); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_abuf_init_size, 0); + +/* Test abuf_printf() */ +static int lib_test_abuf_printf(struct unit_test_state *uts) +{ + struct abuf buf, fmt; + ulong start; + char *ptr; + + start = ut_check_free(); + + /* start with a fresh buffer */ + abuf_init(&buf); + + /* check handling of out-of-memory condition */ + malloc_enable_testing(0); + ut_asserteq(-ENOMEM, abuf_printf(&buf, "%s", "")); + malloc_enable_testing(1); + + ut_asserteq(0, abuf_printf(&buf, "%s", "")); + ut_asserteq(1, buf.size); + ut_asserteq(true, buf.alloced); + ut_asserteq_str("", buf.data); + + /* check expanding it, initially failing */ + ut_asserteq(-ENOMEM, abuf_printf(&buf, "%s", "testing")); + malloc_disable_testing(); + + ut_asserteq(7, abuf_printf(&buf, "%s", "testing")); + ut_asserteq(8, buf.size); + ut_asserteq_str("testing", buf.data); + + ut_asserteq(11, abuf_printf(&buf, "testing %d", 123)); + ut_asserteq(12, buf.size); + ut_asserteq_str("testing 123", buf.data); + + /* make it smaller; buffer should not shrink */ + ut_asserteq(9, abuf_printf(&buf, "test %d", 456)); + ut_asserteq(12, buf.size); + ut_asserteq_str("test 456", buf.data); + + /* test the maximum size */ + abuf_init(&fmt); + ut_assert(abuf_realloc(&fmt, 4100)); + memset(fmt.data, 'x', 4100); + ptr = fmt.data; + ptr[4096] = '\0'; + + /* we are allowed up to 4K including the terminator */ + ut_asserteq(-E2BIG, abuf_printf(&buf, "%s", ptr)); + ptr[4095] = '\0'; + ut_asserteq(4095, abuf_printf(&buf, "%s", ptr)); + + abuf_uninit(&fmt); + abuf_uninit(&buf); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_abuf_printf, 0); diff --git a/test/lib/slre.c b/test/lib/slre.c new file mode 100644 index 00000000000..ff2386d614a --- /dev/null +++ b/test/lib/slre.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +#include <test/lib.h> +#include <test/ut.h> +#include <slre.h> + +struct re_test { + const char *str; + const char *re; + int match; +}; + +static const struct re_test re_test[] = { + { "123", "^\\d+$", 1}, + { "x23", "^\\d+$", 0}, + { "banana", "^([bn]a)*$", 1}, + { "panama", "^([bn]a)*$", 0}, + { "xby", "^a|b", 1}, + { "xby", "b|^a", 1}, + { "xby", "b|c$", 1}, + { "xby", "c$|b", 1}, + { "", "x*$", 1}, + { "", "^x*$", 1}, + { "yy", "x*$", 1}, + { "yy", "^x*$", 0}, + { "Gadsby", "^[^eE]*$", 1}, + { "Ernest", "^[^eE]*$", 0}, + { "6d41f0a39d6", "^[0123456789abcdef]*$", 1 }, + /* DIGIT is 17 */ + { "##\x11%%\x11", "^[#%\\d]*$", 0 }, + { "##23%%45", "^[#%\\d]*$", 1 }, + { "U-Boot", "^[B-Uo-t]*$", 0 }, + { "U-Boot", "^[A-Zm-v-]*$", 1 }, + { "U-Boot", "^[-A-Za-z]*$", 1 }, + /* The range --C covers both - and B. */ + { "U-Boot", "^[--CUot]*$", 1 }, + { "U-Boot", "^[^0-9]*$", 1 }, + { "U-Boot", "^[^0-9<->]*$", 1 }, + { "U-Boot", "^[^0-9<\\->]*$", 0 }, + {} +}; + +static int lib_slre(struct unit_test_state *uts) +{ + const struct re_test *t; + + for (t = re_test; t->str; t++) { + struct slre slre; + + ut_assert(slre_compile(&slre, t->re)); + ut_assertf(!!slre_match(&slre, t->str, strlen(t->str), NULL) == t->match, + "'%s' unexpectedly %s '%s'\n", t->str, + t->match ? "didn't match" : "matched", t->re); + } + + return 0; +} +LIB_TEST(lib_slre, 0); diff --git a/test/lib/string.c b/test/lib/string.c index 31391a387b9..f56c2e4c946 100644 --- a/test/lib/string.c +++ b/test/lib/string.c @@ -261,3 +261,40 @@ static int lib_strstr(struct unit_test_state *uts) return 0; } LIB_TEST(lib_strstr, 0); + +static int lib_strim(struct unit_test_state *uts) +{ + char buf[BUFLEN], *p; + + strcpy(buf, "abc"); + ut_asserteq_str("abc", strim(buf)); + + /* leading space */ + strcpy(buf, " abc"); + ut_asserteq_str("abc", strim(buf)); + + /* multiple leading spaces */ + strcpy(buf, " abc"); + ut_asserteq_str("abc", strim(buf)); + + /* multiple internal spaces */ + strcpy(buf, " a bc"); + ut_asserteq_str("a bc", strim(buf)); + + /* with trailing space */ + strcpy(buf, " a bc "); + ut_asserteq_str("a bc", strim(buf)); + + /* with multiple trailing spaces */ + strcpy(buf, " a bc "); + ut_asserteq_str("a bc", strim(buf)); + + /* with only spaces */ + strcpy(buf, " "); + p = strim(buf); + ut_asserteq_ptr(p, buf); + ut_asserteq_str("", p); + + return 0; +} +LIB_TEST(lib_strim, 0); |