1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for the KUnit executor.
5 * Copyright (C) 2021, Google LLC.
6 * Author: Daniel Latypov <dlatypov@google.com>
9 #include <kunit/test.h>
11 static void kfree_at_end(struct kunit
*test
, const void *to_free
);
12 static struct kunit_suite
*alloc_fake_suite(struct kunit
*test
,
13 const char *suite_name
);
15 static void filter_subsuite_test(struct kunit
*test
)
17 struct kunit_suite
*subsuite
[3] = {NULL
, NULL
, NULL
};
18 struct kunit_suite
* const *filtered
;
20 subsuite
[0] = alloc_fake_suite(test
, "suite1");
21 subsuite
[1] = alloc_fake_suite(test
, "suite2");
23 /* Want: suite1, suite2, NULL -> suite2, NULL */
24 filtered
= kunit_filter_subsuite(subsuite
, "suite2*");
25 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, filtered
);
26 kfree_at_end(test
, filtered
);
28 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, filtered
[0]);
29 KUNIT_EXPECT_STREQ(test
, (const char *)filtered
[0]->name
, "suite2");
31 KUNIT_EXPECT_FALSE(test
, filtered
[1]);
34 static void filter_subsuite_to_empty_test(struct kunit
*test
)
36 struct kunit_suite
*subsuite
[3] = {NULL
, NULL
, NULL
};
37 struct kunit_suite
* const *filtered
;
39 subsuite
[0] = alloc_fake_suite(test
, "suite1");
40 subsuite
[1] = alloc_fake_suite(test
, "suite2");
42 filtered
= kunit_filter_subsuite(subsuite
, "not_found");
43 kfree_at_end(test
, filtered
); /* just in case */
45 KUNIT_EXPECT_FALSE_MSG(test
, filtered
,
46 "should be NULL to indicate no match");
49 static void kfree_subsuites_at_end(struct kunit
*test
, struct suite_set
*suite_set
)
51 struct kunit_suite
* const * const *suites
;
53 kfree_at_end(test
, suite_set
->start
);
54 for (suites
= suite_set
->start
; suites
< suite_set
->end
; suites
++)
55 kfree_at_end(test
, *suites
);
58 static void filter_suites_test(struct kunit
*test
)
60 /* Suites per-file are stored as a NULL terminated array */
61 struct kunit_suite
*subsuites
[2][2] = {
65 /* Match the memory layout of suite_set */
66 struct kunit_suite
* const * const suites
[2] = {
67 subsuites
[0], subsuites
[1],
70 const struct suite_set suite_set
= {
74 struct suite_set filtered
= {.start
= NULL
, .end
= NULL
};
76 /* Emulate two files, each having one suite */
77 subsuites
[0][0] = alloc_fake_suite(test
, "suite0");
78 subsuites
[1][0] = alloc_fake_suite(test
, "suite1");
80 /* Filter out suite1 */
81 filtered
= kunit_filter_suites(&suite_set
, "suite0");
82 kfree_subsuites_at_end(test
, &filtered
); /* let us use ASSERTs without leaking */
83 KUNIT_ASSERT_EQ(test
, filtered
.end
- filtered
.start
, (ptrdiff_t)1);
85 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, filtered
.start
);
86 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, filtered
.start
[0]);
87 KUNIT_EXPECT_STREQ(test
, (const char *)filtered
.start
[0][0]->name
, "suite0");
90 static struct kunit_case executor_test_cases
[] = {
91 KUNIT_CASE(filter_subsuite_test
),
92 KUNIT_CASE(filter_subsuite_to_empty_test
),
93 KUNIT_CASE(filter_suites_test
),
97 static struct kunit_suite executor_test_suite
= {
98 .name
= "kunit_executor_test",
99 .test_cases
= executor_test_cases
,
102 kunit_test_suites(&executor_test_suite
);
106 static void kfree_res_free(struct kunit_resource
*res
)
111 /* Use the resource API to register a call to kfree(to_free).
112 * Since we never actually use the resource, it's safe to use on const data.
114 static void kfree_at_end(struct kunit
*test
, const void *to_free
)
116 /* kfree() handles NULL already, but avoid allocating a no-op cleanup. */
117 if (IS_ERR_OR_NULL(to_free
))
119 kunit_alloc_and_get_resource(test
, NULL
, kfree_res_free
, GFP_KERNEL
,
123 static struct kunit_suite
*alloc_fake_suite(struct kunit
*test
,
124 const char *suite_name
)
126 struct kunit_suite
*suite
;
128 /* We normally never expect to allocate suites, hence the non-const cast. */
129 suite
= kunit_kzalloc(test
, sizeof(*suite
), GFP_KERNEL
);
130 strncpy((char *)suite
->name
, suite_name
, sizeof(suite
->name
) - 1);