Commit a185a099 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'linux-kselftest-kunit-6.1-rc1-2' of...

Merge tag 'linux-kselftest-kunit-6.1-rc1-2' of git://git.kernel.org/pub/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
parents 661e0096 e98c4f6a
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -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:
+6 −22
Original line number Diff line number Diff line
@@ -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,
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,
@@ -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.
@@ -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					       \
}
@@ -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							       \
}
@@ -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.
@@ -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					       \
+0 −16
Original line number Diff line number Diff line
@@ -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.
+66 −54
Original line number Diff line number Diff line
@@ -473,28 +473,28 @@ 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))) {					       \
#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
	static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;	       \
		struct assert_class __assertion = INITIALIZER;		       \
	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,						       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			false,						       \
		      kunit_fail_assert,				       \
			KUNIT_INIT_FAIL_ASSERT_STRUCT,			       \
		      kunit_fail_assert_format,				       \
		      {},						       \
		      fmt,						       \
		      ##__VA_ARGS__)

@@ -521,14 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test,
			      expected_true,				       \
			      fmt,					       \
			      ...)					       \
	KUNIT_ASSERTION(test,						       \
do {									       \
	if (likely(!!(condition) == !!expected_true))			       \
		break;							       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			!!(condition) == !!expected_true,		       \
		      kunit_unary_assert,				       \
		      kunit_unary_assert_format,			       \
		      KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition,	       \
						     expected_true),	       \
		      fmt,						       \
			##__VA_ARGS__)
		      ##__VA_ARGS__);					       \
} while (0)

#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
	KUNIT_UNARY_ASSERTION(test,					       \
@@ -578,12 +583,14 @@ do { \
		.right_text = #right,					       \
	};								       \
									       \
	KUNIT_ASSERTION(test,						       \
	if (likely(__left op __right))					       \
		break;							       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			__left op __right,				       \
		      assert_class,					       \
			KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func,	       \
							&__text,	       \
		      format_func,					       \
		      KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text,		       \
						      __left,		       \
						      __right),		       \
		      fmt,						       \
@@ -636,12 +643,15 @@ do { \
		.right_text = #right,					       \
	};								       \
									       \
	KUNIT_ASSERTION(test,						       \
	if (likely(strcmp(__left, __right) op 0))			       \
		break;							       \
									       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			strcmp(__left, __right) op 0,			       \
		      kunit_binary_str_assert,				       \
			KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
							&__text,	       \
		      kunit_binary_str_assert_format,			       \
		      KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text,		       \
						      __left,		       \
						      __right),		       \
		      fmt,						       \
@@ -656,12 +666,14 @@ do { \
do {									       \
	const typeof(ptr) __ptr = (ptr);				       \
									       \
	KUNIT_ASSERTION(test,						       \
	if (!IS_ERR_OR_NULL(__ptr))					       \
		break;							       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			!IS_ERR_OR_NULL(__ptr),				       \
		      kunit_ptr_not_err_assert,				       \
			KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr,		       \
						      __ptr),		       \
		      kunit_ptr_not_err_assert_format,			       \
		      KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr),	       \
		      fmt,						       \
		      ##__VA_ARGS__);					       \
} while (0)
+7 −0
Original line number Diff line number Diff line
@@ -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
Loading