blob: 03f3ecaa1ef92ea3e50fb6c66c609a7104062a9f [file] [log] [blame]
Brendan Higginse4eb1172019-09-23 02:02:40 -07001// 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>
9
Alan Maguire9bbb11c2020-01-06 22:28:19 +000010#include "try-catch-impl.h"
11
Brendan Higginse4eb1172019-09-23 02:02:40 -070012struct kunit_try_catch_test_context {
13 struct kunit_try_catch *try_catch;
14 bool function_called;
15};
16
17static void kunit_test_successful_try(void *data)
18{
19 struct kunit *test = data;
20 struct kunit_try_catch_test_context *ctx = test->priv;
21
22 ctx->function_called = true;
23}
24
25static void kunit_test_no_catch(void *data)
26{
27 struct kunit *test = data;
28
29 KUNIT_FAIL(test, "Catch should not be called\n");
30}
31
32static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
33{
34 struct kunit_try_catch_test_context *ctx = test->priv;
35 struct kunit_try_catch *try_catch = ctx->try_catch;
36
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);
42
43 KUNIT_EXPECT_TRUE(test, ctx->function_called);
44}
45
46static void kunit_test_unsuccessful_try(void *data)
47{
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;
51
52 kunit_try_catch_throw(try_catch);
53 KUNIT_FAIL(test, "This line should never be reached\n");
54}
55
56static void kunit_test_catch(void *data)
57{
58 struct kunit *test = data;
59 struct kunit_try_catch_test_context *ctx = test->priv;
60
61 ctx->function_called = true;
62}
63
64static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
65{
66 struct kunit_try_catch_test_context *ctx = test->priv;
67 struct kunit_try_catch *try_catch = ctx->try_catch;
68
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);
74
75 KUNIT_EXPECT_TRUE(test, ctx->function_called);
76}
77
78static int kunit_try_catch_test_init(struct kunit *test)
79{
80 struct kunit_try_catch_test_context *ctx;
81
82 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
Brendan Higginse4aea8f2019-09-23 02:02:41 -070083 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
Brendan Higginse4eb1172019-09-23 02:02:40 -070084 test->priv = ctx;
85
86 ctx->try_catch = kunit_kmalloc(test,
87 sizeof(*ctx->try_catch),
88 GFP_KERNEL);
Brendan Higginse4aea8f2019-09-23 02:02:41 -070089 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
Brendan Higginse4eb1172019-09-23 02:02:40 -070090
91 return 0;
92}
93
94static 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),
97 {}
98};
99
100static 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,
104};
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700105
106/*
107 * Context for testing test managed resources
108 * is_resource_initialized is used to test arbitrary resources
109 */
110struct kunit_test_resource_context {
111 struct kunit test;
112 bool is_resource_initialized;
113 int allocate_order[2];
114 int free_order[2];
115};
116
117static int fake_resource_init(struct kunit_resource *res, void *context)
118{
119 struct kunit_test_resource_context *ctx = context;
120
Alan Maguired4cdd142020-05-29 22:46:20 +0100121 res->data = &ctx->is_resource_initialized;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700122 ctx->is_resource_initialized = true;
123 return 0;
124}
125
126static void fake_resource_free(struct kunit_resource *res)
127{
Alan Maguired4cdd142020-05-29 22:46:20 +0100128 bool *is_resource_initialized = res->data;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700129
130 *is_resource_initialized = false;
131}
132
133static void kunit_resource_test_init_resources(struct kunit *test)
134{
135 struct kunit_test_resource_context *ctx = test->priv;
136
Alan Maguiree2219db2020-03-26 14:25:07 +0000137 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700138
139 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
140}
141
142static void kunit_resource_test_alloc_resource(struct kunit *test)
143{
144 struct kunit_test_resource_context *ctx = test->priv;
145 struct kunit_resource *res;
146 kunit_resource_free_t free = fake_resource_free;
147
148 res = kunit_alloc_and_get_resource(&ctx->test,
149 fake_resource_init,
150 fake_resource_free,
151 GFP_KERNEL,
152 ctx);
153
154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
155 KUNIT_EXPECT_PTR_EQ(test,
156 &ctx->is_resource_initialized,
Alan Maguired4cdd142020-05-29 22:46:20 +0100157 (bool *)res->data);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700158 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
159 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
Alan Maguired4cdd142020-05-29 22:46:20 +0100160
161 kunit_put_resource(res);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700162}
163
Alan Maguired4cdd142020-05-29 22:46:20 +0100164/*
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).
171 */
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700172static void kunit_resource_test_destroy_resource(struct kunit *test)
173{
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);
181
Alan Maguired4cdd142020-05-29 22:46:20 +0100182 kunit_put_resource(res);
183
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700184 KUNIT_ASSERT_FALSE(test,
Alan Maguired4cdd142020-05-29 22:46:20 +0100185 kunit_destroy_resource(&ctx->test,
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700186 kunit_resource_instance_match,
Alan Maguired4cdd142020-05-29 22:46:20 +0100187 res->data));
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700188
189 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
190 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
191}
192
193static void kunit_resource_test_cleanup_resources(struct kunit *test)
194{
195 int i;
196 struct kunit_test_resource_context *ctx = test->priv;
197 struct kunit_resource *resources[5];
198
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);
Alan Maguired4cdd142020-05-29 22:46:20 +0100205 kunit_put_resource(resources[i]);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700206 }
207
208 kunit_cleanup(&ctx->test);
209
210 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
211}
212
213static void kunit_resource_test_mark_order(int order_array[],
214 size_t order_size,
215 int key)
216{
217 int i;
218
219 for (i = 0; i < order_size && order_array[i]; i++)
220 ;
221
222 order_array[i] = key;
223}
224
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)
229
230static int fake_resource_2_init(struct kunit_resource *res, void *context)
231{
232 struct kunit_test_resource_context *ctx = context;
233
234 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
235
Alan Maguired4cdd142020-05-29 22:46:20 +0100236 res->data = ctx;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700237
238 return 0;
239}
240
241static void fake_resource_2_free(struct kunit_resource *res)
242{
Alan Maguired4cdd142020-05-29 22:46:20 +0100243 struct kunit_test_resource_context *ctx = res->data;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700244
245 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
246}
247
248static int fake_resource_1_init(struct kunit_resource *res, void *context)
249{
250 struct kunit_test_resource_context *ctx = context;
Alan Maguired4cdd142020-05-29 22:46:20 +0100251 struct kunit_resource *res2;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700252
Alan Maguired4cdd142020-05-29 22:46:20 +0100253 res2 = kunit_alloc_and_get_resource(&ctx->test,
254 fake_resource_2_init,
255 fake_resource_2_free,
256 GFP_KERNEL,
257 ctx);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700258
259 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
260
Alan Maguired4cdd142020-05-29 22:46:20 +0100261 res->data = ctx;
262
263 kunit_put_resource(res2);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700264
265 return 0;
266}
267
268static void fake_resource_1_free(struct kunit_resource *res)
269{
Alan Maguired4cdd142020-05-29 22:46:20 +0100270 struct kunit_test_resource_context *ctx = res->data;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700271
272 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
273}
274
275/*
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.
279 */
280static void kunit_resource_test_proper_free_ordering(struct kunit *test)
281{
282 struct kunit_test_resource_context *ctx = test->priv;
Alan Maguired4cdd142020-05-29 22:46:20 +0100283 struct kunit_resource *res;
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700284
285 /* fake_resource_1 allocates a fake_resource_2 in its init. */
Alan Maguired4cdd142020-05-29 22:46:20 +0100286 res = kunit_alloc_and_get_resource(&ctx->test,
287 fake_resource_1_init,
288 fake_resource_1_free,
289 GFP_KERNEL,
290 ctx);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700291
292 /*
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.
296 */
297 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
298 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
299
Alan Maguired4cdd142020-05-29 22:46:20 +0100300 kunit_put_resource(res);
301
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700302 kunit_cleanup(&ctx->test);
303
304 /*
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.
308 */
309 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
310 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
311}
312
Alan Maguired4cdd142020-05-29 22:46:20 +0100313static void kunit_resource_test_static(struct kunit *test)
314{
315 struct kunit_test_resource_context ctx;
316 struct kunit_resource res;
317
318 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
319 0);
320
321 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
322
323 kunit_cleanup(test);
324
325 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
326}
327
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700328static int kunit_resource_test_init(struct kunit *test)
329{
330 struct kunit_test_resource_context *ctx =
331 kzalloc(sizeof(*ctx), GFP_KERNEL);
332
333 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
334
335 test->priv = ctx;
336
Alan Maguiree2219db2020-03-26 14:25:07 +0000337 kunit_init_test(&ctx->test, "test_test_context", NULL);
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700338
339 return 0;
340}
341
342static void kunit_resource_test_exit(struct kunit *test)
343{
344 struct kunit_test_resource_context *ctx = test->priv;
345
346 kunit_cleanup(&ctx->test);
347 kfree(ctx);
348}
349
350static struct kunit_case kunit_resource_test_cases[] = {
351 KUNIT_CASE(kunit_resource_test_init_resources),
352 KUNIT_CASE(kunit_resource_test_alloc_resource),
353 KUNIT_CASE(kunit_resource_test_destroy_resource),
354 KUNIT_CASE(kunit_resource_test_cleanup_resources),
355 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
Alan Maguired4cdd142020-05-29 22:46:20 +0100356 KUNIT_CASE(kunit_resource_test_static),
Avinash Kondareddy73ba5aa2019-09-23 02:02:42 -0700357 {}
358};
359
360static struct kunit_suite kunit_resource_test_suite = {
361 .name = "kunit-resource-test",
362 .init = kunit_resource_test_init,
363 .exit = kunit_resource_test_exit,
364 .test_cases = kunit_resource_test_cases,
365};
Alan Maguireeda8e322020-03-26 14:25:08 +0000366
367static void kunit_log_test(struct kunit *test);
368
369static struct kunit_case kunit_log_test_cases[] = {
370 KUNIT_CASE(kunit_log_test),
371 {}
372};
373
374static struct kunit_suite kunit_log_test_suite = {
375 .name = "kunit-log-test",
376 .test_cases = kunit_log_test_cases,
377};
378
379static void kunit_log_test(struct kunit *test)
380{
381 struct kunit_suite *suite = &kunit_log_test_suite;
382
383 kunit_log(KERN_INFO, test, "put this in log.");
384 kunit_log(KERN_INFO, test, "this too.");
385 kunit_log(KERN_INFO, suite, "add to suite log.");
386 kunit_log(KERN_INFO, suite, "along with this.");
387
388#ifdef CONFIG_KUNIT_DEBUGFS
389 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
390 strstr(test->log, "put this in log."));
391 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
392 strstr(test->log, "this too."));
393 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
394 strstr(suite->log, "add to suite log."));
395 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
396 strstr(suite->log, "along with this."));
397#else
398 KUNIT_EXPECT_PTR_EQ(test, test->log, (char *)NULL);
399 KUNIT_EXPECT_PTR_EQ(test, suite->log, (char *)NULL);
400#endif
401}
402
403kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
404 &kunit_log_test_suite);
Alan Maguirec475c772020-01-06 22:28:20 +0000405
406MODULE_LICENSE("GPL v2");