]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Add test-chid-match 35532/head
authoranonymix007 <48598263+anonymix007@users.noreply.github.com>
Thu, 12 Dec 2024 17:32:24 +0000 (20:32 +0300)
committeranonymix007 <48598263+anonymix007@users.noreply.github.com>
Mon, 16 Dec 2024 18:02:54 +0000 (21:02 +0300)
.github/workflows/linter.yml
src/boot/generate-hwids-section.py [new file with mode: 0644]
src/boot/hwids/device1.json [new file with mode: 0644]
src/boot/hwids/device2.json [new file with mode: 0644]
src/boot/hwids/device3.json [new file with mode: 0644]
src/boot/meson.build
src/boot/test-chid-match.c [new file with mode: 0644]

index 4bcd594a095aa4c7ecc4a1e6a7435c5a251db858..dde1477d5d92c521ba13f0775888ea924925fca3 100644 (file)
@@ -37,7 +37,7 @@ jobs:
           VALIDATE_GITHUB_ACTIONS: true
 
       - name: Check that tabs are not used in Python code
-        run: sh -c '! git grep -P "\\t" -- src/ukify/ukify.py test/integration-test-wrapper.py'
+        run: sh -c '! git grep -P "\\t" -- src/boot/generate-hwids-section.py src/ukify/ukify.py test/integration-test-wrapper.py'
 
       - name: Install ruff and mypy
         run: |
@@ -47,14 +47,14 @@ jobs:
       - name: Run mypy
         run: |
           python3 -m mypy --version
-          python3 -m mypy src/test/generate-sym-test.py src/ukify/ukify.py test/integration-test-wrapper.py
+          python3 -m mypy src/boot/generate-hwids-section.py src/test/generate-sym-test.py src/ukify/ukify.py test/integration-test-wrapper.py
 
       - name: Run ruff check
         run: |
           ruff --version
-          ruff check src/test/generate-sym-test.py src/ukify/ukify.py test/integration-test-wrapper.py
+          ruff check src/boot/generate-hwids-section.py src/test/generate-sym-test.py src/ukify/ukify.py test/integration-test-wrapper.py
 
       - name: Run ruff format
         run: |
           ruff --version
