Loading...
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
18static void kunit_set_failure(struct kunit *test)
19{
20 WRITE_ONCE(test->success, false);
21}
22
23static void kunit_print_tap_version(void)
24{
25 static bool kunit_has_printed_tap_version;
26
27 if (!kunit_has_printed_tap_version) {
28 pr_info("TAP version 14\n");
29 kunit_has_printed_tap_version = true;
30 }
31}
32
33/*
34 * Append formatted message to log, size of which is limited to
35 * KUNIT_LOG_SIZE bytes (including null terminating byte).
36 */
37void kunit_log_append(char *log, const char *fmt, ...)
38{
39 char line[KUNIT_LOG_SIZE];
40 va_list args;
41 int len_left;
42
43 if (!log)
44 return;
45
46 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
47 if (len_left <= 0)
48 return;
49
50 va_start(args, fmt);
51 vsnprintf(line, sizeof(line), fmt, args);
52 va_end(args);
53
54 strncat(log, line, len_left);
55}
56EXPORT_SYMBOL_GPL(kunit_log_append);
57
58size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
59{
60 struct kunit_case *test_case;
61 size_t len = 0;
62
63 kunit_suite_for_each_test_case(suite, test_case)
64 len++;
65
66 return len;
67}
68EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
69
70static void kunit_print_subtest_start(struct kunit_suite *suite)
71{
72 kunit_print_tap_version();
73 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
74 suite->name);
75 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
76 kunit_suite_num_test_cases(suite));
77}
78
79static void kunit_print_ok_not_ok(void *test_or_suite,
80 bool is_test,
81 bool is_ok,
82 size_t test_number,
83 const char *description)
84{
85 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
86 struct kunit *test = is_test ? test_or_suite : NULL;
87
88 /*
89 * We do not log the test suite results as doing so would
90 * mean debugfs display would consist of the test suite
91 * description and status prior to individual test results.
92 * Hence directly printk the suite status, and we will
93 * separately seq_printf() the suite status for the debugfs
94 * representation.
95 */
96 if (suite)
97 pr_info("%s %zd - %s\n",
98 kunit_status_to_string(is_ok),
99 test_number, description);
100 else
101 kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
102 kunit_status_to_string(is_ok),
103 test_number, description);
104}
105
106bool kunit_suite_has_succeeded(struct kunit_suite *suite)
107{
108 const struct kunit_case *test_case;
109
110 kunit_suite_for_each_test_case(suite, test_case) {
111 if (!test_case->success)
112 return false;
113 }
114
115 return true;
116}
117EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
118
119static void kunit_print_subtest_end(struct kunit_suite *suite)
120{
121 static size_t kunit_suite_counter = 1;
122
123 kunit_print_ok_not_ok((void *)suite, false,
124 kunit_suite_has_succeeded(suite),
125 kunit_suite_counter++,
126 suite->name);
127}
128
129unsigned int kunit_test_case_num(struct kunit_suite *suite,
130 struct kunit_case *test_case)
131{
132 struct kunit_case *tc;
133 unsigned int i = 1;
134
135 kunit_suite_for_each_test_case(suite, tc) {
136 if (tc == test_case)
137 return i;
138 i++;
139 }
140
141 return 0;
142}
143EXPORT_SYMBOL_GPL(kunit_test_case_num);
144
145static void kunit_print_string_stream(struct kunit *test,
146 struct string_stream *stream)
147{
148 struct string_stream_fragment *fragment;
149 char *buf;
150
151 if (string_stream_is_empty(stream))
152 return;
153
154 buf = string_stream_get_string(stream);
155 if (!buf) {
156 kunit_err(test,
157 "Could not allocate buffer, dumping stream:\n");
158 list_for_each_entry(fragment, &stream->fragments, node) {
159 kunit_err(test, "%s", fragment->fragment);
160 }
161 kunit_err(test, "\n");
162 } else {
163 kunit_err(test, "%s", buf);
164 kunit_kfree(test, buf);
165 }
166}
167
168static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
169{
170 struct string_stream *stream;
171
172 kunit_set_failure(test);
173
174 stream = alloc_string_stream(test, GFP_KERNEL);
175 if (!stream) {
176 WARN(true,
177 "Could not allocate stream to print failed assertion in %s:%d\n",
178 assert->file,
179 assert->line);
180 return;
181 }
182
183 assert->format(assert, stream);
184
185 kunit_print_string_stream(test, stream);
186
187 WARN_ON(string_stream_destroy(stream));
188}
189
190static void __noreturn kunit_abort(struct kunit *test)
191{
192 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
193
194 /*
195 * Throw could not abort from test.
196 *
197 * XXX: we should never reach this line! As kunit_try_catch_throw is
198 * marked __noreturn.
199 */
200 WARN_ONCE(true, "Throw could not abort from test!\n");
201}
202
203void kunit_do_assertion(struct kunit *test,
204 struct kunit_assert *assert,
205 bool pass,
206 const char *fmt, ...)
207{
208 va_list args;
209
210 if (pass)
211 return;
212
213 va_start(args, fmt);
214
215 assert->message.fmt = fmt;
216 assert->message.va = &args;
217
218 kunit_fail(test, assert);
219
220 va_end(args);
221
222 if (assert->type == KUNIT_ASSERTION)
223 kunit_abort(test);
224}
225EXPORT_SYMBOL_GPL(kunit_do_assertion);
226
227void kunit_init_test(struct kunit *test, const char *name, char *log)
228{
229 spin_lock_init(&test->lock);
230 INIT_LIST_HEAD(&test->resources);
231 test->name = name;
232 test->log = log;
233 if (test->log)
234 test->log[0] = '\0';
235 test->success = true;
236}
237EXPORT_SYMBOL_GPL(kunit_init_test);
238
239/*
240 * Initializes and runs test case. Does not clean up or do post validations.
241 */
242static void kunit_run_case_internal(struct kunit *test,
243 struct kunit_suite *suite,
244 struct kunit_case *test_case)
245{
246 if (suite->init) {
247 int ret;
248
249 ret = suite->init(test);
250 if (ret) {
251 kunit_err(test, "failed to initialize: %d\n", ret);
252 kunit_set_failure(test);
253 return;
254 }
255 }
256
257 test_case->run_case(test);
258}
259
260static void kunit_case_internal_cleanup(struct kunit *test)
261{
262 kunit_cleanup(test);
263}
264
265/*
266 * Performs post validations and cleanup after a test case was run.
267 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
268 */
269static void kunit_run_case_cleanup(struct kunit *test,
270 struct kunit_suite *suite)
271{
272 if (suite->exit)
273 suite->exit(test);
274
275 kunit_case_internal_cleanup(test);
276}
277
278struct kunit_try_catch_context {
279 struct kunit *test;
280 struct kunit_suite *suite;
281 struct kunit_case *test_case;
282};
283
284static void kunit_try_run_case(void *data)
285{
286 struct kunit_try_catch_context *ctx = data;
287 struct kunit *test = ctx->test;
288 struct kunit_suite *suite = ctx->suite;
289 struct kunit_case *test_case = ctx->test_case;
290
291 /*
292 * kunit_run_case_internal may encounter a fatal error; if it does,
293 * abort will be called, this thread will exit, and finally the parent
294 * thread will resume control and handle any necessary clean up.
295 */
296 kunit_run_case_internal(test, suite, test_case);
297 /* This line may never be reached. */
298 kunit_run_case_cleanup(test, suite);
299}
300
301static void kunit_catch_run_case(void *data)
302{
303 struct kunit_try_catch_context *ctx = data;
304 struct kunit *test = ctx->test;
305 struct kunit_suite *suite = ctx->suite;
306 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
307
308 if (try_exit_code) {
309 kunit_set_failure(test);
310 /*
311 * Test case could not finish, we have no idea what state it is
312 * in, so don't do clean up.
313 */
314 if (try_exit_code == -ETIMEDOUT) {
315 kunit_err(test, "test case timed out\n");
316 /*
317 * Unknown internal error occurred preventing test case from
318 * running, so there is nothing to clean up.
319 */
320 } else {
321 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
322 try_exit_code);
323 }
324 return;
325 }
326
327 /*
328 * Test case was run, but aborted. It is the test case's business as to
329 * whether it failed or not, we just need to clean up.
330 */
331 kunit_run_case_cleanup(test, suite);
332}
333
334/*
335 * Performs all logic to run a test case. It also catches most errors that
336 * occur in a test case and reports them as failures.
337 */
338static void kunit_run_case_catch_errors(struct kunit_suite *suite,
339 struct kunit_case *test_case)
340{
341 struct kunit_try_catch_context context;
342 struct kunit_try_catch *try_catch;
343 struct kunit test;
344
345 kunit_init_test(&test, test_case->name, test_case->log);
346 try_catch = &test.try_catch;
347
348 kunit_try_catch_init(try_catch,
349 &test,
350 kunit_try_run_case,
351 kunit_catch_run_case);
352 context.test = &test;
353 context.suite = suite;
354 context.test_case = test_case;
355 kunit_try_catch_run(try_catch, &context);
356
357 test_case->success = test.success;
358
359 kunit_print_ok_not_ok(&test, true, test_case->success,
360 kunit_test_case_num(suite, test_case),
361 test_case->name);
362}
363
364int kunit_run_tests(struct kunit_suite *suite)
365{
366 struct kunit_case *test_case;
367
368 kunit_print_subtest_start(suite);
369
370 kunit_suite_for_each_test_case(suite, test_case)
371 kunit_run_case_catch_errors(suite, test_case);
372
373 kunit_print_subtest_end(suite);
374
375 return 0;
376}
377EXPORT_SYMBOL_GPL(kunit_run_tests);
378
379static void kunit_init_suite(struct kunit_suite *suite)
380{
381 kunit_debugfs_create_suite(suite);
382}
383
384int __kunit_test_suites_init(struct kunit_suite **suites)
385{
386 unsigned int i;
387
388 for (i = 0; suites[i] != NULL; i++) {
389 kunit_init_suite(suites[i]);
390 kunit_run_tests(suites[i]);
391 }
392 return 0;
393}
394EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
395
396static void kunit_exit_suite(struct kunit_suite *suite)
397{
398 kunit_debugfs_destroy_suite(suite);
399}
400
401void __kunit_test_suites_exit(struct kunit_suite **suites)
402{
403 unsigned int i;
404
405 for (i = 0; suites[i] != NULL; i++)
406 kunit_exit_suite(suites[i]);
407}
408EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
409
410/*
411 * Used for static resources and when a kunit_resource * has been created by
412 * kunit_alloc_resource(). When an init function is supplied, @data is passed
413 * into the init function; otherwise, we simply set the resource data field to
414 * the data value passed in.
415 */
416int kunit_add_resource(struct kunit *test,
417 kunit_resource_init_t init,
418 kunit_resource_free_t free,
419 struct kunit_resource *res,
420 void *data)
421{
422 int ret = 0;
423
424 res->free = free;
425 kref_init(&res->refcount);
426
427 if (init) {
428 ret = init(res, data);
429 if (ret)
430 return ret;
431 } else {
432 res->data = data;
433 }
434
435 spin_lock(&test->lock);
436 list_add_tail(&res->node, &test->resources);
437 /* refcount for list is established by kref_init() */
438 spin_unlock(&test->lock);
439
440 return ret;
441}
442EXPORT_SYMBOL_GPL(kunit_add_resource);
443
444int kunit_add_named_resource(struct kunit *test,
445 kunit_resource_init_t init,
446 kunit_resource_free_t free,
447 struct kunit_resource *res,
448 const char *name,
449 void *data)
450{
451 struct kunit_resource *existing;
452
453 if (!name)
454 return -EINVAL;
455
456 existing = kunit_find_named_resource(test, name);
457 if (existing) {
458 kunit_put_resource(existing);
459 return -EEXIST;
460 }
461
462 res->name = name;
463
464 return kunit_add_resource(test, init, free, res, data);
465}
466EXPORT_SYMBOL_GPL(kunit_add_named_resource);
467
468struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
469 kunit_resource_init_t init,
470 kunit_resource_free_t free,
471 gfp_t internal_gfp,
472 void *data)
473{
474 struct kunit_resource *res;
475 int ret;
476
477 res = kzalloc(sizeof(*res), internal_gfp);
478 if (!res)
479 return NULL;
480
481 ret = kunit_add_resource(test, init, free, res, data);
482 if (!ret) {
483 /*
484 * bump refcount for get; kunit_resource_put() should be called
485 * when done.
486 */
487 kunit_get_resource(res);
488 return res;
489 }
490 return NULL;
491}
492EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
493
494void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
495{
496 spin_lock(&test->lock);
497 list_del(&res->node);
498 spin_unlock(&test->lock);
499 kunit_put_resource(res);
500}
501EXPORT_SYMBOL_GPL(kunit_remove_resource);
502
503int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
504 void *match_data)
505{
506 struct kunit_resource *res = kunit_find_resource(test, match,
507 match_data);
508
509 if (!res)
510 return -ENOENT;
511
512 kunit_remove_resource(test, res);
513
514 /* We have a reference also via _find(); drop it. */
515 kunit_put_resource(res);
516
517 return 0;
518}
519EXPORT_SYMBOL_GPL(kunit_destroy_resource);
520
521struct kunit_kmalloc_params {
522 size_t size;
523 gfp_t gfp;
524};
525
526static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
527{
528 struct kunit_kmalloc_params *params = context;
529
530 res->data = kmalloc(params->size, params->gfp);
531 if (!res->data)
532 return -ENOMEM;
533
534 return 0;
535}
536
537static void kunit_kmalloc_free(struct kunit_resource *res)
538{
539 kfree(res->data);
540}
541
542void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
543{
544 struct kunit_kmalloc_params params = {
545 .size = size,
546 .gfp = gfp
547 };
548
549 return kunit_alloc_resource(test,
550 kunit_kmalloc_init,
551 kunit_kmalloc_free,
552 gfp,
553 ¶ms);
554}
555EXPORT_SYMBOL_GPL(kunit_kmalloc);
556
557void kunit_kfree(struct kunit *test, const void *ptr)
558{
559 struct kunit_resource *res;
560
561 res = kunit_find_resource(test, kunit_resource_instance_match,
562 (void *)ptr);
563
564 /*
565 * Removing the resource from the list of resources drops the
566 * reference count to 1; the final put will trigger the free.
567 */
568 kunit_remove_resource(test, res);
569
570 kunit_put_resource(res);
571
572}
573EXPORT_SYMBOL_GPL(kunit_kfree);
574
575void kunit_cleanup(struct kunit *test)
576{
577 struct kunit_resource *res;
578
579 /*
580 * test->resources is a stack - each allocation must be freed in the
581 * reverse order from which it was added since one resource may depend
582 * on another for its entire lifetime.
583 * Also, we cannot use the normal list_for_each constructs, even the
584 * safe ones because *arbitrary* nodes may be deleted when
585 * kunit_resource_free is called; the list_for_each_safe variants only
586 * protect against the current node being deleted, not the next.
587 */
588 while (true) {
589 spin_lock(&test->lock);
590 if (list_empty(&test->resources)) {
591 spin_unlock(&test->lock);
592 break;
593 }
594 res = list_last_entry(&test->resources,
595 struct kunit_resource,
596 node);
597 /*
598 * Need to unlock here as a resource may remove another
599 * resource, and this can't happen if the test->lock
600 * is held.
601 */
602 spin_unlock(&test->lock);
603 kunit_remove_resource(test, res);
604 }
605}
606EXPORT_SYMBOL_GPL(kunit_cleanup);
607
608static int __init kunit_init(void)
609{
610 kunit_debugfs_init();
611
612 return 0;
613}
614late_initcall(kunit_init);
615
616static void __exit kunit_exit(void)
617{
618 kunit_debugfs_cleanup();
619}
620module_exit(kunit_exit);
621
622MODULE_LICENSE("GPL v2");
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/resource.h>
10#include <kunit/test.h>
11#include <kunit/test-bug.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/panic.h>
16#include <linux/sched/debug.h>
17#include <linux/sched.h>
18
19#include "debugfs.h"
20#include "string-stream.h"
21#include "try-catch-impl.h"
22
23DEFINE_STATIC_KEY_FALSE(kunit_running);
24EXPORT_SYMBOL_GPL(kunit_running);
25
26#if IS_BUILTIN(CONFIG_KUNIT)
27/*
28 * Fail the current test and print an error message to the log.
29 */
30void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
31{
32 va_list args;
33 int len;
34 char *buffer;
35
36 if (!current->kunit_test)
37 return;
38
39 kunit_set_failure(current->kunit_test);
40
41 /* kunit_err() only accepts literals, so evaluate the args first. */
42 va_start(args, fmt);
43 len = vsnprintf(NULL, 0, fmt, args) + 1;
44 va_end(args);
45
46 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
47 if (!buffer)
48 return;
49
50 va_start(args, fmt);
51 vsnprintf(buffer, len, fmt, args);
52 va_end(args);
53
54 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
55 kunit_kfree(current->kunit_test, buffer);
56}
57EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
58#endif
59
60/*
61 * Enable KUnit tests to run.
62 */
63#ifdef CONFIG_KUNIT_DEFAULT_ENABLED
64static bool enable_param = true;
65#else
66static bool enable_param;
67#endif
68module_param_named(enable, enable_param, bool, 0);
69MODULE_PARM_DESC(enable, "Enable KUnit tests");
70
71/*
72 * KUnit statistic mode:
73 * 0 - disabled
74 * 1 - only when there is more than one subtest
75 * 2 - enabled
76 */
77static int kunit_stats_enabled = 1;
78module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
79MODULE_PARM_DESC(stats_enabled,
80 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
81
82struct kunit_result_stats {
83 unsigned long passed;
84 unsigned long skipped;
85 unsigned long failed;
86 unsigned long total;
87};
88
89static bool kunit_should_print_stats(struct kunit_result_stats stats)
90{
91 if (kunit_stats_enabled == 0)
92 return false;
93
94 if (kunit_stats_enabled == 2)
95 return true;
96
97 return (stats.total > 1);
98}
99
100static void kunit_print_test_stats(struct kunit *test,
101 struct kunit_result_stats stats)
102{
103 if (!kunit_should_print_stats(stats))
104 return;
105
106 kunit_log(KERN_INFO, test,
107 KUNIT_SUBTEST_INDENT
108 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
109 test->name,
110 stats.passed,
111 stats.failed,
112 stats.skipped,
113 stats.total);
114}
115
116/*
117 * Append formatted message to log, size of which is limited to
118 * KUNIT_LOG_SIZE bytes (including null terminating byte).
119 */
120void kunit_log_append(char *log, const char *fmt, ...)
121{
122 char line[KUNIT_LOG_SIZE];
123 va_list args;
124 int len_left;
125
126 if (!log)
127 return;
128
129 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
130 if (len_left <= 0)
131 return;
132
133 va_start(args, fmt);
134 vsnprintf(line, sizeof(line), fmt, args);
135 va_end(args);
136
137 strncat(log, line, len_left);
138}
139EXPORT_SYMBOL_GPL(kunit_log_append);
140
141size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
142{
143 struct kunit_case *test_case;
144 size_t len = 0;
145
146 kunit_suite_for_each_test_case(suite, test_case)
147 len++;
148
149 return len;
150}
151EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
152
153static void kunit_print_suite_start(struct kunit_suite *suite)
154{
155 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
156 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
157 suite->name);
158 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
159 kunit_suite_num_test_cases(suite));
160}
161
162static void kunit_print_ok_not_ok(void *test_or_suite,
163 bool is_test,
164 enum kunit_status status,
165 size_t test_number,
166 const char *description,
167 const char *directive)
168{
169 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
170 struct kunit *test = is_test ? test_or_suite : NULL;
171 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
172
173 /*
174 * We do not log the test suite results as doing so would
175 * mean debugfs display would consist of the test suite
176 * description and status prior to individual test results.
177 * Hence directly printk the suite status, and we will
178 * separately seq_printf() the suite status for the debugfs
179 * representation.
180 */
181 if (suite)
182 pr_info("%s %zd %s%s%s\n",
183 kunit_status_to_ok_not_ok(status),
184 test_number, description, directive_header,
185 (status == KUNIT_SKIPPED) ? directive : "");
186 else
187 kunit_log(KERN_INFO, test,
188 KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
189 kunit_status_to_ok_not_ok(status),
190 test_number, description, directive_header,
191 (status == KUNIT_SKIPPED) ? directive : "");
192}
193
194enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
195{
196 const struct kunit_case *test_case;
197 enum kunit_status status = KUNIT_SKIPPED;
198
199 if (suite->suite_init_err)
200 return KUNIT_FAILURE;
201
202 kunit_suite_for_each_test_case(suite, test_case) {
203 if (test_case->status == KUNIT_FAILURE)
204 return KUNIT_FAILURE;
205 else if (test_case->status == KUNIT_SUCCESS)
206 status = KUNIT_SUCCESS;
207 }
208
209 return status;
210}
211EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
212
213static size_t kunit_suite_counter = 1;
214
215static void kunit_print_suite_end(struct kunit_suite *suite)
216{
217 kunit_print_ok_not_ok((void *)suite, false,
218 kunit_suite_has_succeeded(suite),
219 kunit_suite_counter++,
220 suite->name,
221 suite->status_comment);
222}
223
224unsigned int kunit_test_case_num(struct kunit_suite *suite,
225 struct kunit_case *test_case)
226{
227 struct kunit_case *tc;
228 unsigned int i = 1;
229
230 kunit_suite_for_each_test_case(suite, tc) {
231 if (tc == test_case)
232 return i;
233 i++;
234 }
235
236 return 0;
237}
238EXPORT_SYMBOL_GPL(kunit_test_case_num);
239
240static void kunit_print_string_stream(struct kunit *test,
241 struct string_stream *stream)
242{
243 struct string_stream_fragment *fragment;
244 char *buf;
245
246 if (string_stream_is_empty(stream))
247 return;
248
249 buf = string_stream_get_string(stream);
250 if (!buf) {
251 kunit_err(test,
252 "Could not allocate buffer, dumping stream:\n");
253 list_for_each_entry(fragment, &stream->fragments, node) {
254 kunit_err(test, "%s", fragment->fragment);
255 }
256 kunit_err(test, "\n");
257 } else {
258 kunit_err(test, "%s", buf);
259 kunit_kfree(test, buf);
260 }
261}
262
263static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
264 enum kunit_assert_type type, const struct kunit_assert *assert,
265 assert_format_t assert_format, const struct va_format *message)
266{
267 struct string_stream *stream;
268
269 kunit_set_failure(test);
270
271 stream = alloc_string_stream(test, GFP_KERNEL);
272 if (IS_ERR(stream)) {
273 WARN(true,
274 "Could not allocate stream to print failed assertion in %s:%d\n",
275 loc->file,
276 loc->line);
277 return;
278 }
279
280 kunit_assert_prologue(loc, type, stream);
281 assert_format(assert, message, stream);
282
283 kunit_print_string_stream(test, stream);
284
285 string_stream_destroy(stream);
286}
287
288static void __noreturn kunit_abort(struct kunit *test)
289{
290 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
291
292 /*
293 * Throw could not abort from test.
294 *
295 * XXX: we should never reach this line! As kunit_try_catch_throw is
296 * marked __noreturn.
297 */
298 WARN_ONCE(true, "Throw could not abort from test!\n");
299}
300
301void kunit_do_failed_assertion(struct kunit *test,
302 const struct kunit_loc *loc,
303 enum kunit_assert_type type,
304 const struct kunit_assert *assert,
305 assert_format_t assert_format,
306 const char *fmt, ...)
307{
308 va_list args;
309 struct va_format message;
310 va_start(args, fmt);
311
312 message.fmt = fmt;
313 message.va = &args;
314
315 kunit_fail(test, loc, type, assert, assert_format, &message);
316
317 va_end(args);
318
319 if (type == KUNIT_ASSERTION)
320 kunit_abort(test);
321}
322EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
323
324void kunit_init_test(struct kunit *test, const char *name, char *log)
325{
326 spin_lock_init(&test->lock);
327 INIT_LIST_HEAD(&test->resources);
328 test->name = name;
329 test->log = log;
330 if (test->log)
331 test->log[0] = '\0';
332 test->status = KUNIT_SUCCESS;
333 test->status_comment[0] = '\0';
334}
335EXPORT_SYMBOL_GPL(kunit_init_test);
336
337/*
338 * Initializes and runs test case. Does not clean up or do post validations.
339 */
340static void kunit_run_case_internal(struct kunit *test,
341 struct kunit_suite *suite,
342 struct kunit_case *test_case)
343{
344 if (suite->init) {
345 int ret;
346
347 ret = suite->init(test);
348 if (ret) {
349 kunit_err(test, "failed to initialize: %d\n", ret);
350 kunit_set_failure(test);
351 return;
352 }
353 }
354
355 test_case->run_case(test);
356}
357
358static void kunit_case_internal_cleanup(struct kunit *test)
359{
360 kunit_cleanup(test);
361}
362
363/*
364 * Performs post validations and cleanup after a test case was run.
365 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
366 */
367static void kunit_run_case_cleanup(struct kunit *test,
368 struct kunit_suite *suite)
369{
370 if (suite->exit)
371 suite->exit(test);
372
373 kunit_case_internal_cleanup(test);
374}
375
376struct kunit_try_catch_context {
377 struct kunit *test;
378 struct kunit_suite *suite;
379 struct kunit_case *test_case;
380};
381
382static void kunit_try_run_case(void *data)
383{
384 struct kunit_try_catch_context *ctx = data;
385 struct kunit *test = ctx->test;
386 struct kunit_suite *suite = ctx->suite;
387 struct kunit_case *test_case = ctx->test_case;
388
389 current->kunit_test = test;
390
391 /*
392 * kunit_run_case_internal may encounter a fatal error; if it does,
393 * abort will be called, this thread will exit, and finally the parent
394 * thread will resume control and handle any necessary clean up.
395 */
396 kunit_run_case_internal(test, suite, test_case);
397 /* This line may never be reached. */
398 kunit_run_case_cleanup(test, suite);
399}
400
401static void kunit_catch_run_case(void *data)
402{
403 struct kunit_try_catch_context *ctx = data;
404 struct kunit *test = ctx->test;
405 struct kunit_suite *suite = ctx->suite;
406 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
407
408 if (try_exit_code) {
409 kunit_set_failure(test);
410 /*
411 * Test case could not finish, we have no idea what state it is
412 * in, so don't do clean up.
413 */
414 if (try_exit_code == -ETIMEDOUT) {
415 kunit_err(test, "test case timed out\n");
416 /*
417 * Unknown internal error occurred preventing test case from
418 * running, so there is nothing to clean up.
419 */
420 } else {
421 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
422 try_exit_code);
423 }
424 return;
425 }
426
427 /*
428 * Test case was run, but aborted. It is the test case's business as to
429 * whether it failed or not, we just need to clean up.
430 */
431 kunit_run_case_cleanup(test, suite);
432}
433
434/*
435 * Performs all logic to run a test case. It also catches most errors that
436 * occur in a test case and reports them as failures.
437 */
438static void kunit_run_case_catch_errors(struct kunit_suite *suite,
439 struct kunit_case *test_case,
440 struct kunit *test)
441{
442 struct kunit_try_catch_context context;
443 struct kunit_try_catch *try_catch;
444
445 kunit_init_test(test, test_case->name, test_case->log);
446 try_catch = &test->try_catch;
447
448 kunit_try_catch_init(try_catch,
449 test,
450 kunit_try_run_case,
451 kunit_catch_run_case);
452 context.test = test;
453 context.suite = suite;
454 context.test_case = test_case;
455 kunit_try_catch_run(try_catch, &context);
456
457 /* Propagate the parameter result to the test case. */
458 if (test->status == KUNIT_FAILURE)
459 test_case->status = KUNIT_FAILURE;
460 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
461 test_case->status = KUNIT_SUCCESS;
462}
463
464static void kunit_print_suite_stats(struct kunit_suite *suite,
465 struct kunit_result_stats suite_stats,
466 struct kunit_result_stats param_stats)
467{
468 if (kunit_should_print_stats(suite_stats)) {
469 kunit_log(KERN_INFO, suite,
470 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
471 suite->name,
472 suite_stats.passed,
473 suite_stats.failed,
474 suite_stats.skipped,
475 suite_stats.total);
476 }
477
478 if (kunit_should_print_stats(param_stats)) {
479 kunit_log(KERN_INFO, suite,
480 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
481 param_stats.passed,
482 param_stats.failed,
483 param_stats.skipped,
484 param_stats.total);
485 }
486}
487
488static void kunit_update_stats(struct kunit_result_stats *stats,
489 enum kunit_status status)
490{
491 switch (status) {
492 case KUNIT_SUCCESS:
493 stats->passed++;
494 break;
495 case KUNIT_SKIPPED:
496 stats->skipped++;
497 break;
498 case KUNIT_FAILURE:
499 stats->failed++;
500 break;
501 }
502
503 stats->total++;
504}
505
506static void kunit_accumulate_stats(struct kunit_result_stats *total,
507 struct kunit_result_stats add)
508{
509 total->passed += add.passed;
510 total->skipped += add.skipped;
511 total->failed += add.failed;
512 total->total += add.total;
513}
514
515int kunit_run_tests(struct kunit_suite *suite)
516{
517 char param_desc[KUNIT_PARAM_DESC_SIZE];
518 struct kunit_case *test_case;
519 struct kunit_result_stats suite_stats = { 0 };
520 struct kunit_result_stats total_stats = { 0 };
521
522 /* Taint the kernel so we know we've run tests. */
523 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
524
525 if (suite->suite_init) {
526 suite->suite_init_err = suite->suite_init(suite);
527 if (suite->suite_init_err) {
528 kunit_err(suite, KUNIT_SUBTEST_INDENT
529 "# failed to initialize (%d)", suite->suite_init_err);
530 goto suite_end;
531 }
532 }
533
534 kunit_print_suite_start(suite);
535
536 kunit_suite_for_each_test_case(suite, test_case) {
537 struct kunit test = { .param_value = NULL, .param_index = 0 };
538 struct kunit_result_stats param_stats = { 0 };
539 test_case->status = KUNIT_SKIPPED;
540
541 if (!test_case->generate_params) {
542 /* Non-parameterised test. */
543 kunit_run_case_catch_errors(suite, test_case, &test);
544 kunit_update_stats(¶m_stats, test.status);
545 } else {
546 /* Get initial param. */
547 param_desc[0] = '\0';
548 test.param_value = test_case->generate_params(NULL, param_desc);
549 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
550 "KTAP version 1\n");
551 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
552 "# Subtest: %s", test_case->name);
553
554 while (test.param_value) {
555 kunit_run_case_catch_errors(suite, test_case, &test);
556
557 if (param_desc[0] == '\0') {
558 snprintf(param_desc, sizeof(param_desc),
559 "param-%d", test.param_index);
560 }
561
562 kunit_log(KERN_INFO, &test,
563 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
564 "%s %d %s",
565 kunit_status_to_ok_not_ok(test.status),
566 test.param_index + 1, param_desc);
567
568 /* Get next param. */
569 param_desc[0] = '\0';
570 test.param_value = test_case->generate_params(test.param_value, param_desc);
571 test.param_index++;
572
573 kunit_update_stats(¶m_stats, test.status);
574 }
575 }
576
577
578 kunit_print_test_stats(&test, param_stats);
579
580 kunit_print_ok_not_ok(&test, true, test_case->status,
581 kunit_test_case_num(suite, test_case),
582 test_case->name,
583 test.status_comment);
584
585 kunit_update_stats(&suite_stats, test_case->status);
586 kunit_accumulate_stats(&total_stats, param_stats);
587 }
588
589 if (suite->suite_exit)
590 suite->suite_exit(suite);
591
592 kunit_print_suite_stats(suite, suite_stats, total_stats);
593suite_end:
594 kunit_print_suite_end(suite);
595
596 return 0;
597}
598EXPORT_SYMBOL_GPL(kunit_run_tests);
599
600static void kunit_init_suite(struct kunit_suite *suite)
601{
602 kunit_debugfs_create_suite(suite);
603 suite->status_comment[0] = '\0';
604 suite->suite_init_err = 0;
605}
606
607bool kunit_enabled(void)
608{
609 return enable_param;
610}
611
612int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
613{
614 unsigned int i;
615
616 if (!kunit_enabled() && num_suites > 0) {
617 pr_info("kunit: disabled\n");
618 return 0;
619 }
620
621 static_branch_inc(&kunit_running);
622
623 for (i = 0; i < num_suites; i++) {
624 kunit_init_suite(suites[i]);
625 kunit_run_tests(suites[i]);
626 }
627
628 static_branch_dec(&kunit_running);
629 return 0;
630}
631EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
632
633static void kunit_exit_suite(struct kunit_suite *suite)
634{
635 kunit_debugfs_destroy_suite(suite);
636}
637
638void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
639{
640 unsigned int i;
641
642 if (!kunit_enabled())
643 return;
644
645 for (i = 0; i < num_suites; i++)
646 kunit_exit_suite(suites[i]);
647
648 kunit_suite_counter = 1;
649}
650EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
651
652#ifdef CONFIG_MODULES
653static void kunit_module_init(struct module *mod)
654{
655 __kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites);
656}
657
658static void kunit_module_exit(struct module *mod)
659{
660 __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites);
661}
662
663static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
664 void *data)
665{
666 struct module *mod = data;
667
668 switch (val) {
669 case MODULE_STATE_LIVE:
670 kunit_module_init(mod);
671 break;
672 case MODULE_STATE_GOING:
673 kunit_module_exit(mod);
674 break;
675 case MODULE_STATE_COMING:
676 case MODULE_STATE_UNFORMED:
677 break;
678 }
679
680 return 0;
681}
682
683static struct notifier_block kunit_mod_nb = {
684 .notifier_call = kunit_module_notify,
685 .priority = 0,
686};
687#endif
688
689struct kunit_kmalloc_array_params {
690 size_t n;
691 size_t size;
692 gfp_t gfp;
693};
694
695static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
696{
697 struct kunit_kmalloc_array_params *params = context;
698
699 res->data = kmalloc_array(params->n, params->size, params->gfp);
700 if (!res->data)
701 return -ENOMEM;
702
703 return 0;
704}
705
706static void kunit_kmalloc_array_free(struct kunit_resource *res)
707{
708 kfree(res->data);
709}
710
711void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
712{
713 struct kunit_kmalloc_array_params params = {
714 .size = size,
715 .n = n,
716 .gfp = gfp
717 };
718
719 return kunit_alloc_resource(test,
720 kunit_kmalloc_array_init,
721 kunit_kmalloc_array_free,
722 gfp,
723 ¶ms);
724}
725EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
726
727static inline bool kunit_kfree_match(struct kunit *test,
728 struct kunit_resource *res, void *match_data)
729{
730 /* Only match resources allocated with kunit_kmalloc() and friends. */
731 return res->free == kunit_kmalloc_array_free && res->data == match_data;
732}
733
734void kunit_kfree(struct kunit *test, const void *ptr)
735{
736 if (!ptr)
737 return;
738
739 if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr))
740 KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
741}
742EXPORT_SYMBOL_GPL(kunit_kfree);
743
744void kunit_cleanup(struct kunit *test)
745{
746 struct kunit_resource *res;
747 unsigned long flags;
748
749 /*
750 * test->resources is a stack - each allocation must be freed in the
751 * reverse order from which it was added since one resource may depend
752 * on another for its entire lifetime.
753 * Also, we cannot use the normal list_for_each constructs, even the
754 * safe ones because *arbitrary* nodes may be deleted when
755 * kunit_resource_free is called; the list_for_each_safe variants only
756 * protect against the current node being deleted, not the next.
757 */
758 while (true) {
759 spin_lock_irqsave(&test->lock, flags);
760 if (list_empty(&test->resources)) {
761 spin_unlock_irqrestore(&test->lock, flags);
762 break;
763 }
764 res = list_last_entry(&test->resources,
765 struct kunit_resource,
766 node);
767 /*
768 * Need to unlock here as a resource may remove another
769 * resource, and this can't happen if the test->lock
770 * is held.
771 */
772 spin_unlock_irqrestore(&test->lock, flags);
773 kunit_remove_resource(test, res);
774 }
775 current->kunit_test = NULL;
776}
777EXPORT_SYMBOL_GPL(kunit_cleanup);
778
779static int __init kunit_init(void)
780{
781 kunit_debugfs_init();
782#ifdef CONFIG_MODULES
783 return register_module_notifier(&kunit_mod_nb);
784#else
785 return 0;
786#endif
787}
788late_initcall(kunit_init);
789
790static void __exit kunit_exit(void)
791{
792#ifdef CONFIG_MODULES
793 unregister_module_notifier(&kunit_mod_nb);
794#endif
795 kunit_debugfs_cleanup();
796}
797module_exit(kunit_exit);
798
799MODULE_LICENSE("GPL v2");