Skip to content

eBPF-based security platform

testing: add unit tests for util, starting with string.c

Daniel Borkmanne6b7bdbf395c
diff --git a/Makefile.shirudo b/Makefile.shirudo
index abbac3fc..275fc241 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -36,6 +36,7 @@ obj = $(OBJ_PREFIX)$(1)
CC ?= cc
LD := $(CC)
+AR ?= ar
LDFLAGS ?=
SHIRUDO_BUNDLE_RPATH ?= 0
CLANG ?= clang
@@ -295,7 +296,7 @@ DEPS := $(DEP_OBJS:.o=.d)
# ----- top-level goals --------------------------------------------
.PHONY: all install install-common install-target install-operator clean distclean check test \
- test-helpers verity-test-helpers \
+ test-helpers verity-test-helpers unit-tests \
bpf libbpf
all: shirudo
@@ -597,7 +598,6 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-config-attest \
testing/helper/test-image-keys \
testing/helper/test-json-parser \
- testing/helper/test-string \
testing/helper/test-conditions \
testing/helper/test-config-image \
testing/helper/test-harden \
@@ -636,9 +636,6 @@ testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o u
util/string.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
-testing/helper/test-string: testing/helper/test-string.c util/string.o util/malloc.o
- @printf ' CC $@\n'
- $(Q)$(CC) $(CFLAGS) -o $@ $^
testing/helper/test-conditions: testing/helper/test-conditions.c util/conditions.o \
util/string.o util/malloc.o
@printf ' CC $@\n'
@@ -883,6 +880,28 @@ testing/helper/test-mode-objects: testing/helper/test-mode-objects.c \
test-helpers: $(TEST_HELPERS)
+# ----- unit tests -------------------------------------------------
+
+# util/<m>.c is tested by testing/unit/<m>.c. Each test links the util
+# objects from an archive, so it pulls in only what its module needs.
+UNIT_SRCS := $(filter-out testing/unit/unit.c,$(wildcard testing/unit/*.c))
+UNIT_TESTS := $(patsubst testing/unit/%.c,build/unit/%,$(UNIT_SRCS))
+UNIT_LIB := build/unit/libshirudo.a
+
+$(UNIT_LIB): $(UTIL_OBJS) $(VERSION_OBJ)
+ @printf ' AR $@\n'
+ $(Q)mkdir -p $(dir $@)
+ $(Q)rm -f $@
+ $(Q)$(AR) rcs $@ $^
+
+build/unit/%: testing/unit/%.c testing/unit/unit.c testing/unit/unit.h \
+ $(UNIT_LIB) $(LIBBPF_A)
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) -Itesting/unit -o $@ $< testing/unit/unit.c \
+ $(UNIT_LIB) $(LIBBPF_A) $(DYNAMIC_LIBS)
+
+unit-tests: $(UNIT_TESTS)
+
# ----- fuzzing ----------------------------------------------------
# libFuzzer targets for the parsers of input from outside the host, built
@@ -998,7 +1017,7 @@ $(FUZZ_TARGETS): testing/fuzz/fuzz.h
fuzz-targets: $(FUZZ_TARGETS)
-check test: shirudo $(TEST_HELPERS)
+check test: shirudo $(TEST_HELPERS) $(UNIT_TESTS)
@printf ' TEST\n'
$(Q)cd testing && ./run-tests.sh
@@ -1008,7 +1027,7 @@ clean:
$(Q)rm -f shirudo shirudo-target tools/shirudo-build/shirudo-build $(TEST_HELPERS)
$(Q)rm -f $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) \
$(BUILD_OBJS) $(VERSION_OBJ) util/version_id.c
- $(Q)rm -rf build/target
+ $(Q)rm -rf build/target build/unit
$(Q)rm -f $(DEPS) testing/helper/*.d
$(Q)rm -f $(BPF_OBJS) $(VMLINUX_H) systemd/shirudo.service \
systemd/shirudo-initrd.service \
diff --git a/testing/Makefile b/testing/Makefile
index 0132ad59..a92c0088 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -19,7 +19,7 @@ test-vm-arch = $(shell TEST_VM_ARCH='$(TEST_VM_ARCH)'; \
. $(ROOT)/testing/vm/arch.sh && printf '%s' "$$$(1)")
.DEFAULT_GOAL := help
-.PHONY: help clean test-helpers test-op fuzz test bundle test-vm-bundle kernel \
+.PHONY: help clean test-helpers test-unit test-op fuzz test bundle test-vm-bundle kernel \
bpf-next vm vm-console vm-ssh vm-stop vm-clean test-tgt test-tgt-obs \
test-tgt-register test-vm test-vm-clean
@@ -288,6 +288,13 @@ test-helpers:
+$(Q)$(MAKE) -C $(ROOT) config
+$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo test-helpers
+test-unit:
+ +$(Q)$(MAKE) -C $(ROOT) config
+ +$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo unit-tests
+ $(Q)rc=0; for t in $(ROOT)/build/unit/*; do \
+ [ -f "$$t" ] && [ -x "$$t" ] || continue; "$$t" || rc=1; \
+ done; exit $$rc
+
# The parsers of input from outside the host under libFuzzer, each target
# for FUZZ_SECONDS on its seed corpus; needs clang with its fuzzer runtime.
FUZZ_SECONDS ?= 60
@@ -311,6 +318,7 @@ help:
'Testing targets:' \
' make -C testing test' \
' make -C testing test-op' \
+ ' make -C testing test-unit' \
' make -C testing test-helpers' \
' make -C testing fuzz [FUZZ_SECONDS=60]' \
' make -C testing test-tgt (dm-verity, dracut initrd, root required)' \
diff --git a/testing/helper/test-string.c b/testing/helper/test-string.c
deleted file mode 100644
index 65e57812..00000000
--- a/testing/helper/test-string.c
+++ /dev/null
@@ -1,125 +0,0 @@
-// SPDX-License-Identifier: BSD-2-Clause
-/* Copyright (c) 2026 Authors of shirudo */
-
-/*
- * Drives util/string.c for t0049. The text under test sits in a buffer of
- * exactly the length given, so a read past it shows up under ASan.
- *
- * test-string digits <text>
- * test-string u64|x64 <max> <text> [<len>]
- * test-string s64 <min> <max> <text> [<len>]
- * test-string strscpy <size> <text>
- * test-string strsprintf <size> <text>
- * test-string xasprintf <text>
- */
-
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-
-#include <shirudo/string.h>
-
-#include "util/malloc.h"
-
-static int bound_u64(const char *s, __u64 *out)
-{
- return parse_u64(s, strlen(s), UINT64_MAX, out);
-}
-
-static int bound_s64(const char *s, __s64 *out)
-{
- return parse_s64(s, strlen(s), INT64_MIN, INT64_MAX, out);
-}
-
-/* The text in a buffer of its own, len bytes long and unterminated. */
-static char *exact(const char *text, const char *len_arg, size_t *len)
-{
- __u64 n = strlen(text);
- char *buf;
-
- if (len_arg && (bound_u64(len_arg, &n) || n > strlen(text)))
- return NULL;
- buf = xmalloc(n ? n : 1);
- if (buf)
- memcpy(buf, text, n);
- *len = n;
- return buf;
-}
-
-int main(int argc, char **argv)
-{
- const char *op = argc > 1 ? argv[1] : "";
- __s64 min, smax, s = 0;
- __u64 max, u = 0;
- size_t len;
- char *buf;
- int rc;
-
- if (!strcmp(op, "digits") && argc == 3) {
- printf("n=%zu\n", digits(argv[2], strlen(argv[2])));
- return 0;
- }
- if ((!strcmp(op, "u64") || !strcmp(op, "x64")) &&
- (argc == 4 || argc == 5)) {
- if (bound_u64(argv[2], &max))
- return 2;
- buf = exact(argv[3], argc == 5 ? argv[4] : NULL, &len);
- if (!buf)
- return 2;
- rc = op[0] == 'u' ? parse_u64(buf, len, max, &u) :
- parse_x64(buf, len, max, &u);
- printf("rc=%d v=%llu\n", rc, u);
- xfree(buf, len ? len : 1);
- return 0;
- }
- if (!strcmp(op, "s64") && (argc == 5 || argc == 6)) {
- if (bound_s64(argv[2], &min) || bound_s64(argv[3], &smax))
- return 2;
- buf = exact(argv[4], argc == 6 ? argv[5] : NULL, &len);
- if (!buf)
- return 2;
- rc = parse_s64(buf, len, min, smax, &s);
- printf("rc=%d v=%lld\n", rc, s);
- xfree(buf, len ? len : 1);
- return 0;
- }
- if (!strcmp(op, "strscpy") && argc == 4) {
- ssize_t n;
-
- if (bound_u64(argv[2], &max))
- return 2;
- buf = xmalloc(max ? max : 1);
- if (!buf)
- return 2;
- n = strscpy(buf, argv[3], max);
- printf("rc=%zd dst=%s\n", n, max ? buf : "-");
- xfree(buf, max ? max : 1);
- return 0;
- }
- if (!strcmp(op, "strsprintf") && argc == 4) {
- int n;
-
- if (bound_u64(argv[2], &max))
- return 2;
- buf = xmalloc(max ? max : 1);
- if (!buf)
- return 2;
- buf[0] = 'x';
- n = strsprintf(buf, max, "<%s>", argv[3]);
- printf("rc=%d buf=%s\n", n, max ? buf : "-");
- xfree(buf, max ? max : 1);
- return 0;
- }
- if (!strcmp(op, "xasprintf") && argc == 3) {
- char *str = xasprintf("<%s|%d>", argv[2], 42);
-
- if (!str)
- return 2;
- printf("s=%s\n", str);
- xstrfree(str);
- return 0;
- }
- fprintf(stderr, "usage: test-string "
- "digits|u64|x64|s64|strscpy|strsprintf|xasprintf ...\n");
- return 2;
-}
diff --git a/testing/run-tests.sh b/testing/run-tests.sh
index 2a14788f..b3f3ad72 100755
--- a/testing/run-tests.sh
+++ b/testing/run-tests.sh
@@ -15,6 +15,19 @@ cd "$(dirname "$0")"
fail=0
found=0
: "${SHIRUDO_TEST_SUITE_FAILURES_FILE:=}"
+for t in ../build/unit/*; do
+ [ -f "$t" ] && [ -x "$t" ] || continue
+ found=1
+ if "$t"; then
+ :
+ else
+ fail=$((fail + 1))
+ if [ -n "$SHIRUDO_TEST_SUITE_FAILURES_FILE" ]; then
+ printf '%s\n' "$t" >>"$SHIRUDO_TEST_SUITE_FAILURES_FILE" 2>/dev/null || :
+ fi
+ fi
+done
+
for t in t0[0-9][0-9][0-9]-*.sh; do
[ -e "$t" ] || continue
found=1
diff --git a/testing/t0049-string.sh b/testing/t0049-string.sh
deleted file mode 100755
index 6e4fa561..00000000
--- a/testing/t0049-string.sh
+++ /dev/null
@@ -1,114 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: BSD-2-Clause
-# Copyright (c) 2026 Authors of shirudo
-#
-# t0049 -- util/string.c: numbers parsed from a bounded, unterminated
-# span, digits only, within the bounds the caller gives, strings copied
-# into a buffer that is always terminated and says when it cut, output
-# formatted whole or not at all, and xasprintf() from util/malloc.c.
-
-cd "$(dirname "$0")"
-. ./test-lib.sh
-
-H=helper/test-string
-U64_MAX=18446744073709551615
-S64_MIN=-9223372036854775808
-S64_MAX=9223372036854775807
-
-# E2BIG=7, EINVAL=22, ERANGE=34 on Linux.
-
-test_expect_success 'leading digits are counted' '
- ./$H digits 123abc | grep -qx "n=3" &&
- ./$H digits abc | grep -qx "n=0" &&
- ./$H digits "" | grep -qx "n=0"
-'
-
-test_expect_success 'a decimal fills its span' '
- ./$H u64 $U64_MAX 0 | grep -qx "rc=0 v=0" &&
- ./$H u64 $U64_MAX 007 | grep -qx "rc=0 v=7" &&
- ./$H u64 $U64_MAX $U64_MAX | grep -qx "rc=0 v=$U64_MAX"
-'
-
-test_expect_success 'nothing past the span is read' '
- ./$H u64 $U64_MAX 123456 3 | grep -qx "rc=0 v=123" &&
- ./$H u64 $U64_MAX 123 0 | grep -qx "rc=-22 v=0"
-'
-
-test_expect_success 'a sign, blank or base prefix is not a number' '
- for t in "" -1 +1 " 1" "1 " 0x10 1.5 1e3 12a; do
- ./$H u64 $U64_MAX "$t" | grep -qx "rc=-22 v=0" || return 1
- done
-'
-
-test_expect_success 'a number past its bound is out of range' '
- ./$H u64 $U64_MAX 18446744073709551616 | grep -qx "rc=-34 v=0" &&
- ./$H u64 10 10 | grep -qx "rc=0 v=10" &&
- ./$H u64 10 11 | grep -qx "rc=-34 v=0" &&
- ./$H u64 5 9 | grep -qx "rc=-34 v=0" &&
- ./$H u64 0 0 | grep -qx "rc=0 v=0"
-'
-
-test_expect_success 'a malformed number is not one, however long' '
- ./$H u64 $U64_MAX 99999999999999999999x | grep -qx "rc=-22 v=0"
-'
-
-test_expect_success 'hexadecimal takes either case and no prefix' '
- ./$H x64 $U64_MAX ff | grep -qx "rc=0 v=255" &&
- ./$H x64 $U64_MAX FF | grep -qx "rc=0 v=255" &&
- ./$H x64 $U64_MAX ffffffffffffffff | grep -qx "rc=0 v=$U64_MAX" &&
- ./$H x64 $U64_MAX 10000000000000000 | grep -qx "rc=-34 v=0" &&
- ./$H x64 $U64_MAX 0x10 | grep -qx "rc=-22 v=0" &&
- ./$H x64 $U64_MAX g | grep -qx "rc=-22 v=0"
-'
-
-test_expect_success 'a signed number spans the whole range' '
- ./$H s64 $S64_MIN $S64_MAX $S64_MIN | grep -qx "rc=0 v=$S64_MIN" &&
- ./$H s64 $S64_MIN $S64_MAX $S64_MAX | grep -qx "rc=0 v=$S64_MAX" &&
- ./$H s64 $S64_MIN $S64_MAX -0 | grep -qx "rc=0 v=0" &&
- ./$H s64 $S64_MIN $S64_MAX 9223372036854775808 |
- grep -qx "rc=-34 v=0" &&
- ./$H s64 $S64_MIN $S64_MAX -9223372036854775809 |
- grep -qx "rc=-34 v=0"
-'
-
-test_expect_success 'a signed number keeps to its bounds and form' '
- ./$H s64 1 10 0 | grep -qx "rc=-34 v=0" &&
- ./$H s64 -5 5 -6 | grep -qx "rc=-34 v=0" &&
- ./$H s64 -5 5 -5 | grep -qx "rc=0 v=-5" &&
- for t in - -- --1 +1 " -1" -x; do
- ./$H s64 $S64_MIN $S64_MAX "$t" | grep -qx "rc=-22 v=0" ||
- return 1
- done
-'
-
-test_expect_success 'a string that fits is copied whole' '
- ./$H strscpy 6 hello | grep -qx "rc=5 dst=hello" &&
- ./$H strscpy 64 hello | grep -qx "rc=5 dst=hello" &&
- ./$H strscpy 1 "" | grep -qx "rc=0 dst="
-'
-
-test_expect_success 'a string that does not fit is cut and says so' '
- ./$H strscpy 5 hello | grep -qx "rc=-7 dst=hell" &&
- ./$H strscpy 1 hello | grep -qx "rc=-7 dst=" &&
- ./$H strscpy 0 hello | grep -qx "rc=-7 dst=-"
-'
-
-test_expect_success 'formatted output that fits is written whole' '
- ./$H strsprintf 8 hello | grep -qx "rc=7 buf=<hello>" &&
- ./$H strsprintf 64 "" | grep -qx "rc=2 buf=<>"
-'
-
-test_expect_success 'formatted output that does not fit leaves nothing' '
- ./$H strsprintf 7 hello | grep -qx "rc=-7 buf=" &&
- ./$H strsprintf 1 hello | grep -qx "rc=-7 buf=" &&
- ./$H strsprintf 0 hello | grep -qx "rc=-7 buf=-"
-'
-
-test_expect_success 'xasprintf() formats into memory of the size needed' '
- ./$H xasprintf hello | grep -qx "s=<hello|42>" &&
- ./$H xasprintf "" | grep -qx "s=<|42>" &&
- long=$(printf "%0300d" 0) &&
- ./$H xasprintf "$long" | grep -qx "s=<$long|42>"
-'
-
-test_done
diff --git a/testing/unit/string.c b/testing/unit/string.c
new file mode 100644
index 00000000..9e90fcab
--- /dev/null
+++ b/testing/unit/string.c
@@ -0,0 +1,261 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <stdint.h>
+
+#include <shirudo/string.h>
+
+#include "malloc.h"
+#include "unit.h"
+
+/*
+ * The span under test sits in a buffer of exactly its length, so a read
+ * past it shows up under ASan.
+ */
+static char *span(const char *text, size_t len)
+{
+ char *buf = xmalloc(len ? len : 1);
+
+ ASSERT_TRUE(buf);
+ memcpy(buf, text, len);
+ return buf;
+}
+
+static int u64_of(const char *text, size_t len, __u64 max, __u64 *v)
+{
+ char *buf = span(text, len);
+ int rc;
+
+ *v = 0;
+ rc = parse_u64(buf, len, max, v);
+ xfree(buf, len ? len : 1);
+ return rc;
+}
+
+static int x64_of(const char *text, __u64 max, __u64 *v)
+{
+ size_t len = strlen(text);
+ char *buf = span(text, len);
+ int rc;
+
+ *v = 0;
+ rc = parse_x64(buf, len, max, v);
+ xfree(buf, len ? len : 1);
+ return rc;
+}
+
+static int s64_of(const char *text, __s64 min, __s64 max, __s64 *v)
+{
+ size_t len = strlen(text);
+ char *buf = span(text, len);
+ int rc;
+
+ *v = 0;
+ rc = parse_s64(buf, len, min, max, v);
+ xfree(buf, len ? len : 1);
+ return rc;
+}
+
+static int u64_str(const char *text, __u64 max, __u64 *v)
+{
+ return u64_of(text, strlen(text), max, v);
+}
+
+#define EXPECT_U64(text, max, rc, value) \
+ do { \
+ __u64 v_; \
+ \
+ EXPECT_EQ(u64_str(text, max, &v_), rc); \
+ EXPECT_UEQ(v_, value); \
+ } while (0)
+
+static void digits_counts_leading_digits(void)
+{
+ EXPECT_UEQ(digits("123abc", 6), 3);
+ EXPECT_UEQ(digits("abc", 3), 0);
+ EXPECT_UEQ(digits("", 0), 0);
+}
+
+static void decimal_fills_its_span(void)
+{
+ EXPECT_U64("0", UINT64_MAX, 0, 0);
+ EXPECT_U64("007", UINT64_MAX, 0, 7);
+ EXPECT_U64("18446744073709551615", UINT64_MAX, 0, UINT64_MAX);
+}
+
+static void nothing_past_the_span_is_read(void)
+{
+ __u64 v;
+
+ EXPECT_EQ(u64_of("123456", 3, UINT64_MAX, &v), 0);
+ EXPECT_UEQ(v, 123);
+ EXPECT_EQ(u64_of("123", 0, UINT64_MAX, &v), -EINVAL);
+ EXPECT_UEQ(v, 0);
+}
+
+static void sign_blank_or_prefix_is_no_number(void)
+{
+ static const char *const bad[] = {
+ "", "-1", "+1", " 1", "1 ", "0x10", "1.5", "1e3", "12a",
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(bad); i++)
+ EXPECT_U64(bad[i], UINT64_MAX, -EINVAL, 0);
+}
+
+static void number_past_its_bound_is_out_of_range(void)
+{
+ EXPECT_U64("18446744073709551616", UINT64_MAX, -ERANGE, 0);
+ EXPECT_U64("10", 10, 0, 10);
+ EXPECT_U64("11", 10, -ERANGE, 0);
+ EXPECT_U64("9", 5, -ERANGE, 0);
+ EXPECT_U64("0", 0, 0, 0);
+}
+
+static void malformed_number_however_long(void)
+{
+ EXPECT_U64("99999999999999999999x", UINT64_MAX, -EINVAL, 0);
+}
+
+static void hex_takes_either_case_and_no_prefix(void)
+{
+ __u64 v;
+
+ EXPECT_EQ(x64_of("ff", UINT64_MAX, &v), 0);
+ EXPECT_UEQ(v, 255);
+ EXPECT_EQ(x64_of("FF", UINT64_MAX, &v), 0);
+ EXPECT_UEQ(v, 255);
+ EXPECT_EQ(x64_of("ffffffffffffffff", UINT64_MAX, &v), 0);
+ EXPECT_UEQ(v, UINT64_MAX);
+ EXPECT_EQ(x64_of("10000000000000000", UINT64_MAX, &v), -ERANGE);
+ EXPECT_UEQ(v, 0);
+ EXPECT_EQ(x64_of("0x10", UINT64_MAX, &v), -EINVAL);
+ EXPECT_UEQ(v, 0);
+ EXPECT_EQ(x64_of("g", UINT64_MAX, &v), -EINVAL);
+ EXPECT_UEQ(v, 0);
+}
+
+static void signed_number_spans_the_whole_range(void)
+{
+ __s64 v;
+
+ EXPECT_EQ(s64_of("-9223372036854775808", INT64_MIN, INT64_MAX, &v), 0);
+ EXPECT_EQ(v, INT64_MIN);
+ EXPECT_EQ(s64_of("9223372036854775807", INT64_MIN, INT64_MAX, &v), 0);
+ EXPECT_EQ(v, INT64_MAX);
+ EXPECT_EQ(s64_of("-0", INT64_MIN, INT64_MAX, &v), 0);
+ EXPECT_EQ(v, 0);
+ EXPECT_EQ(s64_of("9223372036854775808", INT64_MIN, INT64_MAX, &v),
+ -ERANGE);
+ EXPECT_EQ(v, 0);
+ EXPECT_EQ(s64_of("-9223372036854775809", INT64_MIN, INT64_MAX, &v),
+ -ERANGE);
+ EXPECT_EQ(v, 0);
+}
+
+static void signed_number_keeps_bounds_and_form(void)
+{
+ static const char *const bad[] = { "-", "--", "--1", "+1", " -1", "-x" };
+ __s64 v;
+
+ EXPECT_EQ(s64_of("0", 1, 10, &v), -ERANGE);
+ EXPECT_EQ(v, 0);
+ EXPECT_EQ(s64_of("-6", -5, 5, &v), -ERANGE);
+ EXPECT_EQ(v, 0);
+ EXPECT_EQ(s64_of("-5", -5, 5, &v), 0);
+ EXPECT_EQ(v, -5);
+ for (size_t i = 0; i < ARRAY_SIZE(bad); i++) {
+ EXPECT_EQ(s64_of(bad[i], INT64_MIN, INT64_MAX, &v), -EINVAL);
+ EXPECT_EQ(v, 0);
+ }
+}
+
+static void strscpy_copies_what_fits_whole(void)
+{
+ char six[6], big[64], one[1];
+
+ EXPECT_EQ(strscpy(six, "hello", sizeof(six)), 5);
+ EXPECT_STREQ(six, "hello");
+ EXPECT_EQ(strscpy(big, "hello", sizeof(big)), 5);
+ EXPECT_STREQ(big, "hello");
+ EXPECT_EQ(strscpy(one, "", sizeof(one)), 0);
+ EXPECT_STREQ(one, "");
+}
+
+static void strscpy_cuts_and_says_so(void)
+{
+ char five[5], one[1], none[1] = { 'x' };
+
+ EXPECT_EQ(strscpy(five, "hello", sizeof(five)), -E2BIG);
+ EXPECT_STREQ(five, "hell");
+ EXPECT_EQ(strscpy(one, "hello", sizeof(one)), -E2BIG);
+ EXPECT_STREQ(one, "");
+ EXPECT_EQ(strscpy(none, "hello", 0), -E2BIG);
+ EXPECT_EQ(none[0], 'x');
+}
+
+static void strsprintf_writes_what_fits_whole(void)
+{
+ char eight[8], big[64];
+
+ EXPECT_EQ(strsprintf(eight, sizeof(eight), "<%s>", "hello"), 7);
+ EXPECT_STREQ(eight, "<hello>");
+ EXPECT_EQ(strsprintf(big, sizeof(big), "<%s>", ""), 2);
+ EXPECT_STREQ(big, "<>");
+}
+
+static void strsprintf_leaves_nothing_when_cut(void)
+{
+ char seven[7] = { 'x' }, one[1] = { 'x' }, none[1] = { 'x' };
+
+ EXPECT_EQ(strsprintf(seven, sizeof(seven), "<%s>", "hello"), -E2BIG);
+ EXPECT_STREQ(seven, "");
+ EXPECT_EQ(strsprintf(one, sizeof(one), "<%s>", "hello"), -E2BIG);
+ EXPECT_STREQ(one, "");
+ EXPECT_EQ(strsprintf(none, 0, "<%s>", "hello"), -E2BIG);
+ EXPECT_EQ(none[0], 'x');
+}
+
+static void xasprintf_allocates_the_size_needed(void)
+{
+ char text[301], want[310], *s;
+
+ s = xasprintf("<%s|%d>", "hello", 42);
+ ASSERT_TRUE(s);
+ EXPECT_STREQ(s, "<hello|42>");
+ xstrfree(s);
+
+ s = xasprintf("<%s|%d>", "", 42);
+ ASSERT_TRUE(s);
+ EXPECT_STREQ(s, "<|42>");
+ xstrfree(s);
+
+ memset(text, '0', sizeof(text) - 1);
+ text[sizeof(text) - 1] = '\0';
+ ASSERT_EQ(strsprintf(want, sizeof(want), "<%s|42>", text), 305);
+ s = xasprintf("<%s|%d>", text, 42);
+ ASSERT_TRUE(s);
+ EXPECT_STREQ(s, want);
+ xstrfree(s);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(digits_counts_leading_digits),
+ UNIT_CASE(decimal_fills_its_span),
+ UNIT_CASE(nothing_past_the_span_is_read),
+ UNIT_CASE(sign_blank_or_prefix_is_no_number),
+ UNIT_CASE(number_past_its_bound_is_out_of_range),
+ UNIT_CASE(malformed_number_however_long),
+ UNIT_CASE(hex_takes_either_case_and_no_prefix),
+ UNIT_CASE(signed_number_spans_the_whole_range),
+ UNIT_CASE(signed_number_keeps_bounds_and_form),
+ UNIT_CASE(strscpy_copies_what_fits_whole),
+ UNIT_CASE(strscpy_cuts_and_says_so),
+ UNIT_CASE(strsprintf_writes_what_fits_whole),
+ UNIT_CASE(strsprintf_leaves_nothing_when_cut),
+ UNIT_CASE(xasprintf_allocates_the_size_needed),
+ {}
+};
+
+UNIT_SUITE(string, cases);
diff --git a/testing/unit/unit.c b/testing/unit/unit.c
new file mode 100644
index 00000000..63ad639a
--- /dev/null
+++ b/testing/unit/unit.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Runs a unit suite in TAP, each case in a child so one crash fails it alone.
+ */
+
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+
+#include "unit.h"
+
+#define UNIT_TIMEOUT 60
+#define UNIT_SKIPPED 77
+#define UNIT_REASON_MAX 256
+
+static int unit_failed;
+static char *unit_reason;
+
+void unit_fail(const char *file, int line, const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s:%d: ", file, line);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+ unit_failed = 1;
+}
+
+void unit_fail_mem(const char *file, int line, const char *what,
+ const void *a, const void *b, size_t len)
+{
+ const __u8 *x = a, *y = b;
+ size_t i = 0;
+
+ while (i < len && x[i] == y[i])
+ i++;
+ unit_fail(file, line, "%s: differ at byte %zu of %zu: %02x vs %02x",
+ what, i, len, x[i], y[i]);
+}
+
+void unit_abort(void)
+{
+ fflush(stdout);
+ fflush(stderr);
+ _exit(1);
+}
+
+void unit_skip(const char *reason)
+{
+ size_t n = strlen(reason);
+
+ if (n >= UNIT_REASON_MAX)
+ n = UNIT_REASON_MAX - 1;
+ memcpy(unit_reason, reason, n);
+ unit_reason[n] = '\0';
+ fflush(stdout);
+ fflush(stderr);
+ _exit(UNIT_SKIPPED);
+}
+
+/* A failing case's stderr comes back as TAP diagnostics. */
+static void replay(int fd)
+{
+ char buf[4096];
+ int bol = 1;
+ ssize_t n;
+
+ if (lseek(fd, 0, SEEK_SET) < 0)
+ return;
+ while ((n = read(fd, buf, sizeof(buf))) > 0) {
+ for (ssize_t i = 0; i < n; i++) {
+ if (bol)
+ fputs("# ", stdout);
+ fputc(buf[i], stdout);
+ bol = buf[i] == '\n';
+ }
+ }
+ if (!bol)
+ fputc('\n', stdout);
+}
+
+static int run_case(const struct unit_case *c, size_t nr)
+{
+ int fd, status;
+ pid_t pid;
+
+ fd = memfd_create("unit-stderr", MFD_CLOEXEC);
+ if (fd < 0) {
+ printf("not ok %zu - %s # memfd: %s\n", nr, c->name,
+ strerror(errno));
+ return 1;
+ }
+ unit_reason[0] = '\0';
+ fflush(stdout);
+ fflush(stderr);
+ pid = fork();
+ if (pid < 0) {
+ printf("not ok %zu - %s # fork: %s\n", nr, c->name,
+ strerror(errno));
+ close(fd);
+ return 1;
+ }
+ if (pid == 0) {
+ dup2(fd, STDERR_FILENO);
+ alarm(UNIT_TIMEOUT);
+ c->run();
+ fflush(stdout);
+ fflush(stderr);
+ _exit(unit_failed ? 1 : 0);
+ }
+ while (waitpid(pid, &status, 0) < 0) {
+ if (errno != EINTR) {
+ status = -1;
+ break;
+ }
+ }
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+ printf("ok %zu - %s\n", nr, c->name);
+ close(fd);
+ return 0;
+ }
+ if (WIFEXITED(status) && WEXITSTATUS(status) == UNIT_SKIPPED) {
+ printf("ok %zu - %s # SKIP %s\n", nr, c->name, unit_reason);
+ close(fd);
+ return 0;
+ }
+ printf("not ok %zu - %s\n", nr, c->name);
+ if (WIFSIGNALED(status))
+ printf("# %s\n", WTERMSIG(status) == SIGALRM ?
+ "timed out" : strsignal(WTERMSIG(status)));
+ replay(fd);
+ close(fd);
+ return 1;
+}
+
+int main(int argc, char **argv)
+{
+ const char *only = argc > 1 ? argv[1] : NULL;
+ size_t nr = 0, failed = 0;
+
+ unit_reason = mmap(NULL, UNIT_REASON_MAX, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (unit_reason == MAP_FAILED) {
+ printf("Bail out! mmap: %s\n", strerror(errno));
+ return 1;
+ }
+ printf("# %s\n", unit_suite.name);
+ for (const struct unit_case *c = unit_suite.cases; c->name; c++) {
+ if (only && !strstr(c->name, only))
+ continue;
+ failed += run_case(c, ++nr);
+ }
+ printf("1..%zu\n", nr);
+ return failed ? 1 : 0;
+}
diff --git a/testing/unit/unit.h b/testing/unit/unit.h
new file mode 100644
index 00000000..08517bb4
--- /dev/null
+++ b/testing/unit/unit.h
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_TESTING_UNIT_H
+#define SHIRUDO_TESTING_UNIT_H
+
+#include <stddef.h>
+#include <string.h>
+
+#include <shirudo/misc.h>
+
+#include "macros.h"
+
+struct unit_case {
+ const char *name;
+ void (*run)(void);
+};
+
+struct unit_suite {
+ const char *name;
+ const struct unit_case *cases;
+};
+
+#define UNIT_CASE(fn) { #fn, fn }
+
+/*
+ * Each testing/unit/<module>.c names its cases in one table that ends with
+ * an empty entry. A case left out of it is an unused static function,
+ * which the build refuses.
+ */
+#define UNIT_SUITE(suite, table) \
+ const struct unit_suite unit_suite = { #suite, table }
+
+extern const struct unit_suite unit_suite;
+
+void unit_fail(const char *file, int line, const char *fmt, ...)
+ __attribute__((format(printf, 3, 4)));
+__attribute__((noreturn)) void unit_abort(void);
+__attribute__((noreturn)) void unit_skip(const char *reason);
+void unit_fail_mem(const char *file, int line, const char *what,
+ const void *a, const void *b, size_t len);
+
+#define UNIT_CHECK(fatal, cond, fmt, ...) \
+ do { \
+ if (!(cond)) { \
+ unit_fail(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
+ if (fatal) \
+ unit_abort(); \
+ } \
+ } while (0)
+
+#define UNIT_CMP_S64(fatal, a, op, b) \
+ do { \
+ __s64 unit_a_ = (__s64)(a), unit_b_ = (__s64)(b); \
+ UNIT_CHECK(fatal, unit_a_ op unit_b_, \
+ "%s %s %s: %lld vs %lld", #a, #op, #b, \
+ unit_a_, unit_b_); \
+ } while (0)
+
+#define UNIT_CMP_U64(fatal, a, op, b) \
+ do { \
+ __u64 unit_a_ = (__u64)(a), unit_b_ = (__u64)(b); \
+ UNIT_CHECK(fatal, unit_a_ op unit_b_, \
+ "%s %s %s: %llu vs %llu", #a, #op, #b, \
+ unit_a_, unit_b_); \
+ } while (0)
+
+#define UNIT_STREQ(fatal, a, b) \
+ do { \
+ const char *unit_a_ = (a), *unit_b_ = (b); \
+ UNIT_CHECK(fatal, unit_a_ && unit_b_ && \
+ !strcmp(unit_a_, unit_b_), \
+ "%s == %s: \"%s\" vs \"%s\"", #a, #b, \
+ unit_a_ ? unit_a_ : "(null)", \
+ unit_b_ ? unit_b_ : "(null)"); \
+ } while (0)
+
+#define UNIT_MEMEQ(fatal, a, b, len) \
+ do { \
+ const void *unit_a_ = (a), *unit_b_ = (b); \
+ size_t unit_n_ = (len); \
+ if (memcmp(unit_a_, unit_b_, unit_n_)) { \
+ unit_fail_mem(__FILE__, __LINE__, #a " == " #b, \
+ unit_a_, unit_b_, unit_n_); \
+ if (fatal) \
+ unit_abort(); \
+ } \
+ } while (0)
+
+/* EXPECT_* records a failure and goes on; ASSERT_* ends the case. */
+#define EXPECT_TRUE(c) UNIT_CHECK(0, (c), "%s", #c)
+#define EXPECT_FALSE(c) UNIT_CHECK(0, !(c), "!(%s)", #c)
+#define EXPECT_EQ(a, b) UNIT_CMP_S64(0, a, ==, b)
+#define EXPECT_NE(a, b) UNIT_CMP_S64(0, a, !=, b)
+#define EXPECT_LT(a, b) UNIT_CMP_S64(0, a, <, b)
+#define EXPECT_GT(a, b) UNIT_CMP_S64(0, a, >, b)
+#define EXPECT_UEQ(a, b) UNIT_CMP_U64(0, a, ==, b)
+#define EXPECT_STREQ(a, b) UNIT_STREQ(0, a, b)
+#define EXPECT_MEMEQ(a, b, n) UNIT_MEMEQ(0, a, b, n)
+
+#define ASSERT_TRUE(c) UNIT_CHECK(1, (c), "%s", #c)
+#define ASSERT_FALSE(c) UNIT_CHECK(1, !(c), "!(%s)", #c)
+#define ASSERT_EQ(a, b) UNIT_CMP_S64(1, a, ==, b)
+#define ASSERT_NE(a, b) UNIT_CMP_S64(1, a, !=, b)
+#define ASSERT_UEQ(a, b) UNIT_CMP_U64(1, a, ==, b)
+#define ASSERT_STREQ(a, b) UNIT_STREQ(1, a, b)
+#define ASSERT_MEMEQ(a, b, n) UNIT_MEMEQ(1, a, b, n)
+
+#endif /* SHIRUDO_TESTING_UNIT_H */