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