mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] exfat: add KUnit tests for checksum and entry count helpers
@ 2026-11-30  1:44 Andrey Zakharov
  0 siblings, 0 replies; only message in thread
From: Andrey Zakharov @ 2026-11-30  1:44 UTC (permalink / raw)
  To: Namjae Jeon, Sungjong Seo
  Cc: Yuezhang Mo, exfat, linux-kernel, Andrey Zakharov

Add a small KUnit suite covering exfat_calc_chksum16(),
exfat_calc_chksum32() and exfat_calc_num_entries(), which have no
tests so far. The tests check that the skipped byte offsets depend on
the checksum type, that a checksum carried over between two calls
matches a single call, and the boundary values of the entry count.

The three helpers are exported with EXPORT_SYMBOL_IF_KUNIT() so the
test can be built as a separate module, as done for hfs and hfsplus.
The expected values were computed with a separate script that uses the
same formula as the code, so the tests pin the current behaviour; they
were not compared with the exFAT specification.

Tested with kunit.py (ARCH=um, EXFAT_FS=y): 3 of 3 pass, and changing
the skipped offset in exfat_calc_chksum16() makes
exfat_chksum16_test fail. Not tested: building the tests as a module,
other architectures, and a mutation of the other two helpers.

Signed-off-by: Andrey Zakharov <dedemorow@gmail.com>
---
Hi,

A few questions, in case they help with review:

 - Is it fine to add EXPORT_SYMBOL_IF_KUNIT() to misc.c and dir.c for
   these three helpers, the way hfs and hfsplus do it? I was not sure
   whether you would prefer another way for the test to reach them.
 - Would you rather keep the test in fs/exfat/ as a separate module, as
   done here, or somewhere else?
 - Is a new fs/exfat/.kunitconfig welcome, or is it better left out?

This is based on exfat/dev at 833f6b288993.

 fs/exfat/.kunitconfig |  6 +++
 fs/exfat/Kconfig      | 13 +++++++
 fs/exfat/Makefile     |  3 ++
 fs/exfat/dir.c        |  2 +
 fs/exfat/exfat_test.c | 89 +++++++++++++++++++++++++++++++++++++++++++
 fs/exfat/misc.c       |  3 ++
 6 files changed, 116 insertions(+)
 create mode 100644 fs/exfat/.kunitconfig
 create mode 100644 fs/exfat/exfat_test.c

diff --git a/fs/exfat/.kunitconfig b/fs/exfat/.kunitconfig
new file mode 100644
index 000000000000..e5e77d8e72d9
--- /dev/null
+++ b/fs/exfat/.kunitconfig
@@ -0,0 +1,6 @@
+CONFIG_KUNIT=y
+CONFIG_EXFAT_FS=y
+CONFIG_EXFAT_KUNIT_TEST=y
+CONFIG_BLOCK=y
+CONFIG_BUFFER_HEAD=y
+CONFIG_NLS=y
diff --git a/fs/exfat/Kconfig b/fs/exfat/Kconfig
index 1fcb10c8d7bc..765a594a1c99 100644
--- a/fs/exfat/Kconfig
+++ b/fs/exfat/Kconfig
@@ -22,3 +22,16 @@ config EXFAT_DEFAULT_IOCHARSET
 	  filenames and the UTF-16 character encoding that the exFAT
 	  filesystem uses.  This can be overridden with the "iocharset" mount
 	  option for the exFAT filesystems.
+
+config EXFAT_KUNIT_TEST
+	tristate "KUnit tests for exFAT helper functions" if !KUNIT_ALL_TESTS
+	depends on EXFAT_FS && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds KUnit tests for exFAT helper functions. Only useful
+	  for kernel developers running the KUnit test harness.
+
+	  For more information on KUnit and unit tests in general, please
+	  refer to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
diff --git a/fs/exfat/Makefile b/fs/exfat/Makefile
index e06bf85870ae..dea1e80efc6a 100644
--- a/fs/exfat/Makefile
+++ b/fs/exfat/Makefile
@@ -6,3 +6,6 @@ obj-$(CONFIG_EXFAT_FS) += exfat.o
 
 exfat-y	:= inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \
 	   file.o balloc.o iomap.o
+
+# KUnit tests
+obj-$(CONFIG_EXFAT_KUNIT_TEST) += exfat_test.o
diff --git a/fs/exfat/dir.c b/fs/exfat/dir.c
index 46514b13bebd..1048e6c28e2a 100644
--- a/fs/exfat/dir.c
+++ b/fs/exfat/dir.c
@@ -8,6 +8,7 @@
 #include <linux/bio.h>
 #include <linux/buffer_head.h>
 #include <linux/filelock.h>
+#include <kunit/visibility.h>
 
 #include "exfat_raw.h"
 #include "exfat_fs.h"
@@ -319,6 +320,7 @@ int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
 	/* 1 file entry + 1 stream entry + name entries */
 	return ES_ENTRY_NUM(len);
 }
