Skip to content

Commit

Permalink
tests: lxc_safe_{u}int() add corner-case tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner authored and stgraber committed May 16, 2017
1 parent d3264db commit ba7e919
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/tests/lxc-test-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,22 @@ void test_detect_ramfs_rootfs(void)

void test_lxc_safe_uint(void)
{
int ret;
unsigned int n;
char numstr[LXC_NUMSTRLEN64];

lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" -123", &n)));
lxc_test_assert_abort((-EINVAL == lxc_safe_uint("-123", &n)));

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)UINT_MAX);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_uint(numstr, &n)) && n == UINT_MAX);

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)UINT_MAX + 1);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_uint(numstr, &n)));

lxc_test_assert_abort((0 == lxc_safe_uint("1234345", &n)) && n == 1234345);
lxc_test_assert_abort((0 == lxc_safe_uint(" 345", &n)) && n == 345);
Expand All @@ -247,7 +262,29 @@ void test_lxc_safe_uint(void)

void test_lxc_safe_int(void)
{
int ret;
signed int n;
char numstr[LXC_NUMSTRLEN64];

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)INT_MAX);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MAX);

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRIu64, (uint64_t)INT_MAX + 1);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRId64, (int64_t)INT_MIN);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MIN);

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%" PRId64, (int64_t)INT_MIN - 1);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
exit(EXIT_FAILURE);
lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));

lxc_test_assert_abort((0 == lxc_safe_int("1234345", &n)) && n == 1234345);
lxc_test_assert_abort((0 == lxc_safe_int(" 345", &n)) && n == 345);
Expand Down

0 comments on commit ba7e919

Please sign in to comment.