Merge tag 'sysctl-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof...
[linux-block.git] / lib / kunit / kunit-test.c
CommitLineData
e4eb117f
BH
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * KUnit test for core test infrastructure.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8#include <kunit/test.h>
a42077b7 9#include <kunit/test-bug.h>
e4eb117f 10
9bbb11c6
AM
11#include "try-catch-impl.h"
12
e4eb117f
BH
13struct kunit_try_catch_test_context {
14 struct kunit_try_catch *try_catch;
15 bool function_called;
16};
17
18static void kunit_test_successful_try(void *data)
19{
20 struct kunit *test = data;
21 struct kunit_try_catch_test_context *ctx = test->priv;
22
23 ctx->function_called = true;
24}
25
26static void kunit_test_no_catch(void *data)
27{
28 struct kunit *test = data;
29
30 KUNIT_FAIL(test, "Catch should not be called\n");
31}
32
33static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
34{
35 struct kunit_try_catch_test_context *ctx = test->priv;
36 struct kunit_try_catch *try_catch = ctx->try_catch;
37
38 kunit_try_catch_init(try_catch,
39 test,
40 kunit_test_successful_try,
41 kunit_test_no_catch);
42 kunit_try_catch_run(try_catch, test);
43
44 KUNIT_EXPECT_TRUE(test, ctx->function_called);
45}
46
47static void kunit_test_unsuccessful_try(void *data)
48{
49 struct kunit *test = data;
50 struct kunit_try_catch_test_context *ctx = test->priv;
51 struct kunit_try_catch *try_catch = ctx->try_catch;
52
53 kunit_try_catch_throw(try_catch);
54 KUNIT_FAIL(test, "This line should never be reached\n");
55}
56
57static void kunit_test_catch(void *data)
58{
59 struct kunit *test = data;
60 struct kunit_try_catch_test_context *ctx = test->priv;
61
62 ctx->function_called = true;
63}
64
65static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
66{
67 struct kunit_try_catch_test_context *ctx = test->priv;
68 struct kunit_try_catch *try_catch = ctx->try_catch;
69
70 kunit_try_catch_init(try_catch,
71 test,
72 kunit_test_unsuccessful_try,
73 kunit_test_catch);
74 kunit_try_catch_run(try_catch, test);
75
76 KUNIT_EXPECT_TRUE(test, ctx->function_called);
77}
78
79static int kunit_try_catch_test_init(struct kunit *test)
80{
81 struct kunit_try_catch_test_context *ctx;
82
83 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
e4aea8f8 84 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
e4eb117f
BH
85 test->priv = ctx;
86
87 ctx->try_catch = kunit_kmalloc(test,
88 sizeof(*ctx->try_catch),
89 GFP_KERNEL);
e4aea8f8 90 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
e4eb117f
BH
91
92 return 0;
93}
94
95static struct kunit_case kunit_try_catch_test_cases[] = {
96 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
97 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
98 {}
99};
100
101static struct kunit_suite kunit_try_catch_test_suite = {
102 .name = "kunit-try-catch-test",
103 .init = kunit_try_catch_test_init,
104 .test_cases = kunit_try_catch_test_cases,
105};
73ba5aaf
AK
106
107/*
108 * Context for testing test managed resources
109 * is_resource_initialized is used to test arbitrary resources
110 */
111struct kunit_test_resource_context {
112 struct kunit test;
113 bool is_resource_initialized;
114 int allocate_order[2];
115 int free_order[2];
116};
117
118static int fake_resource_init(struct kunit_resource *res, void *context)
119{
120 struct kunit_test_resource_context *ctx = context;
121
d4cdd146 122 res->data = &ctx->is_resource_initialized;
73ba5aaf
AK
123 ctx->is_resource_initialized = true;
124 return 0;
125}
126
127static void fake_resource_free(struct kunit_resource *res)
128{
d4cdd146 129 bool *is_resource_initialized = res->data;
73ba5aaf
AK
130
131 *is_resource_initialized = false;
132}
133
134static void kunit_resource_test_init_resources(struct kunit *test)
135{
136 struct kunit_test_resource_context *ctx = test->priv;
137
e2219db2 138 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
73ba5aaf
AK
139
140 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
141}
142
143static void kunit_resource_test_alloc_resource(struct kunit *test)
144{
145 struct kunit_test_resource_context *ctx = test->priv;
146 struct kunit_resource *res;
147 kunit_resource_free_t free = fake_resource_free;
148
149 res = kunit_alloc_and_get_resource(&ctx->test,
150 fake_resource_init,
151 fake_resource_free,
152 GFP_KERNEL,
153 ctx);
154
155 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
156 KUNIT_EXPECT_PTR_EQ(test,
157 &ctx->is_resource_initialized,
d4cdd146 158 (bool *)res->data);
73ba5aaf
AK
159 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
160 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
d4cdd146
AM
161
162 kunit_put_resource(res);
73ba5aaf
AK
163}
164
047a8a0a
DL
165static inline bool kunit_resource_instance_match(struct kunit *test,
166 struct kunit_resource *res,
167 void *match_data)
168{
169 return res->data == match_data;
170}
171
d4cdd146
AM
172/*
173 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
174 * they have a reference to the associated resource that they must release
175 * via kunit_put_resource(). In normal operation, users will only
176 * have to do this for cases where they use kunit_find_resource(), and the
177 * kunit_alloc_resource() function will be used (which does not take a
178 * resource reference).
179 */
73ba5aaf
AK
180static void kunit_resource_test_destroy_resource(struct kunit *test)
181{
182 struct kunit_test_resource_context *ctx = test->priv;
183 struct kunit_resource *res = kunit_alloc_and_get_resource(
184 &ctx->test,
185 fake_resource_init,
186 fake_resource_free,
187 GFP_KERNEL,
188 ctx);
189
d4cdd146
AM
190 kunit_put_resource(res);
191
73ba5aaf 192 KUNIT_ASSERT_FALSE(test,
d4cdd146 193 kunit_destroy_resource(&ctx->test,
73ba5aaf 194 kunit_resource_instance_match,
d4cdd146 195 res->data));
73ba5aaf
AK
196
197 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
198 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
199}
200
59729170
DG
201static void kunit_resource_test_remove_resource(struct kunit *test)
202{
203 struct kunit_test_resource_context *ctx = test->priv;
204 struct kunit_resource *res = kunit_alloc_and_get_resource(
205 &ctx->test,
206 fake_resource_init,
207 fake_resource_free,
208 GFP_KERNEL,
209 ctx);
210
211 /* The resource is in the list */
212 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
213
214 /* Remove the resource. The pointer is still valid, but it can't be
215 * found.
216 */
217 kunit_remove_resource(test, res);
218 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
219 /* We haven't been freed yet. */
220 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
221
222 /* Removing the resource multiple times is valid. */
223 kunit_remove_resource(test, res);
224 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
225 /* Despite having been removed twice (from only one reference), the
226 * resource still has not been freed.
227 */
228 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
229
230 /* Free the resource. */
231 kunit_put_resource(res);
232 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
233}
234
73ba5aaf
AK
235static void kunit_resource_test_cleanup_resources(struct kunit *test)
236{
237 int i;
238 struct kunit_test_resource_context *ctx = test->priv;
239 struct kunit_resource *resources[5];
240
241 for (i = 0; i < ARRAY_SIZE(resources); i++) {
242 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
243 fake_resource_init,
244 fake_resource_free,
245 GFP_KERNEL,
246 ctx);
d4cdd146 247 kunit_put_resource(resources[i]);
73ba5aaf
AK
248 }
249
250 kunit_cleanup(&ctx->test);
251
252 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
253}
254
255static void kunit_resource_test_mark_order(int order_array[],
256 size_t order_size,
257 int key)
258{
259 int i;
260
261 for (i = 0; i < order_size && order_array[i]; i++)
262 ;
263
264 order_array[i] = key;
265}
266
267#define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
268 kunit_resource_test_mark_order(ctx->order_field, \
269 ARRAY_SIZE(ctx->order_field), \
270 key)
271
272static int fake_resource_2_init(struct kunit_resource *res, void *context)
273{
274 struct kunit_test_resource_context *ctx = context;
275
276 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
277
d4cdd146 278 res->data = ctx;
73ba5aaf
AK
279
280 return 0;
281}
282
283static void fake_resource_2_free(struct kunit_resource *res)
284{
d4cdd146 285 struct kunit_test_resource_context *ctx = res->data;
73ba5aaf
AK
286
287 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
288}
289
290static int fake_resource_1_init(struct kunit_resource *res, void *context)
291{
292 struct kunit_test_resource_context *ctx = context;
d4cdd146 293 struct kunit_resource *res2;
73ba5aaf 294
d4cdd146
AM
295 res2 = kunit_alloc_and_get_resource(&ctx->test,
296 fake_resource_2_init,
297 fake_resource_2_free,
298 GFP_KERNEL,
299 ctx);
73ba5aaf
AK
300
301 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
302
d4cdd146
AM
303 res->data = ctx;
304
305 kunit_put_resource(res2);
73ba5aaf
AK
306
307 return 0;
308}
309
310static void fake_resource_1_free(struct kunit_resource *res)
311{
d4cdd146 312 struct kunit_test_resource_context *ctx = res->data;
73ba5aaf
AK
313
314 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
315}
316
317/*
318 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
319 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
320 * to assert allocation and freeing order when the feature becomes available.
321 */
322static void kunit_resource_test_proper_free_ordering(struct kunit *test)
323{
324 struct kunit_test_resource_context *ctx = test->priv;
d4cdd146 325 struct kunit_resource *res;
73ba5aaf
AK
326
327 /* fake_resource_1 allocates a fake_resource_2 in its init. */
d4cdd146
AM
328 res = kunit_alloc_and_get_resource(&ctx->test,
329 fake_resource_1_init,
330 fake_resource_1_free,
331 GFP_KERNEL,
332 ctx);
73ba5aaf
AK
333
334 /*
335 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
336 * before returning to fake_resource_1_init, it should be the first to
337 * put its key in the allocate_order array.
338 */
339 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
340 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
341
d4cdd146
AM
342 kunit_put_resource(res);
343
73ba5aaf
AK
344 kunit_cleanup(&ctx->test);
345
346 /*
347 * Because fake_resource_2 finishes allocation before fake_resource_1,
348 * fake_resource_1 should be freed first since it could depend on
349 * fake_resource_2.
350 */
351 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
352 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
353}
354
d4cdd146
AM
355static void kunit_resource_test_static(struct kunit *test)
356{
357 struct kunit_test_resource_context ctx;
358 struct kunit_resource res;
359
360 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
361 0);
362
363 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
364
365 kunit_cleanup(test);
366
367 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
368}
369
725aca95
AM
370static void kunit_resource_test_named(struct kunit *test)
371{
372 struct kunit_resource res1, res2, *found = NULL;
373 struct kunit_test_resource_context ctx;
374
375 KUNIT_EXPECT_EQ(test,
376 kunit_add_named_resource(test, NULL, NULL, &res1,
377 "resource_1", &ctx),
378 0);
379 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
380
381 KUNIT_EXPECT_EQ(test,
382 kunit_add_named_resource(test, NULL, NULL, &res1,
383 "resource_1", &ctx),
384 -EEXIST);
385
386 KUNIT_EXPECT_EQ(test,
387 kunit_add_named_resource(test, NULL, NULL, &res2,
388 "resource_2", &ctx),
389 0);
390
391 found = kunit_find_named_resource(test, "resource_1");
392
393 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
394
395 if (found)
396 kunit_put_resource(&res1);
397
398 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
399 0);
400
401 kunit_cleanup(test);
402
403 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
404}
405
73ba5aaf
AK
406static int kunit_resource_test_init(struct kunit *test)
407{
408 struct kunit_test_resource_context *ctx =
409 kzalloc(sizeof(*ctx), GFP_KERNEL);
410
411 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
412
413 test->priv = ctx;
414
e2219db2 415 kunit_init_test(&ctx->test, "test_test_context", NULL);
73ba5aaf
AK
416
417 return 0;
418}
419
420static void kunit_resource_test_exit(struct kunit *test)
421{
422 struct kunit_test_resource_context *ctx = test->priv;
423
424 kunit_cleanup(&ctx->test);
425 kfree(ctx);
426}
427
428static struct kunit_case kunit_resource_test_cases[] = {
429 KUNIT_CASE(kunit_resource_test_init_resources),
430 KUNIT_CASE(kunit_resource_test_alloc_resource),
431 KUNIT_CASE(kunit_resource_test_destroy_resource),
59729170 432 KUNIT_CASE(kunit_resource_test_remove_resource),
73ba5aaf
AK
433 KUNIT_CASE(kunit_resource_test_cleanup_resources),
434 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
d4cdd146 435 KUNIT_CASE(kunit_resource_test_static),
725aca95 436 KUNIT_CASE(kunit_resource_test_named),
73ba5aaf
AK
437 {}
438};
439
440static struct kunit_suite kunit_resource_test_suite = {
441 .name = "kunit-resource-test",
442 .init = kunit_resource_test_init,
443 .exit = kunit_resource_test_exit,
444 .test_cases = kunit_resource_test_cases,
445};
eda8e324 446
eda8e324
AM
447static void kunit_log_test(struct kunit *test)
448{
b7cbaef3
DL
449 struct kunit_suite suite;
450
451 suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
452 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
eda8e324
AM
453
454 kunit_log(KERN_INFO, test, "put this in log.");
455 kunit_log(KERN_INFO, test, "this too.");
b7cbaef3
DL
456 kunit_log(KERN_INFO, &suite, "add to suite log.");
457 kunit_log(KERN_INFO, &suite, "along with this.");
eda8e324
AM
458
459#ifdef CONFIG_KUNIT_DEBUGFS
460 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
461 strstr(test->log, "put this in log."));
462 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
463 strstr(test->log, "this too."));
464 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
b7cbaef3 465 strstr(suite.log, "add to suite log."));
eda8e324 466 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
b7cbaef3 467 strstr(suite.log, "along with this."));
eda8e324 468#else
de82c15d 469 KUNIT_EXPECT_NULL(test, test->log);
eda8e324
AM
470#endif
471}
472
2c6a96da
RM
473static void kunit_log_newline_test(struct kunit *test)
474{
475 kunit_info(test, "Add newline\n");
476 if (test->log) {
477 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"),
478 "Missing log line, full log:\n%s", test->log);
479 KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n"));
480 } else {
481 kunit_skip(test, "only useful when debugfs is enabled");
482 }
483}
484
485static struct kunit_case kunit_log_test_cases[] = {
486 KUNIT_CASE(kunit_log_test),
487 KUNIT_CASE(kunit_log_newline_test),
488 {}
489};
490
491static struct kunit_suite kunit_log_test_suite = {
492 .name = "kunit-log-test",
493 .test_cases = kunit_log_test_cases,
494};
495
6d2426b2
DG
496static void kunit_status_set_failure_test(struct kunit *test)
497{
498 struct kunit fake;
499
500 kunit_init_test(&fake, "fake test", NULL);
501
502 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
503 kunit_set_failure(&fake);
504 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
505}
506
507static void kunit_status_mark_skipped_test(struct kunit *test)
508{
509 struct kunit fake;
510
511 kunit_init_test(&fake, "fake test", NULL);
512
513 /* Before: Should be SUCCESS with no comment. */
514 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
515 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
516
517 /* Mark the test as skipped. */
518 kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
519
520 /* After: Should be SKIPPED with our comment. */
521 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
522 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
523}
524
525static struct kunit_case kunit_status_test_cases[] = {
526 KUNIT_CASE(kunit_status_set_failure_test),
527 KUNIT_CASE(kunit_status_mark_skipped_test),
528 {}
529};
530
531static struct kunit_suite kunit_status_test_suite = {
532 .name = "kunit_status",
533 .test_cases = kunit_status_test_cases,
534};
535
a42077b7
RM
536static void kunit_current_test(struct kunit *test)
537{
538 /* Check results of both current->kunit_test and
539 * kunit_get_current_test() are equivalent to current test.
540 */
541 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
542 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
543}
544
545static void kunit_current_fail_test(struct kunit *test)
546{
547 struct kunit fake;
548
549 kunit_init_test(&fake, "fake test", NULL);
550 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
551
552 /* Set current->kunit_test to fake test. */
553 current->kunit_test = &fake;
554
555 kunit_fail_current_test("This should make `fake` test fail.");
556 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
557 kunit_cleanup(&fake);
558
559 /* Reset current->kunit_test to current test. */
560 current->kunit_test = test;
561}
562
563static struct kunit_case kunit_current_test_cases[] = {
564 KUNIT_CASE(kunit_current_test),
565 KUNIT_CASE(kunit_current_fail_test),
566 {}
567};
568
569static struct kunit_suite kunit_current_test_suite = {
570 .name = "kunit_current",
571 .test_cases = kunit_current_test_cases,
572};
573
eda8e324 574kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
a42077b7
RM
575 &kunit_log_test_suite, &kunit_status_test_suite,
576 &kunit_current_test_suite);
c475c77d
AM
577
578MODULE_LICENSE("GPL v2");