platform-test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for KUnit platform driver infrastructure.
  4. */
  5. #include <linux/platform_device.h>
  6. #include <kunit/platform_device.h>
  7. #include <kunit/test.h>
  8. /*
  9. * Test that kunit_platform_device_alloc() creates a platform device.
  10. */
  11. static void kunit_platform_device_alloc_test(struct kunit *test)
  12. {
  13. KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
  14. kunit_platform_device_alloc(test, "kunit-platform", 1));
  15. }
  16. /*
  17. * Test that kunit_platform_device_add() registers a platform device on the
  18. * platform bus with the proper name and id.
  19. */
  20. static void kunit_platform_device_add_test(struct kunit *test)
  21. {
  22. struct platform_device *pdev;
  23. const char *name = "kunit-platform-add";
  24. const int id = -1;
  25. pdev = kunit_platform_device_alloc(test, name, id);
  26. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  27. KUNIT_EXPECT_EQ(test, 0, kunit_platform_device_add(test, pdev));
  28. KUNIT_EXPECT_TRUE(test, dev_is_platform(&pdev->dev));
  29. KUNIT_EXPECT_STREQ(test, pdev->name, name);
  30. KUNIT_EXPECT_EQ(test, pdev->id, id);
  31. }
  32. /*
  33. * Test that kunit_platform_device_add() called twice with the same device name
  34. * and id fails the second time and properly cleans up.
  35. */
  36. static void kunit_platform_device_add_twice_fails_test(struct kunit *test)
  37. {
  38. struct platform_device *pdev;
  39. const char *name = "kunit-platform-add-2";
  40. const int id = -1;
  41. pdev = kunit_platform_device_alloc(test, name, id);
  42. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  43. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
  44. pdev = kunit_platform_device_alloc(test, name, id);
  45. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  46. KUNIT_EXPECT_NE(test, 0, kunit_platform_device_add(test, pdev));
  47. }
  48. static int kunit_platform_device_find_by_name(struct device *dev, const void *data)
  49. {
  50. return strcmp(dev_name(dev), data) == 0;
  51. }
  52. /*
  53. * Test that kunit_platform_device_add() cleans up by removing the platform
  54. * device when the test finishes. */
  55. static void kunit_platform_device_add_cleans_up(struct kunit *test)
  56. {
  57. struct platform_device *pdev;
  58. const char *name = "kunit-platform-clean";
  59. const int id = -1;
  60. struct kunit fake;
  61. struct device *dev;
  62. kunit_init_test(&fake, "kunit_platform_device_add_fake_test", NULL);
  63. KUNIT_ASSERT_EQ(test, fake.status, KUNIT_SUCCESS);
  64. pdev = kunit_platform_device_alloc(&fake, name, id);
  65. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  66. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(&fake, pdev));
  67. dev = bus_find_device(&platform_bus_type, NULL, name,
  68. kunit_platform_device_find_by_name);
  69. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
  70. put_device(dev);
  71. /* Remove pdev */
  72. kunit_cleanup(&fake);
  73. /*
  74. * Failing to migrate the kunit_resource would lead to an extra
  75. * put_device() call on the platform device. The best we can do here is
  76. * make sure the device no longer exists on the bus, but if something
  77. * is wrong we'll see a refcount underflow here. We can't test for a
  78. * refcount underflow because the kref matches the lifetime of the
  79. * device which should already be freed and could be used by something
  80. * else.
  81. */
  82. dev = bus_find_device(&platform_bus_type, NULL, name,
  83. kunit_platform_device_find_by_name);
  84. KUNIT_EXPECT_PTR_EQ(test, NULL, dev);
  85. put_device(dev);
  86. }
  87. /*
  88. * Test suite for struct platform_device kunit APIs
  89. */
  90. static struct kunit_case kunit_platform_device_test_cases[] = {
  91. KUNIT_CASE(kunit_platform_device_alloc_test),
  92. KUNIT_CASE(kunit_platform_device_add_test),
  93. KUNIT_CASE(kunit_platform_device_add_twice_fails_test),
  94. KUNIT_CASE(kunit_platform_device_add_cleans_up),
  95. {}
  96. };
  97. static struct kunit_suite kunit_platform_device_suite = {
  98. .name = "kunit_platform_device",
  99. .test_cases = kunit_platform_device_test_cases,
  100. };
  101. struct kunit_platform_driver_test_context {
  102. struct platform_driver pdrv;
  103. const char *data;
  104. };
  105. static const char * const test_data = "test data";
  106. static inline struct kunit_platform_driver_test_context *
  107. to_test_context(struct platform_device *pdev)
  108. {
  109. return container_of(to_platform_driver(pdev->dev.driver),
  110. struct kunit_platform_driver_test_context,
  111. pdrv);
  112. }
  113. static int kunit_platform_driver_probe(struct platform_device *pdev)
  114. {
  115. struct kunit_platform_driver_test_context *ctx;
  116. ctx = to_test_context(pdev);
  117. ctx->data = test_data;
  118. return 0;
  119. }
  120. /* Test that kunit_platform_driver_register() registers a driver that probes. */
  121. static void kunit_platform_driver_register_test(struct kunit *test)
  122. {
  123. struct platform_device *pdev;
  124. struct kunit_platform_driver_test_context *ctx;
  125. DECLARE_COMPLETION_ONSTACK(comp);
  126. const char *name = "kunit-platform-register";
  127. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  128. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  129. pdev = kunit_platform_device_alloc(test, name, -1);
  130. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  131. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
  132. ctx->pdrv.probe = kunit_platform_driver_probe;
  133. ctx->pdrv.driver.name = name;
  134. ctx->pdrv.driver.owner = THIS_MODULE;
  135. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
  136. KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
  137. KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ));
  138. KUNIT_EXPECT_STREQ(test, ctx->data, test_data);
  139. }
  140. /*
  141. * Test that kunit_platform_device_prepare_wait_for_probe() completes the completion
  142. * when the device is already probed.
  143. */
  144. static void kunit_platform_device_prepare_wait_for_probe_completes_when_already_probed(struct kunit *test)
  145. {
  146. struct platform_device *pdev;
  147. struct kunit_platform_driver_test_context *ctx;
  148. DECLARE_COMPLETION_ONSTACK(comp);
  149. const char *name = "kunit-platform-wait";
  150. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  151. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  152. pdev = kunit_platform_device_alloc(test, name, -1);
  153. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
  154. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
  155. ctx->pdrv.probe = kunit_platform_driver_probe;
  156. ctx->pdrv.driver.name = name;
  157. ctx->pdrv.driver.owner = THIS_MODULE;
  158. /* Make sure driver has actually probed */
  159. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
  160. KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
  161. KUNIT_ASSERT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ));
  162. reinit_completion(&comp);
  163. KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
  164. KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, HZ));
  165. }
  166. static struct kunit_case kunit_platform_driver_test_cases[] = {
  167. KUNIT_CASE(kunit_platform_driver_register_test),
  168. KUNIT_CASE(kunit_platform_device_prepare_wait_for_probe_completes_when_already_probed),
  169. {}
  170. };
  171. /*
  172. * Test suite for struct platform_driver kunit APIs
  173. */
  174. static struct kunit_suite kunit_platform_driver_suite = {
  175. .name = "kunit_platform_driver",
  176. .test_cases = kunit_platform_driver_test_cases,
  177. };
  178. kunit_test_suites(
  179. &kunit_platform_device_suite,
  180. &kunit_platform_driver_suite,
  181. );
  182. MODULE_LICENSE("GPL");
  183. MODULE_DESCRIPTION("KUnit test for KUnit platform driver infrastructure");