Skip to content

Commit b8a926b

Browse files
mairacanalshuahkh
authored andcommitted
kunit: Introduce KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ macros
Currently, in order to compare memory blocks in KUnit, the KUNIT_EXPECT_EQ or KUNIT_EXPECT_FALSE macros are used in conjunction with the memcmp function, such as: KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0); Although this usage produces correct results for the test cases, when the expectation fails, the error message is not very helpful, indicating only the return of the memcmp function. Therefore, create a new set of macros KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ that compare memory blocks until a specified size. In case of expectation failure, those macros print the hex dump of the memory blocks, making it easier to debug test failures for memory blocks. That said, the expectation KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0); would translate to the expectation KUNIT_EXPECT_MEMEQ(test, foo, bar, size); Signed-off-by: Maíra Canal <mairacanal@riseup.net> Reviewed-by: Daniel Latypov <dlatypov@google.com> Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent f13ecba commit b8a926b

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

include/kunit/assert.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,37 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
240240
const struct va_format *message,
241241
struct string_stream *stream);
242242

243+
#define KUNIT_INIT_MEM_ASSERT_STRUCT(text_, left_val, right_val, size_) { \
244+
.text = text_, \
245+
.left_value = left_val, \
246+
.right_value = right_val, \
247+
.size = size_ \
248+
}
249+
250+
/**
251+
* struct kunit_mem_assert - An expectation/assertion that compares two
252+
* memory blocks.
253+
* @assert: The parent of this type.
254+
* @text: Holds the textual representations of the operands and comparator.
255+
* @left_value: The actual evaluated value of the expression in the left slot.
256+
* @right_value: The actual evaluated value of the expression in the right slot.
257+
* @size: Size of the memory block analysed in bytes.
258+
*
259+
* Represents an expectation/assertion that compares two memory blocks. For
260+
* example, to expect that the first three bytes of foo is equal to the
261+
* first three bytes of bar, you can use the expectation
262+
* KUNIT_EXPECT_MEMEQ(test, foo, bar, 3);
263+
*/
264+
struct kunit_mem_assert {
265+
struct kunit_assert assert;
266+
const struct kunit_binary_assert_text *text;
267+
const void *left_value;
268+
const void *right_value;
269+
const size_t size;
270+
};
271+
272+
void kunit_mem_assert_format(const struct kunit_assert *assert,
273+
const struct va_format *message,
274+
struct string_stream *stream);
275+
243276
#endif /* _KUNIT_ASSERT_H */

include/kunit/test.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,39 @@ do { \
658658
##__VA_ARGS__); \
659659
} while (0)
660660

