Skip to content

Commit 893ecc6

Browse files
bebarinorobherring
authored andcommitted
of: Add KUnit test to confirm DTB is loaded
Add a KUnit test that confirms a DTB has been loaded, i.e. there is a root node, and that the of_have_populated_dt() API works properly. We skip the test when CONFIG_OF_EARLY_FLATREE=n because in that case we know architecture code hasn't called unflatten_(and_copy_)?device_tree() which would populate some sort of root node. Cc: Rob Herring <robh+dt@kernel.org> Cc: Frank Rowand <frowand.list@gmail.com> Reviewed-by: David Gow <davidgow@google.com> Cc: Brendan Higgins <brendan.higgins@linux.dev> Signed-off-by: Stephen Boyd <sboyd@kernel.org> Link: https://lore.kernel.org/r/20240217010557.2381548-8-sboyd@kernel.org Signed-off-by: Rob Herring <robh@kernel.org>
1 parent d1eabd2 commit 893ecc6

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

drivers/of/.kunitconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_OF=y
3+
CONFIG_OF_KUNIT_TEST=y

drivers/of/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ config OF_UNITTEST
3636

3737
If unsure, say N here. This option is not safe to enable.
3838

39+
config OF_KUNIT_TEST
40+
tristate "Devicetree KUnit Test" if !KUNIT_ALL_TESTS
41+
depends on KUNIT
42+
default KUNIT_ALL_TESTS
43+
help
44+
This option builds KUnit unit tests for device tree infrastructure.
45+
46+
If unsure, say N here, but this option is safe to enable.
47+
3948
config OF_ALL_DTBS
4049
bool "Build all Device Tree Blobs"
4150
depends on COMPILE_TEST

drivers/of/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ obj-y += kexec.o
1919
endif
2020
endif
2121

22+
obj-$(CONFIG_OF_KUNIT_TEST) += of_test.o
23+
2224
obj-$(CONFIG_OF_UNITTEST) += unittest-data/

drivers/of/of_test.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* KUnit tests for OF APIs
4+
*/
5+
#include <linux/module.h>
6+
#include <linux/of.h>
7+
8+
#include <kunit/test.h>
9+
10+
/*
11+
* Test that the root node "/" can be found by path.
12+
*/
13+
static void of_dtb_root_node_found_by_path(struct kunit *test)
14+
{
15+
struct device_node *np;
16+
17+
np = of_find_node_by_path("/");
18+
KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
19+
of_node_put(np);
20+
}
21+
22+
/*
23+
* Test that the 'of_root' global variable is always populated when DT code is
24+
* enabled. Remove this test once of_root is removed from global access.
25+
*/
26+
static void of_dtb_root_node_populates_of_root(struct kunit *test)
27+
{
28+
KUNIT_EXPECT_NOT_ERR_OR_NULL(test, of_root);
29+
}
30+
31+
static struct kunit_case of_dtb_test_cases[] = {
32+
KUNIT_CASE(of_dtb_root_node_found_by_path),
33+
KUNIT_CASE(of_dtb_root_node_populates_of_root),
34+
{}
35+
};
36+
37+
static int of_dtb_test_init(struct kunit *test)
38+
{
39+
if (!IS_ENABLED(CONFIG_OF_EARLY_FLATTREE))
40+
kunit_skip(test, "requires CONFIG_OF_EARLY_FLATTREE");
41+
42+
return 0;
43+
}
44+
45+
/*
46+
* Test suite to confirm a DTB is loaded.
47+
*/
48+
static struct kunit_suite of_dtb_suite = {
49+
.name = "of_dtb",
50+
.test_cases = of_dtb_test_cases,
51+
.init = of_dtb_test_init,
52+
};
53+
54+
kunit_test_suites(
55+
&of_dtb_suite,
56+
);
57+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)