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 =
542 			kzalloc(sizeof(*ctx), GFP_KERNEL);
543 
544 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
545 
546 	test->priv = ctx;
547 
548 	kunit_init_test(&ctx->test, "test_test_context", NULL);
549 
550 	return 0;
551 }
552 
kunit_resource_test_exit(struct kunit * test)553 static void kunit_resource_test_exit(struct kunit *test)
554 {
555 	struct kunit_test_resource_context *ctx = test->priv;
556 
557 	kunit_cleanup(&ctx->test);
558 	kfree(ctx);
559 }
560 
561 static struct kunit_case kunit_resource_test_cases[] = {
562 	KUNIT_CASE(kunit_resource_test_init_resources),
563 	KUNIT_CASE(kunit_resource_test_alloc_resource),
564 	KUNIT_CASE(kunit_resource_test_destroy_resource),
565 	KUNIT_CASE(kunit_resource_test_remove_resource),
566 	KUNIT_CASE(kunit_resource_test_cleanup_resources),
567 	KUNIT_CASE(kunit_resource_test_proper_free_ordering),
568 	KUNIT_CASE(kunit_resource_test_static),
569 	KUNIT_CASE(kunit_resource_test_named),
570 	KUNIT_CASE(kunit_resource_test_action),
571 	KUNIT_CASE(kunit_resource_test_remove_action),
572 	KUNIT_CASE(kunit_resource_test_release_action),
573 	KUNIT_CASE(kunit_resource_test_action_ordering),
574 	{}
575 };
576 
577 static struct kunit_suite kunit_resource_test_suite = {
578 	.name = "kunit-resource-test",
579 	.init = kunit_resource_test_init,
580 	.exit = kunit_resource_test_exit,
581 	.test_cases = kunit_resource_test_cases,
582 };
583 
584 /*
585  * Log tests call string_stream functions, which aren't exported. So only
586  * build this code if this test is built-in.
587  */
588 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
589 
590 /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
591 KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
592 
kunit_log_test(struct kunit * test)593 static void kunit_log_test(struct kunit *test)
594 {
595 	struct kunit_suite suite;
596 #ifdef CONFIG_KUNIT_DEBUGFS
597 	char *full_log;
598 #endif
599 	suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
600 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
601 	string_stream_set_append_newlines(suite.log, true);
602 
603 	kunit_log(KERN_INFO, test, "put this in log.");
604 	kunit_log(KERN_INFO, test, "this too.");
605 	kunit_log(KERN_INFO, &suite, "add to suite log.");
606 	kunit_log(KERN_INFO, &suite, "along with this.");
607 
608 #ifdef CONFIG_KUNIT_DEBUGFS
609 	KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
610 
611 	full_log = string_stream_get_string(test->log);
612 	kunit_add_action(test, kfree_wrapper, full_log);
613 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
614 				     strstr(full_log, "put this in log."));
615 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
616 				     strstr(full_log, "this too."));
617 
618 	full_log = string_stream_get_string(suite.log);
619 	kunit_add_action(test, kfree_wrapper, full_log);
620 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
621 				     strstr(full_log, "add to suite log."));
622 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
623 				     strstr(full_log, "along with this."));
624 #else
625 	KUNIT_EXPECT_NULL(test, test->log);
626 #endif
627 }
628 
kunit_log_newline_test(struct kunit * test)629 static void kunit_log_newline_test(struct kunit *test)
630 {
631 	char *full_log;
632 
633 	kunit_info(test, "Add newline\n");
634 	if (test->log) {
635 		full_log = string_stream_get_string(test->log);
636 		kunit_add_action(test, kfree_wrapper, full_log);
637 		KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
638 			"Missing log line, full log:\n%s", full_log);
639 		KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
640 	} else {
641 		kunit_skip(test, "only useful when debugfs is enabled");
642 	}
643 }
644 #else
kunit_log_test(struct kunit * test)645 static void kunit_log_test(struct kunit *test)
646 {
647 	kunit_skip(test, "Log tests only run when built-in");
648 }
649 
kunit_log_newline_test(struct kunit * test)650 static void kunit_log_newline_test(struct kunit *test)
651 {
652 	kunit_skip(test, "Log tests only run when built-in");
653 }
654 #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
655 
656 static struct kunit_case kunit_log_test_cases[] = {
657 	KUNIT_CASE(kunit_log_test),
658 	KUNIT_CASE(kunit_log_newline_test),
659 	{}
660 };
661 
662 static struct kunit_suite kunit_log_test_suite = {
663 	.name = "kunit-log-test",
664 	.test_cases = kunit_log_test_cases,
665 };
666 
kunit_status_set_failure_test(struct kunit * test)667 static void kunit_status_set_failure_test(struct kunit *test)
668 {
669 	struct kunit fake;
670 
671 	kunit_init_test(&fake, "fake test", NULL);
672 
673 	KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
674 	kunit_set_failure(&fake);
675 	KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
676 }
677 
kunit_status_mark_skipped_test(struct kunit * test)678 static void kunit_status_mark_skipped_test(struct kunit *test)
679 {
680 	struct kunit fake;
681 
682 	kunit_init_test(&fake, "fake test", NULL);
683 
684 	/* Before: Should be SUCCESS with no comment. */
685 	KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
686 	KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
687 
688 	/* Mark the test as skipped. */
689 	kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
690 
691 	/* After: Should be SKIPPED with our comment. */
692 	KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
693 	KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
694 }
695 
696 static struct kunit_case kunit_status_test_cases[] = {
697 	KUNIT_CASE(kunit_status_set_failure_test),
698 	KUNIT_CASE(kunit_status_mark_skipped_test),
699 	{}
700 };
701 
702 static struct kunit_suite kunit_status_test_suite = {
703 	.name = "kunit_status",
704 	.test_cases = kunit_status_test_cases,
705 };
706 
kunit_current_test(struct kunit * test)707 static void kunit_current_test(struct kunit *test)
708 {
709 	/* Check results of both current->kunit_test and
710 	 * kunit_get_current_test() are equivalent to current test.
711 	 */
712 	KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
713 	KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
714 }
715 
kunit_current_fail_test(struct kunit * test)716 static void kunit_current_fail_test(struct kunit *test)
717 {
718 	struct kunit fake;
719 
720 	kunit_init_test(&fake, "fake test", NULL);
721 	KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
722 
723 	/* Set current->kunit_test to fake test. */
724 	current->kunit_test = &fake;
725 
726 	kunit_fail_current_test("This should make `fake` test fail.");
727 	KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
728 	kunit_cleanup(&fake);
729 
730 	/* Reset current->kunit_test to current test. */
731 	current->kunit_test = test;
732 }
733 
734 static struct kunit_case kunit_current_test_cases[] = {
735 	KUNIT_CASE(kunit_current_test),
736 	KUNIT_CASE(kunit_current_fail_test),
737 	{}
738 };
739 
test_dev_action(void * priv)740 static void test_dev_action(void *priv)
741 {
742 	*(void **)priv = (void *)1;
743 }
744 
kunit_device_test(struct kunit * test)745 static void kunit_device_test(struct kunit *test)
746 {
747 	struct device *test_device;
748 	long action_was_run = 0;
749 
750 	test_device = kunit_device_register(test, "my_device");
751 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
752 
753 	// Add an action to verify cleanup.
754 	devm_add_action(test_device, test_dev_action, &action_was_run);
755 
756 	KUNIT_EXPECT_EQ(test, action_was_run, 0);
757 
758 	kunit_device_unregister(test, test_device);
759 
760 	KUNIT_EXPECT_EQ(test, action_was_run, 1);
761 }
762 
kunit_device_cleanup_test(struct kunit * test)763 static void kunit_device_cleanup_test(struct kunit *test)
764 {
765 	struct device *test_device;
766 	long action_was_run = 0;
767 
768 	test_device = kunit_device_register(test, "my_device");
769 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
770 
771 	/* Add an action to verify cleanup. */
772 	devm_add_action(test_device, test_dev_action, &action_was_run);
773 
774 	KUNIT_EXPECT_EQ(test, action_was_run, 0);
775 
776 	/* Force KUnit to run cleanup early. */
777 	kunit_cleanup(test);
778 
779 	KUNIT_EXPECT_EQ(test, action_was_run, 1);
780 }
781 
782 struct driver_test_state {
783 	bool driver_device_probed;
784 	bool driver_device_removed;
785 	long action_was_run;
786 };
787 
driver_probe_hook(struct device * dev)788 static int driver_probe_hook(struct device *dev)
789 {
790 	struct kunit *test = kunit_get_current_test();
791 	struct driver_test_state *state = (struct driver_test_state *)test->priv;
792 
793 	state->driver_device_probed = true;
794 	return 0;
795 }
796 
driver_remove_hook(struct device * dev)797 static int driver_remove_hook(struct device *dev)
798 {
799 	struct kunit *test = kunit_get_current_test();
800 	struct driver_test_state *state = (struct driver_test_state *)test->priv;
801 
802 	state->driver_device_removed = true;
803 	return 0;
804 }
805 
kunit_device_driver_test(struct kunit * test)806 static void kunit_device_driver_test(struct kunit *test)
807 {
808 	struct device_driver *test_driver;
809 	struct device *test_device;
810 	struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
811 
812 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);
813 
814 	test->priv = test_state;
815 	test_driver = kunit_driver_create(test, "my_driver");
816 
817 	// This can fail with an error pointer.
818 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
819 
820 	test_driver->probe = driver_probe_hook;
821 	test_driver->remove = driver_remove_hook;
822 
823 	test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
824 
825 	// This can fail with an error pointer.
826 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
827 
828 	// Make sure the probe function was called.
829 	KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
830 
831 	// Add an action to verify cleanup.
832 	devm_add_action(test_device, test_dev_action, &test_state->action_was_run);
833 
834 	KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
835 
836 	kunit_device_unregister(test, test_device);
837 	test_device = NULL;
838 
839 	// Make sure the remove hook was called.
840 	KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
841 
842 	// We're going to test this again.
843 	test_state->driver_device_probed = false;
844 
845 	// The driver should not automatically be destroyed by
846 	// kunit_device_unregister, so we can re-use it.
847 	test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
848 
849 	// This can fail with an error pointer.
850 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
851 
852 	// Probe was called again.
853 	KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
854 
855 	// Everything is automatically freed here.
856 }
857 
858 static struct kunit_case kunit_device_test_cases[] = {
859 	KUNIT_CASE(kunit_device_test),
860 	KUNIT_CASE(kunit_device_cleanup_test),
861 	KUNIT_CASE(kunit_device_driver_test),
862 	{}
863 };
864 
865 static struct kunit_suite kunit_device_test_suite = {
866 	.name = "kunit_device",
867 	.test_cases = kunit_device_test_cases,
868 };
869 
870 static struct kunit_suite kunit_current_test_suite = {
871 	.name = "kunit_current",
872 	.test_cases = kunit_current_test_cases,
873 };
874 
kunit_stub_test(struct kunit * test)875 static void kunit_stub_test(struct kunit *test)
876 {
877 	struct kunit fake_test;
878 	const unsigned long fake_real_fn_addr = 0x1234;
879 	const unsigned long fake_replacement_addr = 0x5678;
880 	struct kunit_resource *res;
881 	struct {
882 		void *real_fn_addr;
883 		void *replacement_addr;
884 	} *stub_ctx;
885 
886 	kunit_init_test(&fake_test, "kunit_stub_fake_test", NULL);
887 	KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
888 	KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
889 
890 	__kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr,
891 				     (void *)fake_replacement_addr);
892 	KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
893 	KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 1);
894 
895 	res = list_first_entry(&fake_test.resources, struct kunit_resource, node);
896 	KUNIT_EXPECT_NOT_NULL(test, res);
897 
898 	stub_ctx = res->data;
899 	KUNIT_EXPECT_NOT_NULL(test, stub_ctx);
900 	KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->real_fn_addr, fake_real_fn_addr);
901 	KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->replacement_addr, fake_replacement_addr);
902 
903 	__kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr, NULL);
904 	KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
905 	KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
906 }
907 
908 static struct kunit_case kunit_stub_test_cases[] = {
909 	KUNIT_CASE(kunit_stub_test),
910 	{}
911 };
912 
913 static struct kunit_suite kunit_stub_test_suite = {
914 	.name = "kunit_stub",
915 	.test_cases = kunit_stub_test_cases,
916 };
917 
918 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
919 		  &kunit_log_test_suite, &kunit_status_test_suite,
920 		  &kunit_current_test_suite, &kunit_device_test_suite,
921 		  &kunit_fault_test_suite, &kunit_stub_test_suite);
922 
923 MODULE_DESCRIPTION("KUnit test for core test infrastructure");
924 MODULE_LICENSE("GPL v2");
925