]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/unit/test_atoi_strtou_noneg.c: Test strtou[l]l_noneg()
authorAlejandro Colomar <alx@kernel.org>
Sat, 6 Jan 2024 17:00:19 +0000 (18:00 +0100)
committerSerge Hallyn <serge@hallyn.com>
Mon, 22 Jan 2024 23:17:15 +0000 (17:17 -0600)
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
tests/unit/Makefile.am
tests/unit/test_atoi_strtou_noneg.c [new file with mode: 0644]

index 993bc20072016f4040fa9cb6a7a54265087fddcb..35a39bc3a4763e411b910965f070c4beaf899acd 100644 (file)
@@ -5,6 +5,7 @@ TESTS = $(check_PROGRAMS)
 
 check_PROGRAMS = \
     test_adds \
+    test_atoi_strtou_noneg \
     test_chkname \
     test_sprintf \
     test_strncpy \
@@ -32,6 +33,19 @@ test_adds_LDADD = \
     $(CMOCKA_LIBS) \
     $(NULL)
 
+test_atoi_strtou_noneg_SOURCES = \
+    ../../lib/atoi/strtou_noneg.c \
+    test_atoi_strtou_noneg.c \
+    $(NULL)
+test_atoi_strtou_noneg_CFLAGS = \
+    $(AM_FLAGS) \
+    $(NULL)
+test_atoi_strtou_noneg_LDFLAGS = \
+    $(NULL)
+test_atoi_strtou_noneg_LDADD = \
+    $(CMOCKA_LIBS) \
+    $(NULL)
+
 test_chkname_SOURCES = \
     ../../lib/chkname.c \
     test_chkname.c \
diff --git a/tests/unit/test_atoi_strtou_noneg.c b/tests/unit/test_atoi_strtou_noneg.c
new file mode 100644 (file)
index 0000000..978bc0c
--- /dev/null
@@ -0,0 +1,76 @@
+// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.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/strtou_noneg.h"
+
+
+static void test_strtoul_noneg(void **state);
+static void test_strtoull_noneg(void **state);
+
+
+int
+main(void)
+{
+    const struct CMUnitTest  tests[] = {
+        cmocka_unit_test(test_strtoul_noneg),
+        cmocka_unit_test(test_strtoull_noneg),
+    };
+
+    return cmocka_run_group_tests(tests, NULL, NULL);
+}
+
+
+static void
+test_strtoul_noneg(void **state)
+{
+       errno = 0;
+       assert_true(strtoul_noneg("42", NULL, 0) == 42);
+       assert_true(errno == 0);
+
+       assert_true(strtoul_noneg("-1", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+       errno = 0;
+       assert_true(strtoul_noneg("-3", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+       errno = 0;
+       assert_true(strtoul_noneg("-0xFFFFFFFFFFFFFFFF", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+
+       errno = 0;
+       assert_true(strtoul_noneg("-0x10000000000000000", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+}
+
+
+static void
+test_strtoull_noneg(void **state)
+{
+       errno = 0;
+       assert_true(strtoull_noneg("42", NULL, 0) == 42);
+       assert_true(errno == 0);
+
+       assert_true(strtoull_noneg("-1", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+       errno = 0;
+       assert_true(strtoull_noneg("-3", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+       errno = 0;
+       assert_true(strtoull_noneg("-0xFFFFFFFFFFFFFFFF", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+
+       errno = 0;
+       assert_true(strtoull_noneg("-0x10000000000000000", NULL, 0) == 0);
+       assert_true(errno == ERANGE);
+}