Skip to content

Commit

Permalink
Merge tag 'linux-kselftest-kunit-6.1-rc1-2' of git://git.kernel.org/p…
Browse files Browse the repository at this point in the history
…ub/scm/linux/kernel/git/shuah/linux-kselftest

Pull more KUnit updates from Shuah Khan:
 "Features and fixes:

   - simplify resource use

   - make kunit_malloc() and kunit_free() allocations and frees
     consistent. kunit_free() frees only the memory allocated by
     kunit_malloc()

   - stop downloading risc-v opensbi binaries using wget

   - other fixes and improvements to tool and KUnit framework"

* tag 'linux-kselftest-kunit-6.1-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest:
  Documentation: kunit: Update description of --alltests option
  kunit: declare kunit_assert structs as const
  kunit: rename base KUNIT_ASSERTION macro to _KUNIT_FAILED
  kunit: remove format func from struct kunit_assert, get it to 0 bytes
  kunit: tool: Don't download risc-v opensbi firmware with wget
  kunit: make kunit_kfree(NULL) a no-op to match kfree()
  kunit: make kunit_kfree() not segfault on invalid inputs
  kunit: make kunit_kfree() only work on pointers from kunit_malloc() and friends
  kunit: drop test pointer in string_stream_fragment
  kunit: string-stream: Simplify resource use
  • Loading branch information
Linus Torvalds committed Oct 12, 2022
2 parents 661e009 + e98c4f6 commit a185a09
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 206 deletions.
17 changes: 9 additions & 8 deletions Documentation/dev-tools/kunit/run_wrapper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,15 @@ command line arguments:
compiling a kernel (using ``build`` or ``run`` commands). For example:
to enable compiler warnings, we can pass ``--make_options W=1``.

- ``--alltests``: Builds a UML kernel with all config options enabled
using ``make allyesconfig``. This allows us to run as many tests as
possible.

.. note:: It is slow and prone to breakage as new options are
added or modified. Instead, enable all tests
which have satisfied dependencies by adding
``CONFIG_KUNIT_ALL_TESTS=y`` to your ``.kunitconfig``.
- ``--alltests``: Enable a predefined set of options in order to build
as many tests as possible.

.. note:: The list of enabled options can be found in
``tools/testing/kunit/configs/all_tests.config``.

If you only want to enable all tests with otherwise satisfied
dependencies, instead add ``CONFIG_KUNIT_ALL_TESTS=y`` to your
``.kunitconfig``.

- ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
file. For example:
Expand Down
28 changes: 6 additions & 22 deletions include/kunit/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ struct kunit_loc {

/**
* struct kunit_assert - Data for printing a failed assertion or expectation.
* @format: a function which formats the data in this kunit_assert to a string.
*
* Represents a failed expectation/assertion. Contains all the data necessary to
* format a string to a user reporting the failure.
*/
struct kunit_assert {
void (*format)(const struct kunit_assert *assert,
const struct va_format *message,
struct string_stream *stream);
};
struct kunit_assert {};

typedef void (*assert_format_t)(const struct kunit_assert *assert,
const struct va_format *message,
struct string_stream *stream);

void kunit_assert_prologue(const struct kunit_loc *loc,
enum kunit_assert_type type,
Expand All @@ -71,16 +70,6 @@ void kunit_fail_assert_format(const struct kunit_assert *assert,
const struct va_format *message,
struct string_stream *stream);

/**
* KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
*
* Initializes a &struct kunit_fail_assert. Intended to be used in
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \
.assert = { .format = kunit_fail_assert_format }, \
}

/**
* struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
* @assert: The parent of this type.
Expand Down Expand Up @@ -110,7 +99,6 @@ void kunit_unary_assert_format(const struct kunit_assert *assert,
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
.assert = { .format = kunit_unary_assert_format }, \
.condition = cond, \
.expected_true = expect_true \
}
Expand Down Expand Up @@ -145,7 +133,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
.assert = { .format = kunit_ptr_not_err_assert_format }, \
.text = txt, \
.value = val \
}
Expand Down Expand Up @@ -190,7 +177,6 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
* KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
* kunit_binary_assert, kunit_binary_ptr_assert, etc.
*
* @format_func: a function which formats the assert to a string.
* @text_: Pointer to a kunit_binary_assert_text.
* @left_val: The actual evaluated value of the expression in the left slot.
* @right_val: The actual evaluated value of the expression in the right slot.
Expand All @@ -200,11 +186,9 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
* fields but with different types for left_val/right_val.
* This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
*/
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
text_, \
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \
left_val, \
right_val) { \
.assert = { .format = format_func }, \
.text = text_, \
.left_value = left_val, \
.right_value = right_val \
Expand Down
16 changes: 0 additions & 16 deletions include/kunit/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,6 @@ typedef bool (*kunit_resource_match_t)(struct kunit *test,
struct kunit_resource *res,
void *match_data);

/**
* kunit_resource_instance_match() - Match a resource with the same instance.
* @test: Test case to which the resource belongs.
* @res: The resource.
* @match_data: The resource pointer to match against.
*
* An instance of kunit_resource_match_t that matches a resource whose
* allocation matches @match_data.
*/
static inline bool kunit_resource_instance_match(struct kunit *test,
struct kunit_resource *res,
void *match_data)
{
return res->data == match_data;
}

/**
* kunit_resource_name_match() - Match a resource with the same name.
* @test: Test case to which the resource belongs.
Expand Down
120 changes: 66 additions & 54 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,30 +473,30 @@ void kunit_do_failed_assertion(struct kunit *test,
const struct kunit_loc *loc,
enum kunit_assert_type type,
const struct kunit_assert *assert,
assert_format_t assert_format,
const char *fmt, ...);

#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
if (unlikely(!(pass))) { \
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
struct assert_class __assertion = INITIALIZER; \
kunit_do_failed_assertion(test, \
&__loc, \
assert_type, \
&__assertion.assert, \
fmt, \
##__VA_ARGS__); \
} \
#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
const struct assert_class __assertion = INITIALIZER; \
kunit_do_failed_assertion(test, \
&__loc, \
assert_type, \
&__assertion.assert, \
assert_format, \
fmt, \
##__VA_ARGS__); \
} while (0)


#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
KUNIT_ASSERTION(test, \
assert_type, \
false, \
kunit_fail_assert, \
KUNIT_INIT_FAIL_ASSERT_STRUCT, \
fmt, \
##__VA_ARGS__)
_KUNIT_FAILED(test, \
assert_type, \
kunit_fail_assert, \
kunit_fail_assert_format, \
{}, \
fmt, \
##__VA_ARGS__)

/**
* KUNIT_FAIL() - Always causes a test to fail when evaluated.
Expand All @@ -521,14 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test,
expected_true, \
fmt, \
...) \
KUNIT_ASSERTION(test, \
assert_type, \
!!(condition) == !!expected_true, \
kunit_unary_assert, \
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
expected_true), \
fmt, \
##__VA_ARGS__)
do { \
if (likely(!!(condition) == !!expected_true)) \
break; \
\
_KUNIT_FAILED(test, \
assert_type, \
kunit_unary_assert, \
kunit_unary_assert_format, \
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
expected_true), \
fmt, \
##__VA_ARGS__); \
} while (0)

#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
KUNIT_UNARY_ASSERTION(test, \
Expand Down Expand Up @@ -578,16 +583,18 @@ do { \
.right_text = #right, \
}; \
\
KUNIT_ASSERTION(test, \
assert_type, \
__left op __right, \
assert_class, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
if (likely(__left op __right)) \
break; \
\
_KUNIT_FAILED(test, \
assert_type, \
assert_class, \
format_func, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
} while (0)

#define KUNIT_BINARY_INT_ASSERTION(test, \
Expand Down Expand Up @@ -636,16 +643,19 @@ do { \
.right_text = #right, \
}; \
\
KUNIT_ASSERTION(test, \
assert_type, \
strcmp(__left, __right) op 0, \
kunit_binary_str_assert, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
if (likely(strcmp(__left, __right) op 0)) \
break; \
\
\
_KUNIT_FAILED(test, \
assert_type, \
kunit_binary_str_assert, \
kunit_binary_str_assert_format, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
__left, \
__right), \
fmt, \
##__VA_ARGS__); \
} while (0)

#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
Expand All @@ -656,14 +666,16 @@ do { \
do { \
const typeof(ptr) __ptr = (ptr); \
\
KUNIT_ASSERTION(test, \
assert_type, \
!IS_ERR_OR_NULL(__ptr), \
kunit_ptr_not_err_assert, \
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
__ptr), \
fmt, \
##__VA_ARGS__); \
if (!IS_ERR_OR_NULL(__ptr)) \
break; \
\
_KUNIT_FAILED(test, \
assert_type, \
kunit_ptr_not_err_assert, \
kunit_ptr_not_err_assert_format, \
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
fmt, \
##__VA_ARGS__); \
} while (0)

/**
Expand Down
7 changes: 7 additions & 0 deletions lib/kunit/kunit-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ static void kunit_resource_test_alloc_resource(struct kunit *test)
kunit_put_resource(res);
}

static inline bool kunit_resource_instance_match(struct kunit *test,
struct kunit_resource *res,
void *match_data)
{
return res->data == match_data;
}

/*
* Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
* they have a reference to the associated resource that they must release
Expand Down
Loading

0 comments on commit a185a09

Please sign in to comment.