# \[PATCH v2 08/12\] selftests/livepatch: Test klp-prepare-patch with created and deleted files

- From: Song Liu \<song@kernel.org\>
- Date: 2026-10-28 21:46:19 
- List: live-patching
- Page: https://ratatoskr.run/live-patching/2026/10/17823398
- Thread: https://ratatoskr.run/live-patching/2026/10/17823386/t.md

```text
Add a test of patches which create and delete files.  Both are diffed
against /dev/null, and the created file gets no #line directive, as it
has no original line numbers to preserve.  A file created by one patch
and changed by the next is a new file in the combined patch.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@kernel.org>
---
 .../livepatch/test_klp_build/Makefile         |  3 +-
 .../test-new-and-deleted-files.sh             | 94 +++++++++++++++++++
 2 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh

diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 158a08d8a1b8..2818264fb31a 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/Makefile
+++ b/tools/testing/selftests/livepatch/test_klp_build/Makefile
@@ -3,7 +3,8 @@
 TEST_PROGS := \
 	test-basic.sh \
 	test-independent-patches.sh \
-	test-cancelling-patches.sh
+	test-cancelling-patches.sh \
+	test-new-and-deleted-files.sh
 
 TEST_PROGS_EXTENDED := functions.sh
 
diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh b/tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh
new file mode 100755
index 000000000000..560221854b3e
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-new-and-deleted-files.sh
@@ -0,0 +1,94 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(dirname "$0")/functions.sh"
+
+setup_tree
+
+add_file drivers/foo/old.c <<'EOT'
+int old(void)
+{
+	return 0;
+}
+EOT
+
+# - created and deleted files are diffed against /dev/null, and a created
+#   file gets no #line directive since it has no original line numbers
+
+start_test "created and deleted files"
+
+add_patch 0001-new-and-old.patch <<'EOT'
+--- /dev/null
++++ b/drivers/foo/new.c
+@@ -0,0 +1,4 @@
++int new(void)
++{
++	return 1;
++}
+--- a/drivers/foo/old.c
++++ /dev/null
+@@ -1,4 +0,0 @@
+-int old(void)
+-{
+-	return 0;
+-}
+EOT
+
+run_klp_prepare_patch 0001-new-and-old.patch
+
+expect_patch <<'EOT'
+--- /dev/null
++++ b/drivers/foo/new.c
+@@ -0,0 +1,4 @@
++int new(void)
++{
++	return 1;
++}
+--- a/drivers/foo/old.c
++++ /dev/null
+@@ -1,4 +0,0 @@
+-int old(void)
+-{
+-	return 0;
+-}
+EOT
+
+# - a file created by one patch and changed by the next is a new file in the
+#   combined patch
+
+start_test "created file changed by a later patch"
+
+add_patch 0001-new.patch <<'EOT'
+--- /dev/null
++++ b/drivers/foo/new.c
+@@ -0,0 +1,4 @@
++int new(void)
++{
++	return 1;
++}
+EOT
+
+add_patch 0002-new-again.patch <<'EOT'
+--- a/drivers/foo/new.c
++++ b/drivers/foo/new.c
+@@ -1,4 +1,4 @@
+ int new(void)
+ {
+-	return 1;
++	return 2;
+ }
+EOT
+
+run_klp_prepare_patch 0001-new.patch 0002-new-again.patch
+
+expect_patch <<'EOT'
+--- /dev/null
++++ b/drivers/foo/new.c
+@@ -0,0 +1,4 @@
++int new(void)
++{
++	return 2;
++}
+EOT
+
+exit 0
-- 
2.53.0-Meta

```
