xref: /linux/lib/kunit/test.c (revision 28dce2c4a83d6e34936ba8d5c3ee780861460100)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Base unit test (KUnit) API.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: Brendan Higgins <brendanhiggins@google.com>
7  */
8 
9 #include <kunit/test.h>
10 #include <kunit/test-bug.h>
11 #include <linux/kernel.h>
12 #include <linux/kref.h>
13 #include <linux/sched/debug.h>
14 #include <linux/sched.h>
15 
16 #include "debugfs.h"
17 #include "string-stream.h"
18 #include "try-catch-impl.h"
19 
20 #if IS_BUILTIN(CONFIG_KUNIT)
21 /*
22  * Fail the current test and print an error message to the log.
23  */
24 void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
25 {
26 	va_list args;
27 	int len;
28 	char *buffer;
29 
30 	if (!current->kunit_test)
31 		return;
32 
33 	kunit_set_failure(current->kunit_test);
34 
35 	/* kunit_err() only accepts literals, so evaluate the args first. */
36 	va_start(args, fmt);
37 	len = vsnprintf(NULL, 0, fmt, args) + 1;
38 	va_end(args);
39 
40 	buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
41 	if (!buffer)
42 		return;
43 
44 	va_start(args, fmt);
45 	vsnprintf(buffer, len, fmt, args);
46 	va_end(args);
47 
48 	kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
49 	kunit_kfree(current->kunit_test, buffer);
50 }
51 EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
52 #endif
53 
54 /*
55  * Append formatted message to log, size of which is limited to
56  * KUNIT_LOG_SIZE bytes (including null terminating byte).
57  */
58 void kunit_log_append(char *log, const char *fmt, ...)
59 {
60 	char line[KUNIT_LOG_SIZE];
61 	va_list args;
62 	int len_left;
63 
64 	if (!log)
65 		return;
66 
67 	len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
68 	if (len_left <= 0)
69 		return;
70 
71 	va_start(args, fmt);
72 	vsnprintf(line, sizeof(line), fmt, args);
73 	va_end(args);
74 
75 	strncat(log, line, len_left);
76 }
77 EXPORT_SYMBOL_GPL(kunit_log_append);
78 
79 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
80 {
81 	struct kunit_case *test_case;
82 	size_t len = 0;
83 
84 	kunit_suite_for_each_test_case(suite, test_case)
85 		len++;
86 
87 	return len;
88 }
89 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
90 
91 static void kunit_print_subtest_start(struct kunit_suite *suite)
92 {
93 	kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
94 		  suite->name);
95 	kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
96 		  kunit_suite_num_test_cases(suite));
97 }
98 
99 static void kunit_print_ok_not_ok(void *test_or_suite,
100 				  bool is_test,
101 				  bool is_ok,
102 				  size_t test_number,
103 				  const char *description)
104 {
105 	struct kunit_suite *suite = is_test ? NULL : test_or_suite;
106 	struct kunit *test = is_test ? test_or_suite : NULL;
107 
108 	/*
109 	 * We do not log the test suite results as doing so would
110 	 * mean debugfs display would consist of the test suite
111 	 * description and status prior to individual test results.
112 	 * Hence directly printk the suite status, and we will
113 	 * separately seq_printf() the suite status for the debugfs
114 	 * representation.
115 	 */
116 	if (suite)
117 		pr_info("%s %zd - %s\n",
118 			kunit_status_to_string(is_ok),
119 			test_number, description);
120 	else
121 		kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
122 			  kunit_status_to_string(is_ok),
123 			  test_number, description);
124 }
125 
126 bool kunit_suite_has_succeeded(struct kunit_suite *suite)
127 {
128 	const struct kunit_case *test_case;
129 
130 	kunit_suite_for_each_test_case(suite, test_case) {
131 		if (!test_case->success)
132 			return false;
133 	}
134 
135 	return true;
136 }
137 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
138 
139 static void kunit_print_subtest_end(struct kunit_suite *suite)
140 {
141 	static size_t kunit_suite_counter = 1;
142 
143 	kunit_print_ok_not_ok((void *)suite, false,
144 			      kunit_suite_has_succeeded(suite),
145 			      kunit_suite_counter++,
146 			      suite->name);
147 }
148 
149 unsigned int kunit_test_case_num(struct kunit_suite *suite,
150 				 struct kunit_case *test_case)
151 {
152 	struct kunit_case *tc;
153 	unsigned int i = 1;
154 
155 	kunit_suite_for_each_test_case(suite, tc) {
156 		if (tc == test_case)
157 			return i;
158 		i++;
159 	}
160 
161 	return 0;
162 }
163 EXPORT_SYMBOL_GPL(kunit_test_case_num);
164 
165 static void kunit_print_string_stream(struct kunit *test,
166 				      struct string_stream *stream)
167 {
168 	struct string_stream_fragment *fragment;
169 	char *buf;
170 
171 	if (string_stream_is_empty(stream))
172 		return;
173 
174 	buf = string_stream_get_string(stream);
175 	if (!buf) {
176 		kunit_err(test,
177 			  "Could not allocate buffer, dumping stream:\n");
178 		list_for_each_entry(fragment, &stream->fragments, node) {
179 			kunit_err(test, "%s", fragment->fragment);
180 		}
181 		kunit_err(test, "\n");
182 	} else {
183 		kunit_err(test, "%s", buf);
184 		kunit_kfree(test, buf);
185 	}
186 }
187 
188 static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
189 {
190 	struct string_stream *stream;
191 
192 	kunit_set_failure(test);
193 
194 	stream = alloc_string_stream(test, GFP_KERNEL);
195 	if (!stream) {
196 		WARN(true,
197 		     "Could not allocate stream to print failed assertion in %s:%d\n",
198 		     assert->file,
199 		     assert->line);
200 		return;
201 	}
202 
203 	assert->format(assert, stream);
204 
205 	kunit_print_string_stream(test, stream);
206 
207 	WARN_ON(string_stream_destroy(stream));
208 }
209 
210 static void __noreturn kunit_abort(struct kunit *test)
211 {
212 	kunit_try_catch_throw(&test->try_catch); /* Does not return. */
213 
214 	/*
215 	 * Throw could not abort from test.
216 	 *
217 	 * XXX: we should never reach this line! As kunit_try_catch_throw is
218 	 * marked __noreturn.
219 	 */
220 	WARN_ONCE(true, "Throw could not abort from test!\n");
221 }
222 
223 void kunit_do_assertion(struct kunit *test,
224 			struct kunit_assert *assert,
225 			bool pass,
226 			const char *fmt, ...)
227 {
228 	va_list args;
229 
230 	if (pass)
231 		return;
232 
233 	va_start(args, fmt);
234 
235 	assert->message.fmt = fmt;
236 	assert->message.va = &args;
237 
238 	kunit_fail(test, assert);
239 
240 	va_end(args);
241 
242 	if (assert->type == KUNIT_ASSERTION)
243 		kunit_abort(test);
244 }
245 EXPORT_SYMBOL_GPL(kunit_do_assertion);
246 
247 void kunit_init_test(struct kunit *test, const char *name, char *log)
248 {
249 	spin_lock_init(&test->lock);
250 	INIT_LIST_HEAD(&test->resources);
251 	test->name = name;
252 	test->log = log;
253 	if (test->log)
254 		test->log[0] = '\0';
255 	test->success = true;
256 }
257 EXPORT_SYMBOL_GPL(kunit_init_test);
258 
259 /*
260  * Initializes and runs test case. Does not clean up or do post validations.
261  */
262 static void kunit_run_case_internal(struct kunit *test,
263 				    struct kunit_suite *suite,
264 				    struct kunit_case *test_case)
265 {
266 	if (suite->init) {
267 		int ret;
268 
269 		ret = suite->init(test);
270 		if (ret) {
271 			kunit_err(test, "failed to initialize: %d\n", ret);
272 			kunit_set_failure(test);
273 			return;
274 		}
275 	}
276 
277 	test_case->run_case(test);
278 }
279 
280 static void kunit_case_internal_cleanup(struct kunit *test)
281 {
282 	kunit_cleanup(test);
283 }
284 
285 /*
286  * Performs post validations and cleanup after a test case was run.
287  * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
288  */
289 static void kunit_run_case_cleanup(struct kunit *test,
290 				   struct kunit_suite *suite)
291 {
292 	if (suite->exit)
293 		suite->exit(test);
294 
295 	kunit_case_internal_cleanup(test);
296 }
297 
298 struct kunit_try_catch_context {
299 	struct kunit *test;
300 	struct kunit_suite *suite;
301 	struct kunit_case *test_case;
302 };
303 
304 static void kunit_try_run_case(void *data)
305 {
306 	struct kunit_try_catch_context *ctx = data;
307 	struct kunit *test = ctx->test;
308 	struct kunit_suite *suite = ctx->suite;
309 	struct kunit_case *test_case = ctx->test_case;
310 
311 	current->kunit_test = test;
312 
313 	/*
314 	 * kunit_run_case_internal may encounter a fatal error; if it does,
315 	 * abort will be called, this thread will exit, and finally the parent
316 	 * thread will resume control and handle any necessary clean up.
317 	 */
318 	kunit_run_case_internal(test, suite, test_case);
319 	/* This line may never be reached. */
320 	kunit_run_case_cleanup(test, suite);
321 }
322 
323 static void kunit_catch_run_case(void *data)
324 {
325 	struct kunit_try_catch_context *ctx = data;
326 	struct kunit *test = ctx->test;
327 	struct kunit_suite *suite = ctx->suite;
328 	int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
329 
330 	if (try_exit_code) {
331 		kunit_set_failure(test);
332 		/*
333 		 * Test case could not finish, we have no idea what state it is
334 		 * in, so don't do clean up.
335 		 */
336 		if (try_exit_code == -ETIMEDOUT) {
337 			kunit_err(test, "test case timed out\n");
338 		/*
339 		 * Unknown internal error occurred preventing test case from
340 		 * running, so there is nothing to clean up.
341 		 */
342 		} else {
343 			kunit_err(test, "internal error occurred preventing test case from running: %d\n",
344 				  try_exit_code);
345 		}
346 		return;
347 	}
348 
349 	/*
350 	 * Test case was run, but aborted. It is the test case's business as to
351 	 * whether it failed or not, we just need to clean up.
352 	 */
353 	kunit_run_case_cleanup(test, suite);
354 }
355 
356 /*
357  * Performs all logic to run a test case. It also catches most errors that
358  * occur in a test case and reports them as failures.
359  */
360 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
361 					struct kunit_case *test_case,
362 					struct kunit *test)
363 {
364 	struct kunit_try_catch_context context;
365 	struct kunit_try_catch *try_catch;
366 
367 	kunit_init_test(test, test_case->name, test_case->log);
368 	try_catch = &test->try_catch;
369 
370 	kunit_try_catch_init(try_catch,
371 			     test,
372 			     kunit_try_run_case,
373 			     kunit_catch_run_case);
374 	context.test = test;
375 	context.suite = suite;
376 	context.test_case = test_case;
377 	kunit_try_catch_run(try_catch, &context);
378 
379 	test_case->success &= test->success;
380 }
381 
382 int kunit_run_tests(struct kunit_suite *suite)
383 {
384 	char param_desc[KUNIT_PARAM_DESC_SIZE];
385 	struct kunit_case *test_case;
386 
387 	kunit_print_subtest_start(suite);
388 
389 	kunit_suite_for_each_test_case(suite, test_case) {
390 		struct kunit test = { .param_value = NULL, .param_index = 0 };
391 		test_case->success = true;
392 
393 		if (test_case->generate_params) {
394 			/* Get initial param. */
395 			param_desc[0] = '\0';
396 			test.param_value = test_case->generate_params(NULL, param_desc);
397 		}
398 
399 		do {
400 			kunit_run_case_catch_errors(suite, test_case, &test);
401 
402 			if (test_case->generate_params) {
403 				if (param_desc[0] == '\0') {
404 					snprintf(param_desc, sizeof(param_desc),
405 						 "param-%d", test.param_index);
406 				}
407 
408 				kunit_log(KERN_INFO, &test,
409 					  KUNIT_SUBTEST_INDENT
410 					  "# %s: %s %d - %s",
411 					  test_case->name,
412 					  kunit_status_to_string(test.success),
413 					  test.param_index + 1, param_desc);
414 
415 				/* Get next param. */
416 				param_desc[0] = '\0';
417 				test.param_value = test_case->generate_params(test.param_value, param_desc);
418 				test.param_index++;
419 			}
420 		} while (test.param_value);
421 
422 		kunit_print_ok_not_ok(&test, true, test_case->success,
423 				      kunit_test_case_num(suite, test_case),
424 				      test_case->name);
425 	}
426 
427 	kunit_print_subtest_end(suite);
428 
429 	return 0;
430 }
431 EXPORT_SYMBOL_GPL(kunit_run_tests);
432 
433 static void kunit_init_suite(struct kunit_suite *suite)
434 {
435 	kunit_debugfs_create_suite(suite);
436 }
437 
438 int __kunit_test_suites_init(struct kunit_suite * const * const suites)
439 {
440 	unsigned int i;
441 
442 	for (i = 0; suites[i] != NULL; i++) {
443 		kunit_init_suite(suites[i]);
444 		kunit_run_tests(suites[i]);
445 	}
446 	return 0;
447 }
448 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
449 
450 static void kunit_exit_suite(struct kunit_suite *suite)
451 {
452 	kunit_debugfs_destroy_suite(suite);
453 }
454 
455 void __kunit_test_suites_exit(struct kunit_suite **suites)
456 {
457 	unsigned int i;
458 
459 	for (i = 0; suites[i] != NULL; i++)
460 		kunit_exit_suite(suites[i]);
461 }
462 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
463 
464 /*
465  * Used for static resources and when a kunit_resource * has been created by
466  * kunit_alloc_resource().  When an init function is supplied, @data is passed
467  * into the init function; otherwise, we simply set the resource data field to
468  * the data value passed in.
469  */
470 int kunit_add_resource(struct kunit *test,
471 		       kunit_resource_init_t init,
472 		       kunit_resource_free_t free,
473 		       struct kunit_resource *res,
474 		       void *data)
475 {
476 	int ret = 0;
477 
478 	res->free = free;
479 	kref_init(&res->refcount);
480 
481 	if (init) {
482 		ret = init(res, data);
483 		if (ret)
484 			return ret;
485 	} else {
486 		res->data = data;
487 	}
488 
489 	spin_lock(&test->lock);
490 	list_add_tail(&res->node, &test->resources);
491 	/* refcount for list is established by kref_init() */
492 	spin_unlock(&test->lock);
493 
494 	return ret;
495 }
496 EXPORT_SYMBOL_GPL(kunit_add_resource);
497 
498 int kunit_add_named_resource(struct kunit *test,
499 			     kunit_resource_init_t init,
500 			     kunit_resource_free_t free,
501 			     struct kunit_resource *res,
502 			     const char *name,
503 			     void *data)
504 {
505 	struct kunit_resource *existing;
506 
507 	if (!name)
508 		return -EINVAL;
509 
510 	existing = kunit_find_named_resource(test, name);
511 	if (existing) {
512 		kunit_put_resource(existing);
513 		return -EEXIST;
514 	}
515 
516 	res->name = name;
517 
518 	return kunit_add_resource(test, init, free, res, data);
519 }
520 EXPORT_SYMBOL_GPL(kunit_add_named_resource);
521 
522 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
523 						    kunit_resource_init_t init,
524 						    kunit_resource_free_t free,
525 						    gfp_t internal_gfp,
526 						    void *data)
527 {
528 	struct kunit_resource *res;
529 	int ret;
530 
531 	res = kzalloc(sizeof(*res), internal_gfp);
532 	if (!res)
533 		return NULL;
534 
535 	ret = kunit_add_resource(test, init, free, res, data);
536 	if (!ret) {
537 		/*
538 		 * bump refcount for get; kunit_resource_put() should be called
539 		 * when done.
540 		 */
541 		kunit_get_resource(res);
542 		return res;
543 	}
544 	return NULL;
545 }
546 EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
547 
548 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
549 {
550 	spin_lock(&test->lock);
551 	list_del(&res->node);
552 	spin_unlock(&test->lock);
553 	kunit_put_resource(res);
554 }
555 EXPORT_SYMBOL_GPL(kunit_remove_resource);
556 
557 int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
558 			   void *match_data)
559 {
560 	struct kunit_resource *res = kunit_find_resource(test, match,
561 							 match_data);
562 
563 	if (!res)
564 		return -ENOENT;
565 
566 	kunit_remove_resource(test, res);
567 
568 	/* We have a reference also via _find(); drop it. */
569 	kunit_put_resource(res);
570 
571 	return 0;
572 }
573 EXPORT_SYMBOL_GPL(kunit_destroy_resource);
574 
575 struct kunit_kmalloc_params {
576 	size_t size;
577 	gfp_t gfp;
578 };
579 
580 static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
581 {
582 	struct kunit_kmalloc_params *params = context;
583 
584 	res->data = kmalloc(params->size, params->gfp);
585 	if (!res->data)
586 		return -ENOMEM;
587 
588 	return 0;
589 }
590 
591 static void kunit_kmalloc_free(struct kunit_resource *res)
592 {
593 	kfree(res->data);
594 }
595 
596 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
597 {
598 	struct kunit_kmalloc_params params = {
599 		.size = size,
600 		.gfp = gfp
601 	};
602 
603 	return kunit_alloc_resource(test,
604 				    kunit_kmalloc_init,
605 				    kunit_kmalloc_free,
606 				    gfp,
607 				    &params);
608 }
609 EXPORT_SYMBOL_GPL(kunit_kmalloc);
610 
611 void kunit_kfree(struct kunit *test, const void *ptr)
612 {
613 	struct kunit_resource *res;
614 
615 	res = kunit_find_resource(test, kunit_resource_instance_match,
616 				  (void *)ptr);
617 
618 	/*
619 	 * Removing the resource from the list of resources drops the
620 	 * reference count to 1; the final put will trigger the free.
621 	 */
622 	kunit_remove_resource(test, res);
623 
624 	kunit_put_resource(res);
625 
626 }
627 EXPORT_SYMBOL_GPL(kunit_kfree);
628 
629 void kunit_cleanup(struct kunit *test)
630 {
631 	struct kunit_resource *res;
632 
633 	/*
634 	 * test->resources is a stack - each allocation must be freed in the
635 	 * reverse order from which it was added since one resource may depend
636 	 * on another for its entire lifetime.
637 	 * Also, we cannot use the normal list_for_each constructs, even the
638 	 * safe ones because *arbitrary* nodes may be deleted when
639 	 * kunit_resource_free is called; the list_for_each_safe variants only
640 	 * protect against the current node being deleted, not the next.
641 	 */
642 	while (true) {
643 		spin_lock(&test->lock);
644 		if (list_empty(&test->resources)) {
645 			spin_unlock(&test->lock);
646 			break;
647 		}
648 		res = list_last_entry(&test->resources,
649 				      struct kunit_resource,
650 				      node);
651 		/*
652 		 * Need to unlock here as a resource may remove another
653 		 * resource, and this can't happen if the test->lock
654 		 * is held.
655 		 */
656 		spin_unlock(&test->lock);
657 		kunit_remove_resource(test, res);
658 	}
659 	current->kunit_test = NULL;
660 }
661 EXPORT_SYMBOL_GPL(kunit_cleanup);
662 
663 static int __init kunit_init(void)
664 {
665 	kunit_debugfs_init();
666 
667 	return 0;
668 }
669 late_initcall(kunit_init);
670 
671 static void __exit kunit_exit(void)
672 {
673 	kunit_debugfs_cleanup();
674 }
675 module_exit(kunit_exit);
676 
677 MODULE_LICENSE("GPL v2");
678