Skip to contentSkip to conversation

string: Implement KUnit test for str*cmp functions

PATCH In review Ivan Orlov 2 participants 5 messages Apr 17, 2024 Library code

lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH 1/1 string: Implement KUnit test for str*cmp functions

Currently, str*cmp functions (strcmp, strncmp, strcasecmp and strncasecmp) are not covered with tests. Implement the test which covers them all.

The strcmp test suite consist of 8 test cases:

1) strcmp test
2) strcmp test on long strings (2048 chars)
3) strncmp test
4) strncmp test on long strings (2048 chars)
5) strcasecmp test
6) strcasecmp test on long strings
7) strncasecmp test
8) strncasecmp test on long strings

The test aims at covering as many edge cases as possible, including the tests on empty strings, situations when the different symbol is placed at the end of one of the strings, etc.

---
 MAINTAINERS        |   5 ++
 lib/Kconfig.debug  |  16 +++++
 lib/Makefile       |   1 +
 lib/strcmp_kunit.c | 170 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 192 insertions(+)
 create mode 100644 lib/strcmp_kunit.c
MAINTAINERS +5 -0
index c23fda1aa1f0..e23e6b91e2f1 100644
@@ -21182,6 +21182,11 @@ W:	http://www.stlinux.com
 F:	Documentation/networking/device_drivers/ethernet/stmicro/
 F:	drivers/net/ethernet/stmicro/stmmac/
 
+STRCMP KUNIT TEST
+M:	Ivan Orlov <[email protected]>
+S:	Maintained
+F:	lib/strcmp_kunit.c
+
 SUN HAPPY MEAL ETHERNET DRIVER
 M:	Sean Anderson <[email protected]>
 S:	Maintained
lib/Kconfig.debug +16 -0
index c63a5fbf1f1c..a6992631f32e 100644
@@ -2763,6 +2763,22 @@ config STRCAT_KUNIT_TEST
 	depends on KUNIT
 	default KUNIT_ALL_TESTS
 
+config STRCMP_KUNIT_TEST
+	tristate "Test strcmp() family of functions at runtime" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables the KUnit test for strcmp family of functions:
+	  strcmp, strncmp, strcasecmp and strncasecmp.
+
+	  KUnit tests run during boot and output the results to the debug
+	  log in TAP format (https://testanything.org/). Only useful for
+	  kernel devs running KUnit test harness and are not for inclusion
+	  into a production build.
+
+	  For more information on KUnit and unit tests in general, refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
 config STRSCPY_KUNIT_TEST
 	tristate "Test strscpy*() family of functions at runtime" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/Makefile +1 -0
index ffc6b2341b45..1559d3bbea7d 100644
@@ -404,6 +404,7 @@ 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_STRCMP_KUNIT_TEST) += strcmp_kunit.o
 obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
 obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
 
lib/strcmp_kunit.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 18 lines · Ivan Orlov

Currently, str*cmp functions (strcmp, strncmp, strcasecmp and strncasecmp) are not covered with tests. Implement the test which covers them all.

The strcmp test suite consist of 8 test cases:

1) strcmp test
2) strcmp test on long strings (2048 chars)
3) strncmp test
4) strncmp test on long strings (2048 chars)
5) strcasecmp test
6) strcasecmp test on long strings
7) strncasecmp test
8) strncasecmp test on long strings

The test aims at covering as many edge cases as possible, including the tests on empty strings, situations when the different symbol is placed at the end of one of the strings, etc.

...

Quoted 1 line
 lib/strcmp_kunit.c | 170 +++++++++++++++++++++++++++++++++++++++++++++

Why is not part of the existing string_kunit.c?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#2 lore raw
Quoted 20 lines · Andy Shevchenko
Quoted 15 lines · Ivan Orlov

Currently, str*cmp functions (strcmp, strncmp, strcasecmp and strncasecmp) are not covered with tests. Implement the test which covers them all.

The strcmp test suite consist of 8 test cases:

1) strcmp test
2) strcmp test on long strings (2048 chars)
3) strncmp test
4) strncmp test on long strings (2048 chars)
5) strcasecmp test
6) strcasecmp test on long strings
7) strncasecmp test
8) strncasecmp test on long strings

Quoted 3 lines

The test aims at covering as many edge cases as possible, including the tests on empty strings, situations when the different symbol is placed at the end of one of the strings, etc.

Quoted 6 lines

...

Quoted 1 line
  lib/strcmp_kunit.c | 170 +++++++++++++++++++++++++++++++++++++++++++++

Why is not part of the existing string_kunit.c?

There are already 2 other KUnit tests in lib/ covering different groups of string functions separately (lib/strscpy_kunit.c, lib/strcat_kunit.c), so this patch just follows this pattern. I believe it makes sense: the tests are separated to cover one specific group of string functions with a similar purpose

---
Kind regards,
Ivan Orlov

#3 lore raw
Quoted 2 lines · Ivan Orlov
Quoted 1 line · Andy Shevchenko

On Wed, Apr 17, 2024 at 02:54:15PM +0100, Ivan Orlov wrote:

...

Quoted 9 lines
Quoted 3 lines
Quoted 1 line
  lib/strcmp_kunit.c | 170 +++++++++++++++++++++++++++++++++++++++++++++

Why is not part of the existing string_kunit.c?

There are already 2 other KUnit tests in lib/ covering different groups of string functions separately (lib/strscpy_kunit.c, lib/strcat_kunit.c), so this patch just follows this pattern. I believe it makes sense: the tests are separated to cover one specific group of string functions with a similar purpose

We have handful of the string functions, are you going to have a file per function? Isn't it way too many?

P.S>
Having those does not prove it's a correct approach. I would rather expect somebody to incorporate those into string_kunit.c.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#4 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines

There are already 2 other KUnit tests in lib/ covering different groups of string functions separately (lib/strscpy_kunit.c, lib/strcat_kunit.c), so this patch just follows this pattern. I believe it makes sense: the tests are separated to cover one specific group of string functions with a similar purpose

Quoted 7 lines

We have handful of the string functions, are you going to have a file per function? Isn't it way too many?

P.S>
Having those does not prove it's a correct approach. I would rather expect somebody to incorporate those into string_kunit.c.

Makes sense. Also, probably having all of them in string_kunit.c would fit better into the KUnit test style guidelines.

I'll merge this strcmp test into string_kunit.c in V2 of this patch, thank you for the review.

Signature · 2 lines
Kind regards,
Ivan Orlov