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