[PATCH v2 2/2] firewire: Add KUnit tests for CSR iterator

From: Sreeraj S Kurup

Date: Tue Nov 03 2026 - 10:33:53 EST


Add a separate KUnit test suite for the CSR iterator.

Test iteration over valid entries, an empty directory,
and calling the iterator after reaching the end.

Add a separate Kconfig option to enable the iterator tests
and update the KUnit configuration.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@xxxxxxxxx>
---
drivers/firewire/.kunitconfig | 1 +
drivers/firewire/Kconfig | 12 +++
drivers/firewire/config-rom-iterator-test.c | 93 +++++++++++++++++++++
drivers/firewire/core-device.c | 4 +
4 files changed, 110 insertions(+)
create mode 100644 drivers/firewire/config-rom-iterator-test.c

diff --git a/drivers/firewire/.kunitconfig b/drivers/firewire/.kunitconfig
index ee9129081cef..e36dd34fd344 100644
--- a/drivers/firewire/.kunitconfig
+++ b/drivers/firewire/.kunitconfig
@@ -8,3 +8,4 @@ CONFIG_FIREWIRE_KUNIT_SELF_ID_SEQUENCE_HELPER_TEST=y
CONFIG_FIREWIRE_KUNIT_OHCI_SERDES_TEST=y
CONFIG_FIREWIRE_KUNIT_NODE_TREE_TEST=y
CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_PARSER_AND_GENERATOR_TEST=y
+CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_ITERATOR_TEST=y
diff --git a/drivers/firewire/Kconfig b/drivers/firewire/Kconfig
index 7e1d4b2c440b..e67c2524c799 100644
--- a/drivers/firewire/Kconfig
+++ b/drivers/firewire/Kconfig
@@ -112,6 +112,18 @@ config FIREWIRE_KUNIT_CONFIG_ROM_PARSER_AND_GENERATOR_TEST
For more information on KUnit and unit tests in general, refer
to the KUnit documentation in Documentation/dev-tools/kunit/.

+config FIREWIRE_KUNIT_CONFIG_ROM_ITERATOR_TEST
+ tristate "KUnit tests for config ROM iterator" if !KUNIT_ALL_TESTS
+ depends on FIREWIRE && KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds KUnit tests for the configuration ROM iterator used
+ by the FireWire subsystem.
+
+ The tests check iteration over valid entries, handling empty
+ directories, and calls to the iterator after reaching the end of
+ a directory
+
config FIREWIRE_OHCI
tristate "OHCI-1394 controllers"
depends on PCI && FIREWIRE
diff --git a/drivers/firewire/config-rom-iterator-test.c b/drivers/firewire/config-rom-iterator-test.c
new file mode 100644
index 000000000000..8c846f2f2011
--- /dev/null
+++ b/drivers/firewire/config-rom-iterator-test.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 Sreeraj S Kurup
+ *
+ * config-rom-iterator-test.c - KUnit tests for configuration ROM iterator.
+ */
+
+#include <kunit/test.h>
+
+static void test_csr_iterator_with_valid_entries(struct kunit *test)
+{
+ static const u32 directory[] = {
+ 0x00020000, /* Two entries. */
+ 0x12001234,
+ 0x34005678,
+ };
+ struct fw_csr_iterator ci;
+ int key, value;
+
+ fw_csr_iterator_init(&ci, directory);
+
+ KUNIT_EXPECT_TRUE(test,
+ fw_csr_iterator_next(&ci, &key, &value));
+ KUNIT_EXPECT_EQ(test, key, 0x12);
+ KUNIT_EXPECT_EQ(test, value, 0x001234);
+
+ KUNIT_EXPECT_TRUE(test,
+ fw_csr_iterator_next(&ci, &key, &value));
+ KUNIT_EXPECT_EQ(test, key, 0x34);
+ KUNIT_EXPECT_EQ(test, value, 0x005678);
+
+ KUNIT_EXPECT_FALSE(test,
+ fw_csr_iterator_next(&ci, &key, &value));
+}
+
+static void test_csr_iterator_at_end(struct kunit *test)
+{
+ static const u32 directory[] = {
+ 0x00010000, /* One entry. */
+ 0x12001234,
+ };
+ struct fw_csr_iterator ci;
+ int key, value;
+
+ fw_csr_iterator_init(&ci, directory);
+
+ KUNIT_ASSERT_TRUE(test,
+ fw_csr_iterator_next(&ci, &key, &value));
+
+ KUNIT_EXPECT_EQ(test, key, 0x12);
+ KUNIT_EXPECT_EQ(test, value, 0x001234);
+
+ KUNIT_EXPECT_PTR_EQ(test, ci.p, ci.end);
+
+ /*
+ * Calling next() after the last entry must not
+ * advance the iterator beyond its end.
+ */
+ KUNIT_EXPECT_FALSE(test,
+ fw_csr_iterator_next(&ci, &key, &value));
+
+ KUNIT_EXPECT_PTR_EQ(test, ci.p, ci.end);
+}
+
+static void test_csr_iterator_empty_directory(struct kunit *test)
+{
+ static const u32 directory[] = {
+ 0x00000000,
+ };
+ struct fw_csr_iterator ci;
+ int key, value;
+
+ fw_csr_iterator_init(&ci, directory);
+
+ KUNIT_EXPECT_FALSE(test,
+ fw_csr_iterator_next(&ci, &key, &value));
+
+ KUNIT_EXPECT_PTR_EQ(test, ci.p, ci.end);
+}
+
+static struct kunit_case csr_iterator_test_cases[] = {
+ KUNIT_CASE(test_csr_iterator_with_valid_entries),
+ KUNIT_CASE(test_csr_iterator_at_end),
+ KUNIT_CASE(test_csr_iterator_empty_directory),
+ {}
+};
+
+static struct kunit_suite csr_iterator_test_suite = {
+ .name = "firewire-config-rom-iterator",
+ .test_cases = csr_iterator_test_cases,
+};
+
+kunit_test_suite(csr_iterator_test_suite);
diff --git a/drivers/firewire/core-device.c b/drivers/firewire/core-device.c
index 36667841ca3c..5d5ad4f41ab5 100644
--- a/drivers/firewire/core-device.c
+++ b/drivers/firewire/core-device.c
@@ -1450,3 +1450,7 @@ void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
#ifdef CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_PARSER_AND_GENERATOR_TEST
#include "config-rom-parser-test.c"
#endif
+
+#ifdef CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_ITERATOR_TEST
+#include "config-rom-iterator-test.c"
+#endif
--
2.55.0