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