assert.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <brendanhiggins@google.com>
  7. */
  8. #ifndef _KUNIT_ASSERT_H
  9. #define _KUNIT_ASSERT_H
  10. #include <linux/err.h>
  11. #include <linux/printk.h>
  12. struct kunit;
  13. struct string_stream;
  14. /**
  15. * enum kunit_assert_type - Type of expectation/assertion.
  16. * @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
  17. * @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
  18. *
  19. * Used in conjunction with a &struct kunit_assert to denote whether it
  20. * represents an expectation or an assertion.
  21. */
  22. enum kunit_assert_type {
  23. KUNIT_ASSERTION,
  24. KUNIT_EXPECTATION,
  25. };
  26. /**
  27. * struct kunit_loc - Identifies the source location of a line of code.
  28. * @line: the line number in the file.
  29. * @file: the file name.
  30. */
  31. struct kunit_loc {
  32. int line;
  33. const char *file;
  34. };
  35. #define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
  36. /**
  37. * struct kunit_assert - Data for printing a failed assertion or expectation.
  38. *
  39. * Represents a failed expectation/assertion. Contains all the data necessary to
  40. * format a string to a user reporting the failure.
  41. */
  42. struct kunit_assert {};
  43. typedef void (*assert_format_t)(const struct kunit_assert *assert,
  44. const struct va_format *message,
  45. struct string_stream *stream);
  46. void kunit_assert_prologue(const struct kunit_loc *loc,
  47. enum kunit_assert_type type,
  48. struct string_stream *stream);
  49. /**
  50. * struct kunit_fail_assert - Represents a plain fail expectation/assertion.
  51. * @assert: The parent of this type.
  52. *
  53. * Represents a simple KUNIT_FAIL/KUNIT_FAIL_AND_ABORT that always fails.
  54. */
  55. struct kunit_fail_assert {
  56. struct kunit_assert assert;
  57. };
  58. void kunit_fail_assert_format(const struct kunit_assert *assert,
  59. const struct va_format *message,
  60. struct string_stream *stream);
  61. /**
  62. * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
  63. * @assert: The parent of this type.
  64. * @condition: A string representation of a conditional expression.
  65. * @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
  66. *
  67. * Represents a simple expectation or assertion that simply asserts something is
  68. * true or false. In other words, represents the expectations:
  69. * KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
  70. */
  71. struct kunit_unary_assert {
  72. struct kunit_assert assert;
  73. const char *condition;
  74. bool expected_true;
  75. };
  76. void kunit_unary_assert_format(const struct kunit_assert *assert,
  77. const struct va_format *message,
  78. struct string_stream *stream);
  79. /**
  80. * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
  81. * not NULL and not a -errno.
  82. * @assert: The parent of this type.
  83. * @text: A string representation of the expression passed to the expectation.
  84. * @value: The actual evaluated pointer value of the expression.
  85. *
  86. * Represents an expectation/assertion that a pointer is not null and is does
  87. * not contain a -errno. (See IS_ERR_OR_NULL().)
  88. */
  89. struct kunit_ptr_not_err_assert {
  90. struct kunit_assert assert;
  91. const char *text;
  92. const void *value;
  93. };
  94. void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
  95. const struct va_format *message,
  96. struct string_stream *stream);
  97. /**
  98. * struct kunit_binary_assert_text - holds strings for &struct
  99. * kunit_binary_assert and friends to try and make the structs smaller.
  100. * @operation: A string representation of the comparison operator (e.g. "==").
  101. * @left_text: A string representation of the left expression (e.g. "2+2").
  102. * @right_text: A string representation of the right expression (e.g. "2+2").
  103. */
  104. struct kunit_binary_assert_text {
  105. const char *operation;
  106. const char *left_text;
  107. const char *right_text;
  108. };
  109. /**
  110. * struct kunit_binary_assert - An expectation/assertion that compares two
  111. * non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
  112. * @assert: The parent of this type.
  113. * @text: Holds the textual representations of the operands and op (e.g. "==").
  114. * @left_value: The actual evaluated value of the expression in the left slot.
  115. * @right_value: The actual evaluated value of the expression in the right slot.
  116. *
  117. * Represents an expectation/assertion that compares two non-pointer values. For
  118. * example, to expect that 1 + 1 == 2, you can use the expectation
  119. * KUNIT_EXPECT_EQ(test, 1 + 1, 2);
  120. */
  121. struct kunit_binary_assert {
  122. struct kunit_assert assert;
  123. const struct kunit_binary_assert_text *text;
  124. long long left_value;
  125. long long right_value;
  126. };
  127. void kunit_binary_assert_format(const struct kunit_assert *assert,
  128. const struct va_format *message,
  129. struct string_stream *stream);
  130. /**
  131. * struct kunit_binary_ptr_assert - An expectation/assertion that compares two
  132. * pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
  133. * @assert: The parent of this type.
  134. * @text: Holds the textual representations of the operands and op (e.g. "==").
  135. * @left_value: The actual evaluated value of the expression in the left slot.
  136. * @right_value: The actual evaluated value of the expression in the right slot.
  137. *
  138. * Represents an expectation/assertion that compares two pointer values. For
  139. * example, to expect that foo and bar point to the same thing, you can use the
  140. * expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
  141. */
  142. struct kunit_binary_ptr_assert {
  143. struct kunit_assert assert;
  144. const struct kunit_binary_assert_text *text;
  145. const void *left_value;
  146. const void *right_value;
  147. };
  148. void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
  149. const struct va_format *message,
  150. struct string_stream *stream);
  151. /**
  152. * struct kunit_binary_str_assert - An expectation/assertion that compares two
  153. * string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
  154. * @assert: The parent of this type.
  155. * @text: Holds the textual representations of the operands and comparator.
  156. * @left_value: The actual evaluated value of the expression in the left slot.
  157. * @right_value: The actual evaluated value of the expression in the right slot.
  158. *
  159. * Represents an expectation/assertion that compares two string values. For
  160. * example, to expect that the string in foo is equal to "bar", you can use the
  161. * expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
  162. */
  163. struct kunit_binary_str_assert {
  164. struct kunit_assert assert;
  165. const struct kunit_binary_assert_text *text;
  166. const char *left_value;
  167. const char *right_value;
  168. };
  169. void kunit_binary_str_assert_format(const struct kunit_assert *assert,
  170. const struct va_format *message,
  171. struct string_stream *stream);
  172. /**
  173. * struct kunit_mem_assert - An expectation/assertion that compares two
  174. * memory blocks.
  175. * @assert: The parent of this type.
  176. * @text: Holds the textual representations of the operands and comparator.
  177. * @left_value: The actual evaluated value of the expression in the left slot.
  178. * @right_value: The actual evaluated value of the expression in the right slot.
  179. * @size: Size of the memory block analysed in bytes.
  180. *
  181. * Represents an expectation/assertion that compares two memory blocks. For
  182. * example, to expect that the first three bytes of foo is equal to the
  183. * first three bytes of bar, you can use the expectation
  184. * KUNIT_EXPECT_MEMEQ(test, foo, bar, 3);
  185. */
  186. struct kunit_mem_assert {
  187. struct kunit_assert assert;
  188. const struct kunit_binary_assert_text *text;
  189. const void *left_value;
  190. const void *right_value;
  191. const size_t size;
  192. };
  193. void kunit_mem_assert_format(const struct kunit_assert *assert,
  194. const struct va_format *message,
  195. struct string_stream *stream);
  196. #if IS_ENABLED(CONFIG_KUNIT)
  197. void kunit_assert_print_msg(const struct va_format *message,
  198. struct string_stream *stream);
  199. bool is_literal(const char *text, long long value);
  200. bool is_str_literal(const char *text, const char *value);
  201. void kunit_assert_hexdump(struct string_stream *stream,
  202. const void *buf,
  203. const void *compared_buf,
  204. const size_t len);
  205. #endif
  206. #endif /* _KUNIT_ASSERT_H */