1 // SPDX-License-Identifier: GPL-2.0 1 2 /* 3 * KUnit test for KUnit platform driver infras 4 */ 5 6 #include <linux/platform_device.h> 7 8 #include <kunit/platform_device.h> 9 #include <kunit/test.h> 10 11 /* 12 * Test that kunit_platform_device_alloc() cre 13 */ 14 static void kunit_platform_device_alloc_test(s 15 { 16 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, 17 kunit_platform_device_ 18 } 19 20 /* 21 * Test that kunit_platform_device_add() regis 22 * platform bus with the proper name and id. 23 */ 24 static void kunit_platform_device_add_test(str 25 { 26 struct platform_device *pdev; 27 const char *name = "kunit-platform-add 28 const int id = -1; 29 30 pdev = kunit_platform_device_alloc(tes 31 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pde 32 33 KUNIT_EXPECT_EQ(test, 0, kunit_platfor 34 KUNIT_EXPECT_TRUE(test, dev_is_platfor 35 KUNIT_EXPECT_STREQ(test, pdev->name, n 36 KUNIT_EXPECT_EQ(test, pdev->id, id); 37 } 38 39 /* 40 * Test that kunit_platform_device_add() calle 41 * and id fails the second time and properly c 42 */ 43 static void kunit_platform_device_add_twice_fa 44 { 45 struct platform_device *pdev; 46 const char *name = "kunit-platform-add 47 const int id = -1; 48 49 pdev = kunit_platform_device_alloc(tes 50 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pde 51 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 52 53 pdev = kunit_platform_device_alloc(tes 54 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pde 55 56 KUNIT_EXPECT_NE(test, 0, kunit_platfor 57 } 58 59 static int kunit_platform_device_find_by_name( 60 { 61 return strcmp(dev_name(dev), data) == 62 } 63 64 /* 65 * Test that kunit_platform_device_add() clean 66 * device when the test finishes. */ 67 static void kunit_platform_device_add_cleans_u 68 { 69 struct platform_device *pdev; 70 const char *name = "kunit-platform-cle 71 const int id = -1; 72 struct kunit fake; 73 struct device *dev; 74 75 kunit_init_test(&fake, "kunit_platform 76 KUNIT_ASSERT_EQ(test, fake.status, KUN 77 78 pdev = kunit_platform_device_alloc(&fa 79 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pde 80 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 81 dev = bus_find_device(&platform_bus_ty 82 kunit_platform_d 83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev 84 put_device(dev); 85 86 /* Remove pdev */ 87 kunit_cleanup(&fake); 88 89 /* 90 * Failing to migrate the kunit_resour 91 * put_device() call on the platform d 92 * make sure the device no longer exis 93 * is wrong we'll see a refcount under 94 * refcount underflow because the kref 95 * device which should already be free 96 * else. 97 */ 98 dev = bus_find_device(&platform_bus_ty 99 kunit_platform_d 100 KUNIT_EXPECT_PTR_EQ(test, NULL, dev); 101 put_device(dev); 102 } 103 104 /* 105 * Test suite for struct platform_device kunit 106 */ 107 static struct kunit_case kunit_platform_device 108 KUNIT_CASE(kunit_platform_device_alloc 109 KUNIT_CASE(kunit_platform_device_add_t 110 KUNIT_CASE(kunit_platform_device_add_t 111 KUNIT_CASE(kunit_platform_device_add_c 112 {} 113 }; 114 115 static struct kunit_suite kunit_platform_devic 116 .name = "kunit_platform_device", 117 .test_cases = kunit_platform_device_te 118 }; 119 120 struct kunit_platform_driver_test_context { 121 struct platform_driver pdrv; 122 const char *data; 123 }; 124 125 static const char * const test_data = "test da 126 127 static inline struct kunit_platform_driver_tes 128 to_test_context(struct platform_device *pdev) 129 { 130 return container_of(to_platform_driver 131 struct kunit_platf 132 pdrv); 133 } 134 135 static int kunit_platform_driver_probe(struct 136 { 137 struct kunit_platform_driver_test_cont 138 139 ctx = to_test_context(pdev); 140 ctx->data = test_data; 141 142 return 0; 143 } 144 145 /* Test that kunit_platform_driver_register() 146 static void kunit_platform_driver_register_tes 147 { 148 struct platform_device *pdev; 149 struct kunit_platform_driver_test_cont 150 DECLARE_COMPLETION_ONSTACK(comp); 151 const char *name = "kunit-platform-reg 152 153 ctx = kunit_kzalloc(test, sizeof(*ctx) 154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx 155 156 pdev = kunit_platform_device_alloc(tes 157 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pde 158 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 159 160 ctx->pdrv.probe = kunit_platform_drive 161 ctx->pdrv.driver.name = name; 162 ctx->pdrv.driver.owner = THIS_MODULE; 163 164 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 165 166 KUNIT_EXPECT_EQ(test, 0, kunit_platfor 167 KUNIT_EXPECT_NE(test, 0, wait_for_comp 168 KUNIT_EXPECT_STREQ(test, ctx->data, te 169 } 170 171 /* 172 * Test that kunit_platform_device_prepare_wai 173 * when the device is already probed. 174 */ 175 static void kunit_platform_device_prepare_wait 176 { 177 struct platform_device *pdev; 178 struct kunit_platform_driver_test_cont 179 DECLARE_COMPLETION_ONSTACK(comp); 180 const char *name = "kunit-platform-wai 181 182 ctx = kunit_kzalloc(test, sizeof(*ctx) 183 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx 184 185 pdev = kunit_platform_device_alloc(tes 186 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pde 187 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 188 189 ctx->pdrv.probe = kunit_platform_drive 190 ctx->pdrv.driver.name = name; 191 ctx->pdrv.driver.owner = THIS_MODULE; 192 193 /* Make sure driver has actually probe 194 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 195 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 196 KUNIT_ASSERT_NE(test, 0, wait_for_comp 197 198 reinit_completion(&comp); 199 KUNIT_ASSERT_EQ(test, 0, kunit_platfor 200 201 KUNIT_EXPECT_NE(test, 0, wait_for_comp 202 } 203 204 static struct kunit_case kunit_platform_driver 205 KUNIT_CASE(kunit_platform_driver_regis 206 KUNIT_CASE(kunit_platform_device_prepa 207 {} 208 }; 209 210 /* 211 * Test suite for struct platform_driver kunit 212 */ 213 static struct kunit_suite kunit_platform_drive 214 .name = "kunit_platform_driver", 215 .test_cases = kunit_platform_driver_te 216 }; 217 218 kunit_test_suites( 219 &kunit_platform_device_suite, 220 &kunit_platform_driver_suite, 221 ); 222 223 MODULE_LICENSE("GPL"); 224 MODULE_DESCRIPTION("KUnit test for KUnit platf 225
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.