1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for core test infrastructure.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
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
;
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
,
39 kunit_test_successful_try
,
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
,
71 kunit_test_unsuccessful_try
,
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
);
86 ctx
->try_catch
= kunit_kmalloc(test
,
87 sizeof(*ctx
->try_catch
),
89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
->try_catch
);
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
{
112 bool is_resource_initialized
;
113 int allocate_order
[2];
117 static int fake_resource_init(struct kunit_resource
*res
, void *context
)
119 struct kunit_test_resource_context
*ctx
= context
;
121 res
->allocation
= &ctx
->is_resource_initialized
;
122 ctx
->is_resource_initialized
= true;
126 static void fake_resource_free(struct kunit_resource
*res
)
128 bool *is_resource_initialized
= res
->allocation
;
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");
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
,
154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, res
);
155 KUNIT_EXPECT_PTR_EQ(test
,
156 &ctx
->is_resource_initialized
,
157 (bool *) res
->allocation
);
158 KUNIT_EXPECT_TRUE(test
, list_is_last(&res
->node
, &ctx
->test
.resources
));
159 KUNIT_EXPECT_PTR_EQ(test
, free
, res
->free
);
162 static void kunit_resource_test_destroy_resource(struct kunit
*test
)
164 struct kunit_test_resource_context
*ctx
= test
->priv
;
165 struct kunit_resource
*res
= kunit_alloc_and_get_resource(
172 KUNIT_ASSERT_FALSE(test
,
173 kunit_resource_destroy(&ctx
->test
,
174 kunit_resource_instance_match
,
178 KUNIT_EXPECT_FALSE(test
, ctx
->is_resource_initialized
);
179 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
182 static void kunit_resource_test_cleanup_resources(struct kunit
*test
)
185 struct kunit_test_resource_context
*ctx
= test
->priv
;
186 struct kunit_resource
*resources
[5];
188 for (i
= 0; i
< ARRAY_SIZE(resources
); i
++) {
189 resources
[i
] = kunit_alloc_and_get_resource(&ctx
->test
,
196 kunit_cleanup(&ctx
->test
);
198 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
201 static void kunit_resource_test_mark_order(int order_array
[],
207 for (i
= 0; i
< order_size
&& order_array
[i
]; i
++)
210 order_array
[i
] = key
;
213 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
214 kunit_resource_test_mark_order(ctx->order_field, \
215 ARRAY_SIZE(ctx->order_field), \
218 static int fake_resource_2_init(struct kunit_resource
*res
, void *context
)
220 struct kunit_test_resource_context
*ctx
= context
;
222 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, allocate_order
, 2);
224 res
->allocation
= ctx
;
229 static void fake_resource_2_free(struct kunit_resource
*res
)
231 struct kunit_test_resource_context
*ctx
= res
->allocation
;
233 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, free_order
, 2);
236 static int fake_resource_1_init(struct kunit_resource
*res
, void *context
)
238 struct kunit_test_resource_context
*ctx
= context
;
240 kunit_alloc_and_get_resource(&ctx
->test
,
241 fake_resource_2_init
,
242 fake_resource_2_free
,
246 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, allocate_order
, 1);
248 res
->allocation
= ctx
;
253 static void fake_resource_1_free(struct kunit_resource
*res
)
255 struct kunit_test_resource_context
*ctx
= res
->allocation
;
257 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, free_order
, 1);
261 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
262 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
263 * to assert allocation and freeing order when the feature becomes available.
265 static void kunit_resource_test_proper_free_ordering(struct kunit
*test
)
267 struct kunit_test_resource_context
*ctx
= test
->priv
;
269 /* fake_resource_1 allocates a fake_resource_2 in its init. */
270 kunit_alloc_and_get_resource(&ctx
->test
,
271 fake_resource_1_init
,
272 fake_resource_1_free
,
277 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
278 * before returning to fake_resource_1_init, it should be the first to
279 * put its key in the allocate_order array.
281 KUNIT_EXPECT_EQ(test
, ctx
->allocate_order
[0], 2);
282 KUNIT_EXPECT_EQ(test
, ctx
->allocate_order
[1], 1);
284 kunit_cleanup(&ctx
->test
);
287 * Because fake_resource_2 finishes allocation before fake_resource_1,
288 * fake_resource_1 should be freed first since it could depend on
291 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[0], 1);
292 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[1], 2);
295 static int kunit_resource_test_init(struct kunit
*test
)
297 struct kunit_test_resource_context
*ctx
=
298 kzalloc(sizeof(*ctx
), GFP_KERNEL
);
300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
);
304 kunit_init_test(&ctx
->test
, "test_test_context");
309 static void kunit_resource_test_exit(struct kunit
*test
)
311 struct kunit_test_resource_context
*ctx
= test
->priv
;
313 kunit_cleanup(&ctx
->test
);
317 static struct kunit_case kunit_resource_test_cases
[] = {
318 KUNIT_CASE(kunit_resource_test_init_resources
),
319 KUNIT_CASE(kunit_resource_test_alloc_resource
),
320 KUNIT_CASE(kunit_resource_test_destroy_resource
),
321 KUNIT_CASE(kunit_resource_test_cleanup_resources
),
322 KUNIT_CASE(kunit_resource_test_proper_free_ordering
),
326 static struct kunit_suite kunit_resource_test_suite
= {
327 .name
= "kunit-resource-test",
328 .init
= kunit_resource_test_init
,
329 .exit
= kunit_resource_test_exit
,
330 .test_cases
= kunit_resource_test_cases
,
332 kunit_test_suites(&kunit_try_catch_test_suite
, &kunit_resource_test_suite
);
334 MODULE_LICENSE("GPL v2");