xref: /linux-6.15/include/kunit/assert.h (revision 05a7da89)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Assertion and expectation serialization API.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: Brendan Higgins <[email protected]>
7  */
8 
9 #ifndef _KUNIT_ASSERT_H
10 #define _KUNIT_ASSERT_H
11 
12 #include <linux/err.h>
13 #include <linux/printk.h>
14 
15 struct kunit;
16 struct string_stream;
17 
18 /**
19  * enum kunit_assert_type - Type of expectation/assertion.
20  * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
21  * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
22  *
23  * Used in conjunction with a &struct kunit_assert to denote whether it
24  * represents an expectation or an assertion.
25  */
26 enum kunit_assert_type {
27 	KUNIT_ASSERTION,
28 	KUNIT_EXPECTATION,
29 };
30 
31 /**
32  * struct kunit_loc - Identifies the source location of a line of code.
33  * @line: the line number in the file.
34  * @file: the file name.
35  */
36 struct kunit_loc {
37 	int line;
38 	const char *file;
39 };
40 
41 #define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
42 
43 /**
44  * struct kunit_assert - Data for printing a failed assertion or expectation.
45  * @message: an optional message to provide additional context.
46  * @format: a function which formats the data in this kunit_assert to a string.
47  *
48  * Represents a failed expectation/assertion. Contains all the data necessary to
49  * format a string to a user reporting the failure.
50  */
51 struct kunit_assert {
52 	struct va_format message;
53 	void (*format)(const struct kunit_assert *assert,
54 		       struct string_stream *stream);
55 };
56 
57 /**
58  * KUNIT_INIT_VA_FMT_NULL - Default initializer for struct va_format.
59  *
60  * Used inside a struct initialization block to initialize struct va_format to
61  * default values where fmt and va are null.
62  */
63 #define KUNIT_INIT_VA_FMT_NULL { .fmt = NULL, .va = NULL }
64 
65 /**
66  * KUNIT_INIT_ASSERT_STRUCT() - Initializer for a &struct kunit_assert.
67  * @fmt: The formatting function which builds a string out of this kunit_assert.
68  *
69  * The base initializer for a &struct kunit_assert.
70  */
71 #define KUNIT_INIT_ASSERT_STRUCT(fmt) {					       \
72 	.message = KUNIT_INIT_VA_FMT_NULL,				       \
73 	.format = fmt							       \
74 }
75 
76 void kunit_assert_prologue(const struct kunit_loc *loc,
77 			   enum kunit_assert_type type,
78 			   struct string_stream *stream);
79 
80 void kunit_assert_print_msg(const struct kunit_assert *assert,
81 			    struct string_stream *stream);
82 
83 /**
84  * struct kunit_fail_assert - Represents a plain fail expectation/assertion.
85  * @assert: The parent of this type.
86  *
87  * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
88  */
89 struct kunit_fail_assert {
90 	struct kunit_assert assert;
91 };
92 
93 void kunit_fail_assert_format(const struct kunit_assert *assert,
94 			      struct string_stream *stream);
95 
96 /**
97  * KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
98  *
99  * Initializes a &struct kunit_fail_assert. Intended to be used in
100  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
101  */
102 #define KUNIT_INIT_FAIL_ASSERT_STRUCT {					\
103 	.assert = KUNIT_INIT_ASSERT_STRUCT(kunit_fail_assert_format)	\
104 }
105 
106 /**
107  * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
108  * @assert: The parent of this type.
109  * @condition: A string representation of a conditional expression.
110  * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
111  *
112  * Represents a simple expectation or assertion that simply asserts something is
113  * true or false. In other words, represents the expectations:
114  * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
115  */
116 struct kunit_unary_assert {
117 	struct kunit_assert assert;
118 	const char *condition;
119 	bool expected_true;
120 };
121 
122 void kunit_unary_assert_format(const struct kunit_assert *assert,
123 			       struct string_stream *stream);
124 
125 /**
126  * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_assert.
127  * @cond: A string representation of the expression asserted true or false.
128  * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
129  *
130  * Initializes a &struct kunit_unary_assert. Intended to be used in
131  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
132  */
133 #define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) {		       \
134 	.assert = KUNIT_INIT_ASSERT_STRUCT(kunit_unary_assert_format),	       \
135 	.condition = cond,						       \
136 	.expected_true = expect_true					       \
137 }
138 
139 /**
140  * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
141  *	not NULL and not a -errno.
142  * @assert: The parent of this type.
143  * @text: A string representation of the expression passed to the expectation.
144  * @value: The actual evaluated pointer value of the expression.
145  *
146  * Represents an expectation/assertion that a pointer is not null and is does
147  * not contain a -errno. (See IS_ERR_OR_NULL().)
148  */
149 struct kunit_ptr_not_err_assert {
150 	struct kunit_assert assert;
151 	const char *text;
152 	const void *value;
153 };
154 
155 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
156 				     struct string_stream *stream);
157 
158 /**
159  * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a
160  *	&struct kunit_ptr_not_err_assert.
161  * @txt: A string representation of the expression passed to the expectation.
162  * @val: The actual evaluated pointer value of the expression.
163  *
164  * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in
165  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
166  */
167 #define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) {			       \
168 	.assert = KUNIT_INIT_ASSERT_STRUCT(kunit_ptr_not_err_assert_format),   \
169 	.text = txt,							       \
170 	.value = val							       \
171 }
172 
173 /**
174  * struct kunit_binary_assert - An expectation/assertion that compares two
175  *	non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
176  * @assert: The parent of this type.
177  * @operation: A string representation of the comparison operator (e.g. "==").
178  * @left_text: A string representation of the expression in the left slot.
179  * @left_value: The actual evaluated value of the expression in the left slot.
180  * @right_text: A string representation of the expression in the right slot.
181  * @right_value: The actual evaluated value of the expression in the right slot.
182  *
183  * Represents an expectation/assertion that compares two non-pointer values. For
184  * example, to expect that 1 + 1 == 2, you can use the expectation
185  * KUNIT_EXPECT_EQ(test, 1 + 1, 2);
186  */
187 struct kunit_binary_assert {
188 	struct kunit_assert assert;
189 	const char *operation;
190 	const char *left_text;
191 	long long left_value;
192 	const char *right_text;
193 	long long right_value;
194 };
195 
196 void kunit_binary_assert_format(const struct kunit_assert *assert,
197 				struct string_stream *stream);
198 
199 /**
200  * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a
201  *	&struct kunit_binary_assert.
202  * @op_str: A string representation of the comparison operator (e.g. "==").
203  * @left_str: A string representation of the expression in the left slot.
204  * @left_val: The actual evaluated value of the expression in the left slot.
205  * @right_str: A string representation of the expression in the right slot.
206  * @right_val: The actual evaluated value of the expression in the right slot.
207  *
208  * Initializes a &struct kunit_binary_assert. Intended to be used in
209  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
210  */
211 #define KUNIT_INIT_BINARY_ASSERT_STRUCT(op_str,				       \
212 					left_str,			       \
213 					left_val,			       \
214 					right_str,			       \
215 					right_val) {			       \
216 	.assert = KUNIT_INIT_ASSERT_STRUCT(kunit_binary_assert_format),	       \
217 	.operation = op_str,						       \
218 	.left_text = left_str,						       \
219 	.left_value = left_val,						       \
220 	.right_text = right_str,					       \
221 	.right_value = right_val					       \
222 }
223 
224 /**
225  * struct kunit_binary_ptr_assert - An expectation/assertion that compares two
226  *	pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
227  * @assert: The parent of this type.
228  * @operation: A string representation of the comparison operator (e.g. "==").
229  * @left_text: A string representation of the expression in the left slot.
230  * @left_value: The actual evaluated value of the expression in the left slot.
231  * @right_text: A string representation of the expression in the right slot.
232  * @right_value: The actual evaluated value of the expression in the right slot.
233  *
234  * Represents an expectation/assertion that compares two pointer values. For
235  * example, to expect that foo and bar point to the same thing, you can use the
236  * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
237  */
238 struct kunit_binary_ptr_assert {
239 	struct kunit_assert assert;
240 	const char *operation;
241 	const char *left_text;
242 	const void *left_value;
243 	const char *right_text;
244 	const void *right_value;
245 };
246 
247 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
248 				    struct string_stream *stream);
249 
250 /**
251  * KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT() - Initializes a
252  *	&struct kunit_binary_ptr_assert.
253  * @type: The type (assertion or expectation) of this kunit_assert.
254  * @op_str: A string representation of the comparison operator (e.g. "==").
255  * @left_str: A string representation of the expression in the left slot.
256  * @left_val: The actual evaluated value of the expression in the left slot.
257  * @right_str: A string representation of the expression in the right slot.
258  * @right_val: The actual evaluated value of the expression in the right slot.
259  *
260  * Initializes a &struct kunit_binary_ptr_assert. Intended to be used in
261  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
262  */
263 #define KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT(op_str,			       \
264 					    left_str,			       \
265 					    left_val,			       \
266 					    right_str,			       \
267 					    right_val) {		       \
268 	.assert = KUNIT_INIT_ASSERT_STRUCT(kunit_binary_ptr_assert_format),    \
269 	.operation = op_str,						       \
270 	.left_text = left_str,						       \
271 	.left_value = left_val,						       \
272 	.right_text = right_str,					       \
273 	.right_value = right_val					       \
274 }
275 
276 /**
277  * struct kunit_binary_str_assert - An expectation/assertion that compares two
278  *	string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
279  * @assert: The parent of this type.
280  * @operation: A string representation of the comparison operator (e.g. "==").
281  * @left_text: A string representation of the expression in the left slot.
282  * @left_value: The actual evaluated value of the expression in the left slot.
283  * @right_text: A string representation of the expression in the right slot.
284  * @right_value: The actual evaluated value of the expression in the right slot.
285  *
286  * Represents an expectation/assertion that compares two string values. For
287  * example, to expect that the string in foo is equal to "bar", you can use the
288  * expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
289  */
290 struct kunit_binary_str_assert {
291 	struct kunit_assert assert;
292 	const char *operation;
293 	const char *left_text;
294 	const char *left_value;
295 	const char *right_text;
296 	const char *right_value;
297 };
298 
299 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
300 				    struct string_stream *stream);
301 
302 /**
303  * KUNIT_INIT_BINARY_STR_ASSERT_STRUCT() - Initializes a
304  *	&struct kunit_binary_str_assert.
305  * @op_str: A string representation of the comparison operator (e.g. "==").
306  * @left_str: A string representation of the expression in the left slot.
307  * @left_val: The actual evaluated value of the expression in the left slot.
308  * @right_str: A string representation of the expression in the right slot.
309  * @right_val: The actual evaluated value of the expression in the right slot.
310  *
311  * Initializes a &struct kunit_binary_str_assert. Intended to be used in
312  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
313  */
314 #define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(op_str,			       \
315 					    left_str,			       \
316 					    left_val,			       \
317 					    right_str,			       \
318 					    right_val) {		       \
319 	.assert = KUNIT_INIT_ASSERT_STRUCT(kunit_binary_str_assert_format),    \
320 	.operation = op_str,						       \
321 	.left_text = left_str,						       \
322 	.left_value = left_val,						       \
323 	.right_text = right_str,					       \
324 	.right_value = right_val					       \
325 }
326 
327 #endif /*  _KUNIT_ASSERT_H */
328