Add KUnit tests for copy_page() and copy_mc_page(), modeled after
the existing memcpy_test() style: a static page-aligned src and a
two-page dst, filled with random bytes plus non-zero edges, then
verify byte-for-byte equality and that the adjacent page is
untouched. The copy_mc_page() case additionally checks the return
value is 0 on clean memory and is gated on CONFIG_ARCH_HAS_COPY_MC.

Signed-off-by: Ruidong Tian <[email protected]>
---
 lib/tests/memcpy_kunit.c | 67 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c
index d36933554e46..85df53ccfb0c 100644
--- a/lib/tests/memcpy_kunit.c
+++ b/lib/tests/memcpy_kunit.c
@@ -493,6 +493,67 @@ static void memmove_overlap_test(struct kunit *test)
        }
 }
 
+/* --- Page-sized copy tests --- */
+
+static u8 page_src[PAGE_SIZE] __aligned(PAGE_SIZE);
+static u8 page_dst[PAGE_SIZE * 2] __aligned(PAGE_SIZE);
+static const u8 page_zero[PAGE_SIZE] __aligned(PAGE_SIZE);
+
+static void init_page(struct kunit *test)
+{
+       /* Get many bit patterns. */
+       get_random_bytes(page_src, PAGE_SIZE);
+
+       /* Make sure we have non-zero edges. */
+       set_random_nonzero(test, &page_src[0]);
+       set_random_nonzero(test, &page_src[PAGE_SIZE - 1]);
+
+       /* Explicitly zero the entire destination. */
+       memset(page_dst, 0, ARRAY_SIZE(page_dst));
+}
+
+static void copy_page_test(struct kunit *test)
+{
+       init_page(test);
+
+       /* Copy. */
+       copy_page(page_dst, page_src);
+
+       /* Verify byte-for-byte exact. */
+       KUNIT_ASSERT_EQ_MSG(test,
+               memcmp(page_dst, page_src, PAGE_SIZE), 0,
+               "copy_page content mismatch with random data");
+
+       /* Verify no overflow into second page. */
+       KUNIT_ASSERT_EQ_MSG(test,
+               memcmp(page_dst + PAGE_SIZE, page_zero, PAGE_SIZE), 0,
+               "copy_page overflow into adjacent page");
+}
+
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+static void copy_mc_page_test(struct kunit *test)
+{
+       int ret;
+
+       init_page(test);
+
+       /* Copy and check return value. */
+       ret = copy_mc_page(page_dst, page_src);
+       KUNIT_ASSERT_EQ_MSG(test, ret, 0,
+               "copy_mc_page returned %d on clean memory", ret);
+
+       /* Verify byte-for-byte exact. */
+       KUNIT_ASSERT_EQ_MSG(test,
+               memcmp(page_dst, page_src, PAGE_SIZE), 0,
+               "copy_mc_page content mismatch with random data");
+
+       /* Verify no overflow into second page. */
+       KUNIT_ASSERT_EQ_MSG(test,
+               memcmp(page_dst + PAGE_SIZE, page_zero, PAGE_SIZE), 0,
+               "copy_mc_page overflow into adjacent page");
+}
+#endif /* CONFIG_ARCH_HAS_COPY_MC */
+
 static struct kunit_case memcpy_test_cases[] = {
        KUNIT_CASE(memset_test),
        KUNIT_CASE(memcpy_test),
@@ -500,6 +561,10 @@ static struct kunit_case memcpy_test_cases[] = {
        KUNIT_CASE_SLOW(memmove_test),
        KUNIT_CASE_SLOW(memmove_large_test),
        KUNIT_CASE_SLOW(memmove_overlap_test),
+       KUNIT_CASE(copy_page_test),
+#ifdef CONFIG_ARCH_HAS_COPY_MC
+       KUNIT_CASE(copy_mc_page_test),
+#endif
        {}
 };
 
@@ -510,5 +575,5 @@ static struct kunit_suite memcpy_test_suite = {
 
 kunit_test_suite(memcpy_test_suite);
 
-MODULE_DESCRIPTION("test cases for memcpy(), memmove(), and memset()");
+MODULE_DESCRIPTION("test cases for memcpy(), memmove(), memset() and 
copy_page()");
 MODULE_LICENSE("GPL");
-- 
2.39.3


Reply via email to