]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/unit/test_atoi_strtoi.c: Test strtoi_()
authorAlejandro Colomar <alx@kernel.org>
Sat, 6 Jan 2024 18:06:27 +0000 (19:06 +0100)
committerSerge Hallyn <serge@hallyn.com>
Thu, 1 Feb 2024 04:26:19 +0000 (22:26 -0600)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
tests/unit/Makefile.am
tests/unit/test_atoi_strtoi.c [new file with mode: 0644]

index 35a39bc3a4763e411b910965f070c4beaf899acd..fdc47ed04279563a51512a49be6e2da69f1a0097 100644 (file)
@@ -5,6 +5,7 @@ TESTS = $(check_PROGRAMS)
 
 check_PROGRAMS = \
     test_adds \
+    test_atoi_strtoi \
     test_atoi_strtou_noneg \
     test_chkname \
     test_sprintf \
@@ -33,6 +34,19 @@ test_adds_LDADD = \
     $(CMOCKA_LIBS) \
     $(NULL)
 
+test_atoi_strtoi_SOURCES = \
+    ../../lib/atoi/strtoi.c \
+    test_atoi_strtoi.c \
+    $(NULL)
+test_atoi_strtoi_CFLAGS = \
+    $(AM_FLAGS) \
+    $(NULL)
+test_atoi_strtoi_LDFLAGS = \
+    $(NULL)
+test_atoi_strtoi_LDADD = \
+    $(CMOCKA_LIBS) \
+    $(NULL)
+
 test_atoi_strtou_noneg_SOURCES = \
     ../../lib/atoi/strtou_noneg.c \
     test_atoi_strtou_noneg.c \
diff --git a/tests/unit/test_atoi_strtoi.c b/tests/unit/test_atoi_strtoi.c
new file mode 100644 (file)
index 0000000..c82b17d
--- /dev/null
@@ -0,0 +1,109 @@
+// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdint.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 "atoi/strtoi.h"
+
+
+static void test_strtoi(void **state);
+static void test_strtou(void **state);
+
+
+int
+main(void)
+{
+    const struct CMUnitTest  tests[] = {
+        cmocka_unit_test(test_strtoi),
+        cmocka_unit_test(test_strtou),
+    };
+
+    return cmocka_run_group_tests(tests, NULL, NULL);
+}
+
+
+static void
+test_strtoi(void **state)
+{
+       int   status;
+       char  *end;
+
+       errno = 0;
+       assert_true(strtoi_("42", NULL, -1, 1, 2, &status) == 1);
+       assert_true(status == EINVAL);
+
+       assert_true(strtoi_("40", &end, 5, INTMAX_MIN, INTMAX_MAX, &status) == 20);
+       assert_true(status == 0);
+       assert_true(strcmp(end, "") == 0);
+
+       assert_true(strtoi_("-40", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == -40);
+       assert_true(status == 0);
+       assert_true(strcmp(end, "") == 0);
+
+       assert_true(strtoi_("z", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == 0);
+       assert_true(status == ECANCELED);
+       assert_true(strcmp(end, "z") == 0);
+
+       assert_true(strtoi_(" 5", &end, 0, 3, 4, &status) == 4);
+       assert_true(status == ERANGE);
+       assert_true(strcmp(end, "") == 0);
+
+       assert_true(strtoi_("5z", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == 5);
+       assert_true(status == ENOTSUP);
+       assert_true(strcmp(end, "z") == 0);
+
+       assert_true(strtoi_("5z", &end, 0, INTMAX_MIN, 4, &status) == 4);
+       assert_true(status == ERANGE);
+       assert_true(strcmp(end, "z") == 0);
+
+       assert_true(errno == 0);
+}
+
+
+static void
+test_strtou(void **state)
+{
+       int   status;
+       char  *end;
+
+       errno = 0;
+       assert_true(strtou_("42", NULL, -1, 1, 2, &status) == 1);
+       assert_true(status == EINVAL);
+
+       assert_true(strtou_("40", &end, 5, 0, UINTMAX_MAX, &status) == 20);
+       assert_true(status == 0);
+       assert_true(strcmp(end, "") == 0);
+
+       assert_true(strtou_("-40", &end, 0, 0, UINTMAX_MAX, &status) == -40ull);
+       assert_true(status == 0);
+       assert_true(strcmp(end, "") == 0);
+
+       assert_true(strtou_("z", &end, 0, 0, UINTMAX_MAX, &status) == 0);
+       assert_true(status == ECANCELED);
+       assert_true(strcmp(end, "z") == 0);
+
+       assert_true(strtou_(" 5", &end, 0, 3, 4, &status) == 4);
+       assert_true(status == ERANGE);
+       assert_true(strcmp(end, "") == 0);
+
+       assert_true(strtou_("5z", &end, 0, 0, UINTMAX_MAX, &status) == 5);
+       assert_true(status == ENOTSUP);
+       assert_true(strcmp(end, "z") == 0);
+
+       assert_true(strtou_("5z", &end, 0, 0, 4, &status) == 4);
+       assert_true(status == ERANGE);
+       assert_true(strcmp(end, "z") == 0);
+
+       assert_true(errno == 0);
+}