mirror of
git://github.com/lxc/lxc
synced 2025-08-29 23:10:45 +00:00
tests: add unit tests for lxc_safe_int()
Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
This commit is contained in:
parent
b5f845e7e0
commit
aa75cefcef
@ -221,6 +221,7 @@ non_test_error:
|
||||
if (fret == EXIT_SUCCESS)
|
||||
return;
|
||||
exit(fret);
|
||||
}
|
||||
|
||||
void test_lxc_safe_uint(void)
|
||||
{
|
||||
@ -251,6 +252,37 @@ void test_lxc_safe_uint(void)
|
||||
lxc_test_assert_abort((-ERANGE == lxc_safe_uint(uint_max, &n)));
|
||||
}
|
||||
|
||||
void test_lxc_safe_int(void)
|
||||
{
|
||||
int ret;
|
||||
signed int n;
|
||||
size_t len = /* 2^64 = 21 - 1 */ 21;
|
||||
char int_max[len];
|
||||
|
||||
ret = snprintf(int_max, len, "%ld", (signed long)INT_MAX + 1);
|
||||
if (ret < 0 || (size_t)ret >= len) {
|
||||
lxc_error("%s\n", "Failed to create string via snprintf().");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
lxc_test_assert_abort((0 == lxc_safe_int("1234345", &n)) && n == 1234345);
|
||||
lxc_test_assert_abort((0 == lxc_safe_int(" 345", &n)) && n == 345);
|
||||
lxc_test_assert_abort((0 == lxc_safe_int("-1234345", &n)) && n == -1234345);
|
||||
lxc_test_assert_abort((0 == lxc_safe_int(" -345", &n)) && n == -345);
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 3g45", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 345g", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345 ", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45 ", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g ", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int("g", &n)));
|
||||
lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
|
||||
lxc_test_assert_abort((-ERANGE == lxc_safe_int(int_max, &n)));
|
||||
}
|
||||
|
||||
void test_lxc_string_replace(void)
|
||||
{
|
||||
char *s;
|
||||
@ -310,6 +342,7 @@ int main(int argc, char *argv[])
|
||||
test_lxc_deslashify();
|
||||
test_detect_ramfs_rootfs();
|
||||
test_lxc_safe_uint();
|
||||
test_lxc_safe_int();
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user