eBPF-based security platform
testing: unit test the kernel release and build-id parsers
Daniel Borkmannf3b6f7b9807b
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 7ab44c65..88c1f484 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -610,8 +610,6 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \$(TPMSIM_HELPER) \testing/helper/test-config \testing/helper/test-bpm-source \- testing/helper/test-kernel-release \- testing/helper/test-kernel-build-id \testing/helper/test-bpf-load \testing/helper/test-bpf-cmd \testing/helper/test-perf-attach \@@ -724,15 +722,6 @@ testing/helper/test-bpm-source: testing/helper/test-bpm-source.c util/bpm_source@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^-testing/helper/test-kernel-release: testing/helper/test-kernel-release.c util/kernel_release.o- @printf ' CC $@\n'- $(Q)$(CC) $(CFLAGS) -o $@ $^--testing/helper/test-kernel-build-id: testing/helper/test-kernel-build-id.c util/kernel_build_id.o \- util/hex.o util/malloc.o- @printf ' CC $@\n'- $(Q)$(CC) $(CFLAGS) -o $@ $^-testing/helper/test-bpf-load: testing/helper/test-bpf-load.c@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^diff --git a/testing/helper/test-kernel-build-id.c b/testing/helper/test-kernel-build-id.cdeleted file mode 100644index 924cc7db..00000000--- a/testing/helper/test-kernel-build-id.c+++ /dev/null@@ -1,45 +0,0 @@-// SPDX-License-Identifier: BSD-2-Clause-/* Copyright (c) 2026 Authors of shirudo */--#include <errno.h>-#include <stdint.h>-#include <stdio.h>-#include <string.h>--#include <shirudo/misc.h>--#include "util/kernel_build_id.h"--static void put_u32(__u8 *p, __u32 value)-{- memcpy(p, &value, sizeof(value));-}--int main(void)-{- /* One GNU build-id note with a four-byte descriptor: 01 23 45 67. */- static const __u8 notes[] = {- 4, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0,- 'G', 'N', 'U', 0, 1, 0x23, 0x45, 0x67,- };- __u8 malformed[sizeof(notes)];- char build_id[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX];-- if (kernel_build_id_from_notes(notes, sizeof(notes), build_id) ||- strcmp(build_id, "01234567")) {- fprintf(stderr, "failed to parse GNU build-id note: %s\n", build_id);- return 1;- }- if (kernel_build_id_from_notes(notes, 0, build_id) != -ENOENT) {- fprintf(stderr, "empty notes should return -ENOENT\n");- return 1;- }- memcpy(malformed, notes, sizeof(notes));- put_u32(malformed, UINT32_MAX);- if (kernel_build_id_from_notes(malformed, sizeof(malformed),- build_id) != -EINVAL) {- fprintf(stderr, "malformed notes should return -EINVAL\n");- return 1;- }- return 0;-}diff --git a/testing/helper/test-kernel-release.c b/testing/helper/test-kernel-release.cdeleted file mode 100644index 84686b9c..00000000--- a/testing/helper/test-kernel-release.c+++ /dev/null@@ -1,41 +0,0 @@-// SPDX-License-Identifier: BSD-2-Clause-/* Copyright (c) 2026 Authors of shirudo */--#include <stdio.h>--#include "util/kernel_release.h"-#include "util/macros.h"--struct test_case {- const char *release;- const char *prefix;- int matches;-};--int main(void)-{- static const struct test_case cases[] = {- { "6.1.0", "6.1", 1 },- { "6.1-custom", "6.1", 1 },- { "6.10.0", "6.1", 0 },- { "6.1", "6.1", 1 },- { "", "6.1", 0 },- { "6.1.0", "", 1 },- { NULL, "6.1", 0 },- { "6.1.0", NULL, 0 },- };-- for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {- int matches;-- matches = kernel_release_prefix_matches(cases[i].release,- cases[i].prefix);-- if (matches != cases[i].matches) {- fprintf(stderr, "case %zu: unexpected match result %d\n",- i, matches);- return 1;- }- }- return 0;-}diff --git a/testing/unit/kernel_build_id.c b/testing/unit/kernel_build_id.cnew file mode 100644index 00000000..836e81a7--- /dev/null+++ b/testing/unit/kernel_build_id.c@@ -0,0 +1,48 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>++#include "kernel_build_id.h"+#include "unit.h"++/* One GNU build-id note with a four-byte descriptor: 01 23 45 67. */+static const __u8 notes[] = {+ 4, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0,+ 'G', 'N', 'U', 0, 0x01, 0x23, 0x45, 0x67,+};++static void gnu_note_gives_the_build_id(void)+{+ char id[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX];++ ASSERT_EQ(kernel_build_id_from_notes(notes, sizeof(notes), id), 0);+ EXPECT_STREQ(id, "01234567");+}++static void empty_notes_have_no_build_id(void)+{+ char id[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX];++ EXPECT_EQ(kernel_build_id_from_notes(notes, 0, id), -ENOENT);+}++static void oversized_name_is_malformed(void)+{+ char id[SHIRUDO_KERNEL_BUILD_ID_STRING_MAX];+ __u8 bad[sizeof(notes)];+ __u32 namesz = ~0U;++ memcpy(bad, notes, sizeof(notes));+ memcpy(bad, &namesz, sizeof(namesz));+ EXPECT_EQ(kernel_build_id_from_notes(bad, sizeof(bad), id), -EINVAL);+}++static const struct unit_case cases[] = {+ UNIT_CASE(gnu_note_gives_the_build_id),+ UNIT_CASE(empty_notes_have_no_build_id),+ UNIT_CASE(oversized_name_is_malformed),+ {}+};++UNIT_SUITE(kernel_build_id, cases);diff --git a/testing/unit/kernel_release.c b/testing/unit/kernel_release.cnew file mode 100644index 00000000..4c1cbd4b--- /dev/null+++ b/testing/unit/kernel_release.c@@ -0,0 +1,34 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include "kernel_release.h"+#include "unit.h"++static void prefix_matches_at_a_boundary(void)+{+ EXPECT_TRUE(kernel_release_prefix_matches("6.1.0", "6.1"));+ EXPECT_TRUE(kernel_release_prefix_matches("6.1-custom", "6.1"));+ EXPECT_TRUE(kernel_release_prefix_matches("6.1", "6.1"));+ EXPECT_FALSE(kernel_release_prefix_matches("6.10.0", "6.1"));+}++static void empty_prefix_matches_any_release(void)+{+ EXPECT_TRUE(kernel_release_prefix_matches("6.1.0", ""));+ EXPECT_FALSE(kernel_release_prefix_matches("", "6.1"));+}++static void missing_release_or_prefix_never_matches(void)+{+ EXPECT_FALSE(kernel_release_prefix_matches(NULL, "6.1"));+ EXPECT_FALSE(kernel_release_prefix_matches("6.1.0", NULL));+}++static const struct unit_case cases[] = {+ UNIT_CASE(prefix_matches_at_a_boundary),+ UNIT_CASE(empty_prefix_matches_any_release),+ UNIT_CASE(missing_release_or_prefix_never_matches),+ {}+};++UNIT_SUITE(kernel_release, cases);