Linux 5.9.7
[linux/fpc-iii.git] / lib / kunit / kunit-test.c
blob69f902440a0e95fe2b29c88d4011e030cab7dbac
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * KUnit test for core test infrastructure.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8 #include <kunit/test.h>
10 #include "try-catch-impl.h"
12 struct kunit_try_catch_test_context {
13 struct kunit_try_catch *try_catch;
14 bool function_called;
17 static void kunit_test_successful_try(void *data)
19 struct kunit *test = data;
20 struct kunit_try_catch_test_context *ctx = test->priv;
22 ctx->function_called = true;
25 static void kunit_test_no_catch(void *data)
27 struct kunit *test = data;
29 KUNIT_FAIL(test, "Catch should not be called\n");
32 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
34 struct kunit_try_catch_test_context *ctx = test->priv;
35 struct kunit_try_catch *try_catch = ctx->try_catch;
37 kunit_try_catch_init(try_catch,
38 test,
39 kunit_test_successful_try,
40 kunit_test_no_catch);
41 kunit_try_catch_run(try_catch, test);
43 KUNIT_EXPECT_TRUE(test, ctx->function_called);
46 static void kunit_test_unsuccessful_try(void *data)
48 struct kunit *test = data;
49 struct kunit_try_catch_test_context *ctx = test->priv;
50 struct kunit_try_catch *try_catch = ctx->try_catch;
52 kunit_try_catch_throw(try_catch);
53 KUNIT_FAIL(test, "This line should never be reached\n");
56 static void kunit_test_catch(void *data)
58 struct kunit *test = data;
59 struct kunit_try_catch_test_context *ctx = test->priv;
61 ctx->function_called = true;
64 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
66 struct kunit_try_catch_test_context *ctx = test->priv;
67 struct kunit_try_catch *try_catch = ctx->try_catch;
69 kunit_try_catch_init(try_catch,
70 test,
71 kunit_test_unsuccessful_try,
72 kunit_test_catch);
73 kunit_try_catch_run(try_catch, test);
75 KUNIT_EXPECT_TRUE(test, ctx->function_called);
78 static int kunit_try_catch_test_init(struct kunit *test)
80 struct kunit_try_catch_test_context *ctx;
82 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
84 test->priv = ctx;
86 ctx->try_catch = kunit_kmalloc(test,
87 sizeof(*ctx->try_catch),
88 GFP_KERNEL);
89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
91 return 0;
94 static struct kunit_case kunit_try_catch_test_cases[] = {
95 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
96 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
100 static struct kunit_suite kunit_try_catch_test_suite = {
101 .name = "kunit-try-catch-test",
102 .init = kunit_try_catch_test_init,
103 .test_cases = kunit_try_catch_test_cases,
107 * Context for testing test managed resources
108 * is_resource_initialized is used to test arbitrary resources
110 struct kunit_test_resource_context {
111 struct kunit test;
112 bool is_resource_initialized;
113 int allocate_order[2];
114 int free_order[2];
117 static int fake_resource_init(struct kunit_resource *res, void *context)
119 struct kunit_test_resource_context *ctx = context;
121 res->data = &ctx->is_resource_initialized;
122 ctx->is_resource_initialized = true;
123 return 0;
126 static void fake_resource_free(struct kunit_resource *res)
128 bool *is_resource_initialized = res->data;
130 *is_resource_initialized = false;
133 static void kunit_resource_test_init_resources(struct kunit *test)
135 struct kunit_test_resource_context *ctx = test->priv;
137 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
139 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
142 static void kunit_resource_test_alloc_resource(struct kunit *test)
144 struct kunit_test_resource_context *ctx = test->priv;
145 struct kunit_resource *res;
146 kunit_resource_free_t free = fake_resource_free;
148 res = kunit_alloc_and_get_resource(&ctx->test,
149 fake_resource_init,
150 fake_resource_free,
151 GFP_KERNEL,
152 ctx);
154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
155 KUNIT_EXPECT_PTR_EQ(test,
156 &ctx->is_resource_initialized,
157 (bool *)res->data);
158 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
159 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
161 kunit_put_resource(res);
165 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
166 * they have a reference to the associated resource that they must release
167 * via kunit_put_resource(). In normal operation, users will only
168 * have to do this for cases where they use kunit_find_resource(), and the
169 * kunit_alloc_resource() function will be used (which does not take a
170 * resource reference).
172 static void kunit_resource_test_destroy_resource(struct kunit *test)
174 struct kunit_test_resource_context *ctx = test->priv;
175 struct kunit_resource *res = kunit_alloc_and_get_resource(
176 &ctx->test,
177 fake_resource_init,
178 fake_resource_free,
179 GFP_KERNEL,
180 ctx);
182 kunit_put_resource(res);
184 KUNIT_ASSERT_FALSE(test,
185 kunit_destroy_resource(&ctx->test,
186 kunit_resource_instance_match,
187 res->data));
189 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
190 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
193 static void kunit_resource_test_cleanup_resources(struct kunit *test)
195 int i;
196 struct kunit_test_resource_context *ctx = test->priv;
197 struct kunit_resource *resources[5];
199 for (i = 0; i < ARRAY_SIZE(resources); i++) {
200 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
201 fake_resource_init,
202 fake_resource_free,
203 GFP_KERNEL,
204 ctx);
205 kunit_put_resource(resources[i]);
208 kunit_cleanup(&ctx->test);
210 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
213 static void kunit_resource_test_mark_order(int order_array[],
214 size_t order_size,
215 int key)
217 int i;
219 for (i = 0; i < order_size && order_array[i]; i++)
222 order_array[i] = key;
225 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
226 kunit_resource_test_mark_order(ctx->order_field, \
227 ARRAY_SIZE(ctx->order_field), \
228 key)
230 static int fake_resource_2_init(struct kunit_resource *res, void *context)
232 struct kunit_test_resource_context *ctx = context;
234 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
236 res->data = ctx;
238 return 0;
241 static void fake_resource_2_free(struct kunit_resource *res)
243 struct kunit_test_resource_context *ctx = res->data;
245 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
248 static int fake_resource_1_init(struct kunit_resource *res, void *context)
250 struct kunit_test_resource_context *ctx = context;
251 struct kunit_resource *res2;
253 res2 = kunit_alloc_and_get_resource(&ctx->test,
254 fake_resource_2_init,
255 fake_resource_2_free,
256 GFP_KERNEL,
257 ctx);
259 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
261 res->data = ctx;
263 kunit_put_resource(res2);
265 return 0;
268 static void fake_resource_1_free(struct kunit_resource *res)
270 struct kunit_test_resource_context *ctx = res->data;
272 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
276 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
277 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
278 * to assert allocation and freeing order when the feature becomes available.
280 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
282 struct kunit_test_resource_context *ctx = test->priv;
283 struct kunit_resource *res;
285 /* fake_resource_1 allocates a fake_resource_2 in its init. */
286 res = kunit_alloc_and_get_resource(&ctx->test,
287 fake_resource_1_init,
288 fake_resource_1_free,
289 GFP_KERNEL,
290 ctx);
293 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
294 * before returning to fake_resource_1_init, it should be the first to
295 * put its key in the allocate_order array.
297 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
298 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
300 kunit_put_resource(res);
302 kunit_cleanup(&ctx->test);
305 * Because fake_resource_2 finishes allocation before fake_resource_1,
306 * fake_resource_1 should be freed first since it could depend on
307 * fake_resource_2.
309 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
310 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
313 static void kunit_resource_test_static(struct kunit *test)
315 struct kunit_test_resource_context ctx;
316 struct kunit_resource res;
318 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
321 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
323 kunit_cleanup(test);
325 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
328 static void kunit_resource_test_named(struct kunit *test)
330 struct kunit_resource res1, res2, *found = NULL;
331 struct kunit_test_resource_context ctx;
333 KUNIT_EXPECT_EQ(test,
334 kunit_add_named_resource(test, NULL, NULL, &res1,
335 "resource_1", &ctx),
337 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
339 KUNIT_EXPECT_EQ(test,
340 kunit_add_named_resource(test, NULL, NULL, &res1,
341 "resource_1", &ctx),
342 -EEXIST);
344 KUNIT_EXPECT_EQ(test,
345 kunit_add_named_resource(test, NULL, NULL, &res2,
346 "resource_2", &ctx),
349 found = kunit_find_named_resource(test, "resource_1");
351 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
353 if (found)
354 kunit_put_resource(&res1);
356 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
359 kunit_cleanup(test);
361 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
364 static int kunit_resource_test_init(struct kunit *test)
366 struct kunit_test_resource_context *ctx =
367 kzalloc(sizeof(*ctx), GFP_KERNEL);
369 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
371 test->priv = ctx;
373 kunit_init_test(&ctx->test, "test_test_context", NULL);
375 return 0;
378 static void kunit_resource_test_exit(struct kunit *test)
380 struct kunit_test_resource_context *ctx = test->priv;
382 kunit_cleanup(&ctx->test);
383 kfree(ctx);
386 static struct kunit_case kunit_resource_test_cases[] = {
387 KUNIT_CASE(kunit_resource_test_init_resources),
388 KUNIT_CASE(kunit_resource_test_alloc_resource),
389 KUNIT_CASE(kunit_resource_test_destroy_resource),
390 KUNIT_CASE(kunit_resource_test_cleanup_resources),
391 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
392 KUNIT_CASE(kunit_resource_test_static),
393 KUNIT_CASE(kunit_resource_test_named),
397 static struct kunit_suite kunit_resource_test_suite = {
398 .name = "kunit-resource-test",
399 .init = kunit_resource_test_init,
400 .exit = kunit_resource_test_exit,
401 .test_cases = kunit_resource_test_cases,
404 static void kunit_log_test(struct kunit *test);
406 static struct kunit_case kunit_log_test_cases[] = {
407 KUNIT_CASE(kunit_log_test),
411 static struct kunit_suite kunit_log_test_suite = {
412 .name = "kunit-log-test",
413 .test_cases = kunit_log_test_cases,
416 static void kunit_log_test(struct kunit *test)
418 struct kunit_suite *suite = &kunit_log_test_suite;
420 kunit_log(KERN_INFO, test, "put this in log.");
421 kunit_log(KERN_INFO, test, "this too.");
422 kunit_log(KERN_INFO, suite, "add to suite log.");
423 kunit_log(KERN_INFO, suite, "along with this.");
425 #ifdef CONFIG_KUNIT_DEBUGFS
426 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
427 strstr(test->log, "put this in log."));
428 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
429 strstr(test->log, "this too."));
430 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
431 strstr(suite->log, "add to suite log."));
432 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
433 strstr(suite->log, "along with this."));
434 #else
435 KUNIT_EXPECT_PTR_EQ(test, test->log, (char *)NULL);
436 KUNIT_EXPECT_PTR_EQ(test, suite->log, (char *)NULL);
437 #endif
440 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
441 &kunit_log_test_suite);
443 MODULE_LICENSE("GPL v2");