Skip to content

Commit e3ac635

Browse files
tangyoulingsmfrench
authored andcommitted
smb/client: introduce KUnit test to check ntstatus_to_dos_map search
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>
1 parent 010ad1e commit e3ac635

5 files changed

Lines changed: 95 additions & 0 deletions

File tree

fs/smb/client/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,15 @@ config CIFS_COMPRESSION
217217
Say Y here if you want SMB traffic to be compressed.
218218
If unsure, say N.
219219

220+
config SMB1_KUNIT_TESTS
221+
tristate "KUnit tests for SMB1"
222+
depends on SMB_KUNIT_TESTS && CIFS_ALLOW_INSECURE_LEGACY
223+
default SMB_KUNIT_TESTS
224+
help
225+
This builds the SMB1-specific KUnit tests.
226+
227+
These tests are only enabled when legacy insecure SMB1 support
228+
(CIFS_ALLOW_INSECURE_LEGACY) is enabled.
229+
230+
If unsure, say N.
220231
endif

fs/smb/client/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ $(obj)/smb2maperror.o: $(obj)/smb2_mapping_table.c
7171
quiet_cmd_gen_smb2_mapping = GEN $@
7272
cmd_gen_smb2_mapping = perl $(src)/gen_smb2_mapping $< $@
7373

74+
obj-$(CONFIG_SMB1_KUNIT_TESTS) += smb1maperror_test.o
7475
obj-$(CONFIG_SMB_KUNIT_TESTS) += smb2maperror_test.o
7576

7677
# Let Kbuild handle tracking and cleaning

fs/smb/client/smb1maperror.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,22 @@ int __init smb1_init_maperror(void)
288288

289289
return rc;
290290
}
291+
292+
#if IS_ENABLED(CONFIG_SMB1_KUNIT_TESTS)
293+
#define EXPORT_SYMBOL_FOR_SMB_TEST(sym) \
294+
EXPORT_SYMBOL_FOR_MODULES(sym, "smb1maperror_test")
295+
296+
const struct ntstatus_to_dos_err *
297+
search_ntstatus_to_dos_map_test(__u32 ntstatus)
298+
{
299+
return search_ntstatus_to_dos_map(ntstatus);
300+
}
301+
EXPORT_SYMBOL_FOR_SMB_TEST(search_ntstatus_to_dos_map_test);
302+
303+
const struct ntstatus_to_dos_err *
304+
ntstatus_to_dos_map_test = ntstatus_to_dos_map;
305+
EXPORT_SYMBOL_FOR_SMB_TEST(ntstatus_to_dos_map_test);
306+
307+
unsigned int ntstatus_to_dos_num = ARRAY_SIZE(ntstatus_to_dos_map);
308+
EXPORT_SYMBOL_FOR_SMB_TEST(ntstatus_to_dos_num);
309+
#endif

fs/smb/client/smb1maperror_test.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
*
4+
* KUnit tests of SMB1 maperror
5+
*
6+
* Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
7+
* Author(s): Youling Tang <tangyouling@kylinos.cn>
8+
* ChenXiaoSong <chenxiaosong@kylinos.cn>
9+
*
10+
*/
11+
12+
#include <kunit/test.h>
13+
#include "smb1proto.h"
14+
#include "nterr.h"
15+
16+
#define DEFINE_CHECK_SEARCH_FUNC(__struct_name, __field, \
17+
__array, __num) \
18+
static void check_search_ ## __array(struct kunit *test) \
19+
{ \
20+
unsigned int i; \
21+
const struct __struct_name *expect, *result; \
22+
\
23+
for (i = 0; i < __num; i++) { \
24+
expect = &__array ## _test[i]; \
25+
result = search_ ## __array ## _test(expect->__field); \
26+
KUNIT_ASSERT_NOT_NULL(test, result); \
27+
test_cmp_ ## __struct_name(test, expect, result); \
28+
} \
29+
}
30+
31+
static void
32+
test_cmp_ntstatus_to_dos_err(struct kunit *test,
33+
const struct ntstatus_to_dos_err *expect,
34+
const struct ntstatus_to_dos_err *result)
35+
{
36+
KUNIT_EXPECT_EQ(test, expect->dos_class, result->dos_class);
37+
KUNIT_EXPECT_EQ(test, expect->dos_code, result->dos_code);
38+
KUNIT_EXPECT_EQ(test, expect->ntstatus, result->ntstatus);
39+
KUNIT_EXPECT_STREQ(test, expect->nt_errstr, result->nt_errstr);
40+
}
41+
42+
/* check_search_ntstatus_to_dos_map */
43+
DEFINE_CHECK_SEARCH_FUNC(ntstatus_to_dos_err, ntstatus, ntstatus_to_dos_map,
44+
ntstatus_to_dos_num);
45+
46+
static struct kunit_case maperror_test_cases[] = {
47+
KUNIT_CASE(check_search_ntstatus_to_dos_map),
48+
{}
49+
};
50+
51+
static struct kunit_suite maperror_suite = {
52+
.name = "smb1_maperror",
53+
.test_cases = maperror_test_cases,
54+
};
55+
56+
kunit_test_suite(maperror_suite);
57+
58+
MODULE_LICENSE("GPL");

fs/smb/client/smb1proto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ int map_smb_to_linux_error(char *buf, bool logErr);
237237
int smb1_init_maperror(void);
238238
int map_and_check_smb_error(struct TCP_Server_Info *server,
239239
struct mid_q_entry *mid, bool logErr);
240+
#if IS_ENABLED(CONFIG_SMB1_KUNIT_TESTS)
241+
extern const struct ntstatus_to_dos_err *ntstatus_to_dos_map_test;
242+
extern unsigned int ntstatus_to_dos_num;
243+
const struct ntstatus_to_dos_err *
244+
search_ntstatus_to_dos_map_test(__u32 ntstatus);
245+
#endif
240246

241247
/*
242248
* smb1misc.c

0 commit comments

Comments
 (0)