From: ChenXiaoSong <chenxiaosong@kylinos.cn>
The KUnit tests are executed when cifs.ko is loaded.
Just like fs/ext4/mballoc.c includes fs/ext4/mballoc-test.c.
smb2maperror.c also includes smb2maperror_test.c, allowing KUnit tests to
access any functions and variables in smb2maperror.c.
The maperror_test_check_sort() checks whether the array is properly sorted.
The maperror_test_check_search() checks whether the expected element can be
correctly searched for in the smb2_error_map_table array.
Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
fs/smb/Kconfig | 17 ++++++++
fs/smb/client/smb2maperror.c | 4 ++
fs/smb/client/smb2maperror_test.c | 71 +++++++++++++++++++++++++++++++
3 files changed, 92 insertions(+)
create mode 100644 fs/smb/client/smb2maperror_test.c
diff --git a/fs/smb/Kconfig b/fs/smb/Kconfig
index ef425789fa6a..85f7ad5fbc5e 100644
--- a/fs/smb/Kconfig
+++ b/fs/smb/Kconfig
@@ -9,3 +9,20 @@ config SMBFS
tristate
default y if CIFS=y || SMB_SERVER=y
default m if CIFS=m || SMB_SERVER=m
+
+config SMB_KUNIT_TESTS
+ tristate "KUnit tests for SMB" if !KUNIT_ALL_TESTS
+ depends on SMBFS && KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds the SMB KUnit tests.
+
+ 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 please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c
index 0d46fa98952c..dc2edeafc93b 100644
--- a/fs/smb/client/smb2maperror.c
+++ b/fs/smb/client/smb2maperror.c
@@ -2490,3 +2490,7 @@ void smb2_init_maperror(void)
sizeof(struct status_to_posix_error),
cmp_smb2_status, NULL);
}
+
+#if IS_ENABLED(CONFIG_SMB_KUNIT_TESTS)
+#include "smb2maperror_test.c"
+#endif /* CONFIG_SMB_KUNIT_TESTS */
diff --git a/fs/smb/client/smb2maperror_test.c b/fs/smb/client/smb2maperror_test.c
new file mode 100644
index 000000000000..b807a860ca59
--- /dev/null
+++ b/fs/smb/client/smb2maperror_test.c
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ *
+ * KUnit tests of SMB2 maperror
+ *
+ * Copyright (C) 2025 KylinSoft Co., Ltd. All rights reserved.
+ * Author(s): ChenXiaoSong <chenxiaosong@kylinos.cn>
+ *
+ */
+
+#include <kunit/test.h>
+
+static void maperror_test_check_sort(struct kunit *test)
+{
+ bool is_sorted = true;
+ unsigned int i;
+
+ for (i = 1; i < err_map_num; i++) {
+ if (smb2_error_map_table[i].smb2_status >=
+ smb2_error_map_table[i - 1].smb2_status)
+ continue;
+
+ pr_err("smb2_error_map_table array order is incorrect\n");
+ is_sorted = false;
+ break;
+ }
+
+ KUNIT_EXPECT_EQ(test, true, is_sorted);
+}
+
+static void
+test_cmp_map(struct kunit *test, struct status_to_posix_error *expect)
+{
+ struct status_to_posix_error *result;
+
+ result = smb2_get_err_map(expect->smb2_status);
+ KUNIT_EXPECT_PTR_NE(test, NULL, result);
+ KUNIT_EXPECT_EQ(test, expect->smb2_status, result->smb2_status);
+ KUNIT_EXPECT_EQ(test, expect->posix_error, result->posix_error);
+ KUNIT_EXPECT_STREQ(test, expect->status_string, result->status_string);
+}
+
+static void maperror_test_check_search(struct kunit *test)
+{
+ unsigned int i;
+ struct status_to_posix_error expect;
+
+ for (i = 0; i < err_map_num; i++) {
+ expect = smb2_error_map_table[i];
+ test_cmp_map(test, &expect);
+ }
+}
+
+/*
+ * Before running these test cases, the smb2_init_maperror()
+ * function is called first.
+ */
+static struct kunit_case maperror_test_cases[] = {
+ KUNIT_CASE(maperror_test_check_sort),
+ KUNIT_CASE(maperror_test_check_search),
+ {}
+};
+
+static struct kunit_suite maperror_suite = {
+ .name = "smb2_maperror",
+ .test_cases = maperror_test_cases,
+};
+
+kunit_test_suite(maperror_suite);
+
+MODULE_LICENSE("GPL");
--
2.43.0
Hi,
kernel test robot noticed the following build errors:
[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on linus/master v6.18]
[cannot apply to cifs/for-next next-20251208]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/chenxiaosong-chenxiaosong-linux-dev/smb-client-reduce-loop-count-in-map_smb2_to_linux_error-by-half/20251206-232731
base: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link: https://lore.kernel.org/r/20251206151826.2932970-9-chenxiaosong.chenxiaosong%40linux.dev
patch subject: [PATCH v4 08/10] smb/client: introduce smb2maperror KUnit tests
config: i386-randconfig-063-20251208 (https://download.01.org/0day-ci/archive/20251209/202512091143.FGQ64S2k-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251209/202512091143.FGQ64S2k-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512091143.FGQ64S2k-lkp@intel.com/
All errors (new ones prefixed by >>):
ld: fs/smb/client/smb2maperror.o: in function `test_cmp_map':
>> fs/smb/client/smb2maperror_test.c:40:(.text+0x1d4): undefined reference to `kunit_binary_str_assert_format'
>> ld: fs/smb/client/smb2maperror_test.c:40:(.text+0x20c): undefined reference to `__kunit_do_failed_assertion'
>> ld: fs/smb/client/smb2maperror_test.c:39:(.text+0x245): undefined reference to `kunit_binary_assert_format'
ld: fs/smb/client/smb2maperror_test.c:39:(.text+0x263): undefined reference to `__kunit_do_failed_assertion'
ld: fs/smb/client/smb2maperror_test.c:38:(.text+0x274): undefined reference to `kunit_binary_assert_format'
ld: fs/smb/client/smb2maperror_test.c:38:(.text+0x2ba): undefined reference to `__kunit_do_failed_assertion'
>> ld: fs/smb/client/smb2maperror_test.c:37:(.text+0x2da): undefined reference to `kunit_binary_ptr_assert_format'
ld: fs/smb/client/smb2maperror_test.c:37:(.text+0x314): undefined reference to `__kunit_do_failed_assertion'
ld: fs/smb/client/smb2maperror.o: in function `maperror_test_check_sort':
>> fs/smb/client/smb2maperror_test.c:28:(.text.unlikely+0x4b): undefined reference to `kunit_binary_assert_format'
>> ld: fs/smb/client/smb2maperror_test.c:28:(.text.unlikely+0x60): undefined reference to `__kunit_do_failed_assertion'
vim +40 fs/smb/client/smb2maperror_test.c
12
13 static void maperror_test_check_sort(struct kunit *test)
14 {
15 bool is_sorted = true;
16 unsigned int i;
17
18 for (i = 1; i < err_map_num; i++) {
19 if (smb2_error_map_table[i].smb2_status >=
20 smb2_error_map_table[i - 1].smb2_status)
21 continue;
22
23 pr_err("smb2_error_map_table array order is incorrect\n");
24 is_sorted = false;
25 break;
26 }
27
> 28 KUNIT_EXPECT_EQ(test, true, is_sorted);
29 }
30
31 static void
32 test_cmp_map(struct kunit *test, struct status_to_posix_error *expect)
33 {
34 struct status_to_posix_error *result;
35
36 result = smb2_get_err_map(expect->smb2_status);
> 37 KUNIT_EXPECT_PTR_NE(test, NULL, result);
38 KUNIT_EXPECT_EQ(test, expect->smb2_status, result->smb2_status);
> 39 KUNIT_EXPECT_EQ(test, expect->posix_error, result->posix_error);
> 40 KUNIT_EXPECT_STREQ(test, expect->status_string, result->status_string);
41 }
42
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Hi, kernel test robot noticed the following build errors: [auto build test ERROR on brauner-vfs/vfs.all] [also build test ERROR on linus/master v6.18] [cannot apply to cifs/for-next next-20251208] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/chenxiaosong-chenxiaosong-linux-dev/smb-client-reduce-loop-count-in-map_smb2_to_linux_error-by-half/20251206-232731 base: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all patch link: https://lore.kernel.org/r/20251206151826.2932970-9-chenxiaosong.chenxiaosong%40linux.dev patch subject: [PATCH v4 08/10] smb/client: introduce smb2maperror KUnit tests config: i386-randconfig-004-20251208 (https://download.01.org/0day-ci/archive/20251209/202512090558.gcQGSnU4-lkp@intel.com/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251209/202512090558.gcQGSnU4-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202512090558.gcQGSnU4-lkp@intel.com/ All errors (new ones prefixed by >>): >> ld.lld: error: undefined symbol: kunit_binary_str_assert_format >>> referenced by smb2maperror_test.c:40 (fs/smb/client/smb2maperror_test.c:40) >>> fs/smb/client/smb2maperror.o:(maperror_test_check_search) in archive vmlinux.a -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
© 2016 - 2025 Red Hat, Inc.