+EXPORT_SYMBOL_IF_KUNIT(exfat_calc_num_entries);
 
 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
 {
diff --git a/fs/exfat/exfat_test.c b/fs/exfat/exfat_test.c
new file mode 100644
index 000000000000..bd10e621d34b
--- /dev/null
+++ b/fs/exfat/exfat_test.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * KUnit tests for exFAT helper functions
+ */
+
+#include <kunit/test.h>
+#include <linux/string.h>
+
+#include "exfat_raw.h"
+#include "exfat_fs.h"
+
+static void exfat_chksum16_test(struct kunit *test)
+{
+	u8 a[32], b[32];
+	int i;
+
+	for (i = 0; i < 32; i++)
+		a[i] = i;
+	memcpy(b, a, sizeof(a));
+	b[2] ^= 0xff;
+	b[3] ^= 0x55;
+
+	/* Bytes 2 and 3 are skipped for directory entries only. */
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum16(a, 32, 0, CS_DIR_ENTRY), (u16)0x9);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum16(b, 32, 0, CS_DIR_ENTRY), (u16)0x9);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum16(a, 32, 0, CS_DEFAULT), (u16)0x40);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum16(b, 32, 0, CS_DEFAULT), (u16)0xd48);
+
+	/* Splitting a buffer must give the same result. */
+	KUNIT_EXPECT_EQ(test,
+			exfat_calc_chksum16(a + 16, 16,
+					    exfat_calc_chksum16(a, 16, 0, CS_DEFAULT),
+					    CS_DEFAULT),
+			(u16)0x40);
+}
+
+static void exfat_chksum32_test(struct kunit *test)
+{
+	u8 x[128], y[128];
+	int i;
+
+	for (i = 0; i < 128; i++)
+		x[i] = i;
+	memcpy(y, x, sizeof(x));
+	y[106] ^= 0xff;
+	y[107] ^= 0xff;
+	y[112] ^= 0xff;
+
+	/* Bytes 106, 107 and 112 are skipped for the boot sector only. */
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum32(x, 128, 0, CS_BOOT_SECTOR), 0xfffb811bU);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum32(y, 128, 0, CS_BOOT_SECTOR), 0xfffb811bU);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum32(x, 128, 0, CS_DEFAULT), 0x100U);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum32(y, 128, 0, CS_DEFAULT), 0x41e900U);
+	KUNIT_EXPECT_EQ(test, exfat_calc_chksum32(x, 0, 0x1234, CS_DEFAULT), 0x1234U);
+}
+
+static void exfat_calc_num_entries_test(struct kunit *test)
+{
+	struct exfat_uni_name name = {};
+
+	name.name_len = 0;
+	KUNIT_EXPECT_EQ(test, exfat_calc_num_entries(&name), -EINVAL);
+	name.name_len = 1;
+	KUNIT_EXPECT_EQ(test, exfat_calc_num_entries(&name), 3);
+	name.name_len = 15;
+	KUNIT_EXPECT_EQ(test, exfat_calc_num_entries(&name), 3);
+	name.name_len = 16;
+	KUNIT_EXPECT_EQ(test, exfat_calc_num_entries(&name), 4);
+	name.name_len = MAX_NAME_LENGTH;
+	KUNIT_EXPECT_EQ(test, exfat_calc_num_entries(&name), 19);
+}
+
+static struct kunit_case exfat_test_cases[] = {
+	KUNIT_CASE(exfat_chksum16_test),
+	KUNIT_CASE(exfat_chksum32_test),
+	KUNIT_CASE(exfat_calc_num_entries_test),
+	{}
+};
+
+static struct kunit_suite exfat_test_suite = {
+	.name = "exfat",
+	.test_cases = exfat_test_cases,
+};
+
+kunit_test_suite(exfat_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for exFAT helper functions");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c
index dfd0bbf31c94..a7436ef63807 100644
--- a/fs/exfat/misc.c
+++ b/fs/exfat/misc.c
@@ -11,6 +11,7 @@
 #include <linux/slab.h>
 #include <linux/buffer_head.h>
 #include <linux/blk_types.h>
+#include <kunit/visibility.h>
 
 #include "exfat_raw.h"
 #include "exfat_fs.h"
@@ -146,6 +147,7 @@ u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
 	}
 	return chksum;
 }
+EXPORT_SYMBOL_IF_KUNIT(exfat_calc_chksum16);
 
 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type)
 {
@@ -160,6 +162,7 @@ u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type)
 	}
 	return chksum;
 }
+EXPORT_SYMBOL_IF_KUNIT(exfat_calc_chksum32);
 
 int exfat_update_bh(struct buffer_head *bh, int sync)
 {
-- 
2.56.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30  1:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30  1:44 [PATCH] exfat: add KUnit tests for checksum and entry count helpers Andrey Zakharov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®