661+
#define KUNIT_MEM_ASSERTION(test, \
662+
assert_type, \
663+
left, \
664+
op, \
665+
right, \
666+
size, \
667+
fmt, \
668+
...) \
669+
do { \
670+
const void *__left = (left); \
671+
const void *__right = (right); \
672+
const size_t __size = (size); \
673+
static const struct kunit_binary_assert_text __text = { \
674+
.operation = #op, \
675+
.left_text = #left, \
676+
.right_text = #right, \
677+
}; \
678+
\
679+
if (likely(memcmp(__left, __right, __size) op 0)) \
680+
break; \
681+
\
682+
_KUNIT_FAILED(test, \
683+
assert_type, \
684+
kunit_mem_assert, \
685+
kunit_mem_assert_format, \
686+
KUNIT_INIT_MEM_ASSERT_STRUCT(&__text, \
687+
__left, \
688+
__right, \
689+
__size), \
690+
fmt, \
691+
##__VA_ARGS__); \
692+
} while (0)
693+
661694
#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
662695
assert_type, \
663696
ptr, \
@@ -928,6 +961,60 @@ do { \
928961
fmt, \
929962
##__VA_ARGS__)
930963

964+
/**
965+
* KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
966+
* @test: The test context object.
967+
* @left: An arbitrary expression that evaluates to the specified size.
968+
* @right: An arbitrary expression that evaluates to the specified size.
969+
* @size: Number of bytes compared.
970+
*
971+
* Sets an expectation that the values that @left and @right evaluate to are
972+
* equal. This is semantically equivalent to
973+
* KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
974+
* KUNIT_EXPECT_TRUE() for more information.
975+
*
976+
* Although this expectation works for any memory block, it is not recommended
977+
* for comparing more structured data, such as structs. This expectation is
978+
* recommended for comparing, for example, data arrays.
979+
*/
980+
#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
981+
KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
982+
983+
#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
984+
KUNIT_MEM_ASSERTION(test, \
985+
KUNIT_EXPECTATION, \
986+
left, ==, right, \
987+
size, \
988+
fmt, \
989+
##__VA_ARGS__)
990+
991+
/**
992+
* KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
993+
* @test: The test context object.
994+
* @left: An arbitrary expression that evaluates to the specified size.
995+
* @right: An arbitrary expression that evaluates to the specified size.
996+
* @size: Number of bytes compared.
997+
*
998+
* Sets an expectation that the values that @left and @right evaluate to are
999+
* not equal. This is semantically equivalent to
1000+
* KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1001+
* KUNIT_EXPECT_TRUE() for more information.
1002+
*
1003+
* Although this expectation works for any memory block, it is not recommended
1004+
* for comparing more structured data, such as structs. This expectation is
1005+
* recommended for comparing, for example, data arrays.
1006+
*/
1007+
#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1008+
KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1009+
1010+
#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1011+
KUNIT_MEM_ASSERTION(test, \
1012+
KUNIT_EXPECTATION, \
1013+
left, !=, right, \
1014+
size, \
1015+
fmt, \
1016+
##__VA_ARGS__)
1017+
9311018
/**
9321019
* KUNIT_EXPECT_NULL() - Expects that @ptr is null.
9331020
* @test: The test context object.

lib/kunit/assert.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,59 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
206206
kunit_assert_print_msg(message, stream);
207207
}
208208
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
209+
210+
/* Adds a hexdump of a buffer to a string_stream comparing it with
211+
* a second buffer. The different bytes are marked with <>.
212+
*/
213+
static void kunit_assert_hexdump(struct string_stream *stream,
214+
const void *buf,
215+
const void *compared_buf,
216+
const size_t len)
217+
{
218+
size_t i;
219+
const u8 *buf1 = buf;
220+
const u8 *buf2 = compared_buf;
221+
222+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT);
223+
224+
for (i = 0; i < len; ++i) {
225+
if (!(i % 16) && i)
226+
string_stream_add(stream, "\n" KUNIT_SUBSUBTEST_INDENT);
227+
228+
if (buf1[i] != buf2[i])
229+
string_stream_add(stream, "<%02x>", buf1[i]);
230+
else
231+
string_stream_add(stream, " %02x ", buf1[i]);
232+
}
233+
}
234+
235+
void kunit_mem_assert_format(const struct kunit_assert *assert,
236+
const struct va_format *message,
237+
struct string_stream *stream)
238+
{
239+
struct kunit_mem_assert *mem_assert;
240+
241+
mem_assert = container_of(assert, struct kunit_mem_assert,
242+
assert);
243+
244+
string_stream_add(stream,
245+
KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
246+
mem_assert->text->left_text,
247+
mem_assert->text->operation,
248+
mem_assert->text->right_text);
249+
250+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s ==\n",
251+
mem_assert->text->left_text);
252+
kunit_assert_hexdump(stream, mem_assert->left_value,
253+
mem_assert->right_value, mem_assert->size);
254+
255+
string_stream_add(stream, "\n");
256+
257+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s ==\n",
258+
mem_assert->text->right_text);
259+
kunit_assert_hexdump(stream, mem_assert->right_value,
260+
mem_assert->left_value, mem_assert->size);
261+
262+
kunit_assert_print_msg(message, stream);
263+
}
264+
EXPORT_SYMBOL_GPL(kunit_mem_assert_format);

0 commit comments

Comments
 (0)