overlay_test.c 3.2 KB

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