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,
14 struct kunit_case *test_cases);
16 static void dummy_test(struct kunit *test) {}
18 static struct kunit_case dummy_test_cases[] = {
19 /* .run_case is not important, just needs to be non-NULL */
20 { .name = "test1", .run_case = dummy_test },
21 { .name = "test2", .run_case = dummy_test },
25 static void parse_filter_test(struct kunit *test)
27 struct kunit_test_filter filter = {NULL, NULL};
29 kunit_parse_filter_glob(&filter, "suite");
30 KUNIT_EXPECT_STREQ(test, filter.suite_glob, "suite");
31 KUNIT_EXPECT_FALSE(test, filter.test_glob);
32 kfree(filter.suite_glob);
33 kfree(filter.test_glob);
35 kunit_parse_filter_glob(&filter, "suite.test");
36 KUNIT_EXPECT_STREQ(test, filter.suite_glob, "suite");
37 KUNIT_EXPECT_STREQ(test, filter.test_glob, "test");
38 kfree(filter.suite_glob);
39 kfree(filter.test_glob);
42 static void filter_suites_test(struct kunit *test)
44 struct kunit_suite *subsuite[3] = {NULL, NULL};
45 struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
49 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases);
50 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases);
52 /* Want: suite1, suite2, NULL -> suite2, NULL */
53 got = kunit_filter_suites(&suite_set, "suite2", &err);
54 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start);
55 KUNIT_ASSERT_EQ(test, err, 0);
56 kfree_at_end(test, got.start);
58 /* Validate we just have suite2 */
59 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]);
60 KUNIT_EXPECT_STREQ(test, (const char *)got.start[0]->name, "suite2");
62 /* Contains one element (end is 1 past end) */
63 KUNIT_ASSERT_EQ(test, got.end - got.start, 1);
66 static void filter_suites_test_glob_test(struct kunit *test)
68 struct kunit_suite *subsuite[3] = {NULL, NULL};
69 struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
73 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases);
74 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases);
76 /* Want: suite1, suite2, NULL -> suite2 (just test1), NULL */
77 got = kunit_filter_suites(&suite_set, "suite2.test2", &err);
78 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start);
79 KUNIT_ASSERT_EQ(test, err, 0);
80 kfree_at_end(test, got.start);
82 /* Validate we just have suite2 */
83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]);
84 KUNIT_EXPECT_STREQ(test, (const char *)got.start[0]->name, "suite2");
85 KUNIT_ASSERT_EQ(test, got.end - got.start, 1);
87 /* Now validate we just have test2 */
88 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]->test_cases);
89 KUNIT_EXPECT_STREQ(test, (const char *)got.start[0]->test_cases[0].name, "test2");
90 KUNIT_EXPECT_FALSE(test, got.start[0]->test_cases[1].name);
93 static void filter_suites_to_empty_test(struct kunit *test)
95 struct kunit_suite *subsuite[3] = {NULL, NULL};
96 struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
100 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases);
101 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases);
103 got = kunit_filter_suites(&suite_set, "not_found", &err);
104 KUNIT_ASSERT_EQ(test, err, 0);
105 kfree_at_end(test, got.start); /* just in case */
107 KUNIT_EXPECT_PTR_EQ_MSG(test, got.start, got.end,
108 "should be empty to indicate no match");
111 static struct kunit_case executor_test_cases[] = {
112 KUNIT_CASE(parse_filter_test),
113 KUNIT_CASE(filter_suites_test),
114 KUNIT_CASE(filter_suites_test_glob_test),
115 KUNIT_CASE(filter_suites_to_empty_test),
119 static struct kunit_suite executor_test_suite = {
120 .name = "kunit_executor_test",
121 .test_cases = executor_test_cases,
124 kunit_test_suites(&executor_test_suite);
128 static void kfree_res_free(struct kunit_resource *res)
133 /* Use the resource API to register a call to kfree(to_free).
134 * Since we never actually use the resource, it's safe to use on const data.
136 static void kfree_at_end(struct kunit *test, const void *to_free)
138 /* kfree() handles NULL already, but avoid allocating a no-op cleanup. */
139 if (IS_ERR_OR_NULL(to_free))
141 kunit_alloc_resource(test, NULL, kfree_res_free, GFP_KERNEL,
145 static struct kunit_suite *alloc_fake_suite(struct kunit *test,
146 const char *suite_name,
147 struct kunit_case *test_cases)
149 struct kunit_suite *suite;
151 /* We normally never expect to allocate suites, hence the non-const cast. */
152 suite = kunit_kzalloc(test, sizeof(*suite), GFP_KERNEL);
153 strncpy((char *)suite->name, suite_name, sizeof(suite->name) - 1);
154 suite->test_cases = test_cases;