]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
smb/client: introduce KUnit test to check ntstatus_to_dos_map search
authorYouling Tang <tangyouling@kylinos.cn>
Thu, 2 Apr 2026 14:18:33 +0000 (14:18 +0000)
committerSteve French <stfrench@microsoft.com>
Mon, 6 Apr 2026 00:58:40 +0000 (19:58 -0500)
Check whether all elements can be correctly found in the array.

Introduce CONFIG_SMB1_KUNIT_TESTS for smb1maperror_test.ko since
smb1maperror.o is only built when CONFIG_CIFS_ALLOW_INSECURE_LEGACY
is enabled.

We are going to define 3 functions to check the search results, introduce
the macro DEFINE_CHECK_SEARCH_FUNC() to reduce duplicate code.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/smb/client/Kconfig
fs/smb/client/Makefile
fs/smb/client/smb1maperror.c
fs/smb/client/smb1maperror_test.c [new file with mode: 0644]
fs/smb/client/smb1proto.h

index 17bd368574e942e80b8519b0df3ed29ca376c504..2e72d3c8423fa89a215a96b5f90057700aa5ebc3 100644 (file)
@@ -217,4 +217,15 @@ config CIFS_COMPRESSION
          Say Y here if you want SMB traffic to be compressed.
          If unsure, say N.
 
+config SMB1_KUNIT_TESTS
+       tristate "KUnit tests for SMB1"
+       depends on SMB_KUNIT_TESTS && CIFS_ALLOW_INSECURE_LEGACY
+       default SMB_KUNIT_TESTS
+       help
+         This builds the SMB1-specific KUnit tests.
+
+         These tests are only enabled when legacy insecure SMB1 support
+         (CIFS_ALLOW_INSECURE_LEGACY) is enabled.
+
+         If unsure, say N.
 endif
index 8560fe99ec2bf2d07437ec2bbc3daf0aa128a726..220a97c8a488d55c505371c13d4de1b3f8f83a2b 100644 (file)
@@ -71,6 +71,7 @@ $(obj)/smb2maperror.o: $(obj)/smb2_mapping_table.c
 quiet_cmd_gen_smb2_mapping = GEN     $@
       cmd_gen_smb2_mapping = perl $(src)/gen_smb2_mapping $< $@
 
+obj-$(CONFIG_SMB1_KUNIT_TESTS) += smb1maperror_test.o
 obj-$(CONFIG_SMB_KUNIT_TESTS) += smb2maperror_test.o
 
 # Let Kbuild handle tracking and cleaning
index 66ceebbe535ed6df1cdab78bd7ec7acfef03486f..419057f296a75a24ffd0eb2f0b30095f3b3c5483 100644 (file)
@@ -288,3 +288,22 @@ int __init smb1_init_maperror(void)
 
        return rc;
 }
+
+#if IS_ENABLED(CONFIG_SMB1_KUNIT_TESTS)
+#define EXPORT_SYMBOL_FOR_SMB_TEST(sym) \
+       EXPORT_SYMBOL_FOR_MODULES(sym, "smb1maperror_test")
+
+const struct ntstatus_to_dos_err *
+search_ntstatus_to_dos_map_test(__u32 ntstatus)
+{
+       return search_ntstatus_to_dos_map(ntstatus);
+}
+EXPORT_SYMBOL_FOR_SMB_TEST(search_ntstatus_to_dos_map_test);
+
+const struct ntstatus_to_dos_err *
+ntstatus_to_dos_map_test = ntstatus_to_dos_map;
+EXPORT_SYMBOL_FOR_SMB_TEST(ntstatus_to_dos_map_test);
+
+unsigned int ntstatus_to_dos_num = ARRAY_SIZE(ntstatus_to_dos_map);
+EXPORT_SYMBOL_FOR_SMB_TEST(ntstatus_to_dos_num);
+#endif
diff --git a/fs/smb/client/smb1maperror_test.c b/fs/smb/client/smb1maperror_test.c
new file mode 100644 (file)
index 0000000..1a2d1b9
--- /dev/null
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *
+ *   KUnit tests of SMB1 maperror
+ *
+ *   Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+ *   Author(s): Youling Tang <tangyouling@kylinos.cn>
+ *              ChenXiaoSong <chenxiaosong@kylinos.cn>
+ *
+ */
+
+#include <kunit/test.h>
+#include "smb1proto.h"
+#include "nterr.h"
+
+#define DEFINE_CHECK_SEARCH_FUNC(__struct_name, __field,               \
+                                __array, __num)                        \
+static void check_search_ ## __array(struct kunit *test)               \
+{                                                                      \
+       unsigned int i;                                                 \
+       const struct __struct_name *expect, *result;                    \
+                                                                       \
+       for (i = 0; i < __num; i++) {                                   \
+               expect = &__array ## _test[i];                          \
+               result = search_ ## __array ## _test(expect->__field);  \
+               KUNIT_ASSERT_NOT_NULL(test, result);                    \
+               test_cmp_ ## __struct_name(test, expect, result);       \
+       }                                                               \
+}
+
+static void
+test_cmp_ntstatus_to_dos_err(struct kunit *test,
+                            const struct ntstatus_to_dos_err *expect,
+                            const struct ntstatus_to_dos_err *result)
+{
+       KUNIT_EXPECT_EQ(test, expect->dos_class, result->dos_class);
+       KUNIT_EXPECT_EQ(test, expect->dos_code, result->dos_code);
+       KUNIT_EXPECT_EQ(test, expect->ntstatus, result->ntstatus);
+       KUNIT_EXPECT_STREQ(test, expect->nt_errstr, result->nt_errstr);
+}
+
+/* check_search_ntstatus_to_dos_map */
+DEFINE_CHECK_SEARCH_FUNC(ntstatus_to_dos_err, ntstatus, ntstatus_to_dos_map,
+                        ntstatus_to_dos_num);
+
+static struct kunit_case maperror_test_cases[] = {
+       KUNIT_CASE(check_search_ntstatus_to_dos_map),
+       {}
+};
+
+static struct kunit_suite maperror_suite = {
+       .name = "smb1_maperror",
+       .test_cases = maperror_test_cases,
+};
+
+kunit_test_suite(maperror_suite);
+
+MODULE_LICENSE("GPL");
index dd98d04e837aa8a224405a23191d10a928c478f9..5ec158df1c748daa588891b1587790a546fc39d2 100644 (file)
@@ -237,6 +237,12 @@ int map_smb_to_linux_error(char *buf, bool logErr);
 int smb1_init_maperror(void);
 int map_and_check_smb_error(struct TCP_Server_Info *server,
                            struct mid_q_entry *mid, bool logErr);
+#if IS_ENABLED(CONFIG_SMB1_KUNIT_TESTS)
+extern const struct ntstatus_to_dos_err *ntstatus_to_dos_map_test;
+extern unsigned int ntstatus_to_dos_num;
+const struct ntstatus_to_dos_err *
+search_ntstatus_to_dos_map_test(__u32 ntstatus);
+#endif
 
 /*
  * smb1misc.c