Skip to content

Commit 480afcb

Browse files
ChenXiaoSongsmfrench
authored andcommitted
smb/client: introduce KUnit test to check search result of smb2_error_map_table
The KUnit test 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_search() checks whether all elements can be correctly found in the array. Suggested-by: David Howells <dhowells@redhat.com> Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 75563ad commit 480afcb

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

fs/smb/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,20 @@ config SMBFS
99
tristate
1010
default y if CIFS=y || SMB_SERVER=y
1111
default m if CIFS=m || SMB_SERVER=m
12+
13+
config SMB_KUNIT_TESTS
14+
tristate "KUnit tests for SMB" if !KUNIT_ALL_TESTS
15+
depends on SMBFS && KUNIT
16+
default KUNIT_ALL_TESTS
17+
help
18+
This builds the SMB KUnit tests.
19+
20+
KUnit tests run during boot and output the results to the debug log
21+
in TAP format (https://testanything.org/). Only useful for kernel devs
22+
running KUnit test harness and are not for inclusion into a production
23+
build.
24+
25+
For more information on KUnit and unit tests in general please refer
26+
to the KUnit documentation in Documentation/dev-tools/kunit/.
27+
28+
If unsure, say N.

fs/smb/client/smb2maperror.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,11 @@ int __init smb2_init_maperror(void)
114114

115115
return 0;
116116
}
117+
118+
#define SMB_CLIENT_KUNIT_AVAILABLE \
119+
((IS_MODULE(CONFIG_CIFS) && IS_ENABLED(CONFIG_KUNIT)) || \
120+
(IS_BUILTIN(CONFIG_CIFS) && IS_BUILTIN(CONFIG_KUNIT)))
121+
122+
#if SMB_CLIENT_KUNIT_AVAILABLE && IS_ENABLED(CONFIG_SMB_KUNIT_TESTS)
123+
#include "smb2maperror_test.c"
124+
#endif /* CONFIG_SMB_KUNIT_TESTS */

fs/smb/client/smb2maperror_test.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: LGPL-2.1
2+
/*
3+
*
4+
* KUnit tests of SMB2 maperror
5+
*
6+
* Copyright (C) 2025 KylinSoft Co., Ltd. All rights reserved.
7+
* Author(s): ChenXiaoSong <chenxiaosong@kylinos.cn>
8+
*
9+
*/
10+
11+
#include <kunit/test.h>
12+
13+
static void
14+
test_cmp_map(struct kunit *test, const struct status_to_posix_error *expect)
15+
{
16+
const struct status_to_posix_error *result;
17+
18+
result = smb2_get_err_map(expect->smb2_status);
19+
KUNIT_EXPECT_PTR_NE(test, NULL, result);
20+
KUNIT_EXPECT_EQ(test, expect->smb2_status, result->smb2_status);
21+
KUNIT_EXPECT_EQ(test, expect->posix_error, result->posix_error);
22+
KUNIT_EXPECT_STREQ(test, expect->status_string, result->status_string);
23+
}
24+
25+
static void maperror_test_check_search(struct kunit *test)
26+
{
27+
unsigned int i;
28+
29+
for (i = 0; i < ARRAY_SIZE(smb2_error_map_table); i++)
30+
test_cmp_map(test, &smb2_error_map_table[i]);
31+
}
32+
33+
static struct kunit_case maperror_test_cases[] = {
34+
KUNIT_CASE(maperror_test_check_search),
35+
{}
36+
};
37+
38+
static struct kunit_suite maperror_suite = {
39+
.name = "smb2_maperror",
40+
.test_cases = maperror_test_cases,
41+
};
42+
43+
kunit_test_suite(maperror_suite);
44+
45+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)