string: Merge separate tests into string_kunit.c
cover string: Merge separate tests into string_kunit.c
Hi,
We have a few lone function tests (strscpy and strcat) that are better collected into the common string_kunit.c test suite. Perform various renamings, merge everything, and clean up after them.
-Kees
Kees Cook (5):
string: Prepare to merge strscpy_kunit.c into string_kunit.c
string: Merge strscpy KUnit tests into string_kunit.c
string: Prepare to merge strcat KUnit tests into string_kunit.c
string: Merge strcat KUnit tests into string_kunit.c
string: Convert KUnit test names to standard convention
MAINTAINERS | 2 - lib/Kconfig.debug | 10 -- lib/Makefile | 2 - lib/strcat_kunit.c | 104 ------------------ lib/string_kunit.c | 258 +++++++++++++++++++++++++++++++++++++++----- lib/strscpy_kunit.c | 142 ------------------------ 6 files changed, 230 insertions(+), 288 deletions(-) delete mode 100644 lib/strcat_kunit.c delete mode 100644 lib/strscpy_kunit.c
Signature · 1 line
2.34.1
PATCH 1/5 string: Prepare to merge strscpy_kunit.c into string_kunit.c
In preparation for moving the strscpy_kunit.c tests into string_kunit.c, rename "tc" to "strscpy_check" for better readability.
---
Cc: Andy Shevchenko <[email protected]>
Cc: Ivan Orlov <[email protected]>
Cc: [email protected]
--- lib/strscpy_kunit.c | 51 +++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 25 deletions(-)
lib/strscpy_kunit.c +26 -25
@@ -8,22 +8,23 @@#include <kunit/test.h> #include <linux/string.h> -/* -* tc() - Run a specific test case. +/** +* strscpy_check() - Run a specific test case. +* @test: KUnit test context pointer * @src: Source string, argument to strscpy_pad() * @count: Size of destination buffer, argument to strscpy_pad() * @expected: Expected return value from call to strscpy_pad() -* @terminator: 1 if there should be a terminating null byte 0 otherwise. * @chars: Number of characters from the src string expected to be * written to the dst buffer. +* @terminator: 1 if there should be a terminating null byte 0 otherwise. * @pad: Number of pad characters expected (in the tail of dst buffer). * (@pad does not include the null terminator byte.) * * Calls strscpy_pad() and verifies the return value and state of the * destination buffer after the call returns. */ -static void tc (struct kunit *test,char *src,int count,int expected, -int chars,int terminator,int pad) +static void strscpy_check (struct kunit *test,char *src,int count, +int expected,int chars,int terminator,int pad) {int nr_bytes_poison;int max_expected;
@@ -79,12 +80,12 @@ static void tc(struct kunit *test, char *src, int count, int expected, } } -static void strscpy_test (struct kunit *test) +static void test_strscpy (struct kunit *test) {char dest[8 ];/* -* tc() uses a destination buffer of size 6 and needs at +* strscpy_check() uses a destination buffer of size 6 and needs at * least 2 characters spare (one for null and one to check for * overflow). This means we should only call tc() with * strings up to a maximum of 4 characters long and 'count'
@@ -92,27 +93,27 @@ static void strscpy_test(struct kunit *test)* the buffer size in tc(). */ -/* tc(test, src, count, expected, chars, terminator, pad) */ -tc (test,"a" ,0 , -E2BIG ,0 ,0 ,0 ); -tc (test,"" ,0 , -E2BIG ,0 ,0 ,0 ); +/* strscpy_check(test, src, count, expected, chars, terminator, pad) */ +strscpy_check (test,"a" ,0 , -E2BIG ,0 ,0 ,0 ); +strscpy_check (test,"" ,0 , -E2BIG ,0 ,0 ,0 ); -tc (test,"a" ,1 , -E2BIG ,0 ,1 ,0 ); -tc (test,"" ,1 ,0 ,0 ,1 ,0 ); +strscpy_check (test,"a" ,1 , -E2BIG ,0 ,1 ,0 ); +strscpy_check (test,"" ,1 ,0 ,0 ,1 ,0 ); -tc (test,"ab" ,2 , -E2BIG ,1 ,1 ,0 ); -tc (test,"a" ,2 ,1 ,1 ,1 ,0 ); -tc (test,"" ,2 ,0 ,0 ,1 ,1 ); +strscpy_check (test,"ab" ,2 , -E2BIG ,1 ,1 ,0 ); +strscpy_check (test,"a" ,2 ,1 ,1 ,1 ,0 ); +strscpy_check (test,"" ,2 ,0 ,0 ,1 ,1 ); -tc (test,"abc" ,3 , -E2BIG ,2 ,1 ,0 ); -tc (test,"ab" ,3 ,2 ,2 ,1 ,0 ); -tc (test,"a" ,3 ,1 ,1 ,1 ,1 ); -tc (test,"" ,3 ,0 ,0 ,1 ,2 ); +strscpy_check (test,"abc" ,3 , -E2BIG ,2 ,1 ,0 ); +strscpy_check (test,"ab" ,3 ,2 ,2 ,1 ,0 ); +strscpy_check (test,"a" ,3 ,1 ,1 ,1 ,1 ); +strscpy_check (test,"" ,3 ,0 ,0 ,1 ,2 ); -tc (test,"abcd" ,4 , -E2BIG ,3 ,1 ,0 ); -tc (test,"abc" ,4 ,3 ,3 ,1 ,0 ); -tc (test,"ab" ,4 ,2 ,2 ,1 ,1 ); -tc (test,"a" ,4 ,1 ,1 ,1 ,2 ); -tc (test,"" ,4 ,0 ,0 ,1 ,3 ); +strscpy_check (test,"abcd" ,4 , -E2BIG ,3 ,1 ,0 ); +strscpy_check (test,"abc" ,4 ,3 ,3 ,1 ,0 ); +strscpy_check (test,"ab" ,4 ,2 ,2 ,1 ,1 ); +strscpy_check (test,"a" ,4 ,1 ,1 ,1 ,2 ); +strscpy_check (test,"" ,4 ,0 ,0 ,1 ,3 );/* Compile-time-known source strings. */ KUNIT_EXPECT_EQ (test,strscpy (dest,"" ,ARRAY_SIZE (dest)),0 );
@@ -127,7 +128,7 @@ static void strscpy_test(struct kunit *test) }static struct kunit_case strscpy_test_cases[] = { -KUNIT_CASE (strscpy_test), +KUNIT_CASE (test_strscpy), {} };
Signature · 1 line
2.34.1
PATCH 2/5 string: Merge strscpy KUnit tests into string_kunit.c
In reply to Kees CookMove the strscpy() tests into string_kunit.c. Remove the separate Kconfig and Makefile rule.
---
Cc: Andy Shevchenko <[email protected]>
Cc: Ivan Orlov <[email protected]>
Cc: [email protected]
--- MAINTAINERS | 1 - lib/Kconfig.debug | 5 -- lib/Makefile | 1 - lib/string_kunit.c | 120 +++++++++++++++++++++++++++++++++++++ lib/strscpy_kunit.c | 143 -------------------------------------------- 5 files changed, 120 insertions(+), 150 deletions(-) delete mode 100644 lib/strscpy_kunit.c
MAINTAINERS +0 -1
@@ -8442,7 +8442,6 @@ F: include/linux/fortify-string.h F: lib/fortify_kunit.c F: lib/memcpy_kunit.c F: lib/strcat_kunit.c -F: lib/strscpy_kunit.c F: lib/test_fortify/* F: scripts/test_fortify.sh K: \b__NO_FORTIFY\b
lib/Kconfig.debug +0 -5
@@ -2763,11 +2763,6 @@ config STRCAT_KUNIT_TEST depends on KUNIT default KUNIT_ALL_TESTS -config STRSCPY_KUNIT_TEST - tristate "Test strscpy*() family of functions at runtime" if !KUNIT_ALL_TESTS - depends on KUNIT - default KUNIT_ALL_TESTS - config SIPHASH_KUNIT_TEST tristate "Perform selftest on siphash functions" if !KUNIT_ALL_TESTS depends on KUNIT
lib/Makefile +0 -1
@@ -404,7 +404,6 @@ CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-truncation) CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o obj-$(CONFIG_STRCAT_KUNIT_TEST) += strcat_kunit.o -obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
lib/string_kunit.c +120 -0
132 lines not on this page · raw message
lib/strscpy_kunit.c deleted +0 -143
143 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 3/5 string: Prepare to merge strcat KUnit tests into string_kunit.c
In reply to Kees CookThe test naming convention differs between string_kunit.c and strcat_kunit.c. Move "test" to the beginning of the function name.
---
Cc: Andy Shevchenko <[email protected]>
Cc: Ivan Orlov <[email protected]>
Cc: [email protected]
--- lib/strcat_kunit.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
lib/strcat_kunit.c +6 -6
@@ -10,7 +10,7 @@static volatile int unconst; -static void strcat_test (struct kunit *test) +static void test_strcat (struct kunit *test) {char dest[8 ];
@@ -29,7 +29,7 @@ static void strcat_test(struct kunit *test)KUNIT_EXPECT_STREQ (test, dest,"fourAB" ); } -static void strncat_test (struct kunit *test) +static void test_strncat (struct kunit *test) {char dest[8 ];
@@ -56,7 +56,7 @@ static void strncat_test(struct kunit *test)KUNIT_EXPECT_STREQ (test, dest,"fourAB" ); } -static void strlcat_test (struct kunit *test) +static void test_strlcat (struct kunit *test) {char dest[8 ] ="" ;int len =sizeof (dest) + unconst;
@@ -88,9 +88,9 @@ static void strlcat_test(struct kunit *test) }static struct kunit_case strcat_test_cases[] = { -KUNIT_CASE (strcat_test), -KUNIT_CASE (strncat_test), -KUNIT_CASE (strlcat_test), +KUNIT_CASE (test_strcat), +KUNIT_CASE (test_strncat), +KUNIT_CASE (test_strlcat), {} };
Signature · 1 line
2.34.1
PATCH 4/5 string: Merge strcat KUnit tests into string_kunit.c
In reply to Kees CookMove the strcat() tests into string_kunit.c. Remove the separate Kconfig and Makefile rule.
---
Cc: Andy Shevchenko <[email protected]>
Cc: Ivan Orlov <[email protected]>
Cc: [email protected]
--- MAINTAINERS | 1 - lib/Kconfig.debug | 5 --- lib/Makefile | 1 - lib/strcat_kunit.c | 104 --------------------------------------------- lib/string_kunit.c | 82 +++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 111 deletions(-) delete mode 100644 lib/strcat_kunit.c
MAINTAINERS +0 -1
@@ -8441,7 +8441,6 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/har F: include/linux/fortify-string.h F: lib/fortify_kunit.c F: lib/memcpy_kunit.c -F: lib/strcat_kunit.c F: lib/test_fortify/* F: scripts/test_fortify.sh K: \b__NO_FORTIFY\b
lib/Kconfig.debug +0 -5
@@ -2758,11 +2758,6 @@ config HW_BREAKPOINT_KUNIT_TEST If unsure, say N. -config STRCAT_KUNIT_TEST - tristate "Test strcat() family of functions at runtime" if !KUNIT_ALL_TESTS - depends on KUNIT - default KUNIT_ALL_TESTS - config SIPHASH_KUNIT_TEST tristate "Perform selftest on siphash functions" if !KUNIT_ALL_TESTS depends on KUNIT
lib/Makefile +0 -1
@@ -403,7 +403,6 @@ CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-overread) CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-truncation) CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o -obj-$(CONFIG_STRCAT_KUNIT_TEST) += strcat_kunit.o obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
lib/strcat_kunit.c deleted +0 -104
104 lines not on this page · raw message
lib/string_kunit.c +82 -0
94 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 5/5 string: Convert KUnit test names to standard convention
In reply to Kees CookThe KUnit convention for test names is AREA_test_WHAT. Adjust the string test names to follow this pattern.
---
Cc: Andy Shevchenko <[email protected]>
Cc: Ivan Orlov <[email protected]>
Cc: [email protected]
--- lib/string_kunit.c | 72 +++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 36 deletions(-)
lib/string_kunit.c +36 -36
@@ -17,7 +17,7 @@#define STRCMP_TEST_EXPECT_LOWER (test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0)#define STRCMP_TEST_EXPECT_GREATER (test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0) -static void test_memset16 (struct kunit *test) +static void string_test_memset16 (struct kunit *test) {unsigned i, j, k;u16 v, *p;
@@ -46,7 +46,7 @@ static void test_memset16(struct kunit *test) } } -static void test_memset32 (struct kunit *test) +static void string_test_memset32 (struct kunit *test) {unsigned i, j, k;u32 v, *p;
@@ -75,7 +75,7 @@ static void test_memset32(struct kunit *test) } } -static void test_memset64 (struct kunit *test) +static void string_test_memset64 (struct kunit *test) {unsigned i, j, k;u64 v, *p;
@@ -104,7 +104,7 @@ static void test_memset64(struct kunit *test) } } -static void test_strchr (struct kunit *test) +static void string_test_strchr (struct kunit *test) {const char *test_string ="abcdefghijkl" ;const char *empty_string ="" ;
@@ -127,7 +127,7 @@ static void test_strchr(struct kunit *test)KUNIT_ASSERT_NULL (test, result); } -static void test_strnchr (struct kunit *test) +static void string_test_strnchr (struct kunit *test) {const char *test_string ="abcdefghijkl" ;const char *empty_string ="" ;
@@ -160,7 +160,7 @@ static void test_strnchr(struct kunit *test)KUNIT_ASSERT_NULL (test, result); } -static void test_strspn (struct kunit *test) +static void string_test_strspn (struct kunit *test) { static const struct strspn_test { const char str[16];
@@ -196,7 +196,7 @@ static void strcmp_fill_buffers(char fill1, char fill2) strcmp_buffer2[STRCMP_LARGE_BUF_LEN -1 ] =0 ; } -static void test_strcmp (struct kunit *test) +static void string_test_strcmp (struct kunit *test) {/* Equal strings */ STRCMP_TEST_EXPECT_EQUAL (test, strcmp,"Hello, Kernel!" ,"Hello, Kernel!" );
@@ -214,7 +214,7 @@ static void test_strcmp(struct kunit *test)STRCMP_TEST_EXPECT_LOWER (test, strcmp,"Just a string" ,"Just a string and something else" ); } -static void test_strcmp_long_strings (struct kunit *test) +static void string_test_strcmp_long_strings (struct kunit *test) {strcmp_fill_buffers ('B' ,'B' );STRCMP_TEST_EXPECT_EQUAL (test, strcmp, strcmp_buffer1, strcmp_buffer2);
@@ -226,7 +226,7 @@ static void test_strcmp_long_strings(struct kunit *test)STRCMP_TEST_EXPECT_GREATER (test, strcmp, strcmp_buffer1, strcmp_buffer2); } -static void test_strncmp (struct kunit *test) +static void string_test_strncmp (struct kunit *test) {/* Equal strings */ STRCMP_TEST_EXPECT_EQUAL (test, strncmp,"Hello, KUnit!" ,"Hello, KUnit!" ,13 );
@@ -249,7 +249,7 @@ static void test_strncmp(struct kunit *test) strlen("Just a string")); } -static void test_strncmp_long_strings (struct kunit *test) +static void string_test_strncmp_long_strings (struct kunit *test) {strcmp_fill_buffers ('B' ,'B' ); STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,
@@ -269,7 +269,7 @@ static void test_strncmp_long_strings(struct kunit *test) strcmp_buffer2, STRCMP_CHANGE_POINT + 1); } -static void test_strcasecmp (struct kunit *test) +static void string_test_strcasecmp (struct kunit *test) {/* Same strings in different case should be equal */ STRCMP_TEST_EXPECT_EQUAL (test, strcasecmp,"Hello, Kernel!" ,"HeLLO, KErNeL!" );
@@ -282,7 +282,7 @@ static void test_strcasecmp(struct kunit *test)STRCMP_TEST_EXPECT_EQUAL (test, strcasecmp,"-+**.1230ghTTT~^" ,"-+**.1230Ghttt~^" ); } -static void test_strcasecmp_long_strings (struct kunit *test) +static void string_test_strcasecmp_long_strings (struct kunit *test) {strcmp_fill_buffers ('b' ,'B' );STRCMP_TEST_EXPECT_EQUAL (test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
@@ -294,7 +294,7 @@ static void test_strcasecmp_long_strings(struct kunit *test)STRCMP_TEST_EXPECT_GREATER (test, strcasecmp, strcmp_buffer1, strcmp_buffer2); } -static void test_strncasecmp (struct kunit *test) +static void string_test_strncasecmp (struct kunit *test) {/* Same strings in different case should be equal */ STRCMP_TEST_EXPECT_EQUAL (test, strncasecmp,"AbAcAbA" ,"Abacaba" ,strlen ("Abacaba" ));
@@ -306,7 +306,7 @@ static void test_strncasecmp(struct kunit *test)STRCMP_TEST_EXPECT_EQUAL (test, strncasecmp,"Abacaba" ,"Not abacaba" ,0 ); } -static void test_strncasecmp_long_strings (struct kunit *test) +static void string_test_strncasecmp_long_strings (struct kunit *test) {strcmp_fill_buffers ('b' ,'B' ); STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,
@@ -398,7 +398,7 @@ static void strscpy_check(struct kunit *test, char *src, int count, } } -static void test_strscpy (struct kunit *test) +static void string_test_strscpy (struct kunit *test) {char dest[8 ];
@@ -447,7 +447,7 @@ static void test_strscpy(struct kunit *test)static volatile int unconst; -static void test_strcat (struct kunit *test) +static void string_test_strcat (struct kunit *test) {char dest[8 ];
@@ -466,7 +466,7 @@ static void test_strcat(struct kunit *test)KUNIT_EXPECT_STREQ (test, dest,"fourAB" ); } -static void test_strncat (struct kunit *test) +static void string_test_strncat (struct kunit *test) {char dest[8 ];
@@ -493,7 +493,7 @@ static void test_strncat(struct kunit *test)KUNIT_EXPECT_STREQ (test, dest,"fourAB" ); } -static void test_strlcat (struct kunit *test) +static void string_test_strlcat (struct kunit *test) {char dest[8 ] ="" ;int len =sizeof (dest) + unconst;
@@ -525,24 +525,24 @@ static void test_strlcat(struct kunit *test) }static struct kunit_case string_test_cases[] = { -KUNIT_CASE (test_memset16), -KUNIT_CASE (test_memset32), -KUNIT_CASE (test_memset64), -KUNIT_CASE (test_strchr), -KUNIT_CASE (test_strnchr), -KUNIT_CASE (test_strspn), -KUNIT_CASE (test_strcmp), -KUNIT_CASE (test_strcmp_long_strings), -KUNIT_CASE (test_strncmp), -KUNIT_CASE (test_strncmp_long_strings), -KUNIT_CASE (test_strcasecmp), -KUNIT_CASE (test_strcasecmp_long_strings), -KUNIT_CASE (test_strncasecmp), -KUNIT_CASE (test_strncasecmp_long_strings), -KUNIT_CASE (test_strscpy), -KUNIT_CASE (test_strcat), -KUNIT_CASE (test_strncat), -KUNIT_CASE (test_strlcat), +KUNIT_CASE (string_test_memset16), +KUNIT_CASE (string_test_memset32), +KUNIT_CASE (string_test_memset64), +KUNIT_CASE (string_test_strchr), +KUNIT_CASE (string_test_strnchr), +KUNIT_CASE (string_test_strspn), +KUNIT_CASE (string_test_strcmp), +KUNIT_CASE (string_test_strcmp_long_strings), +KUNIT_CASE (string_test_strncmp), +KUNIT_CASE (string_test_strncmp_long_strings), +KUNIT_CASE (string_test_strcasecmp), +KUNIT_CASE (string_test_strcasecmp_long_strings), +KUNIT_CASE (string_test_strncasecmp), +KUNIT_CASE (string_test_strncasecmp_long_strings), +KUNIT_CASE (string_test_strscpy), +KUNIT_CASE (string_test_strcat), +KUNIT_CASE (string_test_strncat), +KUNIT_CASE (string_test_strlcat), {} };
Signature · 1 line
2.34.1
Quoted 6 lines · Kees Cook
Hi,
We have a few lone function tests (strscpy and strcat) that are better collected into the common
string_kunit.ctest suite. Perform various renamings, merge everything, and clean up after them.
All makes sense to me, thanks for doing this!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 22 lines · Kees Cook
Hi,
We have a few lone function tests (strscpy and strcat) that are better collected into the common
string_kunit.ctest suite. Perform various renamings, merge everything, and clean up after them.-Kees
Kees Cook (5):
string: Prepare to mergestrscpy_kunit.cintostring_kunit.c
string: Merge strscpy KUnit tests intostring_kunit.c
string: Prepare to merge strcat KUnit tests intostring_kunit.c
string: Merge strcat KUnit tests intostring_kunit.c
string: Convert KUnit test names to standard conventionMAINTAINERS | 2 - lib/Kconfig.debug | 10 -- lib/Makefile | 2 - lib/strcat_kunit.c | 104 ------------------ lib/string_kunit.c | 258 +++++++++++++++++++++++++++++++++++++++----- lib/strscpy_kunit.c | 142 ------------------------ 6 files changed, 230 insertions(+), 288 deletions(-)
Quoted 3 lines
delete mode 100644
lib/strcat_kunit.c
delete mode 100644lib/strscpy_kunit.c
I tested all of the patches in the series with kunit_tool, and everything works correctly.
Thank you for doing this!
Signature · 2 lines
Kind regards, Ivan Orlov