Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Assertion and expectation serialization API.
0004  *
0005  * Copyright (C) 2019, Google LLC.
0006  * Author: Brendan Higgins <brendanhiggins@google.com>
0007  */
0008 
0009 #ifndef _KUNIT_ASSERT_H
0010 #define _KUNIT_ASSERT_H
0011 
0012 #include <linux/err.h>
0013 #include <linux/printk.h>
0014 
0015 struct kunit;
0016 struct string_stream;
0017 
0018 /**
0019  * enum kunit_assert_type - Type of expectation/assertion.
0020  * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
0021  * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
0022  *
0023  * Used in conjunction with a &struct kunit_assert to denote whether it
0024  * represents an expectation or an assertion.
0025  */
0026 enum kunit_assert_type {
0027     KUNIT_ASSERTION,
0028     KUNIT_EXPECTATION,
0029 };
0030 
0031 /**
0032  * struct kunit_loc - Identifies the source location of a line of code.
0033  * @line: the line number in the file.
0034  * @file: the file name.
0035  */
0036 struct kunit_loc {
0037     int line;
0038     const char *file;
0039 };
0040 
0041 #define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
0042 
0043 /**
0044  * struct kunit_assert - Data for printing a failed assertion or expectation.
0045  * @format: a function which formats the data in this kunit_assert to a string.
0046  *
0047  * Represents a failed expectation/assertion. Contains all the data necessary to
0048  * format a string to a user reporting the failure.
0049  */
0050 struct kunit_assert {
0051     void (*format)(const struct kunit_assert *assert,
0052                const struct va_format *message,
0053                struct string_stream *stream);
0054 };
0055 
0056 void kunit_assert_prologue(const struct kunit_loc *loc,
0057                enum kunit_assert_type type,
0058                struct string_stream *stream);
0059 
0060 /**
0061  * struct kunit_fail_assert - Represents a plain fail expectation/assertion.
0062  * @assert: The parent of this type.
0063  *
0064  * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
0065  */
0066 struct kunit_fail_assert {
0067     struct kunit_assert assert;
0068 };
0069 
0070 void kunit_fail_assert_format(const struct kunit_assert *assert,
0071                   const struct va_format *message,
0072                   struct string_stream *stream);
0073 
0074 /**
0075  * KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
0076  *
0077  * Initializes a &struct kunit_fail_assert. Intended to be used in
0078  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
0079  */
0080 #define KUNIT_INIT_FAIL_ASSERT_STRUCT {                 \
0081     .assert = { .format = kunit_fail_assert_format },       \
0082 }
0083 
0084 /**
0085  * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
0086  * @assert: The parent of this type.
0087  * @condition: A string representation of a conditional expression.
0088  * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
0089  *
0090  * Represents a simple expectation or assertion that simply asserts something is
0091  * true or false. In other words, represents the expectations:
0092  * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
0093  */
0094 struct kunit_unary_assert {
0095     struct kunit_assert assert;
0096     const char *condition;
0097     bool expected_true;
0098 };
0099 
0100 void kunit_unary_assert_format(const struct kunit_assert *assert,
0101                    const struct va_format *message,
0102                    struct string_stream *stream);
0103 
0104 /**
0105  * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_assert.
0106  * @cond: A string representation of the expression asserted true or false.
0107  * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
0108  *
0109  * Initializes a &struct kunit_unary_assert. Intended to be used in
0110  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
0111  */
0112 #define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) {            \
0113     .assert = { .format = kunit_unary_assert_format },             \
0114     .condition = cond,                             \
0115     .expected_true = expect_true                           \
0116 }
0117 
0118 /**
0119  * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
0120  *  not NULL and not a -errno.
0121  * @assert: The parent of this type.
0122  * @text: A string representation of the expression passed to the expectation.
0123  * @value: The actual evaluated pointer value of the expression.
0124  *
0125  * Represents an expectation/assertion that a pointer is not null and is does
0126  * not contain a -errno. (See IS_ERR_OR_NULL().)
0127  */
0128 struct kunit_ptr_not_err_assert {
0129     struct kunit_assert assert;
0130     const char *text;
0131     const void *value;
0132 };
0133 
0134 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
0135                      const struct va_format *message,
0136                      struct string_stream *stream);
0137 
0138 /**
0139  * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a
0140  *  &struct kunit_ptr_not_err_assert.
0141  * @txt: A string representation of the expression passed to the expectation.
0142  * @val: The actual evaluated pointer value of the expression.
0143  *
0144  * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in
0145  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
0146  */
0147 #define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) {                  \
0148     .assert = { .format = kunit_ptr_not_err_assert_format },           \
0149     .text = txt,                                   \
0150     .value = val                                   \
0151 }
0152 
0153 /**
0154  * struct kunit_binary_assert_text - holds strings for &struct
0155  *  kunit_binary_assert and friends to try and make the structs smaller.
0156  * @operation: A string representation of the comparison operator (e.g. "==").
0157  * @left_text: A string representation of the left expression (e.g. "2+2").
0158  * @right_text: A string representation of the right expression (e.g. "2+2").
0159  */
0160 struct kunit_binary_assert_text {
0161     const char *operation;
0162     const char *left_text;
0163     const char *right_text;
0164 };
0165 
0166 /**
0167  * struct kunit_binary_assert - An expectation/assertion that compares two
0168  *  non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
0169  * @assert: The parent of this type.
0170  * @text: Holds the textual representations of the operands and op (e.g.  "==").
0171  * @left_value: The actual evaluated value of the expression in the left slot.
0172  * @right_value: The actual evaluated value of the expression in the right slot.
0173  *
0174  * Represents an expectation/assertion that compares two non-pointer values. For
0175  * example, to expect that 1 + 1 == 2, you can use the expectation
0176  * KUNIT_EXPECT_EQ(test, 1 + 1, 2);
0177  */
0178 struct kunit_binary_assert {
0179     struct kunit_assert assert;
0180     const struct kunit_binary_assert_text *text;
0181     long long left_value;
0182     long long right_value;
0183 };
0184 
0185 void kunit_binary_assert_format(const struct kunit_assert *assert,
0186                 const struct va_format *message,
0187                 struct string_stream *stream);
0188 
0189 /**
0190  * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
0191  *  kunit_binary_assert, kunit_binary_ptr_assert, etc.
0192  *
0193  * @format_func: a function which formats the assert to a string.
0194  * @text_: Pointer to a kunit_binary_assert_text.
0195  * @left_val: The actual evaluated value of the expression in the left slot.
0196  * @right_val: The actual evaluated value of the expression in the right slot.
0197  *
0198  * Initializes a binary assert like kunit_binary_assert,
0199  * kunit_binary_ptr_assert, etc. This relies on these structs having the same
0200  * fields but with different types for left_val/right_val.
0201  * This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
0202  */
0203 #define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func,                   \
0204                     text_,                     \
0205                     left_val,                  \
0206                     right_val) {                   \
0207     .assert = { .format = format_func },                       \
0208     .text = text_,                                 \
0209     .left_value = left_val,                            \
0210     .right_value = right_val                           \
0211 }
0212 
0213 /**
0214  * struct kunit_binary_ptr_assert - An expectation/assertion that compares two
0215  *  pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
0216  * @assert: The parent of this type.
0217  * @text: Holds the textual representations of the operands and op (e.g.  "==").
0218  * @left_value: The actual evaluated value of the expression in the left slot.
0219  * @right_value: The actual evaluated value of the expression in the right slot.
0220  *
0221  * Represents an expectation/assertion that compares two pointer values. For
0222  * example, to expect that foo and bar point to the same thing, you can use the
0223  * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
0224  */
0225 struct kunit_binary_ptr_assert {
0226     struct kunit_assert assert;
0227     const struct kunit_binary_assert_text *text;
0228     const void *left_value;
0229     const void *right_value;
0230 };
0231 
0232 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
0233                     const struct va_format *message,
0234                     struct string_stream *stream);
0235 
0236 /**
0237  * struct kunit_binary_str_assert - An expectation/assertion that compares two
0238  *  string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
0239  * @assert: The parent of this type.
0240  * @text: Holds the textual representations of the operands and comparator.
0241  * @left_value: The actual evaluated value of the expression in the left slot.
0242  * @right_value: The actual evaluated value of the expression in the right slot.
0243  *
0244  * Represents an expectation/assertion that compares two string values. For
0245  * example, to expect that the string in foo is equal to "bar", you can use the
0246  * expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
0247  */
0248 struct kunit_binary_str_assert {
0249     struct kunit_assert assert;
0250     const struct kunit_binary_assert_text *text;
0251     const char *left_value;
0252     const char *right_value;
0253 };
0254 
0255 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
0256                     const struct va_format *message,
0257                     struct string_stream *stream);
0258 
0259 #endif /*  _KUNIT_ASSERT_H */