From: Alejandro Colomar Date: Sat, 6 Jan 2024 17:00:19 +0000 (+0100) Subject: tests/unit/test_atoi_strtou_noneg.c: Test strtou[l]l_noneg() X-Git-Tag: 4.15.0-rc1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0138819b2abdc9ca020e169d49316b8da2bfc5ff;p=thirdparty%2Fshadow.git tests/unit/test_atoi_strtou_noneg.c: Test strtou[l]l_noneg() Reviewed-by: Iker Pedrosa Signed-off-by: Alejandro Colomar --- diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 993bc2007..35a39bc3a 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -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 index 000000000..978bc0c49 --- /dev/null +++ b/tests/unit/test_atoi_strtou_noneg.c @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include +#include +#include +#include + +#include // Required by +#include // Required by +#include // Required by +#include // Required by +#include + +#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); +}