[PATCH v2 11/12] selftests/livepatch: Test klp-prepare-patch with patches which don't apply

From
Song Liu <song@kernel.org>
Date
2026-10-28 21:46:34
Also in
linux-kselftest, lkml
Reply to
Song Liu, 2026-10-28
Add a test that klp-prepare-patch rejects a patch which doesn't match
the source, and that when the second patch of a series doesn't apply,
the first one is reverted, leaving the source tree as it was.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
 .../livepatch/test_klp_build/Makefile         |  3 +-
 .../test_klp_build/test-patch-not-applying.sh | 73 +++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh
diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 02feb1ad52d0..3650de4d6975 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/Makefile
+++ b/tools/testing/selftests/livepatch/test_klp_build/Makefile
@@ -6,7 +6,8 @@ TEST_PROGS := \
 	test-cancelling-patches.sh \
 	test-new-and-deleted-files.sh \
 	test-line-directives.sh \
-	test-unsupported-files.sh
+	test-unsupported-files.sh \
+	test-patch-not-applying.sh
 
 TEST_PROGS_EXTENDED := functions.sh
 
diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh b/tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh
new file mode 100755
index 000000000000..32e871dc7ffc
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-patch-not-applying.sh
@@ -0,0 +1,73 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(dirname "$0")/functions.sh"
+
+setup_tree
+
+add_file drivers/foo/foo.c <<'EOT'
+int foo(int x)
+{
+	if (x < 0)
+		return -1;
+	return x;
+}
+EOT
+
+# - a patch which doesn't match the source is rejected
+
+start_test "patch which doesn't apply"
+
+add_patch 0001-bad.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -1,6 +1,6 @@
+ int foo(int x)
+ {
+-	if (x > 0)
++	if (x >= 0)
+ 		return -1;
+ 	return x;
+ }
+EOT
+
+run_klp_prepare_patch 0001-bad.patch
+
+expect_error "$PATCH_DIR/0001-bad.patch did not apply"
+
+# - when a later patch doesn't apply, the earlier ones are reverted
+
+start_test "series whose second patch doesn't apply"
+
+add_patch 0001-foo.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -1,6 +1,8 @@
+ int foo(int x)
+ {
++	if (!x)
++		return 0;
+ 	if (x < 0)
+ 		return -1;
+ 	return x;
+ }
+EOT
+
+add_patch 0002-bad.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -3,6 +3,6 @@
+ 	if (!x)
+ 		return 0;
+-	if (x > 0)
++	if (x >= 0)
+ 		return -1;
+ 	return x;
+ }
+EOT
+
+run_klp_prepare_patch 0001-foo.patch 0002-bad.patch
+
+expect_error "$PATCH_DIR/0002-bad.patch did not apply"
+
+exit 0
-- 
2.53.0-Meta
Thread (18 messages) 18 messages, 4 authors, last reply 4d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help