strtonum-tests.c revision 9e2c64c6d4f5560e27207193efea6536a566865e
967e5f3c25249c779575864692935627004d3f9eChristian Maeder/*
967e5f3c25249c779575864692935627004d3f9eChristian Maeder SSSD
81d182b21020b815887e9057959228546cf61b6bChristian Maeder
f11f713bebd8e1e623a0a4361065df256033de47Christian Maeder InfoPipe
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maeder
967e5f3c25249c779575864692935627004d3f9eChristian Maeder Copyright (C) Stephen Gallagher <sgallagh@redhat.com> 2009
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maeder
967e5f3c25249c779575864692935627004d3f9eChristian Maeder This program is free software; you can redistribute it and/or modify
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian Maeder it under the terms of the GNU General Public License as published by
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder the Free Software Foundation; either version 3 of the License, or
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder (at your option) any later version.
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder
967e5f3c25249c779575864692935627004d3f9eChristian Maeder This program is distributed in the hope that it will be useful,
967e5f3c25249c779575864692935627004d3f9eChristian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
967e5f3c25249c779575864692935627004d3f9eChristian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
967e5f3c25249c779575864692935627004d3f9eChristian Maeder GNU General Public License for more details.
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder You should have received a copy of the GNU General Public License
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder*/
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder
ac19f8695aa1b2d2d1cd1319da2530edd8f46a96Christian Maeder#include <stdlib.h>
8b9fda012e5ee53b7b2320c0638896a0ff6e99f3Christian Maeder#include <check.h>
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#include <errno.h>
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder#include <popt.h>
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder#include "util/util.h"
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder#include "util/strtonum.h"
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#include "tests/common.h"
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder
967e5f3c25249c779575864692935627004d3f9eChristian Maeder/********************
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder * Utility routines *
78eeae099616e255ccf2e5f9122387bb10c68338Christian Maeder ********************/
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder#define EXPECT_UNSET_ERRNO(error) \
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder do { \
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maeder fail_unless(error == 0, "errno unexpectedly set to %d[%s]", \
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder error, strerror(error)); \
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder } while(0)
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder#define CHECK_RESULT(expected, actual) \
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder do { \
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder fail_unless(actual == expected, "Expected %ld, got %ld", \
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder expected, actual); \
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder } while(0)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder#define CHECK_ERRNO(expected, actual) \
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder do { \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder fail_unless(actual == expected, "Expected errno %d[%s], got %d[%s]", \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder expected, strerror(expected), \
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder actual, strerror(actual)); \
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder } while(0)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define CHECK_ENDPTR(expected, actual) \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder do { \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder fail_unless(actual == expected, "Expected endptr %p, got %p", \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder expected, actual); \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder } while(0)
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder#define CHECK_ZERO_ENDPTR(endptr) \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder do { \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder fail_unless(endptr && *endptr == '\0', "Invalid endptr"); \
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder } while(0)
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder/******************
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder * strtoint tests *
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder ******************/
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder/* Base-10 */
67d92da5e9610aabad39055a16031154b4dc3748Christian MaederSTART_TEST (test_strtoint32_pos_integer_base_10)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder{
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int32_t result;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *input = "123";
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int32_t expected = 123;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *endptr;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder errno_t error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder result = strtoint32(input, &endptr, 10);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder error = errno;
7a879b08ae0ca30006f9be887a73212b07f10204Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder EXPECT_UNSET_ERRNO(error);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder CHECK_ZERO_ENDPTR(endptr);
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder CHECK_RESULT(expected, result);
a89389521ddf76109168a0b339031575aafbd512Christian Maeder}
a89389521ddf76109168a0b339031575aafbd512Christian MaederEND_TEST
a89389521ddf76109168a0b339031575aafbd512Christian Maeder
a89389521ddf76109168a0b339031575aafbd512Christian MaederSTART_TEST (test_strtoint32_neg_integer_base_10)
a89389521ddf76109168a0b339031575aafbd512Christian Maeder{
a89389521ddf76109168a0b339031575aafbd512Christian Maeder int32_t result;
a89389521ddf76109168a0b339031575aafbd512Christian Maeder const char *input = "-123";
a89389521ddf76109168a0b339031575aafbd512Christian Maeder int32_t expected = -123;
a89389521ddf76109168a0b339031575aafbd512Christian Maeder char *endptr;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder errno_t error;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder result = strtoint32(input, &endptr, 10);
9b30898b139ee02f97ac933b6d935ef0a4206921Christian Maeder error = errno;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder EXPECT_UNSET_ERRNO(error);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder CHECK_ZERO_ENDPTR(endptr);
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder CHECK_RESULT(expected, result);
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder}
67d92da5e9610aabad39055a16031154b4dc3748Christian MaederEND_TEST
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian MaederSTART_TEST (test_strtoint32_pos_integer_intmax_base_10)
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder{
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder int32_t result;
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder const char *input = "2147483647";
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder int32_t expected = INT32_MAX;
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder char *endptr;
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder errno_t error;
51fb5d7edd9369c367dda2f8b15ddd6f8a146606Christian Maeder
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder result = strtoint32(input, &endptr, 10);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder error = errno;
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder EXPECT_UNSET_ERRNO(error);
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder CHECK_ZERO_ENDPTR(endptr);
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian Maeder CHECK_RESULT(expected, result);
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder}
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian MaederEND_TEST
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder
ee93fb771fcf3000d73c8e2f2000adb4b9a5158cChristian MaederSTART_TEST (test_strtoint32_neg_integer_intmin_base_10)
67086e0fe40a985c5e8a3cf50e611f43234580c2Christian Maeder{
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder int32_t result;
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder const char *input = "-2147483648";
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder int32_t expected = INT32_MIN;
d48085f765fca838c1d972d2123601997174583dChristian Maeder char *endptr;
d48085f765fca838c1d972d2123601997174583dChristian Maeder errno_t error;
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder
d48085f765fca838c1d972d2123601997174583dChristian Maeder result = strtoint32(input, &endptr, 10);
d48085f765fca838c1d972d2123601997174583dChristian Maeder error = errno;
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder
d48085f765fca838c1d972d2123601997174583dChristian Maeder EXPECT_UNSET_ERRNO(error);
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder CHECK_ZERO_ENDPTR(endptr);
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder CHECK_RESULT(expected, result);
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder}
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian MaederEND_TEST
9b30898b139ee02f97ac933b6d935ef0a4206921Christian Maeder
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian MaederSTART_TEST (test_strtoint32_pos_integer_overflow_base_10)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder{
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder int32_t result;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *input = "8589934592";
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder int32_t expected = INT32_MAX;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *endptr;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder errno_t error;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder result = strtoint32(input, &endptr, 10);
67d92da5e9610aabad39055a16031154b4dc3748Christian Maeder error = errno;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder CHECK_ERRNO(ERANGE, error);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder CHECK_ZERO_ENDPTR(endptr);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder CHECK_RESULT(expected, result);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder}
f11f713bebd8e1e623a0a4361065df256033de47Christian MaederEND_TEST
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder
967e5f3c25249c779575864692935627004d3f9eChristian MaederSTART_TEST (test_strtoint32_pos_integer_underflow_base_10)
18b513ff41708f24e1a7407f36b719add813ffeaChristian Maeder{
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder int32_t result;
a89e661aad28f1b39f4fc9f9f9a4d46074234123Christian Maeder const char *input = "-8589934592";
a23e572c8f957cc051a1b0831abd6fe9380d45c7Christian Maeder int32_t expected = INT32_MIN;
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder char *endptr;
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder errno_t error;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder result = strtoint32(input, &endptr, 10);
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu error = errno;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder
81d182b21020b815887e9057959228546cf61b6bChristian Maeder CHECK_ERRNO(ERANGE, error);
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder CHECK_ZERO_ENDPTR(endptr);
242397ba0f1cc490e892130bf0df239deeecf5daChristian Maeder CHECK_RESULT(expected, result);
2e9985cd67e4f2414becb670ef33b8f16513e41dChristian Maeder}
242397ba0f1cc490e892130bf0df239deeecf5daChristian MaederEND_TEST
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian MaederSTART_TEST (test_strtoint32_mixed_alphanumeric_base_10)
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder{
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder int32_t result;
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder const char *input = "12b13";
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder int32_t expected = 12;
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder char *endptr;
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder const char *expected_endptr = input+2;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder errno_t error;
d769b9ca726a9b50d661847c0e58c41d6ef334b4Christian Maeder
58b5ac21d1c88344246aaedab0c0bdc7b759d7c6Christian Maeder result = strtoint32(input, &endptr, 10);
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder error = errno;
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder EXPECT_UNSET_ERRNO(error);
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder CHECK_ENDPTR(expected_endptr, endptr);
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder CHECK_RESULT(expected, result);
842eedc62639561781b6c33533d1949693ef6cc5Christian Maeder}
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian MaederEND_TEST
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian MaederSTART_TEST (test_strtoint32_alphaonly_base_10)
842eedc62639561781b6c33533d1949693ef6cc5Christian Maeder{
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till Mossakowski int32_t result;
967e5f3c25249c779575864692935627004d3f9eChristian Maeder const char *input = "alpha";
967e5f3c25249c779575864692935627004d3f9eChristian Maeder int32_t expected = 0;
f2ee9fc53048ea92bad79e3f5d292d83efd7f8beMihai Codescu char *endptr;
967e5f3c25249c779575864692935627004d3f9eChristian Maeder const char *expected_endptr = input;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder errno_t error;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder result = strtoint32(input, &endptr, 10);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder error = errno;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder EXPECT_UNSET_ERRNO(error);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_ENDPTR(expected_endptr, endptr);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_RESULT(expected, result);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder}
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian MaederEND_TEST
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian MaederSTART_TEST (test_strtoint32_alphastart_base_10)
967e5f3c25249c779575864692935627004d3f9eChristian Maeder{
967e5f3c25249c779575864692935627004d3f9eChristian Maeder int32_t result;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder const char *input = "alpha12345";
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder int32_t expected = 0;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder char *endptr;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder const char *expected_endptr = input;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder errno_t error;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder result = strtoint32(input, &endptr, 10);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder error = errno;
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder EXPECT_UNSET_ERRNO(error);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_ENDPTR(expected_endptr, endptr);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_RESULT(expected, result);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder}
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian MaederEND_TEST
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian MaederSTART_TEST (test_strtoint32_emptystring_base_10)
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder{
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int32_t result;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *input = "";
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder int32_t expected = 0;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *endptr;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder const char *expected_endptr = input;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder errno_t error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder result = strtoint32(input, &endptr, 10);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder error = errno;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder EXPECT_UNSET_ERRNO(error);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_ENDPTR(expected_endptr, endptr);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_RESULT(expected, result);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder}
1a75698c909ad515d59c76e65bd783f015c21c4dChristian MaederEND_TEST
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder/*******************
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder * strtouint tests *
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder *******************/
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder/* Base-10 */
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian MaederSTART_TEST (test_strtouint32_pos_integer_base_10)
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder{
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder uint32_t result;
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder const char *input = "123";
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder uint32_t expected = 123;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *endptr;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder errno_t error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder result = strtouint32(input, &endptr, 10);
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder error = errno;
9659c509ce5e78adc51d7b02a76274eddcba9338Christian Maeder
551af0e4ba6d96bb24f3555f3b30ed648e22e34aChristian Maeder EXPECT_UNSET_ERRNO(error);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_ZERO_ENDPTR(endptr);
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder CHECK_RESULT(expected, result);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder}
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian MaederEND_TEST
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder
83814002b4922114cbe7e9ba728472a0bf44aac5Christian MaederSTART_TEST (test_strtouint32_neg_integer_base_10)
a95f5379cabb30d3beb0545002cf50e9e4fc2c86Christian Maeder{
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder uint32_t result;
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder const char *input = "-123";
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder uint32_t expected = UINT32_MAX;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder char *endptr;
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder errno_t error;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder result = strtouint32(input, &endptr, 10);
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder error = errno;
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder CHECK_ERRNO(ERANGE, error);
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder CHECK_ZERO_ENDPTR(endptr);
b66eb6038bfbcd2fe520d87c151bb4f1f510e985Christian Maeder CHECK_RESULT(expected, result);
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian Maeder}
2dfc7b04f2db681992ca04175f2beb0f127c9844Christian MaederEND_TEST
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian MaederSTART_TEST (test_strtouint32_pos_integer_uintmax_base_10)
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder{
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder uint32_t result;
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder const char *input = "4294967295";
1a75698c909ad515d59c76e65bd783f015c21c4dChristian Maeder uint32_t expected = UINT32_MAX;
588c0c022a0f4e129a89c3bc569daf6a835e182dChristian Maeder char *endptr;
ad187062b0009820118c1b773a232e29b879a2faChristian Maeder errno_t error;
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder
result = strtouint32(input, &endptr, 10);
error = errno;
EXPECT_UNSET_ERRNO(error);
CHECK_ZERO_ENDPTR(endptr);
CHECK_RESULT(expected, result);
}
END_TEST
START_TEST (test_strtouint32_pos_integer_overflow_base_10)
{
uint32_t result;
const char *input = "8589934592";
uint32_t expected = UINT32_MAX;
char *endptr;
errno_t error;
result = strtouint32(input, &endptr, 10);
error = errno;
CHECK_ERRNO(ERANGE, error);
CHECK_ZERO_ENDPTR(endptr);
CHECK_RESULT(expected, result);
}
END_TEST
START_TEST (test_strtouint32_mixed_alphanumeric_base_10)
{
uint32_t result;
const char *input = "12b13";
uint32_t expected = 12;
char *endptr;
const char *expected_endptr = input+2;
errno_t error;
result = strtouint32(input, &endptr, 10);
error = errno;
EXPECT_UNSET_ERRNO(error);
CHECK_ENDPTR(expected_endptr, endptr);
CHECK_RESULT(expected, result);
}
END_TEST
START_TEST (test_strtouint32_alphaonly_base_10)
{
uint32_t result;
const char *input = "alpha";
uint32_t expected = 0;
char *endptr;
const char *expected_endptr = input;
errno_t error;
result = strtouint32(input, &endptr, 10);
error = errno;
EXPECT_UNSET_ERRNO(error);
CHECK_ENDPTR(expected_endptr, endptr);
CHECK_RESULT(expected, result);
}
END_TEST
START_TEST (test_strtouint32_alphastart_base_10)
{
uint32_t result;
const char *input = "alpha12345";
uint32_t expected = 0;
char *endptr;
const char *expected_endptr = input;
errno_t error;
result = strtouint32(input, &endptr, 10);
error = errno;
EXPECT_UNSET_ERRNO(error);
CHECK_ENDPTR(expected_endptr, endptr);
CHECK_RESULT(expected, result);
}
END_TEST
START_TEST (test_strtouint32_emptystring_base_10)
{
uint32_t result;
const char *input = "";
uint32_t expected = 0;
char *endptr;
const char *expected_endptr = input;
errno_t error;
result = strtouint32(input, &endptr, 10);
error = errno;
EXPECT_UNSET_ERRNO(error);
CHECK_ENDPTR(expected_endptr, endptr);
CHECK_RESULT(expected, result);
}
END_TEST
Suite *create_strtonum_suite(void)
{
Suite *s = suite_create("strtonum");
TCase *tc_strtoint32 = tcase_create("strtoint32 Tests");
tcase_add_test(tc_strtoint32, test_strtoint32_pos_integer_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_neg_integer_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_pos_integer_intmax_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_neg_integer_intmin_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_pos_integer_overflow_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_pos_integer_underflow_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_mixed_alphanumeric_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_alphaonly_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_alphastart_base_10);
tcase_add_test(tc_strtoint32, test_strtoint32_emptystring_base_10);
TCase *tc_strtouint32 = tcase_create("strtouint32 Tests");
tcase_add_test(tc_strtouint32, test_strtouint32_pos_integer_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_neg_integer_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_pos_integer_uintmax_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_pos_integer_overflow_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_mixed_alphanumeric_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_alphaonly_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_alphastart_base_10);
tcase_add_test(tc_strtouint32, test_strtouint32_emptystring_base_10);
/* Add all test cases to the suite */
suite_add_tcase(s, tc_strtoint32);
suite_add_tcase(s, tc_strtouint32);
return s;
}
int main(int argc, const char *argv[]) {
int opt;
poptContext pc;
int failure_count;
Suite *strtonum_suite;
SRunner *sr;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_MAIN_OPTS
POPT_TABLEEND
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */
debug_level = SSSDBG_INVALID;
pc = poptGetContext(argv[0], argc, argv, long_options, 0);
while((opt = poptGetNextOpt(pc)) != -1) {
switch(opt) {
default:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
return 1;
}
}
poptFreeContext(pc);
DEBUG_INIT(debug_level);
tests_set_cwd();
strtonum_suite = create_strtonum_suite();
sr = srunner_create(strtonum_suite);
/* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
srunner_run_all(sr, CK_ENV);
failure_count = srunner_ntests_failed(sr);
srunner_free(sr);
return (failure_count==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}