From 3fcf65c35253e5f54906f5784f2695f3ce94ceb5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 9 Nov 2017 17:36:07 +0900 Subject: [PATCH] test-parse-util: add more tests, mainly for empty string --- src/test/test-parse-util.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c index 1b29b2e..c0abc81 100644 --- a/src/test/test-parse-util.c +++ b/src/test/test-parse-util.c @@ -79,6 +79,9 @@ static void test_parse_pid(void) { r = parse_pid("junk", &pid); assert_se(r == -EINVAL); + + r = parse_pid("", &pid); + assert_se(r == -EINVAL); } static void test_parse_mode(void) { @@ -98,6 +101,8 @@ static void test_parse_mode(void) { static void test_parse_size(void) { uint64_t bytes; + assert_se(parse_size("", 1024, &bytes) == -EINVAL); + assert_se(parse_size("111", 1024, &bytes) == 0); assert_se(bytes == 111); @@ -257,6 +262,10 @@ static void test_parse_range(void) { assert_se(lower == 9999); assert_se(upper == 9999); + assert_se(parse_range("-123", &lower, &upper) == -EINVAL); + assert_se(lower == 9999); + assert_se(upper == 9999); + assert_se(parse_range("-111-123", &lower, &upper) == -EINVAL); assert_se(lower == 9999); assert_se(upper == 9999); @@ -370,6 +379,15 @@ static void test_safe_atolli(void) { r = safe_atolli("junk", &l); assert_se(r == -EINVAL); + + r = safe_atolli("123x", &l); + assert_se(r == -EINVAL); + + r = safe_atolli("12.3", &l); + assert_se(r == -EINVAL); + + r = safe_atolli("", &l); + assert_se(r == -EINVAL); } static void test_safe_atou16(void) { @@ -398,6 +416,12 @@ static void test_safe_atou16(void) { r = safe_atou16("123x", &l); assert_se(r == -EINVAL); + + r = safe_atou16("12.3", &l); + assert_se(r == -EINVAL); + + r = safe_atou16("", &l); + assert_se(r == -EINVAL); } static void test_safe_atoi16(void) { @@ -431,6 +455,12 @@ static void test_safe_atoi16(void) { r = safe_atoi16("123x", &l); assert_se(r == -EINVAL); + + r = safe_atoi16("12.3", &l); + assert_se(r == -EINVAL); + + r = safe_atoi16("", &l); + assert_se(r == -EINVAL); } static void test_safe_atou64(void) { @@ -459,6 +489,12 @@ static void test_safe_atou64(void) { r = safe_atou64("123x", &l); assert_se(r == -EINVAL); + + r = safe_atou64("12.3", &l); + assert_se(r == -EINVAL); + + r = safe_atou64("", &l); + assert_se(r == -EINVAL); } static void test_safe_atoi64(void) { @@ -492,6 +528,12 @@ static void test_safe_atoi64(void) { r = safe_atoi64("123x", &l); assert_se(r == -EINVAL); + + r = safe_atoi64("12.3", &l); + assert_se(r == -EINVAL); + + r = safe_atoi64("", &l); + assert_se(r == -EINVAL); } static void test_safe_atod(void) { @@ -513,6 +555,9 @@ static void test_safe_atod(void) { strtod("0,5", &e); assert_se(*e == ','); + r = safe_atod("", &d); + assert_se(r == -EINVAL); + /* Check if this really is locale independent */ if (setlocale(LC_NUMERIC, "de_DE.utf8")) { @@ -525,6 +570,9 @@ static void test_safe_atod(void) { errno = 0; assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001); + + r = safe_atod("", &d); + assert_se(r == -EINVAL); } /* And check again, reset */ @@ -540,6 +588,9 @@ static void test_safe_atod(void) { errno = 0; strtod("0,5", &e); assert_se(*e == ','); + + r = safe_atod("", &d); + assert_se(r == -EINVAL); } static void test_parse_percent(void) { @@ -558,6 +609,7 @@ static void test_parse_percent(void) { assert_se(parse_percent("%%") == -EINVAL); assert_se(parse_percent("%1") == -EINVAL); assert_se(parse_percent("1%%") == -EINVAL); + assert_se(parse_percent("3.2%") == -EINVAL); } static void test_parse_percent_unbounded(void) { @@ -596,6 +648,8 @@ static void test_parse_nice(void) { static void test_parse_dev(void) { dev_t dev; + assert_se(parse_dev("", &dev) == -EINVAL); + assert_se(parse_dev("junk", &dev) == -EINVAL); assert_se(parse_dev("0", &dev) == -EINVAL); assert_se(parse_dev("5", &dev) == -EINVAL); assert_se(parse_dev("5:", &dev) == -EINVAL); -- 2.7.4