summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_sprintf.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:39 +0000
commit5242eef8fc54636a41701fd9d7083ba6e4a4e0b3 (patch)
treee6a0980092957865a937cc0f34446df3d5194e99 /tests/unit/test_sprintf.c
parentReleasing progress-linux version 1:4.13+dfsg1-5~progress7.99u1. (diff)
downloadshadow-5242eef8fc54636a41701fd9d7083ba6e4a4e0b3.tar.xz
shadow-5242eef8fc54636a41701fd9d7083ba6e4a4e0b3.zip
Merging upstream version 1:4.15.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/unit/test_sprintf.c')
-rw-r--r--tests/unit/test_sprintf.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/unit/test_sprintf.c b/tests/unit/test_sprintf.c
new file mode 100644
index 0000000..bedcff6
--- /dev/null
+++ b/tests/unit/test_sprintf.c
@@ -0,0 +1,66 @@
+// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include <stdarg.h> // Required by <cmocka.h>
+#include <stddef.h> // Required by <cmocka.h>
+#include <setjmp.h> // Required by <cmocka.h>
+#include <stdint.h> // Required by <cmocka.h>
+#include <cmocka.h>
+
+#include "sizeof.h"
+#include "string/sprintf.h"
+
+
+static void test_SNPRINTF_trunc(void **state);
+static void test_SNPRINTF_ok(void **state);
+
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_SNPRINTF_trunc),
+ cmocka_unit_test(test_SNPRINTF_ok),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
+
+
+static void
+test_SNPRINTF_trunc(void **state)
+{
+ char buf[NITEMS("foo")];
+
+ // Test that we're not returning SIZE_MAX
+ assert_true(SNPRINTF(buf, "f%su", "oo") < 0);
+ assert_true(strcmp(buf, "foo") == 0);
+
+ assert_true(SNPRINTF(buf, "barbaz") == -1);
+ assert_true(strcmp(buf, "bar") == 0);
+}
+
+
+static void
+test_SNPRINTF_ok(void **state)
+{
+ char buf[NITEMS("foo")];
+
+ assert_true(SNPRINTF(buf, "%s", "foo") == strlen("foo"));
+ assert_true(strcmp(buf, "foo") == 0);
+
+ assert_true(SNPRINTF(buf, "%do", 1) == strlen("1o"));
+ assert_true(strcmp(buf, "1o") == 0);
+
+ assert_true(SNPRINTF(buf, "f") == strlen("f"));
+ assert_true(strcmp(buf, "f") == 0);
+
+ assert_true(SNPRINTF(buf, "") == strlen(""));
+ assert_true(strcmp(buf, "") == 0);
+}