|
| 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 | +#ifndef _KUNIT_TEST_H |
| 10 | +#define _KUNIT_TEST_H |
| 11 | + |
| 12 | +#include <linux/types.h> |
| 13 | + |
| 14 | +struct kunit; |
| 15 | + |
| 16 | +/** |
| 17 | + * struct kunit_case - represents an individual test case. |
| 18 | + * |
| 19 | + * @run_case: the function representing the actual test case. |
| 20 | + * @name: the name of the test case. |
| 21 | + * |
| 22 | + * A test case is a function with the signature, |
| 23 | + * ``void (*)(struct kunit *)`` that makes expectations (see |
| 24 | + * KUNIT_EXPECT_TRUE()) about code under test. Each test case is associated |
| 25 | + * with a &struct kunit_suite and will be run after the suite's init |
| 26 | + * function and followed by the suite's exit function. |
| 27 | + * |
| 28 | + * A test case should be static and should only be created with the |
| 29 | + * KUNIT_CASE() macro; additionally, every array of test cases should be |
| 30 | + * terminated with an empty test case. |
| 31 | + * |
| 32 | + * Example: |
| 33 | + * |
| 34 | + * .. code-block:: c |
| 35 | + * |
| 36 | + * void add_test_basic(struct kunit *test) |
| 37 | + * { |
| 38 | + * KUNIT_EXPECT_EQ(test, 1, add(1, 0)); |
| 39 | + * KUNIT_EXPECT_EQ(test, 2, add(1, 1)); |
| 40 | + * KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); |
| 41 | + * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); |
| 42 | + * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); |
| 43 | + * } |
| 44 | + * |
| 45 | + * static struct kunit_case example_test_cases[] = { |
| 46 | + * KUNIT_CASE(add_test_basic), |
| 47 | + * {} |
| 48 | + * }; |
| 49 | + * |
| 50 | + */ |
| 51 | +struct kunit_case { |
| 52 | + void (*run_case)(struct kunit *test); |
| 53 | + const char *name; |
| 54 | + |
| 55 | + /* private: internal use only. */ |
| 56 | + bool success; |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * KUNIT_CASE - A helper for creating a &struct kunit_case |
| 61 | + * |
| 62 | + * @test_name: a reference to a test case function. |
| 63 | + * |
| 64 | + * Takes a symbol for a function representing a test case and creates a |
| 65 | + * &struct kunit_case object from it. See the documentation for |
| 66 | + * &struct kunit_case for an example on how to use it. |
| 67 | + */ |
| 68 | +#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } |
| 69 | + |
| 70 | +/** |
| 71 | + * struct kunit_suite - describes a related collection of &struct kunit_case |
| 72 | + * |
| 73 | + * @name: the name of the test. Purely informational. |
| 74 | + * @init: called before every test case. |
| 75 | + * @exit: called after every test case. |
| 76 | + * @test_cases: a null terminated array of test cases. |
| 77 | + * |
| 78 | + * A kunit_suite is a collection of related &struct kunit_case s, such that |
| 79 | + * @init is called before every test case and @exit is called after every |
| 80 | + * test case, similar to the notion of a *test fixture* or a *test class* |
| 81 | + * in other unit testing frameworks like JUnit or Googletest. |
| 82 | + * |
| 83 | + * Every &struct kunit_case must be associated with a kunit_suite for KUnit |
| 84 | + * to run it. |
| 85 | + */ |
| 86 | +struct kunit_suite { |
| 87 | + const char name[256]; |
| 88 | + int (*init)(struct kunit *test); |
| 89 | + void (*exit)(struct kunit *test); |
| 90 | + struct kunit_case *test_cases; |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * struct kunit - represents a running instance of a test. |
| 95 | + * |
| 96 | + * @priv: for user to store arbitrary data. Commonly used to pass data |
| 97 | + * created in the init function (see &struct kunit_suite). |
| 98 | + * |
| 99 | + * Used to store information about the current context under which the test |
| 100 | + * is running. Most of this data is private and should only be accessed |
| 101 | + * indirectly via public functions; the one exception is @priv which can be |
| 102 | + * used by the test writer to store arbitrary data. |
| 103 | + */ |
| 104 | +struct kunit { |
| 105 | + void *priv; |
| 106 | + |
| 107 | + /* private: internal use only. */ |
| 108 | + const char *name; /* Read only after initialization! */ |
| 109 | + /* |
| 110 | + * success starts as true, and may only be set to false during a |
| 111 | + * test case; thus, it is safe to update this across multiple |
| 112 | + * threads using WRITE_ONCE; however, as a consequence, it may only |
| 113 | + * be read after the test case finishes once all threads associated |
| 114 | + * with the test case have terminated. |
| 115 | + */ |
| 116 | + bool success; /* Read only after test_case finishes! */ |
| 117 | +}; |
| 118 | + |
| 119 | +void kunit_init_test(struct kunit *test, const char *name); |
| 120 | + |
| 121 | +int kunit_run_tests(struct kunit_suite *suite); |
| 122 | + |
| 123 | +/** |
| 124 | + * kunit_test_suite() - used to register a &struct kunit_suite with KUnit. |
| 125 | + * |
| 126 | + * @suite: a statically allocated &struct kunit_suite. |
| 127 | + * |
| 128 | + * Registers @suite with the test framework. See &struct kunit_suite for |
| 129 | + * more information. |
| 130 | + * |
| 131 | + * NOTE: Currently KUnit tests are all run as late_initcalls; this means |
| 132 | + * that they cannot test anything where tests must run at a different init |
| 133 | + * phase. One significant restriction resulting from this is that KUnit |
| 134 | + * cannot reliably test anything that is initialize in the late_init phase; |
| 135 | + * another is that KUnit is useless to test things that need to be run in |
| 136 | + * an earlier init phase. |
| 137 | + * |
| 138 | + * TODO(brendanhiggins@google.com): Don't run all KUnit tests as |
| 139 | + * late_initcalls. I have some future work planned to dispatch all KUnit |
| 140 | + * tests from the same place, and at the very least to do so after |
| 141 | + * everything else is definitely initialized. |
| 142 | + */ |
| 143 | +#define kunit_test_suite(suite) \ |
| 144 | + static int kunit_suite_init##suite(void) \ |
| 145 | + { \ |
| 146 | + return kunit_run_tests(&suite); \ |
| 147 | + } \ |
| 148 | + late_initcall(kunit_suite_init##suite) |
| 149 | + |
| 150 | +void __printf(3, 4) kunit_printk(const char *level, |
| 151 | + const struct kunit *test, |
| 152 | + const char *fmt, ...); |
| 153 | + |
| 154 | +/** |
| 155 | + * kunit_info() - Prints an INFO level message associated with @test. |
| 156 | + * |
| 157 | + * @test: The test context object. |
| 158 | + * @fmt: A printk() style format string. |
| 159 | + * |
| 160 | + * Prints an info level message associated with the test suite being run. |
| 161 | + * Takes a variable number of format parameters just like printk(). |
| 162 | + */ |
| 163 | +#define kunit_info(test, fmt, ...) \ |
| 164 | + kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) |
| 165 | + |
| 166 | +/** |
| 167 | + * kunit_warn() - Prints a WARN level message associated with @test. |
| 168 | + * |
| 169 | + * @test: The test context object. |
| 170 | + * @fmt: A printk() style format string. |
| 171 | + * |
| 172 | + * Prints a warning level message. |
| 173 | + */ |
| 174 | +#define kunit_warn(test, fmt, ...) \ |
| 175 | + kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) |
| 176 | + |
| 177 | +/** |
| 178 | + * kunit_err() - Prints an ERROR level message associated with @test. |
| 179 | + * |
| 180 | + * @test: The test context object. |
| 181 | + * @fmt: A printk() style format string. |
| 182 | + * |
| 183 | + * Prints an error level message. |
| 184 | + */ |
| 185 | +#define kunit_err(test, fmt, ...) \ |
| 186 | + kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) |
| 187 | + |
| 188 | +#endif /* _KUNIT_TEST_H */ |
0 commit comments