|
9 | 9 | #ifndef _KUNIT_TEST_H |
10 | 10 | #define _KUNIT_TEST_H |
11 | 11 |
|
| 12 | +#include <linux/slab.h> |
12 | 13 | #include <linux/types.h> |
13 | 14 |
|
| 15 | +struct kunit_resource; |
| 16 | + |
| 17 | +typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *); |
| 18 | +typedef void (*kunit_resource_free_t)(struct kunit_resource *); |
| 19 | + |
| 20 | +/** |
| 21 | + * struct kunit_resource - represents a *test managed resource* |
| 22 | + * @allocation: for the user to store arbitrary data. |
| 23 | + * @free: a user supplied function to free the resource. Populated by |
| 24 | + * kunit_alloc_resource(). |
| 25 | + * |
| 26 | + * Represents a *test managed resource*, a resource which will automatically be |
| 27 | + * cleaned up at the end of a test case. |
| 28 | + * |
| 29 | + * Example: |
| 30 | + * |
| 31 | + * .. code-block:: c |
| 32 | + * |
| 33 | + * struct kunit_kmalloc_params { |
| 34 | + * size_t size; |
| 35 | + * gfp_t gfp; |
| 36 | + * }; |
| 37 | + * |
| 38 | + * static int kunit_kmalloc_init(struct kunit_resource *res, void *context) |
| 39 | + * { |
| 40 | + * struct kunit_kmalloc_params *params = context; |
| 41 | + * res->allocation = kmalloc(params->size, params->gfp); |
| 42 | + * |
| 43 | + * if (!res->allocation) |
| 44 | + * return -ENOMEM; |
| 45 | + * |
| 46 | + * return 0; |
| 47 | + * } |
| 48 | + * |
| 49 | + * static void kunit_kmalloc_free(struct kunit_resource *res) |
| 50 | + * { |
| 51 | + * kfree(res->allocation); |
| 52 | + * } |
| 53 | + * |
| 54 | + * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) |
| 55 | + * { |
| 56 | + * struct kunit_kmalloc_params params; |
| 57 | + * struct kunit_resource *res; |
| 58 | + * |
| 59 | + * params.size = size; |
| 60 | + * params.gfp = gfp; |
| 61 | + * |
| 62 | + * res = kunit_alloc_resource(test, kunit_kmalloc_init, |
| 63 | + * kunit_kmalloc_free, ¶ms); |
| 64 | + * if (res) |
| 65 | + * return res->allocation; |
| 66 | + * |
| 67 | + * return NULL; |
| 68 | + * } |
| 69 | + */ |
| 70 | +struct kunit_resource { |
| 71 | + void *allocation; |
| 72 | + kunit_resource_free_t free; |
| 73 | + |
| 74 | + /* private: internal use only. */ |
| 75 | + struct list_head node; |
| 76 | +}; |
| 77 | + |
14 | 78 | struct kunit; |
15 | 79 |
|
16 | 80 | /** |
@@ -114,6 +178,13 @@ struct kunit { |
114 | 178 | * with the test case have terminated. |
115 | 179 | */ |
116 | 180 | bool success; /* Read only after test_case finishes! */ |
| 181 | + spinlock_t lock; /* Guards all mutable test state. */ |
| 182 | + /* |
| 183 | + * Because resources is a list that may be updated multiple times (with |
| 184 | + * new resources) from any thread associated with a test case, we must |
| 185 | + * protect it with some type of lock. |
| 186 | + */ |
| 187 | + struct list_head resources; /* Protected by lock. */ |
117 | 188 | }; |
118 | 189 |
|
119 | 190 | void kunit_init_test(struct kunit *test, const char *name); |
@@ -147,6 +218,122 @@ int kunit_run_tests(struct kunit_suite *suite); |
147 | 218 | } \ |
148 | 219 | late_initcall(kunit_suite_init##suite) |
149 | 220 |
|
| 221 | +/* |
| 222 | + * Like kunit_alloc_resource() below, but returns the struct kunit_resource |
| 223 | + * object that contains the allocation. This is mostly for testing purposes. |
| 224 | + */ |
| 225 | +struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, |
| 226 | + kunit_resource_init_t init, |
| 227 | + kunit_resource_free_t free, |
| 228 | + gfp_t internal_gfp, |
| 229 | + void *context); |
| 230 | + |
| 231 | +/** |
| 232 | + * kunit_alloc_resource() - Allocates a *test managed resource*. |
| 233 | + * @test: The test context object. |
| 234 | + * @init: a user supplied function to initialize the resource. |
| 235 | + * @free: a user supplied function to free the resource. |
| 236 | + * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL |
| 237 | + * @context: for the user to pass in arbitrary data to the init function. |
| 238 | + * |
| 239 | + * Allocates a *test managed resource*, a resource which will automatically be |
| 240 | + * cleaned up at the end of a test case. See &struct kunit_resource for an |
| 241 | + * example. |
| 242 | + * |
| 243 | + * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must |
| 244 | + * specify an @internal_gfp that is compatible with the use context of your |
| 245 | + * resource. |
| 246 | + */ |
| 247 | +static inline void *kunit_alloc_resource(struct kunit *test, |
| 248 | + kunit_resource_init_t init, |
| 249 | + kunit_resource_free_t free, |
| 250 | + gfp_t internal_gfp, |
| 251 | + void *context) |
| 252 | +{ |
| 253 | + struct kunit_resource *res; |
| 254 | + |
| 255 | + res = kunit_alloc_and_get_resource(test, init, free, internal_gfp, |
| 256 | + context); |
| 257 | + |
| 258 | + if (res) |
| 259 | + return res->allocation; |
| 260 | + |
| 261 | + return NULL; |
| 262 | +} |
| 263 | + |
| 264 | +typedef bool (*kunit_resource_match_t)(struct kunit *test, |
| 265 | + const void *res, |
| 266 | + void *match_data); |
| 267 | + |
| 268 | +/** |
| 269 | + * kunit_resource_instance_match() - Match a resource with the same instance. |
| 270 | + * @test: Test case to which the resource belongs. |
| 271 | + * @res: The data stored in kunit_resource->allocation. |
| 272 | + * @match_data: The resource pointer to match against. |
| 273 | + * |
| 274 | + * An instance of kunit_resource_match_t that matches a resource whose |
| 275 | + * allocation matches @match_data. |
| 276 | + */ |
| 277 | +static inline bool kunit_resource_instance_match(struct kunit *test, |
| 278 | + const void *res, |
| 279 | + void *match_data) |
| 280 | +{ |
| 281 | + return res == match_data; |
| 282 | +} |
| 283 | + |
| 284 | +/** |
| 285 | + * kunit_resource_destroy() - Find a kunit_resource and destroy it. |
| 286 | + * @test: Test case to which the resource belongs. |
| 287 | + * @match: Match function. Returns whether a given resource matches @match_data. |
| 288 | + * @free: Must match free on the kunit_resource to free. |
| 289 | + * @match_data: Data passed into @match. |
| 290 | + * |
| 291 | + * Free the latest kunit_resource of @test for which @free matches the |
| 292 | + * kunit_resource_free_t associated with the resource and for which @match |
| 293 | + * returns true. |
| 294 | + * |
| 295 | + * RETURNS: |
| 296 | + * 0 if kunit_resource is found and freed, -ENOENT if not found. |
| 297 | + */ |
| 298 | +int kunit_resource_destroy(struct kunit *test, |
| 299 | + kunit_resource_match_t match, |
| 300 | + kunit_resource_free_t free, |
| 301 | + void *match_data); |
| 302 | + |
| 303 | +/** |
| 304 | + * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. |
| 305 | + * @test: The test context object. |
| 306 | + * @size: The size in bytes of the desired memory. |
| 307 | + * @gfp: flags passed to underlying kmalloc(). |
| 308 | + * |
| 309 | + * Just like `kmalloc(...)`, except the allocation is managed by the test case |
| 310 | + * and is automatically cleaned up after the test case concludes. See &struct |
| 311 | + * kunit_resource for more information. |
| 312 | + */ |
| 313 | +void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp); |
| 314 | + |
| 315 | +/** |
| 316 | + * kunit_kfree() - Like kfree except for allocations managed by KUnit. |
| 317 | + * @test: The test case to which the resource belongs. |
| 318 | + * @ptr: The memory allocation to free. |
| 319 | + */ |
| 320 | +void kunit_kfree(struct kunit *test, const void *ptr); |
| 321 | + |
| 322 | +/** |
| 323 | + * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. |
| 324 | + * @test: The test context object. |
| 325 | + * @size: The size in bytes of the desired memory. |
| 326 | + * @gfp: flags passed to underlying kmalloc(). |
| 327 | + * |
| 328 | + * See kzalloc() and kunit_kmalloc() for more information. |
| 329 | + */ |
| 330 | +static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) |
| 331 | +{ |
| 332 | + return kunit_kmalloc(test, size, gfp | __GFP_ZERO); |
| 333 | +} |
| 334 | + |
| 335 | +void kunit_cleanup(struct kunit *test); |
| 336 | + |
150 | 337 | void __printf(3, 4) kunit_printk(const char *level, |
151 | 338 | const struct kunit *test, |
152 | 339 | const char *fmt, ...); |
|
0 commit comments