From 51104c19d8570eb23208e08eac0e9ae09ced1c15 Mon Sep 17 00:00:00 2001 From: Kees Cook Date: Wed, 12 Jun 2024 12:59:18 -0700 Subject: kunit: test: Add vm_mmap() allocation resource manager For tests that need to allocate using vm_mmap() (e.g. usercopy and execve), provide the interface to have the allocation tracked by KUnit itself. This requires bringing up a placeholder userspace mm. This combines my earlier attempt at this with Mark Rutland's version[1]. Normally alloc_mm() and arch_pick_mmap_layout() aren't exported for modules, so export these only for KUnit testing. Link: https://lore.kernel.org/lkml/20230321122514.1743889-2-mark.rutland@arm.com/ [1] Co-developed-by: Mark Rutland Signed-off-by: Mark Rutland Reviewed-by: David Gow Signed-off-by: Kees Cook Signed-off-by: Shuah Khan --- include/kunit/test.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'include/kunit/test.h') diff --git a/include/kunit/test.h b/include/kunit/test.h index e32b4cb7afa2..ec61cad6b71d 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -480,6 +480,23 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO); } +/** + * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area + * @test: The test context object. + * @file: struct file pointer to map from, if any + * @addr: desired address, if any + * @len: how many bytes to allocate + * @prot: mmap PROT_* bits + * @flag: mmap flags + * @offset: offset into @file to start mapping from. + * + * See vm_mmap() for more information. + */ +unsigned long kunit_vm_mmap(struct kunit *test, struct file *file, + unsigned long addr, unsigned long len, + unsigned long prot, unsigned long flag, + unsigned long offset); + void kunit_cleanup(struct kunit *test); void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...); -- cgit v1.2.3-58-ga151 From 2be32bbe6989e071aea1767cb54c7528a0dac07f Mon Sep 17 00:00:00 2001 From: Eric Chan Date: Thu, 11 Jul 2024 19:39:17 +0000 Subject: kunit: Fix the comment of KUNIT_ASSERT_STRNEQ as assertion The current comment for KUNIT_ASSERT_STRNEQ incorrectly describes it as an expectation. Since KUNIT_ASSERT_STRNEQ is an assertion, updates the comment to correctly refer to it as such. Signed-off-by: Eric Chan Reviewed-by: David Gow Signed-off-by: Shuah Khan --- include/kunit/test.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/kunit/test.h') diff --git a/include/kunit/test.h b/include/kunit/test.h index ec61cad6b71d..e37df1df5a92 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1455,12 +1455,12 @@ do { \ ##__VA_ARGS__) /** - * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. + * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal. * @test: The test context object. * @left: an arbitrary expression that evaluates to a null terminated string. * @right: an arbitrary expression that evaluates to a null terminated string. * - * Sets an expectation that the values that @left and @right evaluate to are + * Sets an assertion that the values that @left and @right evaluate to are * not equal. This is semantically equivalent to * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() * for more information. -- cgit v1.2.3-58-ga151 From 7d4087b013895524438f2522367c984f776e09e3 Mon Sep 17 00:00:00 2001 From: Eric Chan Date: Thu, 11 Jul 2024 19:39:31 +0000 Subject: kunit: Rename KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT for readability Both KUNIT_FAIL and KUNIT_ASSERT_FAILURE defined to KUNIT_FAIL_ASSERTION with different tpye of kunit_assert_type. The current naming of KUNIT_ASSERT_FAILURE and KUNIT_FAIL_ASSERTION is confusing due to their similarities. To improve readability and symmetry, renames KUNIT_ASSERT_FAILURE to KUNIT_FAIL_AND_ABORT. Makes the naming consistent, with KUNIT_FAIL and KUNIT_FAIL_AND_ABORT being symmetrical. Additionally, an explanation for KUNIT_FAIL_AND_ABORT has been added to clarify its usage. Signed-off-by: Eric Chan Reviewed-by: David Gow Signed-off-by: Shuah Khan --- drivers/input/tests/input_test.c | 2 +- include/kunit/assert.h | 2 +- include/kunit/test.h | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) (limited to 'include/kunit/test.h') diff --git a/drivers/input/tests/input_test.c b/drivers/input/tests/input_test.c index 2fa5b725ae0a..9711ced180b8 100644 --- a/drivers/input/tests/input_test.c +++ b/drivers/input/tests/input_test.c @@ -31,7 +31,7 @@ static int input_test_init(struct kunit *test) ret = input_register_device(input_dev); if (ret) { input_free_device(input_dev); - KUNIT_ASSERT_FAILURE(test, "Register device failed: %d", ret); + KUNIT_FAIL_AND_ABORT(test, "Register device failed: %d", ret); } test->priv = input_dev; diff --git a/include/kunit/assert.h b/include/kunit/assert.h index 7e7490a74b13..bb879389f11d 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -60,7 +60,7 @@ void kunit_assert_prologue(const struct kunit_loc *loc, * struct kunit_fail_assert - Represents a plain fail expectation/assertion. * @assert: The parent of this type. * - * Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails. + * Represents a simple KUNIT_FAIL/KUNIT_FAIL_AND_ABORT that always fails. */ struct kunit_fail_assert { struct kunit_assert assert; diff --git a/include/kunit/test.h b/include/kunit/test.h index e37df1df5a92..fb58fa530c5e 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1228,7 +1228,18 @@ do { \ fmt, \ ##__VA_ARGS__) -#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \ +/** + * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated. + * @test: The test context object. + * @fmt: an informational message to be printed when the assertion is made. + * @...: string format arguments. + * + * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In + * other words, it always results in a failed assertion, and consequently + * always causes the test case to fail and abort when evaluated. + * See KUNIT_ASSERT_TRUE() for more information. + */ +#define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \ KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) /** -- cgit v1.2.3-58-ga151 From ebf51e460e488511d9ee60b07d00dac68883facf Mon Sep 17 00:00:00 2001 From: Eric Chan Date: Thu, 11 Jul 2024 19:39:45 +0000 Subject: kunit: Introduce KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros Introduces KUNIT_ASSERT_MEMEQ and KUNIT_ASSERT_MEMNEQ macros to provide assert-type equivalents for memory comparison. While KUNIT_EXPECT_MEMEQ and KUNIT_EXPECT_MEMNEQ are available for expectations, the addition of these new macros ensures that assertions can also be used for memory comparisons, enhancing the consistency and completeness of the kunit framework. Signed-off-by: Eric Chan Reviewed-by: David Gow Signed-off-by: Shuah Khan --- include/kunit/test.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'include/kunit/test.h') diff --git a/include/kunit/test.h b/include/kunit/test.h index fb58fa530c5e..e2a1f0928e8b 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1486,6 +1486,60 @@ do { \ fmt, \ ##__VA_ARGS__) +/** + * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal. + * @test: The test context object. + * @left: An arbitrary expression that evaluates to the specified size. + * @right: An arbitrary expression that evaluates to the specified size. + * @size: Number of bytes compared. + * + * Sets an assertion that the values that @left and @right evaluate to are + * equal. This is semantically equivalent to + * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See + * KUNIT_ASSERT_TRUE() for more information. + * + * Although this assertion works for any memory block, it is not recommended + * for comparing more structured data, such as structs. This assertion is + * recommended for comparing, for example, data arrays. + */ +#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \ + KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL) + +#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \ + KUNIT_MEM_ASSERTION(test, \ + KUNIT_ASSERTION, \ + left, ==, right, \ + size, \ + fmt, \ + ##__VA_ARGS__) + +/** + * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal. + * @test: The test context object. + * @left: An arbitrary expression that evaluates to the specified size. + * @right: An arbitrary expression that evaluates to the specified size. + * @size: Number of bytes compared. + * + * Sets an assertion that the values that @left and @right evaluate to are + * not equal. This is semantically equivalent to + * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See + * KUNIT_ASSERT_TRUE() for more information. + * + * Although this assertion works for any memory block, it is not recommended + * for comparing more structured data, such as structs. This assertion is + * recommended for comparing, for example, data arrays. + */ +#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \ + KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL) + +#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \ + KUNIT_MEM_ASSERTION(test, \ + KUNIT_ASSERTION, \ + left, !=, right, \ + size, \ + fmt, \ + ##__VA_ARGS__) + /** * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null. * @test: The test context object. -- cgit v1.2.3-58-ga151