Re: [PATCH 5/6] unicode: kunit: refactor selftest to kunit tests

2024-10-15 Thread Gabriel Krisman Bertazi
David Gow  writes:

> From: Gabriela Bittencourt 
>
> Refactoring 'test' functions into kunit tests, to test utf-8 support in
> unicode subsystem.
>
> This allows the utf8 tests to be run alongside the KUnit test suite
> using kunit-tool, quickly compiling and running all desired tests as
> part of the KUnit test suite, instead of compiling the selftest module
> and loading it.
>
> The refactoring kept the original testing logic intact, while adopting a
> testing pattern across different kernel modules and leveraging KUnit's
> benefits.
>

Acked-by: Gabriel Krisman Bertazi 

-- 
Gabriel Krisman Bertazi



[PATCH 5/6] unicode: kunit: refactor selftest to kunit tests

2024-10-11 Thread David Gow
From: Gabriela Bittencourt 

Refactoring 'test' functions into kunit tests, to test utf-8 support in
unicode subsystem.

This allows the utf8 tests to be run alongside the KUnit test suite
using kunit-tool, quickly compiling and running all desired tests as
part of the KUnit test suite, instead of compiling the selftest module
and loading it.

The refactoring kept the original testing logic intact, while adopting a
testing pattern across different kernel modules and leveraging KUnit's
benefits.

Co-developed-by: Pedro Orlando 
Signed-off-by: Pedro Orlando 
Co-developed-by: Danilo Pereira 
Signed-off-by: Danilo Pereira 
Signed-off-by: Gabriela Bittencourt 
Reviewed-by: David Gow 
Signed-off-by: David Gow 
---
 fs/unicode/.kunitconfig|   3 +
 fs/unicode/Kconfig |   5 +-
 fs/unicode/Makefile|   2 +-
 fs/unicode/utf8-norm.c |   2 +-
 fs/unicode/utf8-selftest.c | 149 +
 5 files changed, 77 insertions(+), 84 deletions(-)
 create mode 100644 fs/unicode/.kunitconfig

diff --git a/fs/unicode/.kunitconfig b/fs/unicode/.kunitconfig
new file mode 100644
index ..62dd5c171f9c
--- /dev/null
+++ b/fs/unicode/.kunitconfig
@@ -0,0 +1,3 @@
+CONFIG_KUNIT=y
+CONFIG_UNICODE=y
+CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST=y
diff --git a/fs/unicode/Kconfig b/fs/unicode/Kconfig
index da786a687fdc..4ad2c36550f1 100644
--- a/fs/unicode/Kconfig
+++ b/fs/unicode/Kconfig
@@ -10,6 +10,7 @@ config UNICODE
  be a separate loadable module that gets requested only when a file
  system actually use it.
 
-config UNICODE_NORMALIZATION_SELFTEST
+config UNICODE_NORMALIZATION_KUNIT_TEST
tristate "Test UTF-8 normalization support"
-   depends on UNICODE
+   depends on UNICODE && KUNIT
+   default KUNIT_ALL_TESTS
diff --git a/fs/unicode/Makefile b/fs/unicode/Makefile
index e309afe2b2bb..37bbcbc628a1 100644
--- a/fs/unicode/Makefile
+++ b/fs/unicode/Makefile
@@ -4,7 +4,7 @@ ifneq ($(CONFIG_UNICODE),)
 obj-y  += unicode.o
 endif
 obj-$(CONFIG_UNICODE)  += utf8data.o
-obj-$(CONFIG_UNICODE_NORMALIZATION_SELFTEST) += utf8-selftest.o
+obj-$(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST) += utf8-selftest.o
 
 unicode-y := utf8-norm.o utf8-core.o
 
diff --git a/fs/unicode/utf8-norm.c b/fs/unicode/utf8-norm.c
index 768f8ab448b8..7b998c99c88d 100644
--- a/fs/unicode/utf8-norm.c
+++ b/fs/unicode/utf8-norm.c
@@ -586,7 +586,7 @@ int utf8byte(struct utf8cursor *u8c)
}
 }
 
-#ifdef CONFIG_UNICODE_NORMALIZATION_SELFTEST_MODULE
+#if IS_MODULE(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST)
 EXPORT_SYMBOL_GPL(utf8version_is_supported);
 EXPORT_SYMBOL_GPL(utf8nlen);
 EXPORT_SYMBOL_GPL(utf8ncursor);
diff --git a/fs/unicode/utf8-selftest.c b/fs/unicode/utf8-selftest.c
index 600e15efe9ed..52ab68ef2bbc 100644
--- a/fs/unicode/utf8-selftest.c
+++ b/fs/unicode/utf8-selftest.c
@@ -1,38 +1,18 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * Kernel module for testing utf-8 support.
+ * KUnit tests for utf-8 support.
  *
  * Copyright 2017 Collabora Ltd.
  */
 
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
-
-#include 
-#include 
 #include 
-#include 
+#include 
 
 #include "utf8n.h"
 
-static unsigned int failed_tests;
-static unsigned int total_tests;
-
 /* Tests will be based on this version. */
 #define UTF8_LATESTUNICODE_AGE(12, 1, 0)
 
-#define _test(cond, func, line, fmt, ...) do { \
-   total_tests++;  \
-   if (!cond) {\
-   failed_tests++; \
-   pr_err("test %s:%d Failed: %s%s",   \
-  func, line, #cond, (fmt?":":"."));   \
-   if (fmt)\
-   pr_err(fmt, ##__VA_ARGS__); \
-   }   \
-   } while (0)
-#define test_f(cond, fmt, ...) _test(cond, __func__, __LINE__, fmt, 
##__VA_ARGS__)
-#define test(cond) _test(cond, __func__, __LINE__, "")
-
 static const struct {
/* UTF-8 strings in this vector _must_ be NULL-terminated. */
unsigned char str[10];
@@ -170,69 +150,74 @@ static int utf8cursor(struct utf8cursor *u8c, const 
struct unicode_map *um,
return utf8ncursor(u8c, um, n, s, (unsigned int)-1);
 }
 
-static void check_utf8_nfdi(struct unicode_map *um)
+static void check_utf8_nfdi(struct kunit *test)
 {
int i;
struct utf8cursor u8c;
+   struct unicode_map *um = test->priv;
 
for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
int len = strlen(nfdi_test_data[i].str);
int nlen = strlen(nfdi_test_data[i].dec);
int j = 0;
unsigned char c;
+   int ret;
 
-   test((utf8len(um, UTF8_NFDI, nfdi_test_data[i].str)