1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * KUnit test for the KUnit executor. 4 * 5 * Copyright (C) 2021, Google LLC. 6 * Author: Daniel Latypov <dlatypov@google.com> 7 */ 8 9 #include <kunit/test.h> 10 #include <kunit/attributes.h> 11 12 static void kfree_at_end(struct kunit *test, const void *to_free); 13 static struct kunit_suite *alloc_fake_suite(struct kunit *test, 14 const char *suite_name, 15 struct kunit_case *test_cases); 16 17 static void dummy_test(struct kunit *test) {} 18 19 static struct kunit_case dummy_test_cases[] = { 20 /* .run_case is not important, just needs to be non-NULL */ 21 { .name = "test1", .run_case = dummy_test }, 22 { .name = "test2", .run_case = dummy_test }, 23 {}, 24 }; 25 26 static void parse_filter_test(struct kunit *test) 27 { 28 struct kunit_glob_filter filter = {NULL, NULL}; 29 30 kunit_parse_glob_filter(&filter, "suite"); 31 KUNIT_EXPECT_STREQ(test, filter.suite_glob, "suite"); 32 KUNIT_EXPECT_FALSE(test, filter.test_glob); 33 kfree(filter.suite_glob); 34 kfree(filter.test_glob); 35 36 kunit_parse_glob_filter(&filter, "suite.test"); 37 KUNIT_EXPECT_STREQ(test, filter.suite_glob, "suite"); 38 KUNIT_EXPECT_STREQ(test, filter.test_glob, "test"); 39 kfree(filter.suite_glob); 40 kfree(filter.test_glob); 41 } 42 43 static void filter_suites_test(struct kunit *test) 44 { 45 struct kunit_suite *subsuite[3] = {NULL, NULL}; 46 struct kunit_suite_set suite_set = { 47 .start = subsuite, .end = &subsuite[2], 48 }; 49 struct kunit_suite_set got; 50 int err = 0; 51 52 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases); 53 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases); 54 55 /* Want: suite1, suite2, NULL -> suite2, NULL */ 56 got = kunit_filter_suites(&suite_set, "suite2", NULL, NULL, &err); 57 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start); 58 KUNIT_ASSERT_EQ(test, err, 0); 59 kfree_at_end(test, got.start); 60 61 /* Validate we just have suite2 */ 62 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]); 63 KUNIT_EXPECT_STREQ(test, (const char *)got.start[0]->name, "suite2"); 64 65 /* Contains one element (end is 1 past end) */ 66 KUNIT_ASSERT_EQ(test, got.end - got.start, 1); 67 } 68 69 static void filter_suites_test_glob_test(struct kunit *test) 70 { 71 struct kunit_suite *subsuite[3] = {NULL, NULL}; 72 struct kunit_suite_set suite_set = { 73 .start = subsuite, .end = &subsuite[2], 74 }; 75 struct kunit_suite_set got; 76 int err = 0; 77 78 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases); 79 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases); 80 81 /* Want: suite1, suite2, NULL -> suite2 (just test1), NULL */ 82 got = kunit_filter_suites(&suite_set, "suite2.test2", NULL, NULL, &err); 83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start); 84 KUNIT_ASSERT_EQ(test, err, 0); 85 kfree_at_end(test, got.start); 86 87 /* Validate we just have suite2 */ 88 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]); 89 KUNIT_EXPECT_STREQ(test, (const char *)got.start[0]->name, "suite2"); 90 KUNIT_ASSERT_EQ(test, got.end - got.start, 1); 91 92 /* Now validate we just have test2 */ 93 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]->test_cases); 94 KUNIT_EXPECT_STREQ(test, (const char *)got.start[0]->test_cases[0].name, "test2"); 95 KUNIT_EXPECT_FALSE(test, got.start[0]->test_cases[1].name); 96 } 97 98 static void filter_suites_to_empty_test(struct kunit *test) 99 { 100 struct kunit_suite *subsuite[3] = {NULL, NULL}; 101 struct kunit_suite_set suite_set = { 102 .start = subsuite, .end = &subsuite[2], 103 }; 104 struct kunit_suite_set got; 105 int err = 0; 106 107 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases); 108 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_test_cases); 109 110 got = kunit_filter_suites(&suite_set, "not_found", NULL, NULL, &err); 111 KUNIT_ASSERT_EQ(test, err, 0); 112 kfree_at_end(test, got.start); /* just in case */ 113 114 KUNIT_EXPECT_PTR_EQ_MSG(test, got.start, got.end, 115 "should be empty to indicate no match"); 116 } 117 118 static void parse_filter_attr_test(struct kunit *test) 119 { 120 int j, filter_count; 121 struct kunit_attr_filter *parsed_filters; 122 char *filters = "speed>slow, module!=example"; 123 int err = 0; 124 125 filter_count = kunit_get_filter_count(filters); 126 KUNIT_EXPECT_EQ(test, filter_count, 2); 127 128 parsed_filters = kunit_kcalloc(test, filter_count, sizeof(*parsed_filters), 129 GFP_KERNEL); 130 for (j = 0; j < filter_count; j++) { 131 parsed_filters[j] = kunit_next_attr_filter(&filters, &err); 132 KUNIT_ASSERT_EQ_MSG(test, err, 0, "failed to parse filter '%s'", filters[j]); 133 } 134 135 KUNIT_EXPECT_STREQ(test, kunit_attr_filter_name(parsed_filters[0]), "speed"); 136 KUNIT_EXPECT_STREQ(test, parsed_filters[0].input, ">slow"); 137 138 KUNIT_EXPECT_STREQ(test, kunit_attr_filter_name(parsed_filters[1]), "module"); 139 KUNIT_EXPECT_STREQ(test, parsed_filters[1].input, "!=example"); 140 } 141 142 static struct kunit_case dummy_attr_test_cases[] = { 143 /* .run_case is not important, just needs to be non-NULL */ 144 { .name = "slow", .run_case = dummy_test, .module_name = "dummy", 145 .attr.speed = KUNIT_SPEED_SLOW }, 146 { .name = "normal", .run_case = dummy_test, .module_name = "dummy" }, 147 {}, 148 }; 149 150 static void filter_attr_test(struct kunit *test) 151 { 152 struct kunit_suite *subsuite[3] = {NULL, NULL}; 153 struct kunit_suite_set suite_set = { 154 .start = subsuite, .end = &subsuite[2], 155 }; 156 struct kunit_suite_set got; 157 int err = 0; 158 159 subsuite[0] = alloc_fake_suite(test, "normal_suite", dummy_attr_test_cases); 160 subsuite[1] = alloc_fake_suite(test, "slow_suite", dummy_attr_test_cases); 161 subsuite[1]->attr.speed = KUNIT_SPEED_SLOW; // Set suite attribute 162 163 /* 164 * Want: normal_suite(slow, normal), slow_suite(slow, normal), 165 * NULL -> normal_suite(normal), NULL 166 * 167 * The normal test in slow_suite is filtered out because the speed 168 * attribute is unset and thus, the filtering is based on the parent attribute 169 * of slow. 170 */ 171 got = kunit_filter_suites(&suite_set, NULL, "speed>slow", NULL, &err); 172 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start); 173 KUNIT_ASSERT_EQ(test, err, 0); 174 kfree_at_end(test, got.start); 175 176 /* Validate we just have normal_suite */ 177 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]); 178 KUNIT_EXPECT_STREQ(test, got.start[0]->name, "normal_suite"); 179 KUNIT_ASSERT_EQ(test, got.end - got.start, 1); 180 181 /* Now validate we just have normal test case */ 182 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]->test_cases); 183 KUNIT_EXPECT_STREQ(test, got.start[0]->test_cases[0].name, "normal"); 184 KUNIT_EXPECT_FALSE(test, got.start[0]->test_cases[1].name); 185 } 186 187 static void filter_attr_empty_test(struct kunit *test) 188 { 189 struct kunit_suite *subsuite[3] = {NULL, NULL}; 190 struct kunit_suite_set suite_set = { 191 .start = subsuite, .end = &subsuite[2], 192 }; 193 struct kunit_suite_set got; 194 int err = 0; 195 196 subsuite[0] = alloc_fake_suite(test, "suite1", dummy_attr_test_cases); 197 subsuite[1] = alloc_fake_suite(test, "suite2", dummy_attr_test_cases); 198 199 got = kunit_filter_suites(&suite_set, NULL, "module!=dummy", NULL, &err); 200 KUNIT_ASSERT_EQ(test, err, 0); 201 kfree_at_end(test, got.start); /* just in case */ 202 203 KUNIT_EXPECT_PTR_EQ_MSG(test, got.start, got.end, 204 "should be empty to indicate no match"); 205 } 206 207 static void filter_attr_skip_test(struct kunit *test) 208 { 209 struct kunit_suite *subsuite[2] = {NULL}; 210 struct kunit_suite_set suite_set = { 211 .start = subsuite, .end = &subsuite[1], 212 }; 213 struct kunit_suite_set got; 214 int err = 0; 215 216 subsuite[0] = alloc_fake_suite(test, "suite", dummy_attr_test_cases); 217 218 /* Want: suite(slow, normal), NULL -> suite(slow with SKIP, normal), NULL */ 219 got = kunit_filter_suites(&suite_set, NULL, "speed>slow", "skip", &err); 220 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start); 221 KUNIT_ASSERT_EQ(test, err, 0); 222 kfree_at_end(test, got.start); 223 224 /* Validate we have both the slow and normal test */ 225 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]->test_cases); 226 KUNIT_ASSERT_EQ(test, kunit_suite_num_test_cases(got.start[0]), 2); 227 KUNIT_EXPECT_STREQ(test, got.start[0]->test_cases[0].name, "slow"); 228 KUNIT_EXPECT_STREQ(test, got.start[0]->test_cases[1].name, "normal"); 229 230 /* Now ensure slow is skipped and normal is not */ 231 KUNIT_EXPECT_EQ(test, got.start[0]->test_cases[0].status, KUNIT_SKIPPED); 232 KUNIT_EXPECT_FALSE(test, got.start[0]->test_cases[1].status); 233 } 234 235 static struct kunit_case executor_test_cases[] = { 236 KUNIT_CASE(parse_filter_test), 237 KUNIT_CASE(filter_suites_test), 238 KUNIT_CASE(filter_suites_test_glob_test), 239 KUNIT_CASE(filter_suites_to_empty_test), 240 KUNIT_CASE(parse_filter_attr_test), 241 KUNIT_CASE(filter_attr_test), 242 KUNIT_CASE(filter_attr_empty_test), 243 KUNIT_CASE(filter_attr_skip_test), 244 {} 245 }; 246 247 static struct kunit_suite executor_test_suite = { 248 .name = "kunit_executor_test", 249 .test_cases = executor_test_cases, 250 }; 251 252 kunit_test_suites(&executor_test_suite); 253 254 /* Test helpers */ 255 256 /* Use the resource API to register a call to kfree(to_free). 257 * Since we never actually use the resource, it's safe to use on const data. 258 */ 259 static void kfree_at_end(struct kunit *test, const void *to_free) 260 { 261 /* kfree() handles NULL already, but avoid allocating a no-op cleanup. */ 262 if (IS_ERR_OR_NULL(to_free)) 263 return; 264 265 kunit_add_action(test, 266 (kunit_action_t *)kfree, 267 (void *)to_free); 268 } 269 270 static struct kunit_suite *alloc_fake_suite(struct kunit *test, 271 const char *suite_name, 272 struct kunit_case *test_cases) 273 { 274 struct kunit_suite *suite; 275 276 /* We normally never expect to allocate suites, hence the non-const cast. */ 277 suite = kunit_kzalloc(test, sizeof(*suite), GFP_KERNEL); 278 strncpy((char *)suite->name, suite_name, sizeof(suite->name) - 1); 279 suite->test_cases = test_cases; 280 281 return suite; 282 } 283