-          ruff format --check src/test/generate-sym-test.py src/ukify/ukify.py test/integration-test-wrapper.py
+          ruff format --check src/boot/generate-hwids-section.py src/test/generate-sym-test.py src/ukify/ukify.py test/integration-test-wrapper.py
diff --git a/src/boot/generate-hwids-section.py b/src/boot/generate-hwids-section.py
new file mode 100644 (file)
index 0000000..621183c
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+import os
+import sys
+from pathlib import Path
+
+# We import ukify.py, which is a template file. But only __version__ is
+# substituted, which we don't care about here. Having the .py suffix makes it
+# easier to import the file.
+sys.path.append(os.path.dirname(__file__) + '/../ukify')
+import ukify
+
+BYTES_PER_LINE = 16
+
+hwids = ukify.parse_hwid_dir(Path(sys.argv[1]))
+
+print(
+    """/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#include <stddef.h>
+#include <stdint.h>
+
+const uint8_t hwids_section_data[] = {
+    """,
+    end='',
+)
+
+for i, b in enumerate(hwids):
+    print(f'0x{b:02X}, ', end='')
+    if i % BYTES_PER_LINE == BYTES_PER_LINE - 1:
+        print('\n    ', end='')
+    elif i == len(hwids) - 1:
+        print('')
+
+print(
+    """};
+const size_t hwids_section_len =""",
+    f'{len(hwids)};',
+)
diff --git a/src/boot/hwids/device1.json b/src/boot/hwids/device1.json
new file mode 100644 (file)
index 0000000..030afc8
--- /dev/null
@@ -0,0 +1,8 @@
+{
+    "name": "Device 1",
+    "compatible": "test,device-1",
+    "hwids": [
+        "2bdcafa9-727c-5e8b-b13b-236f1391c25a",
+        "c87886da-8026-5149-8c95-6d2504c1b7f9"
+    ]
+}
diff --git a/src/boot/hwids/device2.json b/src/boot/hwids/device2.json
new file mode 100644 (file)
index 0000000..dc9b8e8
--- /dev/null
@@ -0,0 +1,8 @@
+{
+    "name": "Device 2",
+    "compatible": "test,device-2",
+    "hwids": [
+        "6ff16153-a32f-548a-9cd7-3574a5d0caa4",
+        "0afb4762-c5bb-5fba-887b-d56ce73ee793"
+    ]
+}
diff --git a/src/boot/hwids/device3.json b/src/boot/hwids/device3.json
new file mode 100644 (file)
index 0000000..d139462
--- /dev/null
@@ -0,0 +1,8 @@
+{
+    "name": "Device 3",
+    "compatible": "test,device-3",
+    "hwids": [
+        "06b74502-e5d8-5ecd-a67a-e77654cf3b17",
+        "a8d84e4e-23d9-5629-86da-a49d859b5cde"
+    ]
+}
index 9c7483d6d1d3c413f8d0ad5dae59536d59daabb0..13b35b47b28863a130c00229ca1f88ee498e8151 100644 (file)
@@ -26,6 +26,16 @@ efitest_base = {
 efi_test_template = test_template + efitest_base
 efi_fuzz_template = fuzz_template + efitest_base
 
+generate_hwids_section_py = find_program('generate-hwids-section.py')
+
+test_hwids_section_c = custom_target(
+        'test-hwids-section.c',
+        input : ['hwids/device1.json', 'hwids/device2.json', 'hwids/device2.json'],
+        output : 'test-hwids-section.c',
+        command : [generate_hwids_section_py, meson.current_source_dir()/'hwids'],
+        capture : true,
+        build_by_default : want_tests != 'false')
+
 executables += [
         efi_test_template + {
                 'sources' : files('test-bcd.c'),
@@ -36,6 +46,11 @@ executables += [
                 'sources' : files('test-efi-string.c'),
                 'conditions' : ['ENABLE_BOOTLOADER'],
         },
+        efi_test_template + {
+                'sources' : files('test-chid-match.c') +
+                            test_hwids_section_c,
+                'conditions' : ['ENABLE_BOOTLOADER'],
+        },
         efi_fuzz_template + {
                 'sources' : files('fuzz-bcd.c'),
         },
diff --git a/src/boot/test-chid-match.c b/src/boot/test-chid-match.c
new file mode 100644 (file)
index 0000000..e9df4e1
--- /dev/null
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "chid.h"
+#include "smbios.h"
+#include "tests.h"
+
+extern uint8_t hwids_section_data[];
+extern size_t hwids_section_len;
+
+static const RawSmbiosInfo smbios_info[] = {
+        {
+                .manufacturer           = "First Vendor",
+                .product_name           = "Device 1",
+                .product_sku            = "KD01",
+                .family                 = "Laptop X",
+                .baseboard_product      = "FODM1",
+                .baseboard_manufacturer = "First ODM",
+        },
+        {
+                .manufacturer           = "Second Vendor",
+                .product_name           = "Device 2",
+                .product_sku            = "KD02",
+                .family                 = "Laptop 2",
+                .baseboard_product      = "SODM2",
+                .baseboard_manufacturer = "Second ODM",
+        },
+        {
+                .manufacturer           = "First Vendor",
+                .product_name           = "Device 3",
+                .product_sku            = "KD03",
+                .family                 = "Tablet Y",
+                .baseboard_product      = "FODM2",
+                .baseboard_manufacturer = "First ODM",
+        },
+};
+
+static struct {
+        const char *name;
+        const char *compatible;
+} results[] = {
+        { "Device 1", "test,device-1" },
+        { "Device 2", "test,device-2" },
+        { "Device 3", "test,device-3" },
+};
+
+static RawSmbiosInfo current_info = {};
+
+/* This is a dummy implementation for testing purposes */
+void smbios_raw_info_get_cached(RawSmbiosInfo *ret_info) {
+        assert(ret_info);
+        *ret_info = current_info;
+}
+
+TEST(chid_match) {
+        for (size_t i = 0; i < ELEMENTSOF(smbios_info); i++) {
+                current_info = smbios_info[i];
+                const Device *dev = NULL;
+                /* Match and check */
+                ASSERT_EQ(chid_match(hwids_section_data, hwids_section_len, &dev), EFI_SUCCESS);
+                ASSERT_NOT_NULL(dev);
+                ASSERT_STREQ(device_get_name(hwids_section_data, dev), results[i].name);
+                ASSERT_STREQ(device_get_compatible(hwids_section_data, dev), results[i].compatible);
+        }
+}
+
+static int intro(void) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+        return EXIT_SUCCESS;
+#else
+        return log_tests_skipped("cannot run CHID calculation on big-endian machine");
+#endif
+}
+
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);