Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hfs: introduce KUnit tests for HFS string operations

This patch implements the initial Kunit based set of
unit tests for HFS string operations. It checks
functionality of hfs_strcmp(), hfs_hash_dentry(),
and hfs_compare_dentry() methods.

./tools/testing/kunit/kunit.py run --kunitconfig ./fs/hfs/.kunitconfig

[16:04:50] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
[16:04:51] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=22
[16:04:59] Starting KUnit Kernel (1/1)...
[16:04:59] ============================================================
Running tests with:
$ .kunit/linux kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
[16:04:59] ================= hfs_string (3 subtests) ==================
[16:04:59] [PASSED] hfs_strcmp_test
[16:04:59] [PASSED] hfs_hash_dentry_test
[16:04:59] [PASSED] hfs_compare_dentry_test
[16:04:59] =================== [PASSED] hfs_string ====================
[16:04:59] ============================================================
[16:04:59] Testing complete. Ran 3 tests: passed: 3
[16:04:59] Elapsed time: 9.087s total, 1.310s configuring, 7.611s building, 0.125s running

v2
Fix linker error.

v3
Chen Linxuan suggested to use EXPORT_SYMBOL_IF_KUNIT.

Signed-off-by: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com>
cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
cc: Yangtao Li <frank.li@vivo.com>
cc: linux-fsdevel@vger.kernel.org
cc: Chen Linxuan <me@black-desk.cn>
Reviewed-by: Chen Linxuan <me@black-desk.cn>
Signed-off-by: Viacheslav Dubeyko <slava@dubeyko.com>
Link: https://lore.kernel.org/r/20250912225022.1083313-1-slava@dubeyko.com
Signed-off-by: Viacheslav Dubeyko <slava@dubeyko.com>

authored by

Viacheslav Dubeyko and committed by
Viacheslav Dubeyko
150ec68f 24e17a29

