Loading...
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>
9
10#include "try-catch-impl.h"
11
12struct 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);
83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
84 test->priv = ctx;
85
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);
90
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};
105
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
121 res->data = &ctx->is_resource_initialized;
122 ctx->is_resource_initialized = true;
123 return 0;
124}
125
126static void fake_resource_free(struct kunit_resource *res)
127{
128 bool *is_resource_initialized = res->data;
129
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
137 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
138
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,
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);
160
161 kunit_put_resource(res);
162}
163
164/*
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 */
172static 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
182 kunit_put_resource(res);
183
184 KUNIT_ASSERT_FALSE(test,
185 kunit_destroy_resource(&ctx->test,
186 kunit_resource_instance_match,
187 res->data));
188
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);
205 kunit_put_resource(resources[i]);
206 }
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
236 res->data = ctx;
237
238 return 0;
239}
240
241static void fake_resource_2_free(struct kunit_resource *res)
242{
243 struct kunit_test_resource_context *ctx = res->data;
244
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;
251 struct kunit_resource *res2;
252
253 res2 = kunit_alloc_and_get_resource(&ctx->test,
254 fake_resource_2_init,
255 fake_resource_2_free,
256 GFP_KERNEL,
257 ctx);
258
259 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
260
261 res->data = ctx;
262
263 kunit_put_resource(res2);
264
265 return 0;
266}
267
268static void fake_resource_1_free(struct kunit_resource *res)
269{
270 struct kunit_test_resource_context *ctx = res->data;
271
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;
283 struct kunit_resource *res;
284
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);
291
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
300 kunit_put_resource(res);
301
302 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
313static 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
328static void kunit_resource_test_named(struct kunit *test)
329{
330 struct kunit_resource res1, res2, *found = NULL;
331 struct kunit_test_resource_context ctx;
332
333 KUNIT_EXPECT_EQ(test,
334 kunit_add_named_resource(test, NULL, NULL, &res1,
335 "resource_1", &ctx),
336 0);
337 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
338
339 KUNIT_EXPECT_EQ(test,
340 kunit_add_named_resource(test, NULL, NULL, &res1,
341 "resource_1", &ctx),
342 -EEXIST);
343
344 KUNIT_EXPECT_EQ(test,
345 kunit_add_named_resource(test, NULL, NULL, &res2,
346 "resource_2", &ctx),
347 0);
348
349 found = kunit_find_named_resource(test, "resource_1");
350
351 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
352
353 if (found)
354 kunit_put_resource(&res1);
355
356 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
357 0);
358
359 kunit_cleanup(test);
360
361 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
362}
363
364static int kunit_resource_test_init(struct kunit *test)
365{
366 struct kunit_test_resource_context *ctx =
367 kzalloc(sizeof(*ctx), GFP_KERNEL);
368
369 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
370
371 test->priv = ctx;
372
373 kunit_init_test(&ctx->test, "test_test_context", NULL);
374
375 return 0;
376}
377
378static void kunit_resource_test_exit(struct kunit *test)
379{
380 struct kunit_test_resource_context *ctx = test->priv;
381
382 kunit_cleanup(&ctx->test);
383 kfree(ctx);
384}
385
386static 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),
394 {}
395};
396
397static 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,
402};
403
404static void kunit_log_test(struct kunit *test);
405
406static struct kunit_case kunit_log_test_cases[] = {
407 KUNIT_CASE(kunit_log_test),
408 {}
409};
410
411static struct kunit_suite kunit_log_test_suite = {
412 .name = "kunit-log-test",
413 .test_cases = kunit_log_test_cases,
414};
415
416static void kunit_log_test(struct kunit *test)
417{
418 struct kunit_suite *suite = &kunit_log_test_suite;
419
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.");
424
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
438}
439
440kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
441 &kunit_log_test_suite);
442
443MODULE_LICENSE("GPL v2");
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>
9
10#include "try-catch-impl.h"
11
12struct 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);
83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
84 test->priv = ctx;
85
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);
90
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};
105
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
121 res->data = &ctx->is_resource_initialized;
122 ctx->is_resource_initialized = true;
123 return 0;
124}
125
126static void fake_resource_free(struct kunit_resource *res)
127{
128 bool *is_resource_initialized = res->data;
129
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
137 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
138
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,
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);
160
161 kunit_put_resource(res);
162}
163
164static inline bool kunit_resource_instance_match(struct kunit *test,
165 struct kunit_resource *res,
166 void *match_data)
167{
168 return res->data == match_data;
169}
170
171/*
172 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
173 * they have a reference to the associated resource that they must release
174 * via kunit_put_resource(). In normal operation, users will only
175 * have to do this for cases where they use kunit_find_resource(), and the
176 * kunit_alloc_resource() function will be used (which does not take a
177 * resource reference).
178 */
179static void kunit_resource_test_destroy_resource(struct kunit *test)
180{
181 struct kunit_test_resource_context *ctx = test->priv;
182 struct kunit_resource *res = kunit_alloc_and_get_resource(
183 &ctx->test,
184 fake_resource_init,
185 fake_resource_free,
186 GFP_KERNEL,
187 ctx);
188
189 kunit_put_resource(res);
190
191 KUNIT_ASSERT_FALSE(test,
192 kunit_destroy_resource(&ctx->test,
193 kunit_resource_instance_match,
194 res->data));
195
196 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
197 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
198}
199
200static void kunit_resource_test_remove_resource(struct kunit *test)
201{
202 struct kunit_test_resource_context *ctx = test->priv;
203 struct kunit_resource *res = kunit_alloc_and_get_resource(
204 &ctx->test,
205 fake_resource_init,
206 fake_resource_free,
207 GFP_KERNEL,
208 ctx);
209
210 /* The resource is in the list */
211 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
212
213 /* Remove the resource. The pointer is still valid, but it can't be
214 * found.
215 */
216 kunit_remove_resource(test, res);
217 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
218 /* We haven't been freed yet. */
219 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
220
221 /* Removing the resource multiple times is valid. */
222 kunit_remove_resource(test, res);
223 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
224 /* Despite having been removed twice (from only one reference), the
225 * resource still has not been freed.
226 */
227 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
228
229 /* Free the resource. */
230 kunit_put_resource(res);
231 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
232}
233
234static void kunit_resource_test_cleanup_resources(struct kunit *test)
235{
236 int i;
237 struct kunit_test_resource_context *ctx = test->priv;
238 struct kunit_resource *resources[5];
239
240 for (i = 0; i < ARRAY_SIZE(resources); i++) {
241 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
242 fake_resource_init,
243 fake_resource_free,
244 GFP_KERNEL,
245 ctx);
246 kunit_put_resource(resources[i]);
247 }
248
249 kunit_cleanup(&ctx->test);
250
251 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
252}
253
254static void kunit_resource_test_mark_order(int order_array[],
255 size_t order_size,
256 int key)
257{
258 int i;
259
260 for (i = 0; i < order_size && order_array[i]; i++)
261 ;
262
263 order_array[i] = key;
264}
265
266#define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
267 kunit_resource_test_mark_order(ctx->order_field, \
268 ARRAY_SIZE(ctx->order_field), \
269 key)
270
271static int fake_resource_2_init(struct kunit_resource *res, void *context)
272{
273 struct kunit_test_resource_context *ctx = context;
274
275 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
276
277 res->data = ctx;
278
279 return 0;
280}
281
282static void fake_resource_2_free(struct kunit_resource *res)
283{
284 struct kunit_test_resource_context *ctx = res->data;
285
286 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
287}
288
289static int fake_resource_1_init(struct kunit_resource *res, void *context)
290{
291 struct kunit_test_resource_context *ctx = context;
292 struct kunit_resource *res2;
293
294 res2 = kunit_alloc_and_get_resource(&ctx->test,
295 fake_resource_2_init,
296 fake_resource_2_free,
297 GFP_KERNEL,
298 ctx);
299
300 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
301
302 res->data = ctx;
303
304 kunit_put_resource(res2);
305
306 return 0;
307}
308
309static void fake_resource_1_free(struct kunit_resource *res)
310{
311 struct kunit_test_resource_context *ctx = res->data;
312
313 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
314}
315
316/*
317 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
318 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
319 * to assert allocation and freeing order when the feature becomes available.
320 */
321static void kunit_resource_test_proper_free_ordering(struct kunit *test)
322{
323 struct kunit_test_resource_context *ctx = test->priv;
324 struct kunit_resource *res;
325
326 /* fake_resource_1 allocates a fake_resource_2 in its init. */
327 res = kunit_alloc_and_get_resource(&ctx->test,
328 fake_resource_1_init,
329 fake_resource_1_free,
330 GFP_KERNEL,
331 ctx);
332
333 /*
334 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
335 * before returning to fake_resource_1_init, it should be the first to
336 * put its key in the allocate_order array.
337 */
338 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
339 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
340
341 kunit_put_resource(res);
342
343 kunit_cleanup(&ctx->test);
344
345 /*
346 * Because fake_resource_2 finishes allocation before fake_resource_1,
347 * fake_resource_1 should be freed first since it could depend on
348 * fake_resource_2.
349 */
350 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
351 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
352}
353
354static void kunit_resource_test_static(struct kunit *test)
355{
356 struct kunit_test_resource_context ctx;
357 struct kunit_resource res;
358
359 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
360 0);
361
362 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
363
364 kunit_cleanup(test);
365
366 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
367}
368
369static void kunit_resource_test_named(struct kunit *test)
370{
371 struct kunit_resource res1, res2, *found = NULL;
372 struct kunit_test_resource_context ctx;
373
374 KUNIT_EXPECT_EQ(test,
375 kunit_add_named_resource(test, NULL, NULL, &res1,
376 "resource_1", &ctx),
377 0);
378 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
379
380 KUNIT_EXPECT_EQ(test,
381 kunit_add_named_resource(test, NULL, NULL, &res1,
382 "resource_1", &ctx),
383 -EEXIST);
384
385 KUNIT_EXPECT_EQ(test,
386 kunit_add_named_resource(test, NULL, NULL, &res2,
387 "resource_2", &ctx),
388 0);
389
390 found = kunit_find_named_resource(test, "resource_1");
391
392 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
393
394 if (found)
395 kunit_put_resource(&res1);
396
397 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
398 0);
399
400 kunit_cleanup(test);
401
402 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
403}
404
405static int kunit_resource_test_init(struct kunit *test)
406{
407 struct kunit_test_resource_context *ctx =
408 kzalloc(sizeof(*ctx), GFP_KERNEL);
409
410 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
411
412 test->priv = ctx;
413
414 kunit_init_test(&ctx->test, "test_test_context", NULL);
415
416 return 0;
417}
418
419static void kunit_resource_test_exit(struct kunit *test)
420{
421 struct kunit_test_resource_context *ctx = test->priv;
422
423 kunit_cleanup(&ctx->test);
424 kfree(ctx);
425}
426
427static struct kunit_case kunit_resource_test_cases[] = {
428 KUNIT_CASE(kunit_resource_test_init_resources),
429 KUNIT_CASE(kunit_resource_test_alloc_resource),
430 KUNIT_CASE(kunit_resource_test_destroy_resource),
431 KUNIT_CASE(kunit_resource_test_remove_resource),
432 KUNIT_CASE(kunit_resource_test_cleanup_resources),
433 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
434 KUNIT_CASE(kunit_resource_test_static),
435 KUNIT_CASE(kunit_resource_test_named),
436 {}
437};
438
439static struct kunit_suite kunit_resource_test_suite = {
440 .name = "kunit-resource-test",
441 .init = kunit_resource_test_init,
442 .exit = kunit_resource_test_exit,
443 .test_cases = kunit_resource_test_cases,
444};
445
446static void kunit_log_test(struct kunit *test);
447
448static struct kunit_case kunit_log_test_cases[] = {
449 KUNIT_CASE(kunit_log_test),
450 {}
451};
452
453static struct kunit_suite kunit_log_test_suite = {
454 .name = "kunit-log-test",
455 .test_cases = kunit_log_test_cases,
456};
457
458static void kunit_log_test(struct kunit *test)
459{
460 struct kunit_suite suite;
461
462 suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
463 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
464
465 kunit_log(KERN_INFO, test, "put this in log.");
466 kunit_log(KERN_INFO, test, "this too.");
467 kunit_log(KERN_INFO, &suite, "add to suite log.");
468 kunit_log(KERN_INFO, &suite, "along with this.");
469
470#ifdef CONFIG_KUNIT_DEBUGFS
471 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
472 strstr(test->log, "put this in log."));
473 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
474 strstr(test->log, "this too."));
475 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
476 strstr(suite.log, "add to suite log."));
477 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
478 strstr(suite.log, "along with this."));
479#else
480 KUNIT_EXPECT_NULL(test, test->log);
481#endif
482}
483
484static void kunit_status_set_failure_test(struct kunit *test)
485{
486 struct kunit fake;
487
488 kunit_init_test(&fake, "fake test", NULL);
489
490 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
491 kunit_set_failure(&fake);
492 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
493}
494
495static void kunit_status_mark_skipped_test(struct kunit *test)
496{
497 struct kunit fake;
498
499 kunit_init_test(&fake, "fake test", NULL);
500
501 /* Before: Should be SUCCESS with no comment. */
502 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
503 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
504
505 /* Mark the test as skipped. */
506 kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
507
508 /* After: Should be SKIPPED with our comment. */
509 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
510 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
511}
512
513static struct kunit_case kunit_status_test_cases[] = {
514 KUNIT_CASE(kunit_status_set_failure_test),
515 KUNIT_CASE(kunit_status_mark_skipped_test),
516 {}
517};
518
519static struct kunit_suite kunit_status_test_suite = {
520 .name = "kunit_status",
521 .test_cases = kunit_status_test_cases,
522};
523
524kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
525 &kunit_log_test_suite, &kunit_status_test_suite);
526
527MODULE_LICENSE("GPL v2");