overlay_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_EARLY_FLATTREE))
  56. kunit_skip(test, "requires CONFIG_OF_EARLY_FLATTREE for root node");
  57. kunit_init_test(&fake, "fake test", NULL);
  58. KUNIT_ASSERT_EQ(test, fake.status, KUNIT_SUCCESS);
  59. KUNIT_ASSERT_EQ(test, 0,
  60. of_overlay_apply_kunit(&fake, kunit_overlay_test));
  61. np = of_find_node_by_name(NULL, kunit_node_name);
  62. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
  63. of_node_put_kunit(&fake, np);
  64. pdev = of_find_device_by_node(np);
  65. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  66. put_device(&pdev->dev); /* Not derefing 'pdev' after this */
  67. /* Remove overlay */
  68. kunit_cleanup(&fake);
  69. /* The node and device should be removed */
  70. np = of_find_node_by_name(NULL, kunit_node_name);
  71. KUNIT_EXPECT_PTR_EQ(test, NULL, np);
  72. of_node_put(np);
  73. dev = bus_find_device(&platform_bus_type, NULL, kunit_compatible,
  74. of_overlay_bus_match_compatible);
  75. KUNIT_EXPECT_PTR_EQ(test, NULL, dev);
  76. put_device(dev);
  77. }
  78. static struct kunit_case of_overlay_apply_kunit_test_cases[] = {
  79. KUNIT_CASE(of_overlay_apply_kunit_apply),
  80. KUNIT_CASE(of_overlay_apply_kunit_platform_device),
  81. KUNIT_CASE(of_overlay_apply_kunit_cleanup),
  82. {}
  83. };
  84. /*
  85. * Test suite for test managed device tree overlays.
  86. */
  87. static struct kunit_suite of_overlay_apply_kunit_suite = {
  88. .name = "of_overlay_apply_kunit",
  89. .test_cases = of_overlay_apply_kunit_test_cases,
  90. };
  91. kunit_test_suites(
  92. &of_overlay_apply_kunit_suite,
  93. );
  94. MODULE_LICENSE("GPL");
  95. MODULE_DESCRIPTION("KUnit tests for device tree overlays");