Skip to content

Commit 2c7afc2

Browse files
ivanorlov2206shuahkh
authored andcommitted
kunit: Cover 'assert.c' with tests
There are multiple assertion formatting functions in the `assert.c` file, which are not covered with tests yet. Implement the KUnit test for these functions. The test consists of 11 test cases for the following functions: 1) 'is_literal' 2) 'is_str_literal' 3) 'kunit_assert_prologue', test case for multiple assert types 4) 'kunit_assert_print_msg' 5) 'kunit_unary_assert_format' 6) 'kunit_ptr_not_err_assert_format' 7) 'kunit_binary_assert_format' 8) 'kunit_binary_ptr_assert_format' 9) 'kunit_binary_str_assert_format' 10) 'kunit_assert_hexdump' 11) 'kunit_mem_assert_format' The test aims at maximizing the branch coverage for the assertion formatting functions. As you can see, it covers some of the static helper functions as well, so mark the static functions in `assert.c` as 'VISIBLE_IF_KUNIT' and conditionally export them with EXPORT_SYMBOL_IF_KUNIT. Add the corresponding definitions to `assert.h`. Build the assert test when CONFIG_KUNIT_TEST is enabled, similar to how it is done for the string stream test. Signed-off-by: Ivan Orlov <[email protected]> Reviewed-by: Rae Moar <[email protected]> Acked-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 1613e60 commit 2c7afc2

File tree

4 files changed

+411
-8
lines changed

4 files changed

+411
-8
lines changed

include/kunit/assert.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,15 @@ void kunit_mem_assert_format(const struct kunit_assert *assert,
218218
const struct va_format *message,
219219
struct string_stream *stream);
220220

221+
#if IS_ENABLED(CONFIG_KUNIT)
222+
void kunit_assert_print_msg(const struct va_format *message,
223+
struct string_stream *stream);
224+
bool is_literal(const char *text, long long value);
225+
bool is_str_literal(const char *text, const char *value);
226+
void kunit_assert_hexdump(struct string_stream *stream,
227+
const void *buf,
228+
const void *compared_buf,
229+
const size_t len);
230+
#endif
231+
221232
#endif /* _KUNIT_ASSERT_H */

lib/kunit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
2222
# string-stream-test compiles built-in only.
2323
ifeq ($(CONFIG_KUNIT_TEST),y)
2424
obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o
25+
obj-$(CONFIG_KUNIT_TEST) += assert_test.o
2526
endif
2627

2728
obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o

lib/kunit/assert.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#include <kunit/assert.h>
99
#include <kunit/test.h>
10+
#include <kunit/visibility.h>
1011

1112
#include "string-stream.h"
1213

@@ -30,8 +31,9 @@ void kunit_assert_prologue(const struct kunit_loc *loc,
3031
}
3132
EXPORT_SYMBOL_GPL(kunit_assert_prologue);
3233

33-
static void kunit_assert_print_msg(const struct va_format *message,
34-
struct string_stream *stream)
34+
VISIBLE_IF_KUNIT
35+
void kunit_assert_print_msg(const struct va_format *message,
36+
struct string_stream *stream)
3537
{
3638
if (message->fmt)
3739
string_stream_add(stream, "\n%pV", message);
@@ -89,7 +91,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
8991
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
9092

9193
/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */
92-
static bool is_literal(const char *text, long long value)
94+
VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)
9395
{
9496
char *buffer;
9597
int len;
@@ -166,7 +168,7 @@ EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
166168
/* Checks if KUNIT_EXPECT_STREQ() args were string literals.
167169
* Note: `text` will have ""s where as `value` will not.
168170
*/
169-
static bool is_str_literal(const char *text, const char *value)
171+
VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value)
170172
{
171173
int len;
172174

@@ -208,10 +210,11 @@ EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
208210
/* Adds a hexdump of a buffer to a string_stream comparing it with
209211
* a second buffer. The different bytes are marked with <>.
210212
*/
211-
static void kunit_assert_hexdump(struct string_stream *stream,
212-
const void *buf,
213-
const void *compared_buf,
214-
const size_t len)
213+
VISIBLE_IF_KUNIT
214+
void kunit_assert_hexdump(struct string_stream *stream,
215+
const void *buf,
216+
const void *compared_buf,
217+
const size_t len)
215218
{
216219
size_t i;
217220
const u8 *buf1 = buf;

0 commit comments

Comments
 (0)