1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * KUnit test for core test infrastructure.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8 #include "linux/gfp_types.h"
9 #include <kunit/test.h>
10 #include <kunit/test-bug.h>
11 #include <kunit/static_stub.h>
12
13 #include <linux/device.h>
14 #include <kunit/device.h>
15
16 #include "string-stream.h"
17 #include "try-catch-impl.h"
18
19 struct kunit_try_catch_test_context {
20 struct kunit_try_catch *try_catch;
21 bool function_called;
22 };
23
kunit_test_successful_try(void * data)24 static void kunit_test_successful_try(void *data)
25 {
26 struct kunit *test = data;
27 struct kunit_try_catch_test_context *ctx = test->priv;
28
29 ctx->function_called = true;
30 }
31
kunit_test_no_catch(void * data)32 static void kunit_test_no_catch(void *data)
33 {
34 struct kunit *test = data;
35
36 KUNIT_FAIL(test, "Catch should not be called\n");
37 }
38
kunit_test_try_catch_successful_try_no_catch(struct kunit * test)39 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
40 {
41 struct kunit_try_catch_test_context *ctx = test->priv;
42 struct kunit_try_catch *try_catch = ctx->try_catch;
43
44 kunit_try_catch_init(try_catch,
45 test,
46 kunit_test_successful_try,
47 kunit_test_no_catch,
48 300 * msecs_to_jiffies(MSEC_PER_SEC));
49 kunit_try_catch_run(try_catch, test);
50
51 KUNIT_EXPECT_TRUE(test, ctx->function_called);
52 }
53
kunit_test_unsuccessful_try(void * data)54 static void kunit_test_unsuccessful_try(void *data)
55 {
56 struct kunit *test = data;
57 struct kunit_try_catch_test_context *ctx = test->priv;
58 struct kunit_try_catch *try_catch = ctx->try_catch;
59
60 kunit_try_catch_throw(try_catch);
61 KUNIT_FAIL(test, "This line should never be reached\n");
62 }
63
kunit_test_catch(void * data)64 static void kunit_test_catch(void *data)
65 {
66 struct kunit *test = data;
67 struct kunit_try_catch_test_context *ctx = test->priv;
68
69 ctx->function_called = true;
70 }
71
kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit * test)72 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
73 {
74 struct kunit_try_catch_test_context *ctx = test->priv;
75 struct kunit_try_catch *try_catch = ctx->try_catch;
76
77 kunit_try_catch_init(try_catch,
78 test,
79 kunit_test_unsuccessful_try,
80 kunit_test_catch,
81 300 * msecs_to_jiffies(MSEC_PER_SEC));
82 kunit_try_catch_run(try_catch, test);
83
84 KUNIT_EXPECT_TRUE(test, ctx->function_called);
85 }
86
kunit_try_catch_test_init(struct kunit * test)87 static int kunit_try_catch_test_init(struct kunit *test)
88 {
89 struct kunit_try_catch_test_context *ctx;
90
91 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
92 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
93 test->priv = ctx;
94
95 ctx->try_catch = kunit_kmalloc(test,
96 sizeof(*ctx->try_catch),
97 GFP_KERNEL);
98 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
99
100 return 0;
101 }
102
103 static struct kunit_case kunit_try_catch_test_cases[] = {
104 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
105 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
106 {}
107 };
108
109 static struct kunit_suite kunit_try_catch_test_suite = {
110 .name = "kunit-try-catch-test",
111 .init = kunit_try_catch_test_init,
112 .test_cases = kunit_try_catch_test_cases,
113 };
114
115 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
116
kunit_test_null_dereference(void * data)117 static void kunit_test_null_dereference(void *data)
118 {
119 struct kunit *test = data;
120 int *null = NULL;
121
122 *null = 0;
123
124 KUNIT_FAIL(test, "This line should never be reached\n");
125 }
126
kunit_test_fault_null_dereference(struct kunit * test)127 static void kunit_test_fault_null_dereference(struct kunit *test)
128 {
129 struct kunit_try_catch_test_context *ctx = test->priv;
130 struct kunit_try_catch *try_catch = ctx->try_catch;
131
132 kunit_try_catch_init(try_catch,
133 test,
134 kunit_test_null_dereference,
135 kunit_test_catch,
136 300 * msecs_to_jiffies(MSEC_PER_SEC));
137 kunit_try_catch_run(try_catch, test);
138
139 KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
140 KUNIT_EXPECT_TRUE(test, ctx->function_called);
141 }
142
143 #endif /* CONFIG_KUNIT_FAULT_TEST */
144
145 static struct kunit_case kunit_fault_test_cases[] = {
146 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
147 KUNIT_CASE(kunit_test_fault_null_dereference),
148 #endif /* CONFIG_KUNIT_FAULT_TEST */
149 {}
150 };
151
152 static struct kunit_suite kunit_fault_test_suite = {
153 .name = "kunit_fault",
154 .init = kunit_try_catch_test_init,
155 .test_cases = kunit_fault_test_cases,
156 };
157
158 /*
159 * Context for testing test managed resources
160 * is_resource_initialized is used to test arbitrary resources
161 */
162 struct kunit_test_resource_context {
163 struct kunit test;
164 bool is_resource_initialized;
165 int allocate_order[2];
166 int free_order[4];
167 };
168
fake_resource_init(struct kunit_resource * res,void * context)169 static int fake_resource_init(struct kunit_resource *res, void *context)
170 {
171 struct kunit_test_resource_context *ctx = context;
172
173 res->data = &ctx->is_resource_initialized;
174 ctx->is_resource_initialized = true;
175 return 0;
176 }
177
fake_resource_free(struct kunit_resource * res)178 static void fake_resource_free(struct kunit_resource *res)
179 {
180 bool *is_resource_initialized = res->data;
181
182 *is_resource_initialized = false;
183 }
184
kunit_resource_test_init_resources(struct kunit * test)185 static void kunit_resource_test_init_resources(struct kunit *test)
186 {
187 struct kunit_test_resource_context *ctx = test->priv;
188
189 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
190
191 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
192 }
193
kunit_resource_test_alloc_resource(struct kunit * test)194 static void kunit_resource_test_alloc_resource(struct kunit *test)
195 {
196 struct kunit_test_resource_context *ctx = test->priv;
197 struct kunit_resource *res;
198 kunit_resource_free_t free = fake_resource_free;
199
200 res = kunit_alloc_and_get_resource(&ctx->test,
201 fake_resource_init,
202 fake_resource_free,
203 GFP_KERNEL,
204 ctx);
205
206 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
207 KUNIT_EXPECT_PTR_EQ(test,
208 &ctx->is_resource_initialized,
209 (bool *)res->data);
210 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
211 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
212
213 kunit_put_resource(res);
214 }
215
kunit_resource_instance_match(struct kunit * test,struct kunit_resource * res,void * match_data)216 static inline bool kunit_resource_instance_match(struct kunit *test,
217 struct kunit_resource *res,
218 void *match_data)
219 {
220 return res->data == match_data;
221 }
222
223 /*
224 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
225 * they have a reference to the associated resource that they must release
226 * via kunit_put_resource(). In normal operation, users will only
227 * have to do this for cases where they use kunit_find_resource(), and the
228 * kunit_alloc_resource() function will be used (which does not take a
229 * resource reference).
230 */
kunit_resource_test_destroy_resource(struct kunit * test)231 static void kunit_resource_test_destroy_resource(struct kunit *test)
232 {
233 struct kunit_test_resource_context *ctx = test->priv;
234 struct kunit_resource *res = kunit_alloc_and_get_resource(
235 &ctx->test,
236 fake_resource_init,
237 fake_resource_free,
238 GFP_KERNEL,
239 ctx);
240
241 kunit_put_resource(res);
242
243 KUNIT_ASSERT_FALSE(test,
244 kunit_destroy_resource(&ctx->test,
245 kunit_resource_instance_match,
246 res->data));
247
248 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
249 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
250 }
251
kunit_resource_test_remove_resource(struct kunit * test)252 static void kunit_resource_test_remove_resource(struct kunit *test)
253 {
254 struct kunit_test_resource_context *ctx = test->priv;
255 struct kunit_resource *res = kunit_alloc_and_get_resource(
256 &ctx->test,
257 fake_resource_init,
258 fake_resource_free,
259 GFP_KERNEL,
260 ctx);
261
262 /* The resource is in the list */
263 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
264
265 /* Remove the resource. The pointer is still valid, but it can't be
266 * found.
267 */
268 kunit_remove_resource(test, res);
269 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
270 /* We haven't been freed yet. */
271 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
272
273 /* Removing the resource multiple times is valid. */
274 kunit_remove_resource(test, res);
275 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
276 /* Despite having been removed twice (from only one reference), the
277 * resource still has not been freed.
278 */
279 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
280
281 /* Free the resource. */
282 kunit_put_resource(res);
283 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
284 }
285
kunit_resource_test_cleanup_resources(struct kunit * test)286 static void kunit_resource_test_cleanup_resources(struct kunit *test)
287 {
288 int i;
289 struct kunit_test_resource_context *ctx = test->priv;
290 struct kunit_resource *resources[5];
291
292 for (i = 0; i < ARRAY_SIZE(resources); i++) {
293 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
294 fake_resource_init,
295 fake_resource_free,
296 GFP_KERNEL,
297 ctx);
298 kunit_put_resource(resources[i]);
299 }
300
301 kunit_cleanup(&ctx->test);
302
303 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
304 }
305
kunit_resource_test_mark_order(int order_array[],size_t order_size,int key)306 static void kunit_resource_test_mark_order(int order_array[],
307 size_t order_size,
308 int key)
309 {
310 int i;
311
312 for (i = 0; i < order_size && order_array[i]; i++)
313 ;
314
315 order_array[i] = key;
316 }
317
318 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
319 kunit_resource_test_mark_order(ctx->order_field, \
320 ARRAY_SIZE(ctx->order_field), \
321 key)
322
fake_resource_2_init(struct kunit_resource * res,void * context)323 static int fake_resource_2_init(struct kunit_resource *res, void *context)
324 {
325 struct kunit_test_resource_context *ctx = context;
326
327 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
328
329 res->data = ctx;
330
331 return 0;
332 }
333
fake_resource_2_free(struct kunit_resource * res)334 static void fake_resource_2_free(struct kunit_resource *res)
335 {
336 struct kunit_test_resource_context *ctx = res->data;
337
338 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
339 }
340
fake_resource_1_init(struct kunit_resource * res,void * context)341 static int fake_resource_1_init(struct kunit_resource *res, void *context)
342 {
343 struct kunit_test_resource_context *ctx = context;
344 struct kunit_resource *res2;
345
346 res2 = kunit_alloc_and_get_resource(&ctx->test,
347 fake_resource_2_init,
348 fake_resource_2_free,
349 GFP_KERNEL,
350 ctx);
351
352 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
353
354 res->data = ctx;
355
356 kunit_put_resource(res2);
357
358 return 0;
359 }
360
fake_resource_1_free(struct kunit_resource * res)361 static void fake_resource_1_free(struct kunit_resource *res)
362 {
363 struct kunit_test_resource_context *ctx = res->data;
364
365 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
366 }
367
368 /*
369 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
370 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
371 * to assert allocation and freeing order when the feature becomes available.
372 */
kunit_resource_test_proper_free_ordering(struct kunit * test)373 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
374 {
375 struct kunit_test_resource_context *ctx = test->priv;
376 struct kunit_resource *res;
377
378 /* fake_resource_1 allocates a fake_resource_2 in its init. */
379 res = kunit_alloc_and_get_resource(&ctx->test,
380 fake_resource_1_init,
381 fake_resource_1_free,
382 GFP_KERNEL,
383 ctx);
384
385 /*
386 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
387 * before returning to fake_resource_1_init, it should be the first to
388 * put its key in the allocate_order array.
389 */
390 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
391 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
392
393 kunit_put_resource(res);
394
395 kunit_cleanup(&ctx->test);
396
397 /*
398 * Because fake_resource_2 finishes allocation before fake_resource_1,
399 * fake_resource_1 should be freed first since it could depend on
400 * fake_resource_2.
401 */
402 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
403 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
404 }
405
kunit_resource_test_static(struct kunit * test)406 static void kunit_resource_test_static(struct kunit *test)
407 {
408 struct kunit_test_resource_context ctx;
409 struct kunit_resource res;
410
411 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
412 0);
413
414 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
415
416 kunit_cleanup(test);
417
418 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
419 }
420
kunit_resource_test_named(struct kunit * test)421 static void kunit_resource_test_named(struct kunit *test)
422 {
423 struct kunit_resource res1, res2, *found = NULL;
424 struct kunit_test_resource_context ctx;
425
426 KUNIT_EXPECT_EQ(test,
427 kunit_add_named_resource(test, NULL, NULL, &res1,
428 "resource_1", &ctx),
429 0);
430 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
431
432 KUNIT_EXPECT_EQ(test,
433 kunit_add_named_resource(test, NULL, NULL, &res1,
434 "resource_1", &ctx),
435 -EEXIST);
436
437 KUNIT_EXPECT_EQ(test,
438 kunit_add_named_resource(test, NULL, NULL, &res2,
439 "resource_2", &ctx),
440 0);
441
442 found = kunit_find_named_resource(test, "resource_1");
443
444 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
445
446 if (found)
447 kunit_put_resource(&res1);
448
449 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
450 0);
451
452 kunit_cleanup(test);
453
454 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
455 }
456
increment_int(void * ctx)457 static void increment_int(void *ctx)
458 {
459 int *i = (int *)ctx;
460 (*i)++;
461 }
462
kunit_resource_test_action(struct kunit * test)463 static void kunit_resource_test_action(struct kunit *test)
464 {
465 int num_actions = 0;
466
467 kunit_add_action(test, increment_int, &num_actions);
468 KUNIT_EXPECT_EQ(test, num_actions, 0);
469 kunit_cleanup(test);
470 KUNIT_EXPECT_EQ(test, num_actions, 1);
471
472 /* Once we've cleaned up, the action queue is empty. */
473 kunit_cleanup(test);
474 KUNIT_EXPECT_EQ(test, num_actions, 1);
475
476 /* Check the same function can be deferred multiple times. */
477 kunit_add_action(test, increment_int, &num_actions);
478 kunit_add_action(test, increment_int, &num_actions);
479 kunit_cleanup(test);
480 KUNIT_EXPECT_EQ(test, num_actions, 3);
481 }
kunit_resource_test_remove_action(struct kunit * test)482 static void kunit_resource_test_remove_action(struct kunit *test)
483 {
484 int num_actions = 0;
485
486 kunit_add_action(test, increment_int, &num_actions);
487 KUNIT_EXPECT_EQ(test, num_actions, 0);
488
489 kunit_remove_action(test, increment_int, &num_actions);
490 kunit_cleanup(test);
491 KUNIT_EXPECT_EQ(test, num_actions, 0);
492 }
kunit_resource_test_release_action(struct kunit * test)493 static void kunit_resource_test_release_action(struct kunit *test)
494 {
495 int num_actions = 0;
496
497 kunit_add_action(test, increment_int, &num_actions);
498 KUNIT_EXPECT_EQ(test, num_actions, 0);
499 /* Runs immediately on trigger. */
500 kunit_release_action(test, increment_int, &num_actions);
501 KUNIT_EXPECT_EQ(test, num_actions, 1);
502
503 /* Doesn't run again on test exit. */
504 kunit_cleanup(test);
505 KUNIT_EXPECT_EQ(test, num_actions, 1);
506 }
action_order_1(void * ctx)507 static void action_order_1(void *ctx)
508 {
509 struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
510
511 KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 1);
512 kunit_log(KERN_INFO, current->kunit_test, "action_order_1");
513 }
action_order_2(void * ctx)514 static void action_order_2(void *ctx)
515 {
516 struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
517
518 KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 2);
519 kunit_log(KERN_INFO, current->kunit_test, "action_order_2");
520 }
kunit_resource_test_action_ordering(struct kunit * test)521 static void kunit_resource_test_action_ordering(struct kunit *test)
522 {
523 struct kunit_test_resource_context *ctx = test->priv;
524
525 kunit_add_action(test, action_order_1, ctx);
526 kunit_add_action(test, action_order_2, ctx);
527 kunit_add_action(test, action_order_1, ctx);
528 kunit_add_action(test, action_order_2, ctx);
529 kunit_remove_action(test, action_order_1, ctx);
530 kunit_release_action(test, action_order_2, ctx);
531 kunit_cleanup(test);
532
533 /* [2 is triggered] [2], [(1 is cancelled)] [1] */
534 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
535 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
536 KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
537 }
538
kunit_resource_test_init(struct kunit * test)539 static int kunit_resource_test_init(struct kunit *test)
540 {
541 struct kunit_test_resource_context *ctx = kzalloc_obj(*ctx, GFP_KERNEL);
542
543 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
544
545 test->priv = ctx;
546
547 kunit_init_test(&ctx->test, "test_test_context", NULL);
548
549 return 0;
550 }
551
kunit_resource_test_exit(struct kunit * test)552 static void kunit_resource_test_exit(struct kunit *test)
553 {
554 struct kunit_test_resource_context *ctx = test->priv;
555
556 kunit_cleanup(&ctx->test);
557 kfree(ctx);
558 }
559
560 static struct kunit_case kunit_resource_test_cases[] = {
561 KUNIT_CASE(kunit_resource_test_init_resources),
562 KUNIT_CASE(kunit_resource_test_alloc_resource),
563 KUNIT_CASE(kunit_resource_test_destroy_resource),
564 KUNIT_CASE(kunit_resource_test_remove_resource),
565 KUNIT_CASE(kunit_resource_test_cleanup_resources),
566 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
567 KUNIT_CASE(kunit_resource_test_static),
568 KUNIT_CASE(kunit_resource_test_named),
569 KUNIT_CASE(kunit_resource_test_action),
570 KUNIT_CASE(kunit_resource_test_remove_action),
571 KUNIT_CASE(kunit_resource_test_release_action),
572 KUNIT_CASE(kunit_resource_test_action_ordering),
573 {}
574 };
575
576 static struct kunit_suite kunit_resource_test_suite = {
577 .name = "kunit-resource-test",
578 .init = kunit_resource_test_init,
579 .exit = kunit_resource_test_exit,
580 .test_cases = kunit_resource_test_cases,
581 };
582
583 /*
584 * Log tests call string_stream functions, which aren't exported. So only
585 * build this code if this test is built-in.
586 */
587 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
588
589 /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
590 KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
591
kunit_log_test(struct kunit * test)592 static void kunit_log_test(struct kunit *test)
593 {
594 struct kunit_suite suite;
595 #ifdef CONFIG_KUNIT_DEBUGFS
596 char *full_log;
597 #endif
598 suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
599 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
600 string_stream_set_append_newlines(suite.log, true);
601
602 kunit_log(KERN_INFO, test, "put this in log.");
603 kunit_log(KERN_INFO, test, "this too.");
604 kunit_log(KERN_INFO, &suite, "add to suite log.");
605 kunit_log(KERN_INFO, &suite, "along with this.");
606
607 #ifdef CONFIG_KUNIT_DEBUGFS
608 KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
609
610 full_log = string_stream_get_string(test->log);
611 kunit_add_action(test, kfree_wrapper, full_log);
612 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
613 strstr(full_log, "put this in log."));
614 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
615 strstr(full_log, "this too."));
616
617 full_log = string_stream_get_string(suite.log);
618 kunit_add_action(test, kfree_wrapper, full_log);
619 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
620 strstr(full_log, "add to suite log."));
621 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
622 strstr(full_log, "along with this."));
623 #else
624 KUNIT_EXPECT_NULL(test, test->log);
625 #endif
626 }
627
kunit_log_newline_test(struct kunit * test)628 static void kunit_log_newline_test(struct kunit *test)
629 {
630 char *full_log;
631
632 kunit_info(test, "Add newline\n");
633 if (test->log) {
634 full_log = string_stream_get_string(test->log);
635 kunit_add_action(test, kfree_wrapper, full_log);
636 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
637 "Missing log line, full log:\n%s", full_log);
638 KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
639 } else {
640 kunit_skip(test, "only useful when debugfs is enabled");
641 }
642 }
643 #else
kunit_log_test(struct kunit * test)644 static void kunit_log_test(struct kunit *test)
645 {
646 kunit_skip(test, "Log tests only run when built-in");
647 }
648
kunit_log_newline_test(struct kunit * test)649 static void kunit_log_newline_test(struct kunit *test)
650 {
651 kunit_skip(test, "Log tests only run when built-in");
652 }
653 #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
654
655 static struct kunit_case kunit_log_test_cases[] = {
656 KUNIT_CASE(kunit_log_test),
657 KUNIT_CASE(kunit_log_newline_test),
658 {}
659 };
660
661 static struct kunit_suite kunit_log_test_suite = {
662 .name = "kunit-log-test",
663 .test_cases = kunit_log_test_cases,
664 };
665
kunit_status_set_failure_test(struct kunit * test)666 static void kunit_status_set_failure_test(struct kunit *test)
667 {
668 struct kunit fake;
669
670 kunit_init_test(&fake, "fake test", NULL);
671
672 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
673 kunit_set_failure(&fake);
674 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
675 }
676
kunit_status_mark_skipped_test(struct kunit * test)677 static void kunit_status_mark_skipped_test(struct kunit *test)
678 {
679 struct kunit fake;
680
681 kunit_init_test(&fake, "fake test", NULL);
682
683 /* Before: Should be SUCCESS with no comment. */
684 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
685 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
686
687 /* Mark the test as skipped. */
688 kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
689
690 /* After: Should be SKIPPED with our comment. */
691 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
692 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
693 }
694
695 static struct kunit_case kunit_status_test_cases[] = {
696 KUNIT_CASE(kunit_status_set_failure_test),
697 KUNIT_CASE(kunit_status_mark_skipped_test),
698 {}
699 };
700
701 static struct kunit_suite kunit_status_test_suite = {
702 .name = "kunit_status",
703 .test_cases = kunit_status_test_cases,
704 };
705
kunit_current_test(struct kunit * test)706 static void kunit_current_test(struct kunit *test)
707 {
708 /* Check results of both current->kunit_test and
709 * kunit_get_current_test() are equivalent to current test.
710 */
711 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
712 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
713 }
714
kunit_current_fail_test(struct kunit * test)715 static void kunit_current_fail_test(struct kunit *test)
716 {
717 struct kunit fake;
718
719 kunit_init_test(&fake, "fake test", NULL);
720 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
721
722 /* Set current->kunit_test to fake test. */
723 current->kunit_test = &fake;
724
725 kunit_fail_current_test("This should make `fake` test fail.");
726 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
727 kunit_cleanup(&fake);
728
729 /* Reset current->kunit_test to current test. */
730 current->kunit_test = test;
731 }
732
733 static struct kunit_case kunit_current_test_cases[] = {
734 KUNIT_CASE(kunit_current_test),
735 KUNIT_CASE(kunit_current_fail_test),
736 {}
737 };
738
test_dev_action(void * priv)739 static void test_dev_action(void *priv)
740 {
741 *(long *)priv = 1;
742 }
743
kunit_device_test(struct kunit * test)744 static void kunit_device_test(struct kunit *test)
745 {
746 struct device *test_device;
747 long action_was_run = 0;
748
749 test_device = kunit_device_register(test, "my_device");
750 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
751
752 // Add an action to verify cleanup.
753 devm_add_action(test_device, test_dev_action, &action_was_run);
754
755 KUNIT_EXPECT_EQ(test, action_was_run, 0);
756
757 kunit_device_unregister(test, test_device);
758
759 KUNIT_EXPECT_EQ(test, action_was_run, 1);
760 }
761
kunit_device_cleanup_test(struct kunit * test)762 static void kunit_device_cleanup_test(struct kunit *test)
763 {
764 struct device *test_device;
765 long action_was_run = 0;
766
767 test_device = kunit_device_register(test, "my_device");
768 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
769
770 /* Add an action to verify cleanup. */
771 devm_add_action(test_device, test_dev_action, &action_was_run);
772
773 KUNIT_EXPECT_EQ(test, action_was_run, 0);
774
775 /* Force KUnit to run cleanup early. */
776 kunit_cleanup(test);
777
778 KUNIT_EXPECT_EQ(test, action_was_run, 1);
779 }
780
781 struct driver_test_state {
782 bool driver_device_probed;
783 bool driver_device_removed;
784 long action_was_run;
785 };
786
driver_probe_hook(struct device * dev)787 static int driver_probe_hook(struct device *dev)
788 {
789 struct kunit *test = kunit_get_current_test();
790 struct driver_test_state *state = (struct driver_test_state *)test->priv;
791
792 state->driver_device_probed = true;
793 return 0;
794 }
795
driver_remove_hook(struct device * dev)796 static int driver_remove_hook(struct device *dev)
797 {
798 struct kunit *test = kunit_get_current_test();
799 struct driver_test_state *state = (struct driver_test_state *)test->priv;
800
801 state->driver_device_removed = true;
802 return 0;
803 }
804
kunit_device_driver_test(struct kunit * test)805 static void kunit_device_driver_test(struct kunit *test)
806 {
807 struct device_driver *test_driver;
808 struct device *test_device;
809 struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
810
811 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);
812
813 test->priv = test_state;
814 test_driver = kunit_driver_create(test, "my_driver");
815
816 // This can fail with an error pointer.
817 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
818
819 test_driver->probe = driver_probe_hook;
820 test_driver->remove = driver_remove_hook;
821
822 test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
823
824 // This can fail with an error pointer.
825 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
826
827 // Make sure the probe function was called.
828 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
829
830 // Add an action to verify cleanup.
831 devm_add_action(test_device, test_dev_action, &test_state->action_was_run);
832
833 KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
834
835 kunit_device_unregister(test, test_device);
836 test_device = NULL;
837
838 // Make sure the remove hook was called.
839 KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
840
841 // We're going to test this again.
842 test_state->driver_device_probed = false;
843
844 // The driver should not automatically be destroyed by
845 // kunit_device_unregister, so we can re-use it.
846 test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
847
848 // This can fail with an error pointer.
849 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
850
851 // Probe was called again.
852 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
853
854 // Everything is automatically freed here.
855 }
856
857 static struct kunit_case kunit_device_test_cases[] = {
858 KUNIT_CASE(kunit_device_test),
859 KUNIT_CASE(kunit_device_cleanup_test),
860 KUNIT_CASE(kunit_device_driver_test),
861 {}
862 };
863
864 static struct kunit_suite kunit_device_test_suite = {
865 .name = "kunit_device",
866 .test_cases = kunit_device_test_cases,
867 };
868
869 static struct kunit_suite kunit_current_test_suite = {
870 .name = "kunit_current",
871 .test_cases = kunit_current_test_cases,
872 };
873
kunit_stub_test(struct kunit * test)874 static void kunit_stub_test(struct kunit *test)
875 {
876 struct kunit fake_test;
877 const unsigned long fake_real_fn_addr = 0x1234;
878 const unsigned long fake_replacement_addr = 0x5678;
879 struct kunit_resource *res;
880 struct {
881 void *real_fn_addr;
882 void *replacement_addr;
883 } *stub_ctx;
884
885 kunit_init_test(&fake_test, "kunit_stub_fake_test", NULL);
886 KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
887 KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
888
889 __kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr,
890 (void *)fake_replacement_addr);
891 KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
892 KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 1);
893
894 res = list_first_entry(&fake_test.resources, struct kunit_resource, node);
895 KUNIT_EXPECT_NOT_NULL(test, res);
896
897 stub_ctx = res->data;
898 KUNIT_EXPECT_NOT_NULL(test, stub_ctx);
899 KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->real_fn_addr, fake_real_fn_addr);
900 KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->replacement_addr, fake_replacement_addr);
901
902 __kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr, NULL);
903 KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
904 KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
905 }
906
907 static struct kunit_case kunit_stub_test_cases[] = {
908 KUNIT_CASE(kunit_stub_test),
909 {}
910 };
911
912 static struct kunit_suite kunit_stub_test_suite = {
913 .name = "kunit_stub",
914 .test_cases = kunit_stub_test_cases,
915 };
916
917 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
918 &kunit_log_test_suite, &kunit_status_test_suite,
919 &kunit_current_test_suite, &kunit_device_test_suite,
920 &kunit_fault_test_suite, &kunit_stub_test_suite);
921
922 MODULE_DESCRIPTION("KUnit test for core test infrastructure");
923 MODULE_LICENSE("GPL v2");
924