1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Example KUnit test to show how to use KUnit.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: Brendan Higgins <brendanhiggins@google.com>
7  */
8 
9 #include <kunit/test.h>
10 
11 /*
12  * This is the most fundamental element of KUnit, the test case. A test case
13  * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
14  * any expectations or assertions are not met, the test fails; otherwise, the
15  * test passes.
16  *
17  * In KUnit, a test case is just a function with the signature
18  * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
19  * information about the current test.
20  */
21 static void example_simple_test(struct kunit *test)
22 {
23 	/*
24 	 * This is an EXPECTATION; it is how KUnit tests things. When you want
25 	 * to test a piece of code, you set some expectations about what the
26 	 * code should do. KUnit then runs the test and verifies that the code's
27 	 * behavior matched what was expected.
28 	 */
29 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
30 }
31 
32 /*
33  * This is run once before each test case, see the comment on
34  * example_test_suite for more information.
35  */
36 static int example_test_init(struct kunit *test)
37 {
38 	kunit_info(test, "initializing\n");
39 
40 	return 0;
41 }
42 
43 /*
44  * This is run once before all test cases in the suite.
45  * See the comment on example_test_suite for more information.
46  */
47 static int example_test_init_suite(struct kunit_suite *suite)
48 {
49 	kunit_info(suite, "initializing suite\n");
50 
51 	return 0;
52 }
53 
54 /*
55  * This test should always be skipped.
56  */
57 static void example_skip_test(struct kunit *test)
58 {
59 	/* This line should run */
60 	kunit_info(test, "You should not see a line below.");
61 
62 	/* Skip (and abort) the test */
63 	kunit_skip(test, "this test should be skipped");
64 
65 	/* This line should not execute */
66 	KUNIT_FAIL(test, "You should not see this line.");
67 }
68 
69 /*
70  * This test should always be marked skipped.
71  */
72 static void example_mark_skipped_test(struct kunit *test)
73 {
74 	/* This line should run */
75 	kunit_info(test, "You should see a line below.");
76 
77 	/* Skip (but do not abort) the test */
78 	kunit_mark_skipped(test, "this test should be skipped");
79 
80 	/* This line should run */
81 	kunit_info(test, "You should see this line.");
82 }
83 
84 /*
85  * This test shows off all the types of KUNIT_EXPECT macros.
86  */
87 static void example_all_expect_macros_test(struct kunit *test)
88 {
89 	/* Boolean assertions */
90 	KUNIT_EXPECT_TRUE(test, true);
91 	KUNIT_EXPECT_FALSE(test, false);
92 
93 	/* Integer assertions */
94 	KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
95 	KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
96 	KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
97 	KUNIT_EXPECT_NE(test, 1, 0); /* check != */
98 	KUNIT_EXPECT_GT(test, 1, 0); /* check >  */
99 	KUNIT_EXPECT_LT(test, 0, 1); /* check <  */
100 
101 	/* Pointer assertions */
102 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
103 	KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
104 	KUNIT_EXPECT_PTR_NE(test, test, NULL);
105 	KUNIT_EXPECT_NULL(test, NULL);
106 	KUNIT_EXPECT_NOT_NULL(test, test);
107 
108 	/* String assertions */
109 	KUNIT_EXPECT_STREQ(test, "hi", "hi");
110 	KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
111 
112 	/*
113 	 * There are also ASSERT variants of all of the above that abort test
114 	 * execution if they fail. Useful for memory allocations, etc.
115 	 */
116 	KUNIT_ASSERT_GT(test, sizeof(char), 0);
117 
118 	/*
119 	 * There are also _MSG variants of all of the above that let you include
120 	 * additional text on failure.
121 	 */
122 	KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
123 	KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
124 }
125 
126 /*
127  * Here we make a list of all the test cases we want to add to the test suite
128  * below.
129  */
130 static struct kunit_case example_test_cases[] = {
131 	/*
132 	 * This is a helper to create a test case object from a test case
133 	 * function; its exact function is not important to understand how to
134 	 * use KUnit, just know that this is how you associate test cases with a
135 	 * test suite.
136 	 */
137 	KUNIT_CASE(example_simple_test),
138 	KUNIT_CASE(example_skip_test),
139 	KUNIT_CASE(example_mark_skipped_test),
140 	KUNIT_CASE(example_all_expect_macros_test),
141 	{}
142 };
143 
144 /*
145  * This defines a suite or grouping of tests.
146  *
147  * Test cases are defined as belonging to the suite by adding them to
148  * `kunit_cases`.
149  *
150  * Often it is desirable to run some function which will set up things which
151  * will be used by every test; this is accomplished with an `init` function
152  * which runs before each test case is invoked. Similarly, an `exit` function
153  * may be specified which runs after every test case and can be used to for
154  * cleanup. For clarity, running tests in a test suite would behave as follows:
155  *
156  * suite.suite_init(suite);
157  * suite.init(test);
158  * suite.test_case[0](test);
159  * suite.exit(test);
160  * suite.init(test);
161  * suite.test_case[1](test);
162  * suite.exit(test);
163  * suite.suite_exit(suite);
164  * ...;
165  */
166 static struct kunit_suite example_test_suite = {
167 	.name = "example",
168 	.init = example_test_init,
169 	.suite_init = example_test_init_suite,
170 	.test_cases = example_test_cases,
171 };
172 
173 /*
174  * This registers the above test suite telling KUnit that this is a suite of
175  * tests that need to be run.
176  */
177 kunit_test_suites(&example_test_suite);
178 
179 MODULE_LICENSE("GPL v2");
180