119 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * KUnit tests for device tree overlays4 */5#include <linux/device/bus.h>6#include <linux/kconfig.h>7#include <linux/of.h>8#include <linux/of_platform.h>9#include <linux/platform_device.h>10 11#include <kunit/of.h>12#include <kunit/test.h>13 14#include "of_private.h"15 16static const char * const kunit_node_name = "kunit-test";17static const char * const kunit_compatible = "test,empty";18 19/* Test that of_overlay_apply_kunit() adds a node to the live tree */20static void of_overlay_apply_kunit_apply(struct kunit *test)21{22 struct device_node *np;23 24 KUNIT_ASSERT_EQ(test, 0,25 of_overlay_apply_kunit(test, kunit_overlay_test));26 27 np = of_find_node_by_name(NULL, kunit_node_name);28 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);29 of_node_put(np);30}31 32/*33 * Test that of_overlay_apply_kunit() creates platform devices with the34 * expected device_node35 */36static void of_overlay_apply_kunit_platform_device(struct kunit *test)37{38 struct platform_device *pdev;39 struct device_node *np;40 41 KUNIT_ASSERT_EQ(test, 0,42 of_overlay_apply_kunit(test, kunit_overlay_test));43 44 np = of_find_node_by_name(NULL, kunit_node_name);45 of_node_put_kunit(test, np);46 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);47 48 pdev = of_find_device_by_node(np);49 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pdev);50 if (pdev)51 put_device(&pdev->dev);52}53 54static int of_overlay_bus_match_compatible(struct device *dev, const void *data)55{56 return of_device_is_compatible(dev->of_node, data);57}58 59/* Test that of_overlay_apply_kunit() cleans up after the test is finished */60static void of_overlay_apply_kunit_cleanup(struct kunit *test)61{62 struct kunit fake;63 struct platform_device *pdev;64 struct device *dev;65 struct device_node *np;66 67 of_root_kunit_skip(test);68 if (!IS_ENABLED(CONFIG_OF_EARLY_FLATTREE))69 kunit_skip(test, "requires CONFIG_OF_EARLY_FLATTREE for root node");70 71 kunit_init_test(&fake, "fake test", NULL);72 KUNIT_ASSERT_EQ(test, fake.status, KUNIT_SUCCESS);73 74 KUNIT_ASSERT_EQ(test, 0,75 of_overlay_apply_kunit(&fake, kunit_overlay_test));76 77 np = of_find_node_by_name(NULL, kunit_node_name);78 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);79 of_node_put_kunit(&fake, np);80 81 pdev = of_find_device_by_node(np);82 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);83 put_device(&pdev->dev); /* Not derefing 'pdev' after this */84 85 /* Remove overlay */86 kunit_cleanup(&fake);87 88 /* The node and device should be removed */89 np = of_find_node_by_name(NULL, kunit_node_name);90 KUNIT_EXPECT_PTR_EQ(test, NULL, np);91 of_node_put(np);92 93 dev = bus_find_device(&platform_bus_type, NULL, kunit_compatible,94 of_overlay_bus_match_compatible);95 KUNIT_EXPECT_PTR_EQ(test, NULL, dev);96 put_device(dev);97}98 99static struct kunit_case of_overlay_apply_kunit_test_cases[] = {100 KUNIT_CASE(of_overlay_apply_kunit_apply),101 KUNIT_CASE(of_overlay_apply_kunit_platform_device),102 KUNIT_CASE(of_overlay_apply_kunit_cleanup),103 {}104};105 106/*107 * Test suite for test managed device tree overlays.108 */109static struct kunit_suite of_overlay_apply_kunit_suite = {110 .name = "of_overlay_apply_kunit",111 .test_cases = of_overlay_apply_kunit_test_cases,112};113 114kunit_test_suites(115 &of_overlay_apply_kunit_suite,116);117MODULE_LICENSE("GPL");118MODULE_DESCRIPTION("KUnit tests for device tree overlays");119