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