From ced883d92c0568cdb15b5b67106c29a4623b19d8 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Tue, 13 May 2025 10:40:25 +0200 Subject: test: slre: add tests for regex library Inspecting the slre.c code reveals a few bugs; those are easy to demonstrate with the new '=~' test operator. Before fixing them, let's add a place to add test cases. Reviewed-by: Simon Glass Signed-off-by: Rasmus Villemoes --- test/lib/Makefile | 1 + test/lib/slre.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 test/lib/slre.c (limited to 'test/lib') 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/slre.c b/test/lib/slre.c new file mode 100644 index 00000000000..51a50b269aa --- /dev/null +++ b/test/lib/slre.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +#include +#include +#include + +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}, + {} +}; + +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); -- cgit v1.3.1 From 09b48305d3b6492553982df75e3a9f99d1f856d0 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Tue, 13 May 2025 10:40:27 +0200 Subject: test: slre: add more test cases Add some tests for the "drop wrong anchored optimization". Without the previous commit, the first, fifth and seventh of these would fail, i.e. those: { "xby", "^a|b", 1}, { "", "x*$", 1}, { "yy", "x*$", 1}, Reviewed-by: Simon Glass Signed-off-by: Rasmus Villemoes --- test/lib/slre.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/lib') diff --git a/test/lib/slre.c b/test/lib/slre.c index 51a50b269aa..b76d33475dd 100644 --- a/test/lib/slre.c +++ b/test/lib/slre.c @@ -15,6 +15,14 @@ static const struct re_test re_test[] = { { "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}, {} }; -- cgit v1.3.1 From ebdd78c487f6599f693e53e707606d7295f20f6e Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Tue, 13 May 2025 10:40:28 +0200 Subject: test: slre: add some (negative) character class tests Reviewed-by: Simon Glass Signed-off-by: Rasmus Villemoes --- test/lib/slre.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'test/lib') diff --git a/test/lib/slre.c b/test/lib/slre.c index b76d33475dd..9b41ea92f38 100644 --- a/test/lib/slre.c +++ b/test/lib/slre.c @@ -23,6 +23,9 @@ static const struct re_test re_test[] = { { "", "^x*$", 1}, { "yy", "x*$", 1}, { "yy", "^x*$", 0}, + { "Gadsby", "^[^eE]*$", 1}, + { "Ernest", "^[^eE]*$", 0}, + { "6d41f0a39d6", "^[0123456789abcdef]*$", 1 }, {} }; -- cgit v1.3.1 From 4d08883556b588bc1e6ef392349c51eceb550829 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Tue, 13 May 2025 10:40:31 +0200 Subject: test: slre: add test cases for escape char in character class Reviewed-by: Simon Glass Signed-off-by: Rasmus Villemoes --- test/lib/slre.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'test/lib') diff --git a/test/lib/slre.c b/test/lib/slre.c index 9b41ea92f38..053d046075e 100644 --- a/test/lib/slre.c +++ b/test/lib/slre.c @@ -26,6 +26,9 @@ static const struct re_test re_test[] = { { "Gadsby", "^[^eE]*$", 1}, { "Ernest", "^[^eE]*$", 0}, { "6d41f0a39d6", "^[0123456789abcdef]*$", 1 }, + /* DIGIT is 17 */ + { "##\x11%%\x11", "^[#%\\d]*$", 0 }, + { "##23%%45", "^[#%\\d]*$", 1 }, {} }; -- cgit v1.3.1 From de6e54d74dec9da5d7b8572d5a0711c7280eae2c Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Tue, 13 May 2025 10:40:33 +0200 Subject: test: slre: add tests for character ranges The first of these, { "U-Boot", "^[B-Uo-t]*$", 0 }, would match previously when the - and the letters were all interpreted literally. Reviewed-by: Simon Glass Signed-off-by: Rasmus Villemoes --- test/lib/slre.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/lib') diff --git a/test/lib/slre.c b/test/lib/slre.c index 053d046075e..ff2386d614a 100644 --- a/test/lib/slre.c +++ b/test/lib/slre.c @@ -29,6 +29,14 @@ static const struct re_test re_test[] = { /* 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 }, {} }; -- cgit v1.3.1