162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Assertion and expectation serialization API. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2019, Google LLC. 662306a36Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#ifndef _KUNIT_ASSERT_H 1062306a36Sopenharmony_ci#define _KUNIT_ASSERT_H 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/err.h> 1362306a36Sopenharmony_ci#include <linux/printk.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_cistruct kunit; 1662306a36Sopenharmony_cistruct string_stream; 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci/** 1962306a36Sopenharmony_ci * enum kunit_assert_type - Type of expectation/assertion. 2062306a36Sopenharmony_ci * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion. 2162306a36Sopenharmony_ci * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation. 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * Used in conjunction with a &struct kunit_assert to denote whether it 2462306a36Sopenharmony_ci * represents an expectation or an assertion. 2562306a36Sopenharmony_ci */ 2662306a36Sopenharmony_cienum kunit_assert_type { 2762306a36Sopenharmony_ci KUNIT_ASSERTION, 2862306a36Sopenharmony_ci KUNIT_EXPECTATION, 2962306a36Sopenharmony_ci}; 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/** 3262306a36Sopenharmony_ci * struct kunit_loc - Identifies the source location of a line of code. 3362306a36Sopenharmony_ci * @line: the line number in the file. 3462306a36Sopenharmony_ci * @file: the file name. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_cistruct kunit_loc { 3762306a36Sopenharmony_ci int line; 3862306a36Sopenharmony_ci const char *file; 3962306a36Sopenharmony_ci}; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ } 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci/** 4462306a36Sopenharmony_ci * struct kunit_assert - Data for printing a failed assertion or expectation. 4562306a36Sopenharmony_ci * 4662306a36Sopenharmony_ci * Represents a failed expectation/assertion. Contains all the data necessary to 4762306a36Sopenharmony_ci * format a string to a user reporting the failure. 4862306a36Sopenharmony_ci */ 4962306a36Sopenharmony_cistruct kunit_assert {}; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_citypedef void (*assert_format_t)(const struct kunit_assert *assert, 5262306a36Sopenharmony_ci const struct va_format *message, 5362306a36Sopenharmony_ci struct string_stream *stream); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_civoid kunit_assert_prologue(const struct kunit_loc *loc, 5662306a36Sopenharmony_ci enum kunit_assert_type type, 5762306a36Sopenharmony_ci struct string_stream *stream); 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci/** 6062306a36Sopenharmony_ci * struct kunit_fail_assert - Represents a plain fail expectation/assertion. 6162306a36Sopenharmony_ci * @assert: The parent of this type. 6262306a36Sopenharmony_ci * 6362306a36Sopenharmony_ci * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails. 6462306a36Sopenharmony_ci */ 6562306a36Sopenharmony_cistruct kunit_fail_assert { 6662306a36Sopenharmony_ci struct kunit_assert assert; 6762306a36Sopenharmony_ci}; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_civoid kunit_fail_assert_format(const struct kunit_assert *assert, 7062306a36Sopenharmony_ci const struct va_format *message, 7162306a36Sopenharmony_ci struct string_stream *stream); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci/** 7462306a36Sopenharmony_ci * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE} 7562306a36Sopenharmony_ci * @assert: The parent of this type. 7662306a36Sopenharmony_ci * @condition: A string representation of a conditional expression. 7762306a36Sopenharmony_ci * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise. 7862306a36Sopenharmony_ci * 7962306a36Sopenharmony_ci * Represents a simple expectation or assertion that simply asserts something is 8062306a36Sopenharmony_ci * true or false. In other words, represents the expectations: 8162306a36Sopenharmony_ci * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE} 8262306a36Sopenharmony_ci */ 8362306a36Sopenharmony_cistruct kunit_unary_assert { 8462306a36Sopenharmony_ci struct kunit_assert assert; 8562306a36Sopenharmony_ci const char *condition; 8662306a36Sopenharmony_ci bool expected_true; 8762306a36Sopenharmony_ci}; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_civoid kunit_unary_assert_format(const struct kunit_assert *assert, 9062306a36Sopenharmony_ci const struct va_format *message, 9162306a36Sopenharmony_ci struct string_stream *stream); 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci/** 9462306a36Sopenharmony_ci * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is 9562306a36Sopenharmony_ci * not NULL and not a -errno. 9662306a36Sopenharmony_ci * @assert: The parent of this type. 9762306a36Sopenharmony_ci * @text: A string representation of the expression passed to the expectation. 9862306a36Sopenharmony_ci * @value: The actual evaluated pointer value of the expression. 9962306a36Sopenharmony_ci * 10062306a36Sopenharmony_ci * Represents an expectation/assertion that a pointer is not null and is does 10162306a36Sopenharmony_ci * not contain a -errno. (See IS_ERR_OR_NULL().) 10262306a36Sopenharmony_ci */ 10362306a36Sopenharmony_cistruct kunit_ptr_not_err_assert { 10462306a36Sopenharmony_ci struct kunit_assert assert; 10562306a36Sopenharmony_ci const char *text; 10662306a36Sopenharmony_ci const void *value; 10762306a36Sopenharmony_ci}; 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_civoid kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, 11062306a36Sopenharmony_ci const struct va_format *message, 11162306a36Sopenharmony_ci struct string_stream *stream); 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci/** 11462306a36Sopenharmony_ci * struct kunit_binary_assert_text - holds strings for &struct 11562306a36Sopenharmony_ci * kunit_binary_assert and friends to try and make the structs smaller. 11662306a36Sopenharmony_ci * @operation: A string representation of the comparison operator (e.g. "=="). 11762306a36Sopenharmony_ci * @left_text: A string representation of the left expression (e.g. "2+2"). 11862306a36Sopenharmony_ci * @right_text: A string representation of the right expression (e.g. "2+2"). 11962306a36Sopenharmony_ci */ 12062306a36Sopenharmony_cistruct kunit_binary_assert_text { 12162306a36Sopenharmony_ci const char *operation; 12262306a36Sopenharmony_ci const char *left_text; 12362306a36Sopenharmony_ci const char *right_text; 12462306a36Sopenharmony_ci}; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci/** 12762306a36Sopenharmony_ci * struct kunit_binary_assert - An expectation/assertion that compares two 12862306a36Sopenharmony_ci * non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)). 12962306a36Sopenharmony_ci * @assert: The parent of this type. 13062306a36Sopenharmony_ci * @text: Holds the textual representations of the operands and op (e.g. "=="). 13162306a36Sopenharmony_ci * @left_value: The actual evaluated value of the expression in the left slot. 13262306a36Sopenharmony_ci * @right_value: The actual evaluated value of the expression in the right slot. 13362306a36Sopenharmony_ci * 13462306a36Sopenharmony_ci * Represents an expectation/assertion that compares two non-pointer values. For 13562306a36Sopenharmony_ci * example, to expect that 1 + 1 == 2, you can use the expectation 13662306a36Sopenharmony_ci * KUNIT_EXPECT_EQ(test, 1 + 1, 2); 13762306a36Sopenharmony_ci */ 13862306a36Sopenharmony_cistruct kunit_binary_assert { 13962306a36Sopenharmony_ci struct kunit_assert assert; 14062306a36Sopenharmony_ci const struct kunit_binary_assert_text *text; 14162306a36Sopenharmony_ci long long left_value; 14262306a36Sopenharmony_ci long long right_value; 14362306a36Sopenharmony_ci}; 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_civoid kunit_binary_assert_format(const struct kunit_assert *assert, 14662306a36Sopenharmony_ci const struct va_format *message, 14762306a36Sopenharmony_ci struct string_stream *stream); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/** 15062306a36Sopenharmony_ci * struct kunit_binary_ptr_assert - An expectation/assertion that compares two 15162306a36Sopenharmony_ci * pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)). 15262306a36Sopenharmony_ci * @assert: The parent of this type. 15362306a36Sopenharmony_ci * @text: Holds the textual representations of the operands and op (e.g. "=="). 15462306a36Sopenharmony_ci * @left_value: The actual evaluated value of the expression in the left slot. 15562306a36Sopenharmony_ci * @right_value: The actual evaluated value of the expression in the right slot. 15662306a36Sopenharmony_ci * 15762306a36Sopenharmony_ci * Represents an expectation/assertion that compares two pointer values. For 15862306a36Sopenharmony_ci * example, to expect that foo and bar point to the same thing, you can use the 15962306a36Sopenharmony_ci * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar); 16062306a36Sopenharmony_ci */ 16162306a36Sopenharmony_cistruct kunit_binary_ptr_assert { 16262306a36Sopenharmony_ci struct kunit_assert assert; 16362306a36Sopenharmony_ci const struct kunit_binary_assert_text *text; 16462306a36Sopenharmony_ci const void *left_value; 16562306a36Sopenharmony_ci const void *right_value; 16662306a36Sopenharmony_ci}; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_civoid kunit_binary_ptr_assert_format(const struct kunit_assert *assert, 16962306a36Sopenharmony_ci const struct va_format *message, 17062306a36Sopenharmony_ci struct string_stream *stream); 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci/** 17362306a36Sopenharmony_ci * struct kunit_binary_str_assert - An expectation/assertion that compares two 17462306a36Sopenharmony_ci * string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")). 17562306a36Sopenharmony_ci * @assert: The parent of this type. 17662306a36Sopenharmony_ci * @text: Holds the textual representations of the operands and comparator. 17762306a36Sopenharmony_ci * @left_value: The actual evaluated value of the expression in the left slot. 17862306a36Sopenharmony_ci * @right_value: The actual evaluated value of the expression in the right slot. 17962306a36Sopenharmony_ci * 18062306a36Sopenharmony_ci * Represents an expectation/assertion that compares two string values. For 18162306a36Sopenharmony_ci * example, to expect that the string in foo is equal to "bar", you can use the 18262306a36Sopenharmony_ci * expectation KUNIT_EXPECT_STREQ(test, foo, "bar"); 18362306a36Sopenharmony_ci */ 18462306a36Sopenharmony_cistruct kunit_binary_str_assert { 18562306a36Sopenharmony_ci struct kunit_assert assert; 18662306a36Sopenharmony_ci const struct kunit_binary_assert_text *text; 18762306a36Sopenharmony_ci const char *left_value; 18862306a36Sopenharmony_ci const char *right_value; 18962306a36Sopenharmony_ci}; 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_civoid kunit_binary_str_assert_format(const struct kunit_assert *assert, 19262306a36Sopenharmony_ci const struct va_format *message, 19362306a36Sopenharmony_ci struct string_stream *stream); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci/** 19662306a36Sopenharmony_ci * struct kunit_mem_assert - An expectation/assertion that compares two 19762306a36Sopenharmony_ci * memory blocks. 19862306a36Sopenharmony_ci * @assert: The parent of this type. 19962306a36Sopenharmony_ci * @text: Holds the textual representations of the operands and comparator. 20062306a36Sopenharmony_ci * @left_value: The actual evaluated value of the expression in the left slot. 20162306a36Sopenharmony_ci * @right_value: The actual evaluated value of the expression in the right slot. 20262306a36Sopenharmony_ci * @size: Size of the memory block analysed in bytes. 20362306a36Sopenharmony_ci * 20462306a36Sopenharmony_ci * Represents an expectation/assertion that compares two memory blocks. For 20562306a36Sopenharmony_ci * example, to expect that the first three bytes of foo is equal to the 20662306a36Sopenharmony_ci * first three bytes of bar, you can use the expectation 20762306a36Sopenharmony_ci * KUNIT_EXPECT_MEMEQ(test, foo, bar, 3); 20862306a36Sopenharmony_ci */ 20962306a36Sopenharmony_cistruct kunit_mem_assert { 21062306a36Sopenharmony_ci struct kunit_assert assert; 21162306a36Sopenharmony_ci const struct kunit_binary_assert_text *text; 21262306a36Sopenharmony_ci const void *left_value; 21362306a36Sopenharmony_ci const void *right_value; 21462306a36Sopenharmony_ci const size_t size; 21562306a36Sopenharmony_ci}; 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_civoid kunit_mem_assert_format(const struct kunit_assert *assert, 21862306a36Sopenharmony_ci const struct va_format *message, 21962306a36Sopenharmony_ci struct string_stream *stream); 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci#endif /* _KUNIT_ASSERT_H */ 222