Skip to content

eBPF-based security platform

testing: add a unit suite for iso8601() and format_duration()

Daniel Borkmannd44294ffb342
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index d49791de..501d1724 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -928,7 +928,7 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 39 of the modules in
+The unit suites in :src:`testing/unit/` cover 40 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,
@@ -937,8 +937,9 @@ The unit suites in :src:`testing/unit/` cover 39 of the modules in
``keepalive``, ``keepalive_quote``, ``kernel_build_id``,
``kernel_prereq``, ``kernel_release``, ``keys``, ``log``, ``measured``,
``options``,
-``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,
-``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and
+``paths``, ``string``, ``sysinfo``, ``timefmt``, ``upatch``,
+``upatch_decode``, ``upatch_pins``, ``upatch_resolve``, ``upatch_verify``
+and ``xdp``, and
``overflow`` the arithmetic of
:src:`include/shirudo/overflow.h`. ``jar`` covers the
``shirudo-build`` checks of a Java agent's jar in
diff --git a/testing/unit/timefmt.c b/testing/unit/timefmt.c
new file mode 100644
index 00000000..a6895960
--- /dev/null
+++ b/testing/unit/timefmt.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <string.h>
+
+#include <shirudo/misc.h>
+
+#include "timefmt.h"
+#include "unit.h"
+
+#define EPOCH "1970-01-01T00:00:00Z"
+
+static void unterminated(char *out, size_t cap)
+{
+ memset(out, 'X', cap - 1);
+ out[cap - 1] = '\0';
+}
+
+static void iso8601_is_utc_to_the_second(void)
+{
+ char out[32];
+
+ ASSERT_EQ(iso8601(0, out, sizeof(EPOCH)), 0);
+ EXPECT_STREQ(out, EPOCH);
+ ASSERT_EQ(iso8601(1700000000, out, sizeof(out)), 0);
+ EXPECT_STREQ(out, "2023-11-14T22:13:20Z");
+}
+
+/*
+ * The eventlog, status and upatch get show what iso8601() left without
+ * looking at its result.
+ */
+static void iso8601_leaves_an_empty_string_when_it_fails(void)
+{
+ char out[32];
+
+ unterminated(out, sizeof(out));
+ EXPECT_EQ(iso8601(0, out, sizeof(EPOCH) - 1), -EINVAL);
+ EXPECT_STREQ(out, "");
+ unterminated(out, sizeof(out));
+ EXPECT_EQ(iso8601((time_t)1 << 62, out, sizeof(out)), -EINVAL);
+ EXPECT_STREQ(out, "");
+}
+
+static void format_duration_shows_the_largest_whole_unit(void)
+{
+ static const struct {
+ __s64 seconds;
+ const char *text;
+ } durations[] = {
+ { 0, "0s" }, { 59, "59s" }, { 60, "1m" }, { 3599, "59m" },
+ { 3600, "1h" }, { 86399, "23h" }, { 86400, "1d" },
+ { 86400 * 400, "400d" }, { -1, "?" },
+ };
+ char out[16];
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(durations); i++) {
+ ASSERT_EQ(format_duration((long)durations[i].seconds, out,
+ sizeof(out)), 0);
+ EXPECT_STREQ(out, durations[i].text);
+ }
+}
+
+static void format_duration_leaves_an_empty_string_when_it_does_not_fit(void)
+{
+ char out[8];
+
+ unterminated(out, sizeof(out));
+ EXPECT_EQ(format_duration(3599, out, sizeof("59m") - 1), -E2BIG);
+ EXPECT_STREQ(out, "");
+ unterminated(out, sizeof(out));
+ EXPECT_EQ(format_duration(-1, out, sizeof("?") - 1), -E2BIG);
+ EXPECT_STREQ(out, "");
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(iso8601_is_utc_to_the_second),
+ UNIT_CASE(iso8601_leaves_an_empty_string_when_it_fails),
+ UNIT_CASE(format_duration_shows_the_largest_whole_unit),
+ UNIT_CASE(format_duration_leaves_an_empty_string_when_it_does_not_fit),
+ {}
+};
+
+UNIT_SUITE(timefmt, cases);