+162
+7
fs/hfs/.kunitconfig
··· 1 + CONFIG_KUNIT=y 2 + CONFIG_HFS_FS=y 3 + CONFIG_HFS_KUNIT_TEST=y 4 + CONFIG_BLOCK=y 5 + CONFIG_BUFFER_HEAD=y 6 + CONFIG_NLS=y 7 + CONFIG_LEGACY_DIRECT_IO=y
+15
fs/hfs/Kconfig
··· 13 13 14 14 To compile this file system support as a module, choose M here: the 15 15 module will be called hfs. 16 + 17 + config HFS_KUNIT_TEST 18 + tristate "KUnit tests for HFS filesystem" if !KUNIT_ALL_TESTS 19 + depends on HFS_FS && KUNIT 20 + default KUNIT_ALL_TESTS 21 + help 22 + This builds KUnit tests for the HFS filesystem. 23 + 24 + KUnit tests run during boot and output the results to the debug 25 + log in TAP format (https://testanything.org/). Only useful for 26 + kernel devs running KUnit test harness and are not for inclusion 27 + into a production build. 28 + 29 + For more information on KUnit and unit tests in general please 30 + refer to the KUnit documentation in Documentation/dev-tools/kunit/.
+2
fs/hfs/Makefile
··· 9 9 catalog.o dir.o extent.o inode.o attr.o mdb.o \ 10 10 part_tbl.o string.o super.o sysdep.o trans.o 11 11 12 + # KUnit tests 13 + obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o
+5
fs/hfs/string.c
··· 16 16 #include "hfs_fs.h" 17 17 #include <linux/dcache.h> 18 18 19 + #include <kunit/visibility.h> 20 + 19 21 /*================ File-local variables ================*/ 20 22 21 23 /* ··· 67 65 this->hash = end_name_hash(hash); 68 66 return 0; 69 67 } 68 + EXPORT_SYMBOL_IF_KUNIT(hfs_hash_dentry); 70 69 71 70 /* 72 71 * Compare two strings in the HFS filename character ordering ··· 90 87 } 91 88 return len1 - len2; 92 89 } 90 + EXPORT_SYMBOL_IF_KUNIT(hfs_strcmp); 93 91 94 92 /* 95 93 * Test for equality of two strings in the HFS filename character ordering. ··· 116 112 } 117 113 return 0; 118 114 } 115 + EXPORT_SYMBOL_IF_KUNIT(hfs_compare_dentry);
+133
fs/hfs/string_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * KUnit tests for HFS string operations 4 + * 5 + * Copyright (C) 2025 Viacheslav Dubeyko <slava@dubeyko.com> 6 + */ 7 + 8 + #include <kunit/test.h> 9 + #include <linux/dcache.h> 10 + #include "hfs_fs.h" 11 + 12 + /* Test hfs_strcmp function */ 13 + static void hfs_strcmp_test(struct kunit *test) 14 + { 15 + /* Test equal strings */ 16 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("hello", 5, "hello", 5)); 17 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("test", 4, "test", 4)); 18 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("", 0, "", 0)); 19 + 20 + /* Test unequal strings */ 21 + KUNIT_EXPECT_NE(test, 0, hfs_strcmp("hello", 5, "world", 5)); 22 + KUNIT_EXPECT_NE(test, 0, hfs_strcmp("test", 4, "testing", 7)); 23 + 24 + /* Test different lengths */ 25 + KUNIT_EXPECT_LT(test, hfs_strcmp("test", 4, "testing", 7), 0); 26 + KUNIT_EXPECT_GT(test, hfs_strcmp("testing", 7, "test", 4), 0); 27 + 28 + /* Test case insensitive comparison (HFS should handle case) */ 29 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("Test", 4, "TEST", 4)); 30 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("hello", 5, "HELLO", 5)); 31 + 32 + /* Test with special characters */ 33 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("file.txt", 8, "file.txt", 8)); 34 + KUNIT_EXPECT_NE(test, 0, hfs_strcmp("file.txt", 8, "file.dat", 8)); 35 + 36 + /* Test boundary cases */ 37 + KUNIT_EXPECT_EQ(test, 0, hfs_strcmp("a", 1, "a", 1)); 38 + KUNIT_EXPECT_NE(test, 0, hfs_strcmp("a", 1, "b", 1)); 39 + } 40 + 41 + /* Test hfs_hash_dentry function */ 42 + static void hfs_hash_dentry_test(struct kunit *test) 43 + { 44 + struct qstr test_name1, test_name2, test_name3; 45 + struct dentry dentry = {}; 46 + char name1[] = "testfile"; 47 + char name2[] = "TestFile"; 48 + char name3[] = "different"; 49 + 50 + /* Initialize test strings */ 51 + test_name1.name = name1; 52 + test_name1.len = strlen(name1); 53 + test_name1.hash = 0; 54 + 55 + test_name2.name = name2; 56 + test_name2.len = strlen(name2); 57 + test_name2.hash = 0; 58 + 59 + test_name3.name = name3; 60 + test_name3.len = strlen(name3); 61 + test_name3.hash = 0; 62 + 63 + /* Test hashing */ 64 + KUNIT_EXPECT_EQ(test, 0, hfs_hash_dentry(&dentry, &test_name1)); 65 + KUNIT_EXPECT_EQ(test, 0, hfs_hash_dentry(&dentry, &test_name2)); 66 + KUNIT_EXPECT_EQ(test, 0, hfs_hash_dentry(&dentry, &test_name3)); 67 + 68 + /* Case insensitive names should hash the same */ 69 + KUNIT_EXPECT_EQ(test, test_name1.hash, test_name2.hash); 70 + 71 + /* Different names should have different hashes */ 72 + KUNIT_EXPECT_NE(test, test_name1.hash, test_name3.hash); 73 + } 74 + 75 + /* Test hfs_compare_dentry function */ 76 + static void hfs_compare_dentry_test(struct kunit *test) 77 + { 78 + struct qstr test_name; 79 + struct dentry dentry = {}; 80 + char name[] = "TestFile"; 81 + 82 + test_name.name = name; 83 + test_name.len = strlen(name); 84 + 85 + /* Test exact match */ 86 + KUNIT_EXPECT_EQ(test, 0, hfs_compare_dentry(&dentry, 8, 87 + "TestFile", &test_name)); 88 + 89 + /* Test case insensitive match */ 90 + KUNIT_EXPECT_EQ(test, 0, hfs_compare_dentry(&dentry, 8, 91 + "testfile", &test_name)); 92 + KUNIT_EXPECT_EQ(test, 0, hfs_compare_dentry(&dentry, 8, 93 + "TESTFILE", &test_name)); 94 + 95 + /* Test different names */ 96 + KUNIT_EXPECT_EQ(test, 1, hfs_compare_dentry(&dentry, 8, 97 + "DiffFile", &test_name)); 98 + 99 + /* Test different lengths */ 100 + KUNIT_EXPECT_EQ(test, 1, hfs_compare_dentry(&dentry, 7, 101 + "TestFil", &test_name)); 102 + KUNIT_EXPECT_EQ(test, 1, hfs_compare_dentry(&dentry, 9, 103 + "TestFiles", &test_name)); 104 + 105 + /* Test empty string */ 106 + test_name.name = ""; 107 + test_name.len = 0; 108 + KUNIT_EXPECT_EQ(test, 0, hfs_compare_dentry(&dentry, 0, "", &test_name)); 109 + 110 + /* Test HFS_NAMELEN boundary */ 111 + test_name.name = "This_is_a_very_long_filename_that_exceeds_normal_limits"; 112 + test_name.len = strlen(test_name.name); 113 + KUNIT_EXPECT_EQ(test, 0, hfs_compare_dentry(&dentry, HFS_NAMELEN, 114 + "This_is_a_very_long_filename_th", &test_name)); 115 + } 116 + 117 + static struct kunit_case hfs_string_test_cases[] = { 118 + KUNIT_CASE(hfs_strcmp_test), 119 + KUNIT_CASE(hfs_hash_dentry_test), 120 + KUNIT_CASE(hfs_compare_dentry_test), 121 + {} 122 + }; 123 + 124 + static struct kunit_suite hfs_string_test_suite = { 125 + .name = "hfs_string", 126 + .test_cases = hfs_string_test_cases, 127 + }; 128 + 129 + kunit_test_suite(hfs_string_test_suite); 130 + 131 + MODULE_DESCRIPTION("KUnit tests for HFS string operations"); 132 + MODULE_LICENSE("GPL"); 133 + MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");