string: Convert selftests to KUnit
v2 cover string: Convert selftests to KUnit
Hi,
I realized the string selftests hadn't been converted to KUnit yet. Do that.
-Kees
v2: rebase onto https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/linux-hardening/[email protected]/
Kees Cook (2):
string: Convert selftest to KUnit
string: Convert helpers selftest to KUnit
MAINTAINERS | 4 +-
lib/Kconfig.debug | 12 +-
lib/Makefile | 4 +-
...tring_helpers.c => string_helpers_kunit.c} | 177 ++++++++++--------
lib/{test_string.c => string_kunit.c} | 166 ++++++----------
5 files changed, 164 insertions(+), 199 deletions(-)
rename lib/{test-string_helpers.c => string_helpers_kunit.c} (78%)
rename lib/{test_string.c => string_kunit.c} (54%)Signature · 1 line
2.34.1
PATCH v2 1/2 string: Convert selftest to KUnit
Convert test_string.c to KUnit so it can be easily run with everything else.
---
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]
---
MAINTAINERS | 2 +-
lib/Kconfig.debug | 6 +-
lib/Makefile | 2 +-
lib/{test_string.c => string_kunit.c} | 166 +++++++++-----------------
4 files changed, 61 insertions(+), 115 deletions(-)
rename lib/{test_string.c => string_kunit.c} (54%)MAINTAINERS +1 -1
@@ -8976,9 +8976,9 @@ F: include/linux/string.h F: include/linux/string_choices.h F: include/linux/string_helpers.h F: lib/string.c +F: lib/string_kunit.c F: lib/string_helpers.c F: lib/test-string_helpers.c -F: lib/test_string.c F: scripts/coccinelle/api/string_choices.cocci GENERIC UIO DRIVER FOR PCI DEVICES
lib/Kconfig.debug +4 -2
@@ -2352,8 +2352,10 @@ config ASYNC_RAID6_TEST config TEST_HEXDUMP tristate "Test functions located in the hexdump module at runtime" -config STRING_SELFTEST - tristate "Test string functions at runtime" +config STRING_KUNIT_TEST + tristate "KUnit test string functions at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS config TEST_STRING_HELPERS tristate "Test functions located in the string_helpers module at runtime"
lib/Makefile +1 -1
@@ -49,7 +49,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \ percpu-refcount.o rhashtable.o base64.o \ once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \ generic-radix-tree.o bitmap-str.o -obj-$(CONFIG_STRING_SELFTEST) += test_string.o +obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o obj-y += string_helpers.o obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o obj-y += hexdump.o
lib/string_kunit.c renamed from lib/test_string.c +55 -111
293 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 1/2] string: Convert selftest to KUnit
Quoted 3 lines · Kees Cook
Convert
test_string.cto KUnit so it can be easily run with everything else.
Have you run it?
...
lib/string_kunit.c static __init int memset16_selftest(void) · line 41
Quoted 4 lines
if (i < 256)lib/string_kunit.c renamed from lib/test_string.c +1
-2-return (i <<24 ) | (j <<16 ) | k |0x8000 ; -return 0 ; +KUNIT_EXPECT_EQ (test,0 , (i <<24 ) | (j <<16 ) | k |0x8000 );
First of all, this special value encodes the problematic patterns, so you missed proper messaging.
Second, the returned value has a constant, how do you expect 0 to be equal to something (guaranteed not to be 0)?
This needs a good rethink of what you should do in the KUnit approach.
...
Quoted 1 line
+
KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000);
Ditto.
...
Quoted 1 line
+
KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000);
Ditto.
...
lib/string_kunit.c static __init int strchr_selftest(void) · line 113
Quoted 5 lines
for (i = 0; i < strlen(test_string) + 1; i++) { result = strchr(test_string, test_string[i]);lib/string_kunit.c renamed from lib/test_string.c +1
-2-if (result - test_string != i) -return i +'a' ; +KUNIT_ASSERT_EQ (test, result - test_string, i);
In a similar way, all returned values are *special*, you really need to think about them before converting to a simple (and sometimes wrong) checks)
...
I dunno if KUnit has a fault ejection simulation. It should, in order to be sure that test cases are fine when they fail.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] string: Convert selftest to KUnit
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Kees Cook
Convert
test_string.cto KUnit so it can be easily run with everything else.Have you run it?
Yes:
$ ./tools/testing/kunit/kunit.py run string
[09:21:32] Starting KUnit Kernel (1/1)...
[09:21:32] ============================================================ [09:21:32] =================== string (6 subtests) ==================== [09:21:32] [PASSED] test_memset16
[09:21:32] [PASSED] test_memset32
[09:21:32] [PASSED] test_memset64
[09:21:32] [PASSED] test_strchr
[09:21:32] [PASSED] test_strnchr
[09:21:32] [PASSED] test_strspn
[09:21:32] ===================== [PASSED] string ====================== [09:21:32] ============================================================ [09:21:32] Testing complete. Ran 6 tests: passed: 6
[09:21:32] Elapsed time: 11.545s total, 0.001s configuring, 11.327s building, 0.183s running
Quoted 9 lines
...
lib/string_kunit.c static __init int memset16_selftest(void) · line 41
Quoted 4 lines
if (i < 256)lib/string_kunit.c renamed from lib/test_string.c +1
-2-return (i <<24 ) | (j <<16 ) | k |0x8000 ; -return 0 ; +KUNIT_EXPECT_EQ (test,0 , (i <<24 ) | (j <<16 ) | k |0x8000 );First of all, this special value encodes the problematic patterns, so you missed proper messaging.
Yeah, I see now this isn't a test but rather an encoded report. Since the failures are caught earlier, I can improve those messages instead of doing an encoded version.
Quoted 25 lines
Second, the returned value has a constant, how do you expect 0 to be equal to something (guaranteed not to be 0)?
This needs a good rethink of what you should do in the KUnit approach.
...
Quoted 1 line
+
KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000);Ditto.
...
Quoted 1 line
+
KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000);Ditto.
...
lib/string_kunit.c static __init int strchr_selftest(void) · line 113
Quoted 5 lines
for (i = 0; i < strlen(test_string) + 1; i++) { result = strchr(test_string, test_string[i]);lib/string_kunit.c renamed from lib/test_string.c +1
-2-if (result - test_string != i) -return i +'a' ; +KUNIT_ASSERT_EQ (test, result - test_string, i);
Quoted 3 lines
In a similar way, all returned values are *special*, you really need to think about them before converting to a simple (and sometimes wrong) checks)
This encoding is trying to report "i", so I've adjusted the error reporting in v3.
Quoted 2 lines
I dunno if KUnit has a fault ejection simulation. It should, in order to be sure that test cases are fine when they fail.
Yeah, bumping offsets and such produce expected failures.
Signature · 1 line
Kees Cook
Re: [PATCH v2 1/2] string: Convert selftest to KUnit
Quoted 9 lines · Kees Cook
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Kees Cook
Convert
test_string.cto KUnit so it can be easily run with everything else.Have you run it?
Yes:
Of course I new the answer :-)
The problem as I described is the (absence of) fault injection in KUnit itself. When tests are passed the picture is nice, when they are not, the reports become a disaster (if this is applied).
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 2/2 string: Convert helpers selftest to KUnit
In reply to Kees CookConvert test-string_helpers.c to KUnit so it can be easily run with everything else.
---
Cc: Andy Shevchenko <[email protected]>
Cc: Kent Overstreet <[email protected]>
Cc: [email protected]
---
MAINTAINERS | 2 +-
lib/Kconfig.debug | 6 +-
lib/Makefile | 2 +-
...tring_helpers.c => string_helpers_kunit.c} | 177 ++++++++++--------
4 files changed, 103 insertions(+), 84 deletions(-)
rename lib/{test-string_helpers.c => string_helpers_kunit.c} (78%)MAINTAINERS +1 -1
@@ -8978,7 +8978,7 @@ F: include/linux/string_helpers.h F: lib/string.c F: lib/string_kunit.c F: lib/string_helpers.c -F: lib/test-string_helpers.c +F: lib/string_helpers_kunit.c F: scripts/coccinelle/api/string_choices.cocci GENERIC UIO DRIVER FOR PCI DEVICES
lib/Kconfig.debug +4 -2
@@ -2357,8 +2357,10 @@ config STRING_KUNIT_TEST depends on KUNIT default KUNIT_ALL_TESTS -config TEST_STRING_HELPERS - tristate "Test functions located in the string_helpers module at runtime" +config STRING_HELPERS_KUNIT_TEST + tristate "KUnit test string helpers at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS config TEST_KSTRTOX tristate "Test kstrto*() family of functions at runtime"
lib/Makefile +1 -1
@@ -51,7 +51,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \ generic-radix-tree.o bitmap-str.o obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o obj-y += string_helpers.o -obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o +obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o obj-y += hexdump.o obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o obj-y += kstrtox.o
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +97 -80
352 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 2/2] string: Convert helpers selftest to KUnit
Quoted 3 lines · Kees Cook
Convert
test-string_helpers.cto KUnit so it can be easily run with everything else.
...
lib/string_helpers_kunit.c line 7
Quoted 3 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +1
-1-#include <linux/array_size.h> #include <linux/init.h> +#include <kunit/test.h>
I know the order is broken here, but don't make it worse, please. And stick with one schema where to put kunit/test.h always before everything else and somewhere else (like always after linux/*).
Quoted 4 lines
+#include <
linux/array_size.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
...
lib/string_helpers_kunit.c line 24
Quoted 11 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +8
-2+static void test_string_check_buf (struct kunit *test, +const char *name,unsigned int flags, +char *in,size_t p, +char *out_real,size_t q_real, +char *out_test,size_t q_test) { -if (q_real == q_test && !memcmp (out_test, out_real, q_test)) -return true; +int result; + +KUNIT_EXPECT_EQ (test, q_real, q_test);
This needs a message.
lib/string_helpers_kunit.c
Quoted 3 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +2
-0+ result =memcmp (out_test, out_real, q_test); +KUNIT_EXPECT_EQ (test,0 , result);
Why do we need this assertion? We have a dump below to show what's wrong.
lib/string_helpers_kunit.c line 30
Quoted 2 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +2
-0+if (q_real == q_test && result ==0 ) +return ;
I'm not sure this is an equivalent change. IIRC KUnit assertions do not continue on failure. (Long time last time I run KUnit test)
Quoted 10 lines
pr_warn("Test '%s' failed: flags = %#x\n", name, flags);lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +0
-2@@ -28,8 +37,6 @@ static __init bool test_string_check_buf(const char *name, unsigned int flags, out_test, q_test, true);print_hex_dump (KERN_WARNING ,"Got: " ,DUMP_PREFIX_NONE ,16 ,1 , out_real, q_real, true); - -return false; }
...
lib/string_helpers_kunit.c static __init const char *test_string_find_match(const struct test_string_2 *s2, · line 422
Quoted 12 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +4
-3+static void +test_string_escape_overflow (struct kunit *test, +const char *in,int p,unsigned int flags,const char *esc,int q_test,const char *name) {int q_real; q_real =string_escape_mem (in, p,NULL ,0 , flags, esc); -if (q_real != q_test) -pr_warn ("Test '%s' failed: flags = %#x, osz = 0, expected %d, got %d\n" , - name, flags, q_test, q_real); +KUNIT_EXPECT_EQ (test, q_real, q_test);
You killed the message, not good.
Quoted 1 line
}
...
lib/string_helpers_kunit.c static __init void test_string_escape(const char *name, · line 488
Quoted 4 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +4
-0+#define test_string_get_size_one (size, blk_size, exp_result10, exp_result2) \ + do { \ + BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \ + BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
No analogous assertions in KUnit?
Quoted 3 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +2
-0+__test_string_get_size (test, (size), (blk_size), (exp_result10), \ + (exp_result2)); \ }while (0 )
...
lib/string_helpers_kunit.c static __init void test_string_escape(const char *name,
Quoted 5 lines
{
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +2
-1-if (!memcmp (res, exp,strlen (exp) +1 )) +int result =memcmp (res, exp,strlen (exp) +1 ); +KUNIT_EXPECT_EQ (test,0 , result);
As per above, what's the added value of this assertion?
Quoted 2 lines
+ if (!result)
return;
...
Quoted 5 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +1
-0@@ -590,65 +604,68 @@ static void __init test_string_upper_lower(void)for (i =0 ; i <ARRAY_SIZE (strings_upper); i++) {const char *s = strings_upper[i].in;int len =strlen (strings_upper[i].in) +1 ; +int result;
lib/string_helpers_kunit.c static void __init test_string_upper_lower(void) · line 614
Quoted 10 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +3
-3dst =kmalloc (len,GFP_KERNEL ); -if (!dst) -return ; +KUNIT_ASSERT_NOT_NULL (test, dst);string_upper (dst, s); -if (memcmp (dst, strings_upper[i].out, len)) { + result =memcmp (dst, strings_upper[i].out, len); +KUNIT_EXPECT_EQ (test,0 , result);
Ditto.
Quoted 13 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +2
-3+if (result)pr_warn ("Test 'string_upper' failed : expected %s, got %s!\n" , strings_upper[i].out, dst); -kfree (dst); -return ; - }kfree (dst); }for (i =0 ; i <ARRAY_SIZE (strings_lower); i++) {const char *s = strings_lower[i].in;int len =strlen (strings_lower[i].in) +1 ; +int result;
lib/string_helpers_kunit.c static void __init test_string_upper_lower(void) · line 631
Quoted 10 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +3
-3dst =kmalloc (len,GFP_KERNEL ); -if (!dst) -return ; +KUNIT_ASSERT_NOT_NULL (test, dst);string_lower (dst, s); -if (memcmp (dst, strings_lower[i].out, len)) { + result =memcmp (dst, strings_lower[i].out, len); +KUNIT_EXPECT_EQ (test,0 , result);
Ditto.
Quoted 9 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +1
-3+if (result)pr_warn ("Test 'string_lower failed : : expected %s, got %s!\n" , strings_lower[i].out, dst); -kfree (dst); -return ; - }kfree (dst); } }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] string: Convert helpers selftest to KUnit
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Kees Cook
Convert
test-string_helpers.cto KUnit so it can be easily run with everything else....
Quoted 7 lines
lib/string_helpers_kunit.c line 7
Quoted 3 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +1
-1-#include <linux/array_size.h> #include <linux/init.h> +#include <kunit/test.h> I know the order is broken here, but don't make it worse, please. And stick with one schema where to put
kunit/test.halways before everything else and somewhere else (like always after linux/*).
Fixed.
Quoted 22 lines
Quoted 5 lines
+#include <
linux/array_size.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>...
lib/string_helpers_kunit.c line 24
Quoted 11 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +8
-2+static void test_string_check_buf (struct kunit *test, +const char *name,unsigned int flags, +char *in,size_t p, +char *out_real,size_t q_real, +char *out_test,size_t q_test) { -if (q_real == q_test && !memcmp (out_test, out_real, q_test)) -return true; +int result; + +KUNIT_EXPECT_EQ (test, q_real, q_test);This needs a message.
Quoted 5 lines
lib/string_helpers_kunit.c
Quoted 3 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +2
-0+ result =memcmp (out_test, out_real, q_test); +KUNIT_EXPECT_EQ (test,0 , result);Why do we need this assertion? We have a dump below to show what's wrong.
I've improved this all around with ...STREQ and ...MEMEQ calls, and added _MSG versions where needed to retain the prior reported test context.
Quoted 8 lines
...
lib/string_helpers_kunit.c static __init void test_string_escape(const char *name, · line 488
Quoted 4 lines
lib/string_helpers_kunit.c renamed from lib/test-string_helpers.c +4
-0+#define test_string_get_size_one (size, blk_size, exp_result10, exp_result2) \ + do { \ + BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \ + BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \No analogous assertions in KUnit?
It's designed for runtime testing. This is fine as-is for compile-time asserts.
Thanks for the reviews!
-Kees
Signature · 1 line
Kees Cook
Quoted 4 lines · Kees Cook
Hi,
I realized the string selftests hadn't been converted to KUnit yet. Do that.
IIRC last time somebody wanted to do that KUnit was completely sucking in supporting the cases we need in these files. (I mean proper messaging when we need to dump the expected and resulting data). And please Cc Rasmus as well.
Signature · 2 lines
With Best Regards, Andy Shevchenko