From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796003111; b=kCKwQj9YZ6loIoqFmXfxuZIGE7S+QTx66ZcH6hyaLxQw0CZnmSsQHsEgIfF3+9DVxVgI UydCgCxMUTasw52VmkDuVp3Mi7yiZPE9P3BpCxTwe6cQFvH/zjtlfZHTz8i1GdWqXEJN1 J8be02zS7wgAq6vII1rN6vrX7kwqIUWDhvPfaJfH7dSJssulS/5lXX6zZ5+PQhzD6o2mH 4QIsrgf7emUU9fNBq/HO902hQhQMGkQMBC7g7E+05hjzi3bwlw9OoX+UMCvp+gJGuE+rd q04PsGu9I+VrKqorjH7jK/rYBvbFHsUdcFNTPksk1A+6HDGFuQaV4K2fmPac36Pzx7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796003111; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=jGbZGFNFCR2P/FKRXpOihBh/UPUeBQUmDWqjF2mgXsY=; b=eIjHMNtVQmJlna7fjOBx+dBdfYgJmn5Li194jyZ17SHrrZ+rGGCJOZYvV9p7AiDOavSY FNKorbiva7HSLND2+HSm8ui9ww2r9fhS/V5E+ACSV6/D4an5lTLI28Y5Ua/BbacBst+N2 CZ1/wANXhrMVoboN8PAinA950EUAK6mGWIcpHe5P5t0bj+oDpVk8pjT8s25HmViF3gQoW /LwZwjx1NvV54wzcX8MRmtya4HbaHu/ihS3YUIj+wQnHCz9eiELBmI6ubVEdW5UTpUpdi AoN7p2NMFpxNSrl7+G/0nIk89XdSlKpgqF7NWl2fjXfwCWDMtjvuQDsg8iJQ8st4txg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kwfk9zGi; arc=none smtp.remote-ip=209.85.221.53 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1ED86606A31C for ; Mon, 30 Nov 2026 01:45:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1ED86606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kwfk9zGi Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-4ad6219f7d8so8999636f8f.0 for ; Sun, 29 Nov 2026 17:45:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796003108; x=1796607908; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=jGbZGFNFCR2P/FKRXpOihBh/UPUeBQUmDWqjF2mgXsY=; b=kwfk9zGiuzolNLcnN2vi0R1hvhzio5AQCNSeE3ctw8esXgGIwnT6DD+4yJCzQaXYjL MsHtRIkOktOK/uUK1gvP+JYwF+qxWGzmlSVuoX8moYWeUH4ZfuuWZgbdFjHH1qgvbQaL dpWCp3uWD6QCDaAY8+9583kx0PBTxFZGblkWeUbDTL28Sipgqt7lvcru0L0x84yzFcFf +8qPAlCEZE1TRk/HzljFnCZqgDBDwUSqeRcYqpdM4cruerr+jWzH9O7aLzsbDGRHR7QV wP0qlGSUR69d220sE433bMeaEmM9MPXKJVwOutIMSRBlfRVDVsNwhsYsJgTvOKFRbNtx 0Zuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796003108; x=1796607908; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jGbZGFNFCR2P/FKRXpOihBh/UPUeBQUmDWqjF2mgXsY=; b=20P5O+XaqCg+Y2IXgssIgVIyOv5SRge7ClbmzQt9vASnQUodglXMcmqmupELtx37Wo q7VqQKjDTOTJDWoB72pI/5ed1Bp/xUUpYC5hrC9XABjjeU27rY3nPuA9yaX2oBcrjN5f Xh3txzz7Ujbt7X2NN8uM5fw1JMFZeBirLSBzvNUgFzUXeEgEuPP9JHjfrctADwt0pMvd deK8rs3Uf4w/GNy0pigiLuhO9Vjj1rCBmr23rsQsRiO3xITT5KT8n93LsYRZyI7BeVmz lwmBO3svpg5vaxi2LpoUGVTCmPjoKc4aZjW1WFYt1XzeLX31FXcgw0rlA3B2MTJY53wU RuNQ== X-Forwarded-Encrypted: i=1; AL+hErGb9m4vDA3LibiU3qv65ZnVIZ1yWApsrdyCXgmg0ni129ztdJ3t4Vd6OKD1BngaQ1sXkKHWXz7oGq5spEQ=@vger.kernel.org X-Gm-Message-State: APuU4Z8oYnYv9BEHHzyt+mpS6s4Jct9Dh6Xjx+QDpc21bqVjdzaTlo+2 0FDMujFfcgrZXX4LgI8iEaPjn5ZNM+4xmGPZyUeC7pJJ5tlCrnR1IaB+ X-Gm-Gg: AZFdTZft6NFx9MAwlAOsIBxXi046NVOcrePvdCp0Ga4V/Eo9mYEQEtBnc7XlUcTKBvH s8mDdPRPNEsXIWIsANh54Nv+g1T2oIY+3HAZpyrImBp1GJgoiQRbB1euFtVI8rNlrL1H5q+B8Oq JBvV0uN5ahnCzpeaPOA+hEPyQtJ6EPd47UASRWlHi5B+lsq8ZEgsz21DxF7x6TGxqAoTgRw0Ccn PSf0uFLBSDmZ6Eo9RvkayQAZD3vulSRF68fre6x/+jR9KsyS//rvgqESFv3bIKP9foAUOi9vcVu Yrf9RTutYTuL0K7D6Ph3DG2MZMUIcbCIuxqLG5UWl5OsgrIIro9jVS771gyXNi/T7XFghN9Ygrw AeRVyO1zCN3JRQ+c+GWJGC10+Ta2M5vnzgdg0Lp9/vlw0vRpobRroxtJk4LWLzszWHwtdRQzVcd kljiiPGGTWSWMpVuSLMQ4q+RoJfhCOBAGTKuWG0k9+p2ItZc87A1emzAvW1W6Y1fQ+6DRYwk+TP xnvO9aFG6sBGxcTANNmJGyzhrt4QxCXKw8HwmQUCMu/1/S5Xd1HiL6ZbFJkw7bpt9i6qrZ2Ef1c kJpbAqr2/gjG/g2KaXtC8qJG5m9CRg== X-Received: by 2002:a05:6000:184d:b0:4b7:66fb:b15d with SMTP id ffacd0b85a97d-4b766fbb8b1mr37773739f8f.11.1796003108221; Sun, 29 Nov 2026 17:45:08 -0800 (PST) Received: from thinkpad ([2a00:1370:81ac:4181:10b1:754b:3cfe:5139]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2c8csm62056967f8f.2.2026.11.29.17.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 17:45:07 -0800 (PST) From: Andrey Zakharov To: Namjae Jeon , Sungjong Seo Cc: Yuezhang Mo , exfat@lists.linux.dev, linux-kernel@vger.kernel.org, Andrey Zakharov Subject: [PATCH] exfat: add KUnit tests for checksum and entry count helpers Date: Mon, 30 Nov 2026 04:44:48 +0300 Message-ID: <20261130014448.2114125-1-dedemorow@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include #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 +#include + +#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 #include #include +#include #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