xref: /openbmc/linux/lib/kunit/test.c (revision 715f23b6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Base unit test (KUnit) API.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: Brendan Higgins <brendanhiggins@google.com>
7  */
8 
9 #include <kunit/test.h>
10 #include <kunit/try-catch.h>
11 #include <linux/kernel.h>
12 #include <linux/sched/debug.h>
13 
14 static void kunit_set_failure(struct kunit *test)
15 {
16 	WRITE_ONCE(test->success, false);
17 }
18 
19 static void kunit_print_tap_version(void)
20 {
21 	static bool kunit_has_printed_tap_version;
22 
23 	if (!kunit_has_printed_tap_version) {
24 		pr_info("TAP version 14\n");
25 		kunit_has_printed_tap_version = true;
26 	}
27 }
28 
29 static size_t kunit_test_cases_len(struct kunit_case *test_cases)
30 {
31 	struct kunit_case *test_case;
32 	size_t len = 0;
33 
34 	for (test_case = test_cases; test_case->run_case; test_case++)
35 		len++;
36 
37 	return len;
38 }
39 
40 static void kunit_print_subtest_start(struct kunit_suite *suite)
41 {
42 	kunit_print_tap_version();
43 	pr_info("\t# Subtest: %s\n", suite->name);
44 	pr_info("\t1..%zd\n", kunit_test_cases_len(suite->test_cases));
45 }
46 
47 static void kunit_print_ok_not_ok(bool should_indent,
48 				  bool is_ok,
49 				  size_t test_number,
50 				  const char *description)
51 {
52 	const char *indent, *ok_not_ok;
53 
54 	if (should_indent)
55 		indent = "\t";
56 	else
57 		indent = "";
58 
59 	if (is_ok)
60 		ok_not_ok = "ok";
61 	else
62 		ok_not_ok = "not ok";
63 
64 	pr_info("%s%s %zd - %s\n", indent, ok_not_ok, test_number, description);
65 }
66 
67 static bool kunit_suite_has_succeeded(struct kunit_suite *suite)
68 {
69 	const struct kunit_case *test_case;
70 
71 	for (test_case = suite->test_cases; test_case->run_case; test_case++)
72 		if (!test_case->success)
73 			return false;
74 
75 	return true;
76 }
77 
78 static void kunit_print_subtest_end(struct kunit_suite *suite)
79 {
80 	static size_t kunit_suite_counter = 1;
81 
82 	kunit_print_ok_not_ok(false,
83 			      kunit_suite_has_succeeded(suite),
84 			      kunit_suite_counter++,
85 			      suite->name);
86 }
87 
88 static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
89 					    size_t test_number)
90 {
91 	kunit_print_ok_not_ok(true,
92 			      test_case->success,
93 			      test_number,
94 			      test_case->name);
95 }
96 
97 static void kunit_print_string_stream(struct kunit *test,
98 				      struct string_stream *stream)
99 {
100 	struct string_stream_fragment *fragment;
101 	char *buf;
102 
103 	buf = string_stream_get_string(stream);
104 	if (!buf) {
105 		kunit_err(test,
106 			  "Could not allocate buffer, dumping stream:\n");
107 		list_for_each_entry(fragment, &stream->fragments, node) {
108 			kunit_err(test, "%s", fragment->fragment);
109 		}
110 		kunit_err(test, "\n");
111 	} else {
112 		kunit_err(test, "%s", buf);
113 		kunit_kfree(test, buf);
114 	}
115 }
116 
117 static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
118 {
119 	struct string_stream *stream;
120 
121 	kunit_set_failure(test);
122 
123 	stream = alloc_string_stream(test, GFP_KERNEL);
124 	if (!stream) {
125 		WARN(true,
126 		     "Could not allocate stream to print failed assertion in %s:%d\n",
127 		     assert->file,
128 		     assert->line);
129 		return;
130 	}
131 
132 	assert->format(assert, stream);
133 
134 	kunit_print_string_stream(test, stream);
135 
136 	WARN_ON(string_stream_destroy(stream));
137 }
138 
139 static void __noreturn kunit_abort(struct kunit *test)
140 {
141 	kunit_try_catch_throw(&test->try_catch); /* Does not return. */
142 
143 	/*
144 	 * Throw could not abort from test.
145 	 *
146 	 * XXX: we should never reach this line! As kunit_try_catch_throw is
147 	 * marked __noreturn.
148 	 */
149 	WARN_ONCE(true, "Throw could not abort from test!\n");
150 }
151 
152 void kunit_do_assertion(struct kunit *test,
153 			struct kunit_assert *assert,
154 			bool pass,
155 			const char *fmt, ...)
156 {
157 	va_list args;
158 
159 	if (pass)
160 		return;
161 
162 	va_start(args, fmt);
163 
164 	assert->message.fmt = fmt;
165 	assert->message.va = &args;
166 
167 	kunit_fail(test, assert);
168 
169 	va_end(args);
170 
171 	if (assert->type == KUNIT_ASSERTION)
172 		kunit_abort(test);
173 }
174 
175 void kunit_init_test(struct kunit *test, const char *name)
176 {
177 	spin_lock_init(&test->lock);
178 	INIT_LIST_HEAD(&test->resources);
179 	test->name = name;
180 	test->success = true;
181 }
182 
183 /*
184  * Initializes and runs test case. Does not clean up or do post validations.
185  */
186 static void kunit_run_case_internal(struct kunit *test,
187 				    struct kunit_suite *suite,
188 				    struct kunit_case *test_case)
189 {
190 	if (suite->init) {
191 		int ret;
192 
193 		ret = suite->init(test);
194 		if (ret) {
195 			kunit_err(test, "failed to initialize: %d\n", ret);
196 			kunit_set_failure(test);
197 			return;
198 		}
199 	}
200 
201 	test_case->run_case(test);
202 }
203 
204 static void kunit_case_internal_cleanup(struct kunit *test)
205 {
206 	kunit_cleanup(test);
207 }
208 
209 /*
210  * Performs post validations and cleanup after a test case was run.
211  * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
212  */
213 static void kunit_run_case_cleanup(struct kunit *test,
214 				   struct kunit_suite *suite)
215 {
216 	if (suite->exit)
217 		suite->exit(test);
218 
219 	kunit_case_internal_cleanup(test);
220 }
221 
222 struct kunit_try_catch_context {
223 	struct kunit *test;
224 	struct kunit_suite *suite;
225 	struct kunit_case *test_case;
226 };
227 
228 static void kunit_try_run_case(void *data)
229 {
230 	struct kunit_try_catch_context *ctx = data;
231 	struct kunit *test = ctx->test;
232 	struct kunit_suite *suite = ctx->suite;
233 	struct kunit_case *test_case = ctx->test_case;
234 
235 	/*
236 	 * kunit_run_case_internal may encounter a fatal error; if it does,
237 	 * abort will be called, this thread will exit, and finally the parent
238 	 * thread will resume control and handle any necessary clean up.
239 	 */
240 	kunit_run_case_internal(test, suite, test_case);
241 	/* This line may never be reached. */
242 	kunit_run_case_cleanup(test, suite);
243 }
244 
245 static void kunit_catch_run_case(void *data)
246 {
247 	struct kunit_try_catch_context *ctx = data;
248 	struct kunit *test = ctx->test;
249 	struct kunit_suite *suite = ctx->suite;
250 	int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
251 
252 	if (try_exit_code) {
253 		kunit_set_failure(test);
254 		/*
255 		 * Test case could not finish, we have no idea what state it is
256 		 * in, so don't do clean up.
257 		 */
258 		if (try_exit_code == -ETIMEDOUT) {
259 			kunit_err(test, "test case timed out\n");
260 		/*
261 		 * Unknown internal error occurred preventing test case from
262 		 * running, so there is nothing to clean up.
263 		 */
264 		} else {
265 			kunit_err(test, "internal error occurred preventing test case from running: %d\n",
266 				  try_exit_code);
267 		}
268 		return;
269 	}
270 
271 	/*
272 	 * Test case was run, but aborted. It is the test case's business as to
273 	 * whether it failed or not, we just need to clean up.
274 	 */
275 	kunit_run_case_cleanup(test, suite);
276 }
277 
278 /*
279  * Performs all logic to run a test case. It also catches most errors that
280  * occur in a test case and reports them as failures.
281  */
282 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
283 					struct kunit_case *test_case)
284 {
285 	struct kunit_try_catch_context context;
286 	struct kunit_try_catch *try_catch;
287 	struct kunit test;
288 
289 	kunit_init_test(&test, test_case->name);
290 	try_catch = &test.try_catch;
291 
292 	kunit_try_catch_init(try_catch,
293 			     &test,
294 			     kunit_try_run_case,
295 			     kunit_catch_run_case);
296 	context.test = &test;
297 	context.suite = suite;
298 	context.test_case = test_case;
299 	kunit_try_catch_run(try_catch, &context);
300 
301 	test_case->success = test.success;
302 }
303 
304 int kunit_run_tests(struct kunit_suite *suite)
305 {
306 	struct kunit_case *test_case;
307 	size_t test_case_count = 1;
308 
309 	kunit_print_subtest_start(suite);
310 
311 	for (test_case = suite->test_cases; test_case->run_case; test_case++) {
312 		kunit_run_case_catch_errors(suite, test_case);
313 		kunit_print_test_case_ok_not_ok(test_case, test_case_count++);
314 	}
315 
316 	kunit_print_subtest_end(suite);
317 
318 	return 0;
319 }
320 
321 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
322 						    kunit_resource_init_t init,
323 						    kunit_resource_free_t free,
324 						    gfp_t internal_gfp,
325 						    void *context)
326 {
327 	struct kunit_resource *res;
328 	int ret;
329 
330 	res = kzalloc(sizeof(*res), internal_gfp);
331 	if (!res)
332 		return NULL;
333 
334 	ret = init(res, context);
335 	if (ret)
336 		return NULL;
337 
338 	res->free = free;
339 	spin_lock(&test->lock);
340 	list_add_tail(&res->node, &test->resources);
341 	spin_unlock(&test->lock);
342 
343 	return res;
344 }
345 
346 static void kunit_resource_free(struct kunit *test, struct kunit_resource *res)
347 {
348 	res->free(res);
349 	kfree(res);
350 }
351 
352 static struct kunit_resource *kunit_resource_find(struct kunit *test,
353 						  kunit_resource_match_t match,
354 						  kunit_resource_free_t free,
355 						  void *match_data)
356 {
357 	struct kunit_resource *resource;
358 
359 	lockdep_assert_held(&test->lock);
360 
361 	list_for_each_entry_reverse(resource, &test->resources, node) {
362 		if (resource->free != free)
363 			continue;
364 		if (match(test, resource->allocation, match_data))
365 			return resource;
366 	}
367 
368 	return NULL;
369 }
370 
371 static struct kunit_resource *kunit_resource_remove(
372 		struct kunit *test,
373 		kunit_resource_match_t match,
374 		kunit_resource_free_t free,
375 		void *match_data)
376 {
377 	struct kunit_resource *resource;
378 
379 	spin_lock(&test->lock);
380 	resource = kunit_resource_find(test, match, free, match_data);
381 	if (resource)
382 		list_del(&resource->node);
383 	spin_unlock(&test->lock);
384 
385 	return resource;
386 }
387 
388 int kunit_resource_destroy(struct kunit *test,
389 			   kunit_resource_match_t match,
390 			   kunit_resource_free_t free,
391 			   void *match_data)
392 {
393 	struct kunit_resource *resource;
394 
395 	resource = kunit_resource_remove(test, match, free, match_data);
396 
397 	if (!resource)
398 		return -ENOENT;
399 
400 	kunit_resource_free(test, resource);
401 	return 0;
402 }
403 
404 struct kunit_kmalloc_params {
405 	size_t size;
406 	gfp_t gfp;
407 };
408 
409 static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
410 {
411 	struct kunit_kmalloc_params *params = context;
412 
413 	res->allocation = kmalloc(params->size, params->gfp);
414 	if (!res->allocation)
415 		return -ENOMEM;
416 
417 	return 0;
418 }
419 
420 static void kunit_kmalloc_free(struct kunit_resource *res)
421 {
422 	kfree(res->allocation);
423 }
424 
425 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
426 {
427 	struct kunit_kmalloc_params params = {
428 		.size = size,
429 		.gfp = gfp
430 	};
431 
432 	return kunit_alloc_resource(test,
433 				    kunit_kmalloc_init,
434 				    kunit_kmalloc_free,
435 				    gfp,
436 				    &params);
437 }
438 
439 void kunit_kfree(struct kunit *test, const void *ptr)
440 {
441 	int rc;
442 
443 	rc = kunit_resource_destroy(test,
444 				    kunit_resource_instance_match,
445 				    kunit_kmalloc_free,
446 				    (void *)ptr);
447 
448 	WARN_ON(rc);
449 }
450 
451 void kunit_cleanup(struct kunit *test)
452 {
453 	struct kunit_resource *resource;
454 
455 	/*
456 	 * test->resources is a stack - each allocation must be freed in the
457 	 * reverse order from which it was added since one resource may depend
458 	 * on another for its entire lifetime.
459 	 * Also, we cannot use the normal list_for_each constructs, even the
460 	 * safe ones because *arbitrary* nodes may be deleted when
461 	 * kunit_resource_free is called; the list_for_each_safe variants only
462 	 * protect against the current node being deleted, not the next.
463 	 */
464 	while (true) {
465 		spin_lock(&test->lock);
466 		if (list_empty(&test->resources)) {
467 			spin_unlock(&test->lock);
468 			break;
469 		}
470 		resource = list_last_entry(&test->resources,
471 					   struct kunit_resource,
472 					   node);
473 		list_del(&resource->node);
474 		spin_unlock(&test->lock);
475 
476 		kunit_resource_free(test, resource);
477 	}
478 }
479