Skip to content

eBPF-based security platform

testing: unit test util/json.c

Daniel Borkmann9a21509ac798
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 275fc241..c4ca2050 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -597,7 +597,6 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-config-update \
testing/helper/test-config-attest \
testing/helper/test-image-keys \
- testing/helper/test-json-parser \
testing/helper/test-conditions \
testing/helper/test-config-image \
testing/helper/test-harden \
@@ -632,10 +631,6 @@ TEST_HELPERS += testing/helper/test-nv-writer
TEST_HELPERS += testing/helper/test-keyring-seal
TEST_HELPERS += testing/helper/test-tpm-preopened
-testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o \
- 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'
diff --git a/testing/helper/test-json-parser.c b/testing/helper/test-json-parser.c
deleted file mode 100644
index d1547667..00000000
--- a/testing/helper/test-json-parser.c
+++ /dev/null
@@ -1,82 +0,0 @@
-// SPDX-License-Identifier: BSD-2-Clause
-/*
- * Copyright (c) 2026 Authors of shirudo
- *
- * Harness for util/json's schema parser. Reads a JSON document from
- * stdin, parses it against an eventlog-shaped schema (scalars, a
- * string array, and an object array), and prints the return code and
- * extracted fields on success. Doubles as a fuzz entry point: feed it
- * arbitrary bytes -- it must never crash, only return a clean -errno.
- */
-
-#include <stdio.h>
-
-#include <shirudo/misc.h>
-
-#include "json.h"
-#include "macros.h"
-
-static int names_seen;
-static char first_name[64];
-static char name_scratch[64];
-
-static int name_on_elem(void *ctx)
-{
- (void)ctx;
- if (names_seen == 0)
- snprintf(first_name, sizeof(first_name), "%s", name_scratch);
- names_seen++;
- return 0;
-}
-
-static int events_seen;
-static char last_event_type[32];
-static char ev_type[32];
-static char ev_digest[65];
-
-static int event_on_elem(void *ctx)
-{
- (void)ctx;
- snprintf(last_event_type, sizeof(last_event_type), "%s", ev_type);
- events_seen++;
- return 0;
-}
-
-int main(void)
-{
- char digest[65] = {}, type[64] = {};
- static char buf[2 * 1024 * 1024];
- __s64 schema = -1;
- struct shirudo_json_array name_sink = { name_on_elem, NULL };
- struct shirudo_json_field name_sub[] = {
- { "", SHIRUDO_JSON_T_STRING, name_scratch,
- sizeof(name_scratch), 0, NULL, 0 },
- };
- struct shirudo_json_array event_sink = { event_on_elem, NULL };
- struct shirudo_json_field event_sub[] = {
- { "type", SHIRUDO_JSON_T_STRING, ev_type,
- sizeof(ev_type), 0, NULL, 0 },
- { "digest", SHIRUDO_JSON_T_STRING, ev_digest,
- sizeof(ev_digest), 0, NULL, 0 },
- };
- struct shirudo_json_field f[] = {
- { "schema", SHIRUDO_JSON_T_INT, &schema, 0, 0, NULL, 0 },
- { "type", SHIRUDO_JSON_T_STRING, type, sizeof(type), 0, NULL, 0 },
- { "digest", SHIRUDO_JSON_T_STRING, digest, sizeof(digest), 0, NULL, 0 },
- { "names", SHIRUDO_JSON_T_ARRAY, &name_sink, 0, 0, name_sub, 1 },
- { "events", SHIRUDO_JSON_T_ARRAY, &event_sink, 0, 0, event_sub, 2 },
- /* An empty schema slot must be ignored, not dereferenced. */
- { NULL, 0, NULL, 0, 0, NULL, 0 },
- };
- size_t n = fread(buf, 1, sizeof(buf), stdin);
- int rc = json_parse(buf, n, f, ARRAY_SIZE(f));
-
- printf("rc=%d\n", rc);
- if (rc == 0) {
- printf("schema=%lld type=%s digest=%s\n",
- (__s64)schema, type, digest);
- printf("names=%d first_name=%s events=%d last_type=%s\n",
- names_seen, first_name, events_seen, last_event_type);
- }
- return rc ? 1 : 0;
-}
diff --git a/testing/t0001-json-parser.sh b/testing/t0001-json-parser.sh
deleted file mode 100644
index 906a1ca7..00000000
--- a/testing/t0001-json-parser.sh
+++ /dev/null
@@ -1,134 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: BSD-2-Clause
-# Copyright (c) 2026 Authors of shirudo
-#
-# t0001 -- util/json schema parser: extraction, skipping, bounds,
-# and robustness against malformed input.
-
-cd "$(dirname "$0")"
-. ./test-lib.sh
-
-H=helper/test-json-parser
-
-# EINVAL=22, E2BIG=7, ENOSPC=28 on Linux.
-
-test_expect_success 'valid event: extracts digest, skips nested set[]' '
- out=$(printf "%s" "{\"schema\":1,\"digest\":\"abcdef\",\"type\":\"init\",\"set\":[{\"name\":\"x\"}]}" | ./$H) &&
- echo "$out" | grep -qx "rc=0" &&
- echo "$out" | grep -q "digest=abcdef" &&
- echo "$out" | grep -q "type=init" &&
- echo "$out" | grep -q "schema=1"
-'
-
-test_expect_success 'unknown keys and nested objects are skipped' '
- printf "%s" "{\"a\":{\"b\":[1,2,3]},\"digest\":\"ff\",\"c\":true}" | ./$H | grep -q "digest=ff"
-'
-
-test_expect_success 'unconfigured schema entries skip unknown keys without crashing' '
- printf "%s" "{\"unknown\":true,\"digest\":\"ff\"}" | ./$H | grep -q "digest=ff"
-'
-
-test_expect_success 'discarded object key colliding with schema does not mis-dispatch' '
- out=$(printf "%s" "{\"a\":{\"digest\":123,\"type\":true},\"digest\":\"ff\",\"type\":\"init\"}" | ./$H) &&
- echo "$out" | grep -qx "rc=0" &&
- echo "$out" | grep -q "digest=ff" &&
- echo "$out" | grep -q "type=init"
-'
-
-# A value read as a C string would end at the NUL and pass checks
-# meant for the whole value; a key would be dispatched as its prefix.
-test_expect_success 'an escaped NUL in a value or a key -> EINVAL' '
- printf "%s" "{\"digest\":\"ab\\u0000cd\"}" | ./$H | grep -qx "rc=-22" &&
- printf "%s" "{\"digest\\u0000x\":\"ff\"}" | ./$H | grep -qx "rc=-22" &&
- printf "%s" "{\"digest\":\"ab\\u0041\"}" | ./$H | grep -q "digest=abA"
-'
-
-test_expect_success 'unterminated string -> EINVAL' '
- printf "%s" "{\"digest\":\"abc" | ./$H | grep -qx "rc=-22"
-'
-
-test_expect_success 'trailing garbage -> EINVAL' '
- printf "%s" "{\"digest\":\"a\"}xyz" | ./$H | grep -qx "rc=-22"
-'
-
-test_expect_success 'type mismatch (digest as number) -> EINVAL' '
- printf "%s" "{\"digest\":123}" | ./$H | grep -qx "rc=-22"
-'
-
-test_expect_success 'over-long string value -> ENOSPC' '
- big=$(printf "%0200d" 0) &&
- printf "%s" "{\"digest\":\"$big\"}" | ./$H | grep -qx "rc=-28"
-'
-
-test_expect_success 'over-deep nesting -> E2BIG (no crash)' '
- doc=$(python3 -c "print(\"{\\\"a\\\":\"*40 + \"1\" + \"}\"*40, end=\"\")") &&
- printf "%s" "$doc" | ./$H | grep -qx "rc=-7"
-'
-
-test_expect_success 'non-JSON garbage -> clean error, no crash' '
- head -c 4096 /dev/urandom | ./$H >/dev/null 2>&1; rc=$?; [ "$rc" -ne 139 ]
-'
-
-test_expect_success 'empty object parses, no fields seen' '
- printf "%s" "{}" | ./$H | grep -qx "rc=0"
-'
-
-test_expect_success 'string array dispatched element-by-element' '
- out=$(printf "%s" "{\"names\":[\"alpha\",\"beta\",\"gamma\"]}" | ./$H) &&
- echo "$out" | grep -qx "rc=0" &&
- echo "$out" | grep -q "names=3 first_name=alpha"
-'
-
-test_expect_success 'object array dispatched into element sub-schema' '
- out=$(printf "%s" "{\"events\":[{\"type\":\"init\",\"digest\":\"00\"},{\"type\":\"upatch_load\",\"digest\":\"ff\"}]}" | ./$H) &&
- echo "$out" | grep -qx "rc=0" &&
- echo "$out" | grep -q "events=2 last_type=upatch_load"
-'
-
-test_expect_success 'empty array parses, zero elements' '
- printf "%s" "{\"names\":[]}" | ./$H | grep -q "names=0 "
-'
-
-test_expect_success 'array nested in object element, plus trailing scalars' '
- out=$(printf "%s" "{\"events\":[{\"type\":\"init\",\"digest\":\"aa\",\"set\":[{\"name\":\"x\"}]}],\"digest\":\"top\"}" | ./$H) &&
- echo "$out" | grep -q "digest=top" &&
- echo "$out" | grep -q "events=1 last_type=init"
-'
-
-test_expect_success 'array element type mismatch -> EINVAL (no crash)' '
- printf "%s" "{\"names\":[123]}" | ./$H | grep -qx "rc=-22"
-'
-
-test_expect_success 'unterminated array -> EINVAL (no crash)' '
- printf "%s" "{\"names\":[\"a\",\"b\"" | ./$H | grep -qx "rc=-22"
-'
-
-test_expect_success 'an integer field takes an integer as JSON spells one' '
- for v in 1.9 1e9 1E9 7-3 1+1 - . 01 -01 1. .5 1e --1 0x1 +1; do
- printf "%s" "{\"schema\":$v}" | ./$H | grep -qx "rc=-22" ||
- return 1
- done
-'
-
-test_expect_success 'an integer field holds any __s64 and nothing past it' '
- printf "%s" "{\"schema\":-9223372036854775808}" | ./$H |
- grep -q "schema=-9223372036854775808 " &&
- printf "%s" "{\"schema\":9223372036854775807}" | ./$H |
- grep -q "schema=9223372036854775807 " &&
- printf "%s" "{\"schema\":-0}" | ./$H | grep -q "schema=0 " &&
- printf "%s" "{\"schema\":9223372036854775808}" | ./$H |
- grep -qx "rc=-22" &&
- printf "%s" "{\"schema\":-9223372036854775809}" | ./$H |
- grep -qx "rc=-22"
-'
-
-test_expect_success 'a number in a discarded value follows the grammar too' '
- printf "%s" "{\"x\":[1.5e+10,-0.0E-1,2e5,0],\"schema\":2}" | ./$H |
- grep -q "schema=2 " &&
- for v in 1. 01 1e+ -.5 1.e5; do
- printf "%s" "{\"x\":$v,\"schema\":2}" | ./$H |
- grep -qx "rc=-22" || return 1
- done
-'
-
-test_done
diff --git a/testing/unit/json.c b/testing/unit/json.c
new file mode 100644
index 00000000..a9982d47
--- /dev/null
+++ b/testing/unit/json.c
@@ -0,0 +1,294 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <stdint.h>
+
+#include <shirudo/string.h>
+
+#include "json.h"
+#include "unit.h"
+
+/* What an eventlog-shaped schema extracts from one document. */
+struct doc {
+ char digest[65];
+ char type[64];
+ __s64 schema;
+ int names;
+ char first_name[64];
+ char name[64];
+ int events;
+ char last_type[32];
+ char ev_type[32];
+ char ev_digest[65];
+};
+
+static int name_on_elem(void *ctx)
+{
+ struct doc *d = ctx;
+
+ if (!d->names++)
+ strscpy(d->first_name, d->name, sizeof(d->first_name));
+ return 0;
+}
+
+static int event_on_elem(void *ctx)
+{
+ struct doc *d = ctx;
+
+ strscpy(d->last_type, d->ev_type, sizeof(d->last_type));
+ d->events++;
+ return 0;
+}
+
+static int parse_len(const char *text, size_t len, struct doc *d)
+{
+ struct shirudo_json_array event_sink = { event_on_elem, d };
+ struct shirudo_json_array name_sink = { name_on_elem, d };
+ struct shirudo_json_field name_sub[] = {
+ { "", SHIRUDO_JSON_T_STRING, d->name, sizeof(d->name),
+ 0, NULL, 0 },
+ };
+ struct shirudo_json_field event_sub[] = {
+ { "type", SHIRUDO_JSON_T_STRING, d->ev_type,
+ sizeof(d->ev_type), 0, NULL, 0 },
+ { "digest", SHIRUDO_JSON_T_STRING, d->ev_digest,
+ sizeof(d->ev_digest), 0, NULL, 0 },
+ };
+ struct shirudo_json_field f[] = {
+ { "schema", SHIRUDO_JSON_T_INT, &d->schema, 0, 0, NULL, 0 },
+ { "type", SHIRUDO_JSON_T_STRING, d->type, sizeof(d->type),
+ 0, NULL, 0 },
+ { "digest", SHIRUDO_JSON_T_STRING, d->digest,
+ sizeof(d->digest), 0, NULL, 0 },
+ { "names", SHIRUDO_JSON_T_ARRAY, &name_sink, 0, 0,
+ name_sub, ARRAY_SIZE(name_sub) },
+ { "events", SHIRUDO_JSON_T_ARRAY, &event_sink, 0, 0,
+ event_sub, ARRAY_SIZE(event_sub) },
+ /* An empty schema slot must be ignored, not dereferenced. */
+ { NULL, 0, NULL, 0, 0, NULL, 0 },
+ };
+
+ memset(d, 0, sizeof(*d));
+ d->schema = -1;
+ return json_parse(text, len, f, ARRAY_SIZE(f));
+}
+
+static int parse(const char *text, struct doc *d)
+{
+ return parse_len(text, strlen(text), d);
+}
+
+static void extracts_scalars_and_skips_nested_sets(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"schema\":1,\"digest\":\"abcdef\",\"type\":\"init\","
+ "\"set\":[{\"name\":\"x\"}]}", &d), 0);
+ EXPECT_STREQ(d.digest, "abcdef");
+ EXPECT_STREQ(d.type, "init");
+ EXPECT_EQ(d.schema, 1);
+}
+
+static void skips_unknown_keys_and_objects(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"a\":{\"b\":[1,2,3]},\"digest\":\"ff\",\"c\":true}",
+ &d), 0);
+ EXPECT_STREQ(d.digest, "ff");
+ ASSERT_EQ(parse("{\"unknown\":true,\"digest\":\"ff\"}", &d), 0);
+ EXPECT_STREQ(d.digest, "ff");
+}
+
+static void discarded_key_does_not_dispatch(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"a\":{\"digest\":123,\"type\":true},"
+ "\"digest\":\"ff\",\"type\":\"init\"}", &d), 0);
+ EXPECT_STREQ(d.digest, "ff");
+ EXPECT_STREQ(d.type, "init");
+}
+
+/*
+ * A value read as a C string would end at the NUL and pass checks meant
+ * for the whole value; a key would be dispatched as its prefix.
+ */
+static void escaped_nul_is_refused(void)
+{
+ struct doc d;
+
+ EXPECT_EQ(parse("{\"digest\":\"ab\\u0000cd\"}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"digest\\u0000x\":\"ff\"}", &d), -EINVAL);
+ ASSERT_EQ(parse("{\"digest\":\"ab\\u0041\"}", &d), 0);
+ EXPECT_STREQ(d.digest, "abA");
+}
+
+static void malformed_documents_are_refused(void)
+{
+ struct doc d;
+
+ EXPECT_EQ(parse("{\"digest\":\"abc", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"digest\":\"a\"}xyz", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"digest\":123}", &d), -EINVAL);
+}
+
+static void overlong_string_is_enospc(void)
+{
+ char text[256], big[201];
+ struct doc d;
+
+ memset(big, '0', sizeof(big) - 1);
+ big[sizeof(big) - 1] = '\0';
+ ASSERT_TRUE(strsprintf(text, sizeof(text), "{\"digest\":\"%s\"}",
+ big) > 0);
+ EXPECT_EQ(parse(text, &d), -ENOSPC);
+}
+
+static void deep_nesting_is_e2big(void)
+{
+ char text[256];
+ size_t n = 0;
+ struct doc d;
+
+ for (int i = 0; i < 40; i++) {
+ memcpy(text + n, "{\"a\":", 5);
+ n += 5;
+ }
+ text[n++] = '1';
+ memset(text + n, '}', 40);
+ n += 40;
+ EXPECT_EQ(parse_len(text, n, &d), -E2BIG);
+}
+
+static void garbage_fails_cleanly(void)
+{
+ static char buf[4096];
+ __u64 x = 0x9e3779b97f4a7c15ULL;
+ struct doc d;
+
+ for (int r = 0; r < 64; r++) {
+ for (size_t i = 0; i < sizeof(buf); i++) {
+ x ^= x << 13;
+ x ^= x >> 7;
+ x ^= x << 17;
+ buf[i] = (char)x;
+ }
+ EXPECT_TRUE(parse_len(buf, sizeof(buf), &d) <= 0);
+ }
+}
+
+static void empty_object_parses(void)
+{
+ struct doc d;
+
+ EXPECT_EQ(parse("{}", &d), 0);
+}
+
+static void string_array_dispatches_each_element(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"names\":[\"alpha\",\"beta\",\"gamma\"]}", &d), 0);
+ EXPECT_EQ(d.names, 3);
+ EXPECT_STREQ(d.first_name, "alpha");
+ ASSERT_EQ(parse("{\"names\":[]}", &d), 0);
+ EXPECT_EQ(d.names, 0);
+}
+
+static void object_array_uses_element_schema(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"events\":[{\"type\":\"init\",\"digest\":\"00\"},"
+ "{\"type\":\"upatch_load\",\"digest\":\"ff\"}]}", &d), 0);
+ EXPECT_EQ(d.events, 2);
+ EXPECT_STREQ(d.last_type, "upatch_load");
+}
+
+static void nested_array_then_trailing_scalar(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"events\":[{\"type\":\"init\",\"digest\":\"aa\","
+ "\"set\":[{\"name\":\"x\"}]}],\"digest\":\"top\"}", &d), 0);
+ EXPECT_STREQ(d.digest, "top");
+ EXPECT_EQ(d.events, 1);
+ EXPECT_STREQ(d.last_type, "init");
+}
+
+static void malformed_arrays_are_refused(void)
+{
+ struct doc d;
+
+ EXPECT_EQ(parse("{\"names\":[123]}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"names\":[\"a\",\"b\"", &d), -EINVAL);
+}
+
+static void integer_takes_json_integers_only(void)
+{
+ static const char *const bad[] = {
+ "1.9", "1e9", "1E9", "7-3", "1+1", "-", ".", "01", "-01", "1.",
+ ".5", "1e", "--1", "0x1", "+1",
+ };
+ char text[64];
+ struct doc d;
+
+ for (size_t i = 0; i < ARRAY_SIZE(bad); i++) {
+ ASSERT_TRUE(strsprintf(text, sizeof(text), "{\"schema\":%s}",
+ bad[i]) > 0);
+ EXPECT_EQ(parse(text, &d), -EINVAL);
+ }
+}
+
+static void integer_holds_any_s64_and_no_more(void)
+{
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"schema\":-9223372036854775808}", &d), 0);
+ EXPECT_EQ(d.schema, INT64_MIN);
+ ASSERT_EQ(parse("{\"schema\":9223372036854775807}", &d), 0);
+ EXPECT_EQ(d.schema, INT64_MAX);
+ ASSERT_EQ(parse("{\"schema\":-0}", &d), 0);
+ EXPECT_EQ(d.schema, 0);
+ EXPECT_EQ(parse("{\"schema\":9223372036854775808}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"schema\":-9223372036854775809}", &d), -EINVAL);
+}
+
+static void discarded_number_follows_the_grammar(void)
+{
+ static const char *const bad[] = { "1.", "01", "1e+", "-.5", "1.e5" };
+ char text[64];
+ struct doc d;
+
+ ASSERT_EQ(parse("{\"x\":[1.5e+10,-0.0E-1,2e5,0],\"schema\":2}", &d), 0);
+ EXPECT_EQ(d.schema, 2);
+ for (size_t i = 0; i < ARRAY_SIZE(bad); i++) {
+ ASSERT_TRUE(strsprintf(text, sizeof(text),
+ "{\"x\":%s,\"schema\":2}", bad[i]) > 0);
+ EXPECT_EQ(parse(text, &d), -EINVAL);
+ }
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(extracts_scalars_and_skips_nested_sets),
+ UNIT_CASE(skips_unknown_keys_and_objects),
+ UNIT_CASE(discarded_key_does_not_dispatch),
+ UNIT_CASE(escaped_nul_is_refused),
+ UNIT_CASE(malformed_documents_are_refused),
+ UNIT_CASE(overlong_string_is_enospc),
+ UNIT_CASE(deep_nesting_is_e2big),
+ UNIT_CASE(garbage_fails_cleanly),
+ UNIT_CASE(empty_object_parses),
+ UNIT_CASE(string_array_dispatches_each_element),
+ UNIT_CASE(object_array_uses_element_schema),
+ UNIT_CASE(nested_array_then_trailing_scalar),
+ UNIT_CASE(malformed_arrays_are_refused),
+ UNIT_CASE(integer_takes_json_integers_only),
+ UNIT_CASE(integer_holds_any_s64_and_no_more),
+ UNIT_CASE(discarded_number_follows_the_grammar),
+ {}
+};
+
+UNIT_SUITE(json, cases);
diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.sh
index 3aeda861..f51709b1 100755
--- a/testing/vm/build-bundle.sh
+++ b/testing/vm/build-bundle.sh
@@ -321,7 +321,6 @@ install -d "$TMP_DIR/test-helpers/testing/helper" \
while IFS= read -r helper; do
install -m 0755 "$REPO_ROOT/$helper" "$TMP_DIR/test-helpers/$helper"
done <<'EOF'
-testing/helper/test-json-parser
testing/helper/test-chain
testing/helper/test-register
testing/helper/test-register-cli