/* SPDX-License-Identifier: GPL-2.0 */1/*2* Assertion and expectation serialization API.3*4* Copyright (C) 2019, Google LLC.5* Author: Brendan Higgins <[email protected]>6*/78#ifndef _KUNIT_ASSERT_H9#define _KUNIT_ASSERT_H1011#include <linux/err.h>12#include <linux/printk.h>1314struct kunit;15struct string_stream;1617/**18* enum kunit_assert_type - Type of expectation/assertion.19* @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.20* @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.21*22* Used in conjunction with a &struct kunit_assert to denote whether it23* represents an expectation or an assertion.24*/25enum kunit_assert_type {26KUNIT_ASSERTION,27KUNIT_EXPECTATION,28};2930/**31* struct kunit_loc - Identifies the source location of a line of code.32* @line: the line number in the file.33* @file: the file name.34*/35struct kunit_loc {36int line;37const char *file;38};3940#define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }4142/**43* struct kunit_assert - Data for printing a failed assertion or expectation.44*45* Represents a failed expectation/assertion. Contains all the data necessary to46* format a string to a user reporting the failure.47*/48struct kunit_assert {};4950typedef void (*assert_format_t)(const struct kunit_assert *assert,51const struct va_format *message,52struct string_stream *stream);5354void kunit_assert_prologue(const struct kunit_loc *loc,55enum kunit_assert_type type,56struct string_stream *stream);5758/**59* struct kunit_fail_assert - Represents a plain fail expectation/assertion.60* @assert: The parent of this type.61*62* Represents a simple KUNIT_FAIL/KUNIT_FAIL_AND_ABORT that always fails.63*/64struct kunit_fail_assert {65struct kunit_assert assert;66};6768void kunit_fail_assert_format(const struct kunit_assert *assert,69const struct va_format *message,70struct string_stream *stream);7172/**73* struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}74* @assert: The parent of this type.75* @condition: A string representation of a conditional expression.76* @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.77*78* Represents a simple expectation or assertion that simply asserts something is79* true or false. In other words, represents the expectations:80* KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}81*/82struct kunit_unary_assert {83struct kunit_assert assert;84const char *condition;85bool expected_true;86};8788void kunit_unary_assert_format(const struct kunit_assert *assert,89const struct va_format *message,90struct string_stream *stream);9192/**93* struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is94* not NULL and not a -errno.95* @assert: The parent of this type.96* @text: A string representation of the expression passed to the expectation.97* @value: The actual evaluated pointer value of the expression.98*99* Represents an expectation/assertion that a pointer is not null and is does100* not contain a -errno. (See IS_ERR_OR_NULL().)101*/102struct kunit_ptr_not_err_assert {103struct kunit_assert assert;104const char *text;105const void *value;106};107108void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,109const struct va_format *message,110struct string_stream *stream);111112/**113* struct kunit_binary_assert_text - holds strings for &struct114* kunit_binary_assert and friends to try and make the structs smaller.115* @operation: A string representation of the comparison operator (e.g. "==").116* @left_text: A string representation of the left expression (e.g. "2+2").117* @right_text: A string representation of the right expression (e.g. "2+2").118*/119struct kunit_binary_assert_text {120const char *operation;121const char *left_text;122const char *right_text;123};124125/**126* struct kunit_binary_assert - An expectation/assertion that compares two127* non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).128* @assert: The parent of this type.129* @text: Holds the textual representations of the operands and op (e.g. "==").130* @left_value: The actual evaluated value of the expression in the left slot.131* @right_value: The actual evaluated value of the expression in the right slot.132*133* Represents an expectation/assertion that compares two non-pointer values. For134* example, to expect that 1 + 1 == 2, you can use the expectation135* KUNIT_EXPECT_EQ(test, 1 + 1, 2);136*/137struct kunit_binary_assert {138struct kunit_assert assert;139const struct kunit_binary_assert_text *text;140long long left_value;141long long right_value;142};143144void kunit_binary_assert_format(const struct kunit_assert *assert,145const struct va_format *message,146struct string_stream *stream);147148/**149* struct kunit_binary_ptr_assert - An expectation/assertion that compares two150* pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).151* @assert: The parent of this type.152* @text: Holds the textual representations of the operands and op (e.g. "==").153* @left_value: The actual evaluated value of the expression in the left slot.154* @right_value: The actual evaluated value of the expression in the right slot.155*156* Represents an expectation/assertion that compares two pointer values. For157* example, to expect that foo and bar point to the same thing, you can use the158* expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);159*/160struct kunit_binary_ptr_assert {161struct kunit_assert assert;162const struct kunit_binary_assert_text *text;163const void *left_value;164const void *right_value;165};166167void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,168const struct va_format *message,169struct string_stream *stream);170171/**172* struct kunit_binary_str_assert - An expectation/assertion that compares two173* string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).174* @assert: The parent of this type.175* @text: Holds the textual representations of the operands and comparator.176* @left_value: The actual evaluated value of the expression in the left slot.177* @right_value: The actual evaluated value of the expression in the right slot.178*179* Represents an expectation/assertion that compares two string values. For180* example, to expect that the string in foo is equal to "bar", you can use the181* expectation KUNIT_EXPECT_STREQ(test, foo, "bar");182*/183struct kunit_binary_str_assert {184struct kunit_assert assert;185const struct kunit_binary_assert_text *text;186const char *left_value;187const char *right_value;188};189190void kunit_binary_str_assert_format(const struct kunit_assert *assert,191const struct va_format *message,192struct string_stream *stream);193194/**195* struct kunit_mem_assert - An expectation/assertion that compares two196* memory blocks.197* @assert: The parent of this type.198* @text: Holds the textual representations of the operands and comparator.199* @left_value: The actual evaluated value of the expression in the left slot.200* @right_value: The actual evaluated value of the expression in the right slot.201* @size: Size of the memory block analysed in bytes.202*203* Represents an expectation/assertion that compares two memory blocks. For204* example, to expect that the first three bytes of foo is equal to the205* first three bytes of bar, you can use the expectation206* KUNIT_EXPECT_MEMEQ(test, foo, bar, 3);207*/208struct kunit_mem_assert {209struct kunit_assert assert;210const struct kunit_binary_assert_text *text;211const void *left_value;212const void *right_value;213const size_t size;214};215216void kunit_mem_assert_format(const struct kunit_assert *assert,217const struct va_format *message,218struct string_stream *stream);219220#if IS_ENABLED(CONFIG_KUNIT)221void kunit_assert_print_msg(const struct va_format *message,222struct string_stream *stream);223bool is_literal(const char *text, long long value);224bool is_str_literal(const char *text, const char *value);225void kunit_assert_hexdump(struct string_stream *stream,226const void *buf,227const void *compared_buf,228const size_t len);229#endif230231#endif /* _KUNIT_ASSERT_H */232233234