1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-07 01:27:11 +03:00

test-parse-util: verify that ato[ui] actually rejects trailing garbage

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-05-19 14:37:48 -04:00
parent 25f027c5ef
commit bf32e38576

View File

@ -395,6 +395,9 @@ static void test_safe_atou16(void) {
r = safe_atou16("junk", &l); r = safe_atou16("junk", &l);
assert_se(r == -EINVAL); assert_se(r == -EINVAL);
r = safe_atou16("123x", &l);
assert_se(r == -EINVAL);
} }
static void test_safe_atoi16(void) { static void test_safe_atoi16(void) {
@ -425,6 +428,70 @@ static void test_safe_atoi16(void) {
r = safe_atoi16("junk", &l); r = safe_atoi16("junk", &l);
assert_se(r == -EINVAL); assert_se(r == -EINVAL);
r = safe_atoi16("123x", &l);
assert_se(r == -EINVAL);
}
static void test_safe_atou64(void) {
int r;
uint64_t l;
r = safe_atou64("12345", &l);
assert_se(r == 0);
assert_se(l == 12345);
r = safe_atou64(" 12345", &l);
assert_se(r == 0);
assert_se(l == 12345);
r = safe_atou64("18446744073709551617", &l);
assert_se(r == -ERANGE);
r = safe_atou64("-1", &l);
assert_se(r == -ERANGE);
r = safe_atou64(" -1", &l);
assert_se(r == -ERANGE);
r = safe_atou64("junk", &l);
assert_se(r == -EINVAL);
r = safe_atou64("123x", &l);
assert_se(r == -EINVAL);
}
static void test_safe_atoi64(void) {
int r;
int64_t l;
r = safe_atoi64("-12345", &l);
assert_se(r == 0);
assert_se(l == -12345);
r = safe_atoi64(" -12345", &l);
assert_se(r == 0);
assert_se(l == -12345);
r = safe_atoi64("32767", &l);
assert_se(r == 0);
assert_se(l == 32767);
r = safe_atoi64(" 32767", &l);
assert_se(r == 0);
assert_se(l == 32767);
r = safe_atoi64("9223372036854775813", &l);
assert_se(r == -ERANGE);
r = safe_atoi64("-9223372036854775813", &l);
assert_se(r == -ERANGE);
r = safe_atoi64("junk", &l);
assert_se(r == -EINVAL);
r = safe_atoi64("123x", &l);
assert_se(r == -EINVAL);
} }
static void test_safe_atod(void) { static void test_safe_atod(void) {
@ -538,6 +605,8 @@ int main(int argc, char *argv[]) {
test_safe_atolli(); test_safe_atolli();
test_safe_atou16(); test_safe_atou16();
test_safe_atoi16(); test_safe_atoi16();
test_safe_atou64();
test_safe_atoi64();
test_safe_atod(); test_safe_atod();
test_parse_percent(); test_parse_percent();
test_parse_percent_unbounded(); test_parse_percent_unbounded();