All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sam Kent <sam.john.kent@gmail.com>
To: openembedded-core@lists.openembedded.org
Cc: mathieu.dubois-briand@bootlin.com,
	richard.purdie@linuxfoundation.org,
	Sam Kent <sam.john.kent@gmail.com>
Subject: [PATCH v5 2/3] oelib: add unit tests for kernel module detection helpers
Date: Tue,  5 May 2026 19:28:43 +0100	[thread overview]
Message-ID: <20260505182844.1136632-2-sam.john.kent@gmail.com> (raw)
In-Reply-To: <20260505182844.1136632-1-sam.john.kent@gmail.com>

Add tests for the filename pre-filter logic, is_kernel_module(), and
is_kernel_module_signed() to meta/lib/oeqa/selftest/cases/oelib/

Signed-off-by: Sam Kent <sam.john.kent@gmail.com>
---
 meta/lib/oeqa/selftest/cases/oelib/package.py | 108 ++++++++++++++++++
 1 file changed, 108 insertions(+)
 create mode 100644 meta/lib/oeqa/selftest/cases/oelib/package.py

diff --git a/meta/lib/oeqa/selftest/cases/oelib/package.py b/meta/lib/oeqa/selftest/cases/oelib/package.py
new file mode 100644
index 0000000..5751057
--- /dev/null
+++ b/meta/lib/oeqa/selftest/cases/oelib/package.py
@@ -0,0 +1,108 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+
+import mmap
+import os
+import tempfile
+from unittest.case import TestCase
+
+
+class TestKernelModuleFilenameFilter(TestCase):
+    """
+    The pre-filter in process_split_and_strip_files() selects candidates to
+    pass to is_elf(). It must use f.endswith(".ko"), not ".ko" in f, to avoid
+    false-positives on compressed modules (.ko.xz, .ko.gz).
+    """
+
+    ko_files = [
+        "driver.ko",
+        "net/foo.ko",
+    ]
+
+    not_ko_files = [
+        "driver.ko.xz",
+        "driver.ko.gz",
+        "driver.ko2",
+        "myko.c",
+        "vmlinux",
+    ]
+
+    def test_endswith_matches_ko(self):
+        for f in self.ko_files:
+            with self.subTest(f=f):
+                self.assertTrue(f.endswith(".ko"))
+
+    def test_endswith_rejects_non_ko(self):
+        for f in self.not_ko_files:
+            with self.subTest(f=f):
+                self.assertFalse(f.endswith(".ko"))
+
+    def test_old_predicate_had_false_positives(self):
+        # The previous check (".ko" in f) matched compressed modules — this is
+        # the regression the fix addresses.
+        false_positives = [f for f in self.not_ko_files if ".ko" in f]
+        self.assertEqual(false_positives, ["driver.ko.xz", "driver.ko.gz", "driver.ko2"])
+
+
+class TestIsKernelModule(TestCase):
+    """
+    is_kernel_module() detects kernel modules by searching for the
+    "vermagic=" string, which is always present in genuine .ko files.
+    """
+
+    def setUp(self):
+        from oe.package import is_kernel_module
+        self.is_kernel_module = is_kernel_module
+        self._tmpfile = None
+
+    def tearDown(self):
+        if self._tmpfile and os.path.exists(self._tmpfile):
+            os.unlink(self._tmpfile)
+
+    def _make_tmp(self, content):
+        f = tempfile.NamedTemporaryFile(delete=False, suffix=".ko")
+        f.write(content)
+        f.close()
+        return f.name
+
+    def test_detects_vermagic(self):
+        self._tmpfile = self._make_tmp(b"\x7fELF\x00" * 10 + b"vermagic=5.15.0" + b"\x00" * 10)
+        self.assertTrue(self.is_kernel_module(self._tmpfile))
+
+    def test_rejects_plain_elf(self):
+        self._tmpfile = self._make_tmp(b"\x7fELF\x00" * 50)
+        self.assertFalse(self.is_kernel_module(self._tmpfile))
+
+
+class TestIsKernelModuleSigned(TestCase):
+    """
+    is_kernel_module_signed() detects the "Module signature appended" tail
+    that the kernel's modsign infrastructure writes.
+    """
+
+    def setUp(self):
+        from oe.package import is_kernel_module_signed
+        self.is_kernel_module_signed = is_kernel_module_signed
+        self._tmpfile = None
+
+    def tearDown(self):
+        if self._tmpfile and os.path.exists(self._tmpfile):
+            os.unlink(self._tmpfile)
+
+    def _make_tmp(self, content):
+        f = tempfile.NamedTemporaryFile(delete=False, suffix=".ko")
+        f.write(content)
+        f.close()
+        return f.name
+
+    def test_detects_signed(self):
+        tail = b"Module signature appended\n\x00\x00"
+        self._tmpfile = self._make_tmp(b"\x00" * 64 + tail)
+        self.assertTrue(self.is_kernel_module_signed(self._tmpfile))
+
+    def test_rejects_unsigned(self):
+        self._tmpfile = self._make_tmp(b"\x7fELF\x00" * 20)
+        self.assertFalse(self.is_kernel_module_signed(self._tmpfile))
-- 
2.34.1



  reply	other threads:[~2026-05-05 18:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-28  8:10 [PATCH v2 0/3] Fix .ko file pre-filter to use endswith instead of string contains Sam Kent
2026-04-28  8:10 ` [PATCH v2 1/3] package.py: fix kernel module file pre-filter and document strip asymmetry Sam Kent
2026-04-28  8:10 ` [PATCH v2 2/3] oe/package: add unit tests for kernel module detection helpers Sam Kent
2026-04-28 20:48   ` [OE-core] " Richard Purdie
2026-06-04 11:51   ` Richard Purdie
2026-04-28  8:10 ` [PATCH v2 3/3] oeqa/selftest: add oe-selftest for kernel module pre-filter Sam Kent
2026-04-29 12:45 ` [PATCH v3 1/2] oelib: add unit tests for kernel module detection helpers Sam Kent
2026-04-29 12:45   ` [PATCH v3 2/2] oeqa/selftest: add oe-selftest for kernel module pre-filter Sam Kent
2026-05-04  7:59     ` [OE-core] " Mathieu Dubois-Briand
2026-05-04  8:01       ` Mathieu Dubois-Briand
2026-05-05 12:35   ` [OE-core] [PATCH v3 1/2] oelib: add unit tests for kernel module detection helpers Mathieu Dubois-Briand
2026-05-05 17:05   ` [PATCH v4 1/3] oe-pkgdata-util: fix empty runtime-rprovides directory handling Sam Kent
2026-05-05 17:05     ` [PATCH v4 2/3] oelib: add unit tests for kernel module detection helpers Sam Kent
2026-05-05 17:05     ` [PATCH v4 3/3] oeqa/selftest: add oe-selftest for kernel module pre-filter Sam Kent
2026-05-05 17:17     ` Patchtest results for [PATCH v4 1/3] oe-pkgdata-util: fix empty runtime-rprovides directory handling patchtest
2026-05-05 18:28     ` [PATCH v5 " Sam Kent
2026-05-05 18:28       ` Sam Kent [this message]
2026-05-05 18:28       ` [PATCH v5 3/3] oeqa/selftest: add oe-selftest for kernel module pre-filter Sam Kent
2026-06-04 12:04         ` Paul Barker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260505182844.1136632-2-sam.john.kent@gmail.com \
    --to=sam.john.kent@gmail.com \
    --cc=mathieu.dubois-briand@bootlin.com \
    --cc=openembedded-core@lists.openembedded.org \
    --cc=richard.purdie@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.