xref: /linux/fs/hfs/string_test.c (revision 6dfafbd0299a60bfb5d5e277fdf100037c7ded07)
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");
134