From 694baf57ae103954e2337d1fa22d1dcdfa8e2f5a Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:45 +0100 Subject: test-qemu-opts: Cover qemu_opts_parse() The new tests demonstrate a few bugs, all clearly marked. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-2-git-send-email-armbru@redhat.com> [A few additional test cases squashed in, see Message-ID: <871supjijq.fsf@dusky.pond.sub.org>] --- tests/test-qemu-opts.c | 330 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 328 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index a505a3e..310485b 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -8,6 +8,7 @@ */ #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "qapi/error.h" #include "qapi/qmp/qstring.h" #include "qemu/config-file.h" @@ -29,6 +30,9 @@ static QemuOptsList opts_list_01 = { },{ .name = "number1", .type = QEMU_OPT_NUMBER, + },{ + .name = "number2", + .type = QEMU_OPT_NUMBER, }, { /* end of list */ } }, @@ -42,14 +46,23 @@ static QemuOptsList opts_list_02 = { .name = "str1", .type = QEMU_OPT_STRING, },{ + .name = "str2", + .type = QEMU_OPT_STRING, + },{ .name = "bool1", .type = QEMU_OPT_BOOL, },{ - .name = "str2", - .type = QEMU_OPT_STRING, + .name = "bool2", + .type = QEMU_OPT_BOOL, },{ .name = "size1", .type = QEMU_OPT_SIZE, + },{ + .name = "size2", + .type = QEMU_OPT_SIZE, + },{ + .name = "size3", + .type = QEMU_OPT_SIZE, }, { /* end of list */ } }, @@ -57,6 +70,7 @@ static QemuOptsList opts_list_02 = { static QemuOptsList opts_list_03 = { .name = "opts_list_03", + .implied_opt_name = "implied", .head = QTAILQ_HEAD_INITIALIZER(opts_list_03.head), .desc = { /* no elements => accept any params */ @@ -421,6 +435,314 @@ static void test_qemu_opts_set(void) g_assert(opts == NULL); } +static int opts_count_iter(void *opaque, const char *name, const char *value, + Error **errp) +{ + (*(size_t *)opaque)++; + return 0; +} + +static size_t opts_count(QemuOpts *opts) +{ + size_t n = 0; + + qemu_opt_foreach(opts, opts_count_iter, &n, NULL); + return n; +} + +static void test_opts_parse(void) +{ + Error *err = NULL; + QemuOpts *opts; + char long_key[129]; + char *params; + + /* Nothing */ + opts = qemu_opts_parse(&opts_list_03, "", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 0); + + /* Empty key */ + opts = qemu_opts_parse(&opts_list_03, "=val", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "val"); + + /* Long key */ + memset(long_key, 'a', 127); + long_key[127] = 'z'; + long_key[128] = 0; + params = g_strdup_printf("%s=v", long_key); + opts = qemu_opts_parse(&opts_list_03, params + 1, NULL, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opt_get(opts, long_key + 1), ==, "v"); + + /* Overlong key gets truncated */ + opts = qemu_opts_parse(&opts_list_03, params, NULL, &error_abort); + g_assert(opts_count(opts) == 1); + long_key[127] = 0; + g_assert_cmpstr(qemu_opt_get(opts, long_key), ==, "v"); + g_free(params); + + /* Multiple keys, last one wins */ + opts = qemu_opts_parse(&opts_list_03, "a=1,b=2,,x,a=3", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 3); + g_assert_cmpstr(qemu_opt_get(opts, "a"), ==, "3"); + g_assert_cmpstr(qemu_opt_get(opts, "b"), ==, "2,x"); + + /* Except when it doesn't */ + opts = qemu_opts_parse(&opts_list_03, "id=foo,id=bar", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 0); + g_assert_cmpstr(qemu_opts_id(opts), ==, "foo"); + + /* TODO Cover low-level access to repeated keys */ + + /* Trailing comma is ignored */ + opts = qemu_opts_parse(&opts_list_03, "x=y,", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opt_get(opts, "x"), ==, "y"); + + /* Except when it isn't */ + opts = qemu_opts_parse(&opts_list_03, ",", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "on"); + + /* Duplicate ID */ + opts = qemu_opts_parse(&opts_list_03, "x=y,id=foo", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + /* TODO Cover .merge_lists = true */ + + /* Buggy ID recognition */ + opts = qemu_opts_parse(&opts_list_03, "x=,,id=bar", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opts_id(opts), ==, "bar"); /* BUG */ + g_assert_cmpstr(qemu_opt_get(opts, "x"), ==, ",id=bar"); + + /* Anti-social ID */ + opts = qemu_opts_parse(&opts_list_01, "id=666", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + + /* Implied value */ + opts = qemu_opts_parse(&opts_list_03, "an,noaus,noaus=", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 3); + g_assert_cmpstr(qemu_opt_get(opts, "an"), ==, "on"); + g_assert_cmpstr(qemu_opt_get(opts, "aus"), ==, "off"); + g_assert_cmpstr(qemu_opt_get(opts, "noaus"), ==, ""); + + /* Implied key */ + opts = qemu_opts_parse(&opts_list_03, "an,noaus,noaus=", true, + &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 3); + g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, "an"); + g_assert_cmpstr(qemu_opt_get(opts, "aus"), ==, "off"); + g_assert_cmpstr(qemu_opt_get(opts, "noaus"), ==, ""); + + /* Implied key with empty value */ + opts = qemu_opts_parse(&opts_list_03, ",", true, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, ""); + + /* Implied key with comma value */ + opts = qemu_opts_parse(&opts_list_03, ",,,a=1", true, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, ","); + g_assert_cmpstr(qemu_opt_get(opts, "a"), ==, "1"); + + /* Empty key is not an implied key */ + opts = qemu_opts_parse(&opts_list_03, "=val", true, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "val"); + + /* Unknown key */ + opts = qemu_opts_parse(&opts_list_01, "nonexistent=", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + + qemu_opts_reset(&opts_list_01); + qemu_opts_reset(&opts_list_03); +} + +static void test_opts_parse_bool(void) +{ + Error *err = NULL; + QemuOpts *opts; + + opts = qemu_opts_parse(&opts_list_02, "bool1=on,bool2=off", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert(qemu_opt_get_bool(opts, "bool1", false)); + g_assert(!qemu_opt_get_bool(opts, "bool2", true)); + + opts = qemu_opts_parse(&opts_list_02, "bool1=offer", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + + qemu_opts_reset(&opts_list_02); +} + +static void test_opts_parse_number(void) +{ + Error *err = NULL; + QemuOpts *opts; + + /* Lower limit zero */ + opts = qemu_opts_parse(&opts_list_01, "number1=0", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 0); + + /* Upper limit 2^64-1 */ + opts = qemu_opts_parse(&opts_list_01, + "number1=18446744073709551615,number2=-1", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert_cmphex(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX); + g_assert_cmphex(qemu_opt_get_number(opts, "number2", 0), ==, UINT64_MAX); + + /* Above upper limit */ + opts = qemu_opts_parse(&opts_list_01, "number1=18446744073709551616", + false, &error_abort); + /* BUG: should reject */ + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX); + + /* Below lower limit */ + opts = qemu_opts_parse(&opts_list_01, "number1=-18446744073709551616", + false, &error_abort); + /* BUG: should reject */ + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX); + + /* Hex and octal */ + opts = qemu_opts_parse(&opts_list_01, "number1=0x2a,number2=052", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 42); + g_assert_cmpuint(qemu_opt_get_number(opts, "number2", 0), ==, 42); + + /* Invalid */ + opts = qemu_opts_parse(&opts_list_01, "number1=", false, &err); + /* BUG: should reject */ + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 0); + opts = qemu_opts_parse(&opts_list_01, "number1=eins", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + + /* Leading whitespace */ + opts = qemu_opts_parse(&opts_list_01, "number1= \t42", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 42); + + /* Trailing crap */ + opts = qemu_opts_parse(&opts_list_01, "number1=3.14", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + opts = qemu_opts_parse(&opts_list_01, "number1=08", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + opts = qemu_opts_parse(&opts_list_01, "number1=0 ", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + + qemu_opts_reset(&opts_list_01); +} + +static void test_opts_parse_size(void) +{ + Error *err = NULL; + QemuOpts *opts; + + /* Lower limit zero */ + opts = qemu_opts_parse(&opts_list_02, "size1=0", false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0); + + /* Note: precision is 53 bits since we're parsing with strtod() */ + + /* Around limit of precision: 2^53-1, 2^53, 2^54 */ + opts = qemu_opts_parse(&opts_list_02, + "size1=9007199254740991," + "size2=9007199254740992," + "size3=9007199254740993", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 3); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), + ==, 0x1fffffffffffff); + g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), + ==, 0x20000000000000); + g_assert_cmphex(qemu_opt_get_size(opts, "size3", 1), + ==, 0x20000000000000); + + /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */ + opts = qemu_opts_parse(&opts_list_02, + "size1=9223372036854774784," /* 7ffffffffffffc00 */ + "size2=9223372036854775295", /* 7ffffffffffffdff */ + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), + ==, 0x7ffffffffffffc00); + g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), + ==, 0x7ffffffffffffc00); + + /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */ + opts = qemu_opts_parse(&opts_list_02, + "size1=18446744073709549568," /* fffffffffffff800 */ + "size2=18446744073709550591", /* fffffffffffffbff */ + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1), + ==, 0xfffffffffffff800); + g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1), + ==, 0xfffffffffffff800); + + /* Beyond limits */ + opts = qemu_opts_parse(&opts_list_02, "size1=-1", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + opts = qemu_opts_parse(&opts_list_02, + "size1=18446744073709550592", /* fffffffffffffc00 */ + false, &error_abort); + /* BUG: should reject */ + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0); + + /* Suffixes */ + opts = qemu_opts_parse(&opts_list_02, "size1=8b,size2=1.5k,size3=2M", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 3); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), ==, 8); + g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), ==, 1536); + g_assert_cmphex(qemu_opt_get_size(opts, "size3", 0), ==, 2 * M_BYTE); + opts = qemu_opts_parse(&opts_list_02, "size1=0.1G,size2=16777215T", + false, &error_abort); + g_assert_cmpuint(opts_count(opts), ==, 2); + g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), ==, G_BYTE / 10); + g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), + ==, 16777215 * T_BYTE); + + /* Beyond limit with suffix */ + opts = qemu_opts_parse(&opts_list_02, "size1=16777216T", + false, &error_abort); + /* BUG: should reject */ + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0); + + /* Trailing crap */ + opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err); + error_free_or_abort(&err); + g_assert(!opts); + opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &error_abort); + /* BUG: should reject */ + g_assert_cmpuint(opts_count(opts), ==, 1); + g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 16 * G_BYTE); + + qemu_opts_reset(&opts_list_02); +} + int main(int argc, char *argv[]) { register_opts(); @@ -435,6 +757,10 @@ int main(int argc, char *argv[]) g_test_add_func("/qemu-opts/opt_unset", test_qemu_opt_unset); g_test_add_func("/qemu-opts/opts_reset", test_qemu_opts_reset); g_test_add_func("/qemu-opts/opts_set", test_qemu_opts_set); + g_test_add_func("/qemu-opts/opts_parse/general", test_opts_parse); + g_test_add_func("/qemu-opts/opts_parse/bool", test_opts_parse_bool); + g_test_add_func("/qemu-opts/opts_parse/number", test_opts_parse_number); + g_test_add_func("/qemu-opts/opts_parse/size", test_opts_parse_size); g_test_run(); return 0; } -- cgit v1.1 From 73245450b302931ad291de5d8d0f0cb5e9ca3ee6 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:47 +0100 Subject: test-cutils: Add missing qemu_strtol()... endptr checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Philippe Mathieu-Daudé Message-Id: <1487708048-2131-4-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 20b0f59..71681dc 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -262,6 +262,7 @@ static void test_qemu_strtol_empty(void) err = qemu_strtol(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtol_whitespace(void) @@ -275,6 +276,7 @@ static void test_qemu_strtol_whitespace(void) err = qemu_strtol(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtol_invalid(void) @@ -288,6 +290,7 @@ static void test_qemu_strtol_invalid(void) err = qemu_strtol(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtol_trailing(void) @@ -548,6 +551,7 @@ static void test_qemu_strtoul_empty(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoul_whitespace(void) @@ -561,6 +565,7 @@ static void test_qemu_strtoul_whitespace(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoul_invalid(void) @@ -574,6 +579,7 @@ static void test_qemu_strtoul_invalid(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoul_trailing(void) @@ -829,6 +835,7 @@ static void test_qemu_strtoll_empty(void) err = qemu_strtoll(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoll_whitespace(void) @@ -842,6 +849,7 @@ static void test_qemu_strtoll_whitespace(void) err = qemu_strtoll(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoll_invalid(void) @@ -855,6 +863,7 @@ static void test_qemu_strtoll_invalid(void) err = qemu_strtoll(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoll_trailing(void) @@ -1113,6 +1122,7 @@ static void test_qemu_strtoull_empty(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoull_whitespace(void) @@ -1126,6 +1136,7 @@ static void test_qemu_strtoull_whitespace(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoull_invalid(void) @@ -1139,6 +1150,7 @@ static void test_qemu_strtoull_invalid(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); + g_assert(endptr == str); } static void test_qemu_strtoull_trailing(void) -- cgit v1.1 From bc7c08a2c375acb7ae4d433054415588b176d34c Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:48 +0100 Subject: test-cutils: Clean up qemu_strtoul() result checks Use unsigned comparisons to check the result of qemu_strtoul() and strtoull(). Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-5-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 60 ++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 71681dc..749aaa9 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -523,7 +523,7 @@ static void test_qemu_strtoul_correct(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 12345); + g_assert_cmpuint(res, ==, 12345); g_assert(endptr == str + 5); } @@ -593,7 +593,7 @@ static void test_qemu_strtoul_trailing(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + 3); } @@ -608,7 +608,7 @@ static void test_qemu_strtoul_octal(void) err = qemu_strtoul(str, &endptr, 8, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0123); + g_assert_cmpuint(res, ==, 0123); g_assert(endptr == str + strlen(str)); res = 999; @@ -616,7 +616,7 @@ static void test_qemu_strtoul_octal(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0123); + g_assert_cmpuint(res, ==, 0123); g_assert(endptr == str + strlen(str)); } @@ -631,7 +631,7 @@ static void test_qemu_strtoul_decimal(void) err = qemu_strtoul(str, &endptr, 10, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + strlen(str)); str = "123"; @@ -640,7 +640,7 @@ static void test_qemu_strtoul_decimal(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + strlen(str)); } @@ -655,7 +655,7 @@ static void test_qemu_strtoul_hex(void) err = qemu_strtoul(str, &endptr, 16, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0x123); + g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); str = "0x123"; @@ -664,7 +664,7 @@ static void test_qemu_strtoul_hex(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0x123); + g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); } @@ -679,7 +679,7 @@ static void test_qemu_strtoul_max(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, ULONG_MAX); + g_assert_cmphex(res, ==, ULONG_MAX); g_assert(endptr == str + strlen(str)); g_free(str); } @@ -695,7 +695,7 @@ static void test_qemu_strtoul_overflow(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert_cmpint(res, ==, ULONG_MAX); + g_assert_cmphex(res, ==, ULONG_MAX); g_assert(endptr == str + strlen(str)); } @@ -710,7 +710,7 @@ static void test_qemu_strtoul_underflow(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert_cmpint(res, ==, -1ul); + g_assert_cmpuint(res, ==, -1ul); g_assert(endptr == str + strlen(str)); } @@ -725,7 +725,7 @@ static void test_qemu_strtoul_negative(void) err = qemu_strtoul(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, -321ul); + g_assert_cmpuint(res, ==, -321ul); g_assert(endptr == str + strlen(str)); } @@ -738,7 +738,7 @@ static void test_qemu_strtoul_full_correct(void) err = qemu_strtoul(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); } static void test_qemu_strtoul_full_null(void) @@ -769,7 +769,7 @@ static void test_qemu_strtoul_full_negative(void) err = qemu_strtoul(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, -321ul); + g_assert_cmpuint(res, ==, -321ul); } static void test_qemu_strtoul_full_trailing(void) @@ -792,7 +792,7 @@ static void test_qemu_strtoul_full_max(void) err = qemu_strtoul(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, ULONG_MAX); + g_assert_cmphex(res, ==, ULONG_MAX); g_free(str); } @@ -1094,7 +1094,7 @@ static void test_qemu_strtoull_correct(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 12345); + g_assert_cmpuint(res, ==, 12345); g_assert(endptr == str + 5); } @@ -1164,7 +1164,7 @@ static void test_qemu_strtoull_trailing(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + 3); } @@ -1179,7 +1179,7 @@ static void test_qemu_strtoull_octal(void) err = qemu_strtoull(str, &endptr, 8, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0123); + g_assert_cmpuint(res, ==, 0123); g_assert(endptr == str + strlen(str)); endptr = &f; @@ -1187,7 +1187,7 @@ static void test_qemu_strtoull_octal(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0123); + g_assert_cmpuint(res, ==, 0123); g_assert(endptr == str + strlen(str)); } @@ -1202,7 +1202,7 @@ static void test_qemu_strtoull_decimal(void) err = qemu_strtoull(str, &endptr, 10, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + strlen(str)); str = "123"; @@ -1211,7 +1211,7 @@ static void test_qemu_strtoull_decimal(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 123); + g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + strlen(str)); } @@ -1226,7 +1226,7 @@ static void test_qemu_strtoull_hex(void) err = qemu_strtoull(str, &endptr, 16, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0x123); + g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); str = "0x123"; @@ -1235,7 +1235,7 @@ static void test_qemu_strtoull_hex(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0x123); + g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); } @@ -1250,7 +1250,7 @@ static void test_qemu_strtoull_max(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, ULLONG_MAX); + g_assert_cmphex(res, ==, ULLONG_MAX); g_assert(endptr == str + strlen(str)); g_free(str); } @@ -1266,7 +1266,7 @@ static void test_qemu_strtoull_overflow(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert_cmpint(res, ==, ULLONG_MAX); + g_assert_cmphex(res, ==, ULLONG_MAX); g_assert(endptr == str + strlen(str)); } @@ -1281,7 +1281,7 @@ static void test_qemu_strtoull_underflow(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert_cmpint(res, ==, -1); + g_assert_cmphex(res, ==, -1ull); g_assert(endptr == str + strlen(str)); } @@ -1296,7 +1296,7 @@ static void test_qemu_strtoull_negative(void) err = qemu_strtoull(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, -321); + g_assert_cmpuint(res, ==, -321ull); g_assert(endptr == str + strlen(str)); } @@ -1309,7 +1309,7 @@ static void test_qemu_strtoull_full_correct(void) err = qemu_strtoull(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 18446744073709551614LLU); + g_assert_cmpuint(res, ==, 18446744073709551614ull); } static void test_qemu_strtoull_full_null(void) @@ -1342,7 +1342,7 @@ static void test_qemu_strtoull_full_negative(void) err = qemu_strtoull(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 18446744073709551295LLU); + g_assert_cmpuint(res, ==, -321ull); } static void test_qemu_strtoull_full_trailing(void) @@ -1365,7 +1365,7 @@ static void test_qemu_strtoull_full_max(void) err = qemu_strtoull(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, ULLONG_MAX); + g_assert_cmphex(res, ==, ULLONG_MAX); g_free(str); } -- cgit v1.1 From b30d188677456b17c1cd68969e08ddc634cef644 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:50 +0100 Subject: util/cutils: Rename qemu_strtoll(), qemu_strtoull() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The name qemu_strtoll() suggests conversion to long long, but it actually converts to int64_t. Rename to qemu_strtoi64(). The name qemu_strtoull() suggests conversion to unsigned long long, but it actually converts to uint64_t. Rename to qemu_strtou64(). Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Philippe Mathieu-Daudé Message-Id: <1487708048-2131-7-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 368 ++++++++++++++++++++++++++++------------------------ 1 file changed, 200 insertions(+), 168 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 749aaa9..185b023 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -796,7 +796,7 @@ static void test_qemu_strtoul_full_max(void) g_free(str); } -static void test_qemu_strtoll_correct(void) +static void test_qemu_strtoi64_correct(void) { const char *str = "12345 foo"; char f = 'X'; @@ -804,27 +804,27 @@ static void test_qemu_strtoll_correct(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 12345); g_assert(endptr == str + 5); } -static void test_qemu_strtoll_null(void) +static void test_qemu_strtoi64_null(void) { char f = 'X'; const char *endptr = &f; int64_t res = 999; int err; - err = qemu_strtoll(NULL, &endptr, 0, &res); + err = qemu_strtoi64(NULL, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == NULL); } -static void test_qemu_strtoll_empty(void) +static void test_qemu_strtoi64_empty(void) { const char *str = ""; char f = 'X'; @@ -832,13 +832,13 @@ static void test_qemu_strtoll_empty(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } -static void test_qemu_strtoll_whitespace(void) +static void test_qemu_strtoi64_whitespace(void) { const char *str = " \t "; char f = 'X'; @@ -846,13 +846,13 @@ static void test_qemu_strtoll_whitespace(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } -static void test_qemu_strtoll_invalid(void) +static void test_qemu_strtoi64_invalid(void) { const char *str = " xxxx \t abc"; char f = 'X'; @@ -860,13 +860,13 @@ static void test_qemu_strtoll_invalid(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } -static void test_qemu_strtoll_trailing(void) +static void test_qemu_strtoi64_trailing(void) { const char *str = "123xxx"; char f = 'X'; @@ -874,14 +874,14 @@ static void test_qemu_strtoll_trailing(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 123); g_assert(endptr == str + 3); } -static void test_qemu_strtoll_octal(void) +static void test_qemu_strtoi64_octal(void) { const char *str = "0123"; char f = 'X'; @@ -889,7 +889,7 @@ static void test_qemu_strtoll_octal(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 8, &res); + err = qemu_strtoi64(str, &endptr, 8, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0123); @@ -897,14 +897,14 @@ static void test_qemu_strtoll_octal(void) endptr = &f; res = 999; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0123); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoll_decimal(void) +static void test_qemu_strtoi64_decimal(void) { const char *str = "0123"; char f = 'X'; @@ -912,7 +912,7 @@ static void test_qemu_strtoll_decimal(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 10, &res); + err = qemu_strtoi64(str, &endptr, 10, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 123); @@ -921,14 +921,14 @@ static void test_qemu_strtoll_decimal(void) str = "123"; endptr = &f; res = 999; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 123); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoll_hex(void) +static void test_qemu_strtoi64_hex(void) { const char *str = "0123"; char f = 'X'; @@ -936,7 +936,7 @@ static void test_qemu_strtoll_hex(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 16, &res); + err = qemu_strtoi64(str, &endptr, 16, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x123); @@ -945,14 +945,14 @@ static void test_qemu_strtoll_hex(void) str = "0x123"; endptr = &f; res = 999; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x123); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoll_max(void) +static void test_qemu_strtoi64_max(void) { char *str = g_strdup_printf("%lld", LLONG_MAX); char f = 'X'; @@ -960,7 +960,7 @@ static void test_qemu_strtoll_max(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, LLONG_MAX); @@ -968,7 +968,7 @@ static void test_qemu_strtoll_max(void) g_free(str); } -static void test_qemu_strtoll_overflow(void) +static void test_qemu_strtoi64_overflow(void) { const char *str = "99999999999999999999999999999999999999999999"; char f = 'X'; @@ -976,14 +976,14 @@ static void test_qemu_strtoll_overflow(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); g_assert_cmpint(res, ==, LLONG_MAX); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoll_underflow(void) +static void test_qemu_strtoi64_underflow(void) { const char *str = "-99999999999999999999999999999999999999999999"; char f = 'X'; @@ -991,14 +991,14 @@ static void test_qemu_strtoll_underflow(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); g_assert_cmpint(res, ==, LLONG_MIN); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoll_negative(void) +static void test_qemu_strtoi64_negative(void) { const char *str = " \t -321"; char f = 'X'; @@ -1006,84 +1006,84 @@ static void test_qemu_strtoll_negative(void) int64_t res = 999; int err; - err = qemu_strtoll(str, &endptr, 0, &res); + err = qemu_strtoi64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, -321); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoll_full_correct(void) +static void test_qemu_strtoi64_full_correct(void) { const char *str = "123"; int64_t res = 999; int err; - err = qemu_strtoll(str, NULL, 0, &res); + err = qemu_strtoi64(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 123); } -static void test_qemu_strtoll_full_null(void) +static void test_qemu_strtoi64_full_null(void) { int64_t res = 999; int err; - err = qemu_strtoll(NULL, NULL, 0, &res); + err = qemu_strtoi64(NULL, NULL, 0, &res); g_assert_cmpint(err, ==, -EINVAL); } -static void test_qemu_strtoll_full_empty(void) +static void test_qemu_strtoi64_full_empty(void) { const char *str = ""; int64_t res = 999; int err; - err = qemu_strtoll(str, NULL, 0, &res); + err = qemu_strtoi64(str, NULL, 0, &res); g_assert_cmpint(err, ==, -EINVAL); } -static void test_qemu_strtoll_full_negative(void) +static void test_qemu_strtoi64_full_negative(void) { const char *str = " \t -321"; int64_t res = 999; int err; - err = qemu_strtoll(str, NULL, 0, &res); + err = qemu_strtoi64(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, -321); } -static void test_qemu_strtoll_full_trailing(void) +static void test_qemu_strtoi64_full_trailing(void) { const char *str = "123xxx"; int64_t res = 999; int err; - err = qemu_strtoll(str, NULL, 0, &res); + err = qemu_strtoi64(str, NULL, 0, &res); g_assert_cmpint(err, ==, -EINVAL); } -static void test_qemu_strtoll_full_max(void) +static void test_qemu_strtoi64_full_max(void) { char *str = g_strdup_printf("%lld", LLONG_MAX); int64_t res; int err; - err = qemu_strtoll(str, NULL, 0, &res); + err = qemu_strtoi64(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, LLONG_MAX); g_free(str); } -static void test_qemu_strtoull_correct(void) +static void test_qemu_strtou64_correct(void) { const char *str = "12345 foo"; char f = 'X'; @@ -1091,27 +1091,27 @@ static void test_qemu_strtoull_correct(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 12345); g_assert(endptr == str + 5); } -static void test_qemu_strtoull_null(void) +static void test_qemu_strtou64_null(void) { char f = 'X'; const char *endptr = &f; uint64_t res = 999; int err; - err = qemu_strtoull(NULL, &endptr, 0, &res); + err = qemu_strtou64(NULL, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == NULL); } -static void test_qemu_strtoull_empty(void) +static void test_qemu_strtou64_empty(void) { const char *str = ""; char f = 'X'; @@ -1119,13 +1119,13 @@ static void test_qemu_strtoull_empty(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } -static void test_qemu_strtoull_whitespace(void) +static void test_qemu_strtou64_whitespace(void) { const char *str = " \t "; char f = 'X'; @@ -1133,13 +1133,13 @@ static void test_qemu_strtoull_whitespace(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } -static void test_qemu_strtoull_invalid(void) +static void test_qemu_strtou64_invalid(void) { const char *str = " xxxx \t abc"; char f = 'X'; @@ -1147,13 +1147,13 @@ static void test_qemu_strtoull_invalid(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } -static void test_qemu_strtoull_trailing(void) +static void test_qemu_strtou64_trailing(void) { const char *str = "123xxx"; char f = 'X'; @@ -1161,14 +1161,14 @@ static void test_qemu_strtoull_trailing(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + 3); } -static void test_qemu_strtoull_octal(void) +static void test_qemu_strtou64_octal(void) { const char *str = "0123"; char f = 'X'; @@ -1176,7 +1176,7 @@ static void test_qemu_strtoull_octal(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 8, &res); + err = qemu_strtou64(str, &endptr, 8, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 0123); @@ -1184,14 +1184,14 @@ static void test_qemu_strtoull_octal(void) endptr = &f; res = 999; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 0123); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoull_decimal(void) +static void test_qemu_strtou64_decimal(void) { const char *str = "0123"; char f = 'X'; @@ -1199,7 +1199,7 @@ static void test_qemu_strtoull_decimal(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 10, &res); + err = qemu_strtou64(str, &endptr, 10, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 123); @@ -1208,14 +1208,14 @@ static void test_qemu_strtoull_decimal(void) str = "123"; endptr = &f; res = 999; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 123); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoull_hex(void) +static void test_qemu_strtou64_hex(void) { const char *str = "0123"; char f = 'X'; @@ -1223,7 +1223,7 @@ static void test_qemu_strtoull_hex(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 16, &res); + err = qemu_strtou64(str, &endptr, 16, &res); g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, 0x123); @@ -1232,14 +1232,14 @@ static void test_qemu_strtoull_hex(void) str = "0x123"; endptr = &f; res = 999; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoull_max(void) +static void test_qemu_strtou64_max(void) { char *str = g_strdup_printf("%llu", ULLONG_MAX); char f = 'X'; @@ -1247,7 +1247,7 @@ static void test_qemu_strtoull_max(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, ULLONG_MAX); @@ -1255,7 +1255,7 @@ static void test_qemu_strtoull_max(void) g_free(str); } -static void test_qemu_strtoull_overflow(void) +static void test_qemu_strtou64_overflow(void) { const char *str = "99999999999999999999999999999999999999999999"; char f = 'X'; @@ -1263,14 +1263,14 @@ static void test_qemu_strtoull_overflow(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); g_assert_cmphex(res, ==, ULLONG_MAX); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoull_underflow(void) +static void test_qemu_strtou64_underflow(void) { const char *str = "-99999999999999999999999999999999999999999999"; char f = 'X'; @@ -1278,14 +1278,14 @@ static void test_qemu_strtoull_underflow(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, -ERANGE); g_assert_cmphex(res, ==, -1ull); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoull_negative(void) +static void test_qemu_strtou64_negative(void) { const char *str = " \t -321"; char f = 'X'; @@ -1293,76 +1293,76 @@ static void test_qemu_strtoull_negative(void) uint64_t res = 999; int err; - err = qemu_strtoull(str, &endptr, 0, &res); + err = qemu_strtou64(str, &endptr, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, -321ull); g_assert(endptr == str + strlen(str)); } -static void test_qemu_strtoull_full_correct(void) +static void test_qemu_strtou64_full_correct(void) { const char *str = "18446744073709551614"; uint64_t res = 999; int err; - err = qemu_strtoull(str, NULL, 0, &res); + err = qemu_strtou64(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, 18446744073709551614ull); } -static void test_qemu_strtoull_full_null(void) +static void test_qemu_strtou64_full_null(void) { uint64_t res = 999; int err; - err = qemu_strtoull(NULL, NULL, 0, &res); + err = qemu_strtou64(NULL, NULL, 0, &res); g_assert_cmpint(err, ==, -EINVAL); } -static void test_qemu_strtoull_full_empty(void) +static void test_qemu_strtou64_full_empty(void) { const char *str = ""; uint64_t res = 999; int err; - err = qemu_strtoull(str, NULL, 0, &res); + err = qemu_strtou64(str, NULL, 0, &res); g_assert_cmpint(err, ==, -EINVAL); } -static void test_qemu_strtoull_full_negative(void) +static void test_qemu_strtou64_full_negative(void) { const char *str = " \t -321"; uint64_t res = 999; int err; - err = qemu_strtoull(str, NULL, 0, &res); + err = qemu_strtou64(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmpuint(res, ==, -321ull); } -static void test_qemu_strtoull_full_trailing(void) +static void test_qemu_strtou64_full_trailing(void) { const char *str = "18446744073709551614xxxxxx"; uint64_t res = 999; int err; - err = qemu_strtoull(str, NULL, 0, &res); + err = qemu_strtou64(str, NULL, 0, &res); g_assert_cmpint(err, ==, -EINVAL); } -static void test_qemu_strtoull_full_max(void) +static void test_qemu_strtou64_full_max(void) { char *str = g_strdup_printf("%lld", ULLONG_MAX); uint64_t res = 999; int err; - err = qemu_strtoull(str, NULL, 0, &res); + err = qemu_strtou64(str, NULL, 0, &res); g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, ULLONG_MAX); @@ -1471,21 +1471,32 @@ int main(int argc, char **argv) test_parse_uint_full_correct); /* qemu_strtol() tests */ - g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct); - g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null); - g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty); + g_test_add_func("/cutils/qemu_strtol/correct", + test_qemu_strtol_correct); + g_test_add_func("/cutils/qemu_strtol/null", + test_qemu_strtol_null); + g_test_add_func("/cutils/qemu_strtol/empty", + test_qemu_strtol_empty); g_test_add_func("/cutils/qemu_strtol/whitespace", test_qemu_strtol_whitespace); - g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid); - g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing); - g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal); - g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal); - g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex); - g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max); - g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow); + g_test_add_func("/cutils/qemu_strtol/invalid", + test_qemu_strtol_invalid); + g_test_add_func("/cutils/qemu_strtol/trailing", + test_qemu_strtol_trailing); + g_test_add_func("/cutils/qemu_strtol/octal", + test_qemu_strtol_octal); + g_test_add_func("/cutils/qemu_strtol/decimal", + test_qemu_strtol_decimal); + g_test_add_func("/cutils/qemu_strtol/hex", + test_qemu_strtol_hex); + g_test_add_func("/cutils/qemu_strtol/max", + test_qemu_strtol_max); + g_test_add_func("/cutils/qemu_strtol/overflow", + test_qemu_strtol_overflow); g_test_add_func("/cutils/qemu_strtol/underflow", test_qemu_strtol_underflow); - g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative); + g_test_add_func("/cutils/qemu_strtol/negative", + test_qemu_strtol_negative); g_test_add_func("/cutils/qemu_strtol_full/correct", test_qemu_strtol_full_correct); g_test_add_func("/cutils/qemu_strtol_full/null", @@ -1500,18 +1511,26 @@ int main(int argc, char **argv) test_qemu_strtol_full_max); /* qemu_strtoul() tests */ - g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct); - g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null); - g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty); + g_test_add_func("/cutils/qemu_strtoul/correct", + test_qemu_strtoul_correct); + g_test_add_func("/cutils/qemu_strtoul/null", + test_qemu_strtoul_null); + g_test_add_func("/cutils/qemu_strtoul/empty", + test_qemu_strtoul_empty); g_test_add_func("/cutils/qemu_strtoul/whitespace", test_qemu_strtoul_whitespace); - g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid); + g_test_add_func("/cutils/qemu_strtoul/invalid", + test_qemu_strtoul_invalid); g_test_add_func("/cutils/qemu_strtoul/trailing", test_qemu_strtoul_trailing); - g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal); - g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal); - g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex); - g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max); + g_test_add_func("/cutils/qemu_strtoul/octal", + test_qemu_strtoul_octal); + g_test_add_func("/cutils/qemu_strtoul/decimal", + test_qemu_strtoul_decimal); + g_test_add_func("/cutils/qemu_strtoul/hex", + test_qemu_strtoul_hex); + g_test_add_func("/cutils/qemu_strtoul/max", + test_qemu_strtoul_max); g_test_add_func("/cutils/qemu_strtoul/overflow", test_qemu_strtoul_overflow); g_test_add_func("/cutils/qemu_strtoul/underflow", @@ -1531,73 +1550,86 @@ int main(int argc, char **argv) g_test_add_func("/cutils/qemu_strtoul_full/max", test_qemu_strtoul_full_max); - /* qemu_strtoll() tests */ - g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct); - g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null); - g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty); - g_test_add_func("/cutils/qemu_strtoll/whitespace", - test_qemu_strtoll_whitespace); - g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid); - g_test_add_func("/cutils/qemu_strtoll/trailing", - test_qemu_strtoll_trailing); - g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal); - g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal); - g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex); - g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max); - g_test_add_func("/cutils/qemu_strtoll/overflow", - test_qemu_strtoll_overflow); - g_test_add_func("/cutils/qemu_strtoll/underflow", - test_qemu_strtoll_underflow); - g_test_add_func("/cutils/qemu_strtoll/negative", - test_qemu_strtoll_negative); - g_test_add_func("/cutils/qemu_strtoll_full/correct", - test_qemu_strtoll_full_correct); - g_test_add_func("/cutils/qemu_strtoll_full/null", - test_qemu_strtoll_full_null); - g_test_add_func("/cutils/qemu_strtoll_full/empty", - test_qemu_strtoll_full_empty); - g_test_add_func("/cutils/qemu_strtoll_full/negative", - test_qemu_strtoll_full_negative); - g_test_add_func("/cutils/qemu_strtoll_full/trailing", - test_qemu_strtoll_full_trailing); - g_test_add_func("/cutils/qemu_strtoll_full/max", - test_qemu_strtoll_full_max); - - /* qemu_strtoull() tests */ - g_test_add_func("/cutils/qemu_strtoull/correct", - test_qemu_strtoull_correct); - g_test_add_func("/cutils/qemu_strtoull/null", - test_qemu_strtoull_null); - g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty); - g_test_add_func("/cutils/qemu_strtoull/whitespace", - test_qemu_strtoull_whitespace); - g_test_add_func("/cutils/qemu_strtoull/invalid", - test_qemu_strtoull_invalid); - g_test_add_func("/cutils/qemu_strtoull/trailing", - test_qemu_strtoull_trailing); - g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal); - g_test_add_func("/cutils/qemu_strtoull/decimal", - test_qemu_strtoull_decimal); - g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex); - g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max); - g_test_add_func("/cutils/qemu_strtoull/overflow", - test_qemu_strtoull_overflow); - g_test_add_func("/cutils/qemu_strtoull/underflow", - test_qemu_strtoull_underflow); - g_test_add_func("/cutils/qemu_strtoull/negative", - test_qemu_strtoull_negative); - g_test_add_func("/cutils/qemu_strtoull_full/correct", - test_qemu_strtoull_full_correct); - g_test_add_func("/cutils/qemu_strtoull_full/null", - test_qemu_strtoull_full_null); - g_test_add_func("/cutils/qemu_strtoull_full/empty", - test_qemu_strtoull_full_empty); - g_test_add_func("/cutils/qemu_strtoull_full/negative", - test_qemu_strtoull_full_negative); - g_test_add_func("/cutils/qemu_strtoull_full/trailing", - test_qemu_strtoull_full_trailing); - g_test_add_func("/cutils/qemu_strtoull_full/max", - test_qemu_strtoull_full_max); + /* qemu_strtoi64() tests */ + g_test_add_func("/cutils/qemu_strtoi64/correct", + test_qemu_strtoi64_correct); + g_test_add_func("/cutils/qemu_strtoi64/null", + test_qemu_strtoi64_null); + g_test_add_func("/cutils/qemu_strtoi64/empty", + test_qemu_strtoi64_empty); + g_test_add_func("/cutils/qemu_strtoi64/whitespace", + test_qemu_strtoi64_whitespace); + g_test_add_func("/cutils/qemu_strtoi64/invalid" + , + test_qemu_strtoi64_invalid); + g_test_add_func("/cutils/qemu_strtoi64/trailing", + test_qemu_strtoi64_trailing); + g_test_add_func("/cutils/qemu_strtoi64/octal", + test_qemu_strtoi64_octal); + g_test_add_func("/cutils/qemu_strtoi64/decimal", + test_qemu_strtoi64_decimal); + g_test_add_func("/cutils/qemu_strtoi64/hex", + test_qemu_strtoi64_hex); + g_test_add_func("/cutils/qemu_strtoi64/max", + test_qemu_strtoi64_max); + g_test_add_func("/cutils/qemu_strtoi64/overflow", + test_qemu_strtoi64_overflow); + g_test_add_func("/cutils/qemu_strtoi64/underflow", + test_qemu_strtoi64_underflow); + g_test_add_func("/cutils/qemu_strtoi64/negative", + test_qemu_strtoi64_negative); + g_test_add_func("/cutils/qemu_strtoi64_full/correct", + test_qemu_strtoi64_full_correct); + g_test_add_func("/cutils/qemu_strtoi64_full/null", + test_qemu_strtoi64_full_null); + g_test_add_func("/cutils/qemu_strtoi64_full/empty", + test_qemu_strtoi64_full_empty); + g_test_add_func("/cutils/qemu_strtoi64_full/negative", + test_qemu_strtoi64_full_negative); + g_test_add_func("/cutils/qemu_strtoi64_full/trailing", + test_qemu_strtoi64_full_trailing); + g_test_add_func("/cutils/qemu_strtoi64_full/max", + test_qemu_strtoi64_full_max); + + /* qemu_strtou64() tests */ + g_test_add_func("/cutils/qemu_strtou64/correct", + test_qemu_strtou64_correct); + g_test_add_func("/cutils/qemu_strtou64/null", + test_qemu_strtou64_null); + g_test_add_func("/cutils/qemu_strtou64/empty", + test_qemu_strtou64_empty); + g_test_add_func("/cutils/qemu_strtou64/whitespace", + test_qemu_strtou64_whitespace); + g_test_add_func("/cutils/qemu_strtou64/invalid", + test_qemu_strtou64_invalid); + g_test_add_func("/cutils/qemu_strtou64/trailing", + test_qemu_strtou64_trailing); + g_test_add_func("/cutils/qemu_strtou64/octal", + test_qemu_strtou64_octal); + g_test_add_func("/cutils/qemu_strtou64/decimal", + test_qemu_strtou64_decimal); + g_test_add_func("/cutils/qemu_strtou64/hex", + test_qemu_strtou64_hex); + g_test_add_func("/cutils/qemu_strtou64/max", + test_qemu_strtou64_max); + g_test_add_func("/cutils/qemu_strtou64/overflow", + test_qemu_strtou64_overflow); + g_test_add_func("/cutils/qemu_strtou64/underflow", + test_qemu_strtou64_underflow); + g_test_add_func("/cutils/qemu_strtou64/negative", + test_qemu_strtou64_negative); + g_test_add_func("/cutils/qemu_strtou64_full/correct", + test_qemu_strtou64_full_correct); + g_test_add_func("/cutils/qemu_strtou64_full/null", + test_qemu_strtou64_full_null); + g_test_add_func("/cutils/qemu_strtou64_full/empty", + test_qemu_strtou64_full_empty); + g_test_add_func("/cutils/qemu_strtou64_full/negative", + test_qemu_strtou64_full_negative); + g_test_add_func("/cutils/qemu_strtou64_full/trailing", + test_qemu_strtou64_full_trailing); + g_test_add_func("/cutils/qemu_strtou64_full/max", + test_qemu_strtou64_full_max); g_test_add_func("/cutils/strtosz/simple", test_qemu_strtosz_simple); -- cgit v1.1 From 3403e5eb884f3a74c40fe7cccc103f848c040215 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:53 +0100 Subject: option: Fix to reject invalid and overflowing numbers parse_option_number() fails to check for these errors after strtoull(). Has always been broken. Fix that. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-10-git-send-email-armbru@redhat.com> --- tests/test-qemu-opts.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 310485b..8b92f7b 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -603,17 +603,15 @@ static void test_opts_parse_number(void) /* Above upper limit */ opts = qemu_opts_parse(&opts_list_01, "number1=18446744073709551616", - false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), ==, 1); - g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX); + false, &err); + error_free_or_abort(&err); + g_assert(!opts); /* Below lower limit */ opts = qemu_opts_parse(&opts_list_01, "number1=-18446744073709551616", - false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), ==, 1); - g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX); + false, &err); + error_free_or_abort(&err); + g_assert(!opts); /* Hex and octal */ opts = qemu_opts_parse(&opts_list_01, "number1=0x2a,number2=052", @@ -624,9 +622,8 @@ static void test_opts_parse_number(void) /* Invalid */ opts = qemu_opts_parse(&opts_list_01, "number1=", false, &err); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), ==, 1); - g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 0); + error_free_or_abort(&err); + g_assert(!opts); opts = qemu_opts_parse(&opts_list_01, "number1=eins", false, &err); error_free_or_abort(&err); g_assert(!opts); -- cgit v1.1 From 019144b2866f38884fe9996d8738c50e680e3bfa Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:54 +0100 Subject: test-cutils: Add missing qemu_strtosz()... endptr checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Philippe Mathieu-Daudé Message-Id: <1487708048-2131-11-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 185b023..a3eb182 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1393,60 +1393,75 @@ static void test_qemu_strtosz_units(void) const char *t = "1T"; const char *p = "1P"; const char *e = "1E"; + char *endptr = NULL; int64_t res; /* default is M */ - res = qemu_strtosz(none, NULL); + res = qemu_strtosz(none, &endptr); g_assert_cmpint(res, ==, M_BYTE); + g_assert(endptr == none + 1); - res = qemu_strtosz(b, NULL); + res = qemu_strtosz(b, &endptr); g_assert_cmpint(res, ==, 1); + g_assert(endptr == b + 2); - res = qemu_strtosz(k, NULL); + res = qemu_strtosz(k, &endptr); g_assert_cmpint(res, ==, K_BYTE); + g_assert(endptr == k + 2); - res = qemu_strtosz(m, NULL); + res = qemu_strtosz(m, &endptr); g_assert_cmpint(res, ==, M_BYTE); + g_assert(endptr == m + 2); - res = qemu_strtosz(g, NULL); + res = qemu_strtosz(g, &endptr); g_assert_cmpint(res, ==, G_BYTE); + g_assert(endptr == g + 2); - res = qemu_strtosz(t, NULL); + res = qemu_strtosz(t, &endptr); g_assert_cmpint(res, ==, T_BYTE); + g_assert(endptr == t + 2); - res = qemu_strtosz(p, NULL); + res = qemu_strtosz(p, &endptr); g_assert_cmpint(res, ==, P_BYTE); + g_assert(endptr == p + 2); - res = qemu_strtosz(e, NULL); + res = qemu_strtosz(e, &endptr); g_assert_cmpint(res, ==, E_BYTE); + g_assert(endptr == e + 2); } static void test_qemu_strtosz_float(void) { const char *str = "12.345M"; + char *endptr = NULL; int64_t res; - res = qemu_strtosz(str, NULL); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 12.345 * M_BYTE); + g_assert(endptr == str + 7); } static void test_qemu_strtosz_erange(void) { const char *str = "10E"; + char *endptr = NULL; int64_t res; - res = qemu_strtosz(str, NULL); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); + g_assert(endptr == str + 3); } static void test_qemu_strtosz_suffix_unit(void) { const char *str = "12345"; + char *endptr = NULL; int64_t res; - res = qemu_strtosz_suffix_unit(str, NULL, + res = qemu_strtosz_suffix_unit(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_KB, 1000); g_assert_cmpint(res, ==, 12345000); + g_assert(endptr == str + 5); } int main(int argc, char **argv) -- cgit v1.1 From 18aec47967814f4a6261edd0130f7e9da985f65b Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:55 +0100 Subject: test-cutils: Cover qemu_strtosz() invalid input Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-12-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index a3eb182..dc8cd8d 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1441,6 +1441,28 @@ static void test_qemu_strtosz_float(void) g_assert(endptr == str + 7); } +static void test_qemu_strtosz_invalid(void) +{ + const char *str; + char *endptr = NULL; + int64_t res; + + str = ""; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, -EINVAL); + g_assert(endptr == str); + + str = " \t "; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, -EINVAL); + g_assert(endptr == str); + + str = "crap"; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, -EINVAL); + g_assert(endptr == str); +} + static void test_qemu_strtosz_erange(void) { const char *str = "10E"; @@ -1652,6 +1674,8 @@ int main(int argc, char **argv) test_qemu_strtosz_units); g_test_add_func("/cutils/strtosz/float", test_qemu_strtosz_float); + g_test_add_func("/cutils/strtosz/invalid", + test_qemu_strtosz_invalid); g_test_add_func("/cutils/strtosz/erange", test_qemu_strtosz_erange); g_test_add_func("/cutils/strtosz/suffix-unit", -- cgit v1.1 From a6b4373fa257fdd7139a74187851575709a5ecb6 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:56 +0100 Subject: test-cutils: Cover qemu_strtosz() with trailing crap Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-13-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index dc8cd8d..1773f15 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1463,6 +1463,23 @@ static void test_qemu_strtosz_invalid(void) g_assert(endptr == str); } +static void test_qemu_strtosz_trailing(void) +{ + const char *str; + char *endptr = NULL; + int64_t res; + + str = "123xxx"; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, 123 * M_BYTE); + g_assert(endptr == str + 3); + + str = "1kiB"; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, 1024); + g_assert(endptr == str + 2); +} + static void test_qemu_strtosz_erange(void) { const char *str = "10E"; @@ -1676,6 +1693,8 @@ int main(int argc, char **argv) test_qemu_strtosz_float); g_test_add_func("/cutils/strtosz/invalid", test_qemu_strtosz_invalid); + g_test_add_func("/cutils/strtosz/trailing", + test_qemu_strtosz_trailing); g_test_add_func("/cutils/strtosz/erange", test_qemu_strtosz_erange); g_test_add_func("/cutils/strtosz/suffix-unit", -- cgit v1.1 From 0b742797aaada3a2e243175a69d542d2ed997aac Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:57 +0100 Subject: test-cutils: Cover qemu_strtosz() around range limits Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-14-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 1773f15..df6c330 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1371,16 +1371,52 @@ static void test_qemu_strtou64_full_max(void) static void test_qemu_strtosz_simple(void) { - const char *str = "12345M"; + const char *str; char *endptr = NULL; int64_t res; + str = "0"; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, 0); + g_assert(endptr == str + 1); + + str = "12345M"; res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 12345 * M_BYTE); g_assert(endptr == str + 6); res = qemu_strtosz(str, NULL); g_assert_cmpint(res, ==, 12345 * M_BYTE); + + /* Note: precision is 53 bits since we're parsing with strtod() */ + + str = "9007199254740991"; /* 2^53-1 */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, 0x1fffffffffffff); + g_assert(endptr == str + 16); + + str = "9007199254740992"; /* 2^53 */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, 0x20000000000000); + g_assert(endptr == str + 16); + + str = "9007199254740993"; /* 2^53+1 */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */ + g_assert(endptr == str + 16); + + str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, 0x7ffffffffffffc00); + g_assert(endptr == str + 19); + + str = "9223372036854775295"; /* 0x7ffffffffffffdff */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */ + g_assert(endptr == str + 19); + + /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to + * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */ } static void test_qemu_strtosz_units(void) @@ -1482,10 +1518,31 @@ static void test_qemu_strtosz_trailing(void) static void test_qemu_strtosz_erange(void) { - const char *str = "10E"; + const char *str; char *endptr = NULL; int64_t res; + str = "-1"; + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, -ERANGE); + g_assert(endptr == str + 2); + + str = "9223372036854775296"; /* 0x7ffffffffffffe00 */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, -ERANGE); + g_assert(endptr == str + 19); + + str = "9223372036854775807"; /* 2^63-1 */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, -ERANGE); + g_assert(endptr == str + 19); + + str = "9223372036854775808"; /* 2^63 */ + res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + g_assert_cmpint(res, ==, -ERANGE); + g_assert(endptr == str + 19); + + str = "10E"; res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 3); -- cgit v1.1 From d2734d2629266006b0413433778474d5801c60be Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:58 +0100 Subject: util/cutils: New qemu_strtosz_metric() To parse numbers with metric suffixes, we use qemu_strtosz_suffix_unit(nptr, &eptr, QEMU_STRTOSZ_DEFSUFFIX_B, 1000) Capture this in a new function for legibility: qemu_strtosz_metric(nptr, &eptr) Replace test_qemu_strtosz_suffix_unit() by test_qemu_strtosz_metric(). Rename qemu_strtosz_suffix_unit() to do_strtosz() and give it internal linkage. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-15-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index df6c330..e1d4054 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1548,16 +1548,15 @@ static void test_qemu_strtosz_erange(void) g_assert(endptr == str + 3); } -static void test_qemu_strtosz_suffix_unit(void) +static void test_qemu_strtosz_metric(void) { - const char *str = "12345"; + const char *str = "12345k"; char *endptr = NULL; int64_t res; - res = qemu_strtosz_suffix_unit(str, &endptr, - QEMU_STRTOSZ_DEFSUFFIX_KB, 1000); + res = qemu_strtosz_metric(str, &endptr); g_assert_cmpint(res, ==, 12345000); - g_assert(endptr == str + 5); + g_assert(endptr == str + 6); } int main(int argc, char **argv) @@ -1754,8 +1753,8 @@ int main(int argc, char **argv) test_qemu_strtosz_trailing); g_test_add_func("/cutils/strtosz/erange", test_qemu_strtosz_erange); - g_test_add_func("/cutils/strtosz/suffix-unit", - test_qemu_strtosz_suffix_unit); + g_test_add_func("/cutils/strtosz/metric", + test_qemu_strtosz_metric); return g_test_run(); } -- cgit v1.1 From e591591b323772eea733de6027f5e8b50692d0ff Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:13:59 +0100 Subject: util/cutils: Rename qemu_strtosz() to qemu_strtosz_MiB() With qemu_strtosz(), no suffix means mebibytes. It's used rarely. I'm going to add a similar function where no suffix means bytes. Rename qemu_strtosz() to qemu_strtosz_MiB() to make the name qemu_strtosz() available for the new function. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-16-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index e1d4054..9bbfb8f 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1376,16 +1376,16 @@ static void test_qemu_strtosz_simple(void) int64_t res; str = "0"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, 0); g_assert(endptr == str + 1); str = "12345M"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, 12345 * M_BYTE); g_assert(endptr == str + 6); - res = qemu_strtosz(str, NULL); + res = qemu_strtosz_MiB(str, NULL); g_assert_cmpint(res, ==, 12345 * M_BYTE); /* Note: precision is 53 bits since we're parsing with strtod() */ @@ -1433,35 +1433,35 @@ static void test_qemu_strtosz_units(void) int64_t res; /* default is M */ - res = qemu_strtosz(none, &endptr); + res = qemu_strtosz_MiB(none, &endptr); g_assert_cmpint(res, ==, M_BYTE); g_assert(endptr == none + 1); - res = qemu_strtosz(b, &endptr); + res = qemu_strtosz_MiB(b, &endptr); g_assert_cmpint(res, ==, 1); g_assert(endptr == b + 2); - res = qemu_strtosz(k, &endptr); + res = qemu_strtosz_MiB(k, &endptr); g_assert_cmpint(res, ==, K_BYTE); g_assert(endptr == k + 2); - res = qemu_strtosz(m, &endptr); + res = qemu_strtosz_MiB(m, &endptr); g_assert_cmpint(res, ==, M_BYTE); g_assert(endptr == m + 2); - res = qemu_strtosz(g, &endptr); + res = qemu_strtosz_MiB(g, &endptr); g_assert_cmpint(res, ==, G_BYTE); g_assert(endptr == g + 2); - res = qemu_strtosz(t, &endptr); + res = qemu_strtosz_MiB(t, &endptr); g_assert_cmpint(res, ==, T_BYTE); g_assert(endptr == t + 2); - res = qemu_strtosz(p, &endptr); + res = qemu_strtosz_MiB(p, &endptr); g_assert_cmpint(res, ==, P_BYTE); g_assert(endptr == p + 2); - res = qemu_strtosz(e, &endptr); + res = qemu_strtosz_MiB(e, &endptr); g_assert_cmpint(res, ==, E_BYTE); g_assert(endptr == e + 2); } @@ -1472,7 +1472,7 @@ static void test_qemu_strtosz_float(void) char *endptr = NULL; int64_t res; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, 12.345 * M_BYTE); g_assert(endptr == str + 7); } @@ -1484,17 +1484,17 @@ static void test_qemu_strtosz_invalid(void) int64_t res; str = ""; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, -EINVAL); g_assert(endptr == str); str = " \t "; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, -EINVAL); g_assert(endptr == str); str = "crap"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, -EINVAL); g_assert(endptr == str); } @@ -1506,12 +1506,12 @@ static void test_qemu_strtosz_trailing(void) int64_t res; str = "123xxx"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, 123 * M_BYTE); g_assert(endptr == str + 3); str = "1kiB"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, 1024); g_assert(endptr == str + 2); } @@ -1523,7 +1523,7 @@ static void test_qemu_strtosz_erange(void) int64_t res; str = "-1"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 2); @@ -1543,7 +1543,7 @@ static void test_qemu_strtosz_erange(void) g_assert(endptr == str + 19); str = "10E"; - res = qemu_strtosz(str, &endptr); + res = qemu_strtosz_MiB(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 3); } -- cgit v1.1 From 466dea14e677555dd24465aca75d00a3537ad062 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:00 +0100 Subject: util/cutils: New qemu_strtosz() Most callers of qemu_strtosz_suffix() pass QEMU_STRTOSZ_DEFSUFFIX_B. Capture the pattern in new qemu_strtosz(). Inline qemu_strtosz_suffix() into its only remaining caller. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-17-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 9bbfb8f..9eae067 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1391,27 +1391,27 @@ static void test_qemu_strtosz_simple(void) /* Note: precision is 53 bits since we're parsing with strtod() */ str = "9007199254740991"; /* 2^53-1 */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 0x1fffffffffffff); g_assert(endptr == str + 16); str = "9007199254740992"; /* 2^53 */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 0x20000000000000); g_assert(endptr == str + 16); str = "9007199254740993"; /* 2^53+1 */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */ g_assert(endptr == str + 16); str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 0x7ffffffffffffc00); g_assert(endptr == str + 19); str = "9223372036854775295"; /* 0x7ffffffffffffdff */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */ g_assert(endptr == str + 19); @@ -1528,17 +1528,17 @@ static void test_qemu_strtosz_erange(void) g_assert(endptr == str + 2); str = "9223372036854775296"; /* 0x7ffffffffffffe00 */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 19); str = "9223372036854775807"; /* 2^63-1 */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 19); str = "9223372036854775808"; /* 2^63 */ - res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 19); -- cgit v1.1 From 753f8da0e082eac66d4c86168e14f1c55c09aa44 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:02 +0100 Subject: test-cutils: Use qemu_strtosz() more often Use qemu_strtosz() instead of qemu_strtosz_MiB() where it doesn't really make a difference. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-19-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 9eae067..d492d1e 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1376,16 +1376,16 @@ static void test_qemu_strtosz_simple(void) int64_t res; str = "0"; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 0); g_assert(endptr == str + 1); str = "12345M"; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 12345 * M_BYTE); g_assert(endptr == str + 6); - res = qemu_strtosz_MiB(str, NULL); + res = qemu_strtosz(str, NULL); g_assert_cmpint(res, ==, 12345 * M_BYTE); /* Note: precision is 53 bits since we're parsing with strtod() */ @@ -1437,31 +1437,31 @@ static void test_qemu_strtosz_units(void) g_assert_cmpint(res, ==, M_BYTE); g_assert(endptr == none + 1); - res = qemu_strtosz_MiB(b, &endptr); + res = qemu_strtosz(b, &endptr); g_assert_cmpint(res, ==, 1); g_assert(endptr == b + 2); - res = qemu_strtosz_MiB(k, &endptr); + res = qemu_strtosz(k, &endptr); g_assert_cmpint(res, ==, K_BYTE); g_assert(endptr == k + 2); - res = qemu_strtosz_MiB(m, &endptr); + res = qemu_strtosz(m, &endptr); g_assert_cmpint(res, ==, M_BYTE); g_assert(endptr == m + 2); - res = qemu_strtosz_MiB(g, &endptr); + res = qemu_strtosz(g, &endptr); g_assert_cmpint(res, ==, G_BYTE); g_assert(endptr == g + 2); - res = qemu_strtosz_MiB(t, &endptr); + res = qemu_strtosz(t, &endptr); g_assert_cmpint(res, ==, T_BYTE); g_assert(endptr == t + 2); - res = qemu_strtosz_MiB(p, &endptr); + res = qemu_strtosz(p, &endptr); g_assert_cmpint(res, ==, P_BYTE); g_assert(endptr == p + 2); - res = qemu_strtosz_MiB(e, &endptr); + res = qemu_strtosz(e, &endptr); g_assert_cmpint(res, ==, E_BYTE); g_assert(endptr == e + 2); } @@ -1472,7 +1472,7 @@ static void test_qemu_strtosz_float(void) char *endptr = NULL; int64_t res; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 12.345 * M_BYTE); g_assert(endptr == str + 7); } @@ -1484,17 +1484,17 @@ static void test_qemu_strtosz_invalid(void) int64_t res; str = ""; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -EINVAL); g_assert(endptr == str); str = " \t "; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -EINVAL); g_assert(endptr == str); str = "crap"; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -EINVAL); g_assert(endptr == str); } @@ -1511,7 +1511,7 @@ static void test_qemu_strtosz_trailing(void) g_assert(endptr == str + 3); str = "1kiB"; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 1024); g_assert(endptr == str + 2); } @@ -1523,7 +1523,7 @@ static void test_qemu_strtosz_erange(void) int64_t res; str = "-1"; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 2); @@ -1543,7 +1543,7 @@ static void test_qemu_strtosz_erange(void) g_assert(endptr == str + 19); str = "10E"; - res = qemu_strtosz_MiB(str, &endptr); + res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, -ERANGE); g_assert(endptr == str + 3); } -- cgit v1.1 From dab9cc9237e3e1794053780e43787be8a07b22db Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:03 +0100 Subject: test-cutils: Drop suffix from test_qemu_strtosz_simple() Leave testing unit suffixes to test_qemu_strtosz_units(). Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-20-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index d492d1e..c4437d9 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1380,13 +1380,13 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, ==, 0); g_assert(endptr == str + 1); - str = "12345M"; + str = "12345"; res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, 12345 * M_BYTE); - g_assert(endptr == str + 6); + g_assert_cmpint(res, ==, 12345); + g_assert(endptr == str + 5); res = qemu_strtosz(str, NULL); - g_assert_cmpint(res, ==, 12345 * M_BYTE); + g_assert_cmpint(res, ==, 12345); /* Note: precision is 53 bits since we're parsing with strtod() */ -- cgit v1.1 From 4fcdf65ae2c00ae69f7625f26ed41f37d77b403c Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:05 +0100 Subject: util/cutils: Let qemu_strtosz*() optionally reject trailing crap Change the qemu_strtosz() & friends to return -EINVAL when @endptr is null and the conversion doesn't consume the string completely. Matches how qemu_strtol() & friends work. Only test_qemu_strtosz_simple() passes a null @endptr. No functional change there, because its conversion consumes the string. Simplify callers that use @endptr only to fail when it doesn't point to '\0' to pass a null @endptr instead. Cc: Dr. David Alan Gilbert Cc: Eduardo Habkost (maintainer:X86) Cc: Kevin Wolf (supporter:Block layer core) Cc: Max Reitz (supporter:Block layer core) Cc: qemu-block@nongnu.org (open list:Block layer core) Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Dr. David Alan Gilbert Message-Id: <1487708048-2131-22-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index c4437d9..f2ecb7a 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1510,10 +1510,16 @@ static void test_qemu_strtosz_trailing(void) g_assert_cmpint(res, ==, 123 * M_BYTE); g_assert(endptr == str + 3); + res = qemu_strtosz(str, NULL); + g_assert_cmpint(res, ==, -EINVAL); + str = "1kiB"; res = qemu_strtosz(str, &endptr); g_assert_cmpint(res, ==, 1024); g_assert(endptr == str + 2); + + res = qemu_strtosz(str, NULL); + g_assert_cmpint(res, ==, -EINVAL); } static void test_qemu_strtosz_erange(void) -- cgit v1.1 From f17fd4fdf0df3d2f3444399d04c38d22b9a3e1b7 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:06 +0100 Subject: util/cutils: Return qemu_strtosz*() error and value separately This makes qemu_strtosz(), qemu_strtosz_mebi() and qemu_strtosz_metric() similar to qemu_strtoi64(), except negative values are rejected. Cc: Dr. David Alan Gilbert Cc: Eduardo Habkost (maintainer:X86) Cc: Kevin Wolf (supporter:Block layer core) Cc: Max Reitz (supporter:Block layer core) Cc: qemu-block@nongnu.org (open list:Block layer core) Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Dr. David Alan Gilbert Message-Id: <1487708048-2131-23-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 120 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 47 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index f2ecb7a..d0137de 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1373,45 +1373,54 @@ static void test_qemu_strtosz_simple(void) { const char *str; char *endptr = NULL; - int64_t res; + int err; + int64_t res = 0xbaadf00d; str = "0"; - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0); g_assert(endptr == str + 1); str = "12345"; - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 12345); g_assert(endptr == str + 5); - res = qemu_strtosz(str, NULL); + err = qemu_strtosz(str, NULL, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 12345); /* Note: precision is 53 bits since we're parsing with strtod() */ str = "9007199254740991"; /* 2^53-1 */ - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x1fffffffffffff); g_assert(endptr == str + 16); str = "9007199254740992"; /* 2^53 */ - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x20000000000000); g_assert(endptr == str + 16); str = "9007199254740993"; /* 2^53+1 */ - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */ g_assert(endptr == str + 16); str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */ - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x7ffffffffffffc00); g_assert(endptr == str + 19); str = "9223372036854775295"; /* 0x7ffffffffffffdff */ - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */ g_assert(endptr == str + 19); @@ -1429,39 +1438,48 @@ static void test_qemu_strtosz_units(void) const char *t = "1T"; const char *p = "1P"; const char *e = "1E"; + int err; char *endptr = NULL; - int64_t res; + int64_t res = 0xbaadf00d; /* default is M */ - res = qemu_strtosz_MiB(none, &endptr); + err = qemu_strtosz_MiB(none, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, M_BYTE); g_assert(endptr == none + 1); - res = qemu_strtosz(b, &endptr); + err = qemu_strtosz(b, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 1); g_assert(endptr == b + 2); - res = qemu_strtosz(k, &endptr); + err = qemu_strtosz(k, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, K_BYTE); g_assert(endptr == k + 2); - res = qemu_strtosz(m, &endptr); + err = qemu_strtosz(m, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, M_BYTE); g_assert(endptr == m + 2); - res = qemu_strtosz(g, &endptr); + err = qemu_strtosz(g, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, G_BYTE); g_assert(endptr == g + 2); - res = qemu_strtosz(t, &endptr); + err = qemu_strtosz(t, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, T_BYTE); g_assert(endptr == t + 2); - res = qemu_strtosz(p, &endptr); + err = qemu_strtosz(p, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, P_BYTE); g_assert(endptr == p + 2); - res = qemu_strtosz(e, &endptr); + err = qemu_strtosz(e, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, E_BYTE); g_assert(endptr == e + 2); } @@ -1469,10 +1487,12 @@ static void test_qemu_strtosz_units(void) static void test_qemu_strtosz_float(void) { const char *str = "12.345M"; + int err; char *endptr = NULL; - int64_t res; + int64_t res = 0xbaadf00d; - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 12.345 * M_BYTE); g_assert(endptr == str + 7); } @@ -1481,21 +1501,22 @@ static void test_qemu_strtosz_invalid(void) { const char *str; char *endptr = NULL; - int64_t res; + int err; + int64_t res = 0xbaadf00d; str = ""; - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -EINVAL); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); str = " \t "; - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -EINVAL); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); str = "crap"; - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -EINVAL); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -EINVAL); g_assert(endptr == str); } @@ -1503,64 +1524,69 @@ static void test_qemu_strtosz_trailing(void) { const char *str; char *endptr = NULL; - int64_t res; + int err; + int64_t res = 0xbaadf00d; str = "123xxx"; - res = qemu_strtosz_MiB(str, &endptr); + err = qemu_strtosz_MiB(str, &endptr, &res); g_assert_cmpint(res, ==, 123 * M_BYTE); g_assert(endptr == str + 3); - res = qemu_strtosz(str, NULL); - g_assert_cmpint(res, ==, -EINVAL); + err = qemu_strtosz(str, NULL, &res); + g_assert_cmpint(err, ==, -EINVAL); str = "1kiB"; - res = qemu_strtosz(str, &endptr); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 1024); g_assert(endptr == str + 2); - res = qemu_strtosz(str, NULL); - g_assert_cmpint(res, ==, -EINVAL); + err = qemu_strtosz(str, NULL, &res); + g_assert_cmpint(err, ==, -EINVAL); } static void test_qemu_strtosz_erange(void) { const char *str; char *endptr = NULL; - int64_t res; + int err; + int64_t res = 0xbaadf00d; str = "-1"; - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -ERANGE); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 2); str = "9223372036854775296"; /* 0x7ffffffffffffe00 */ - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -ERANGE); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 19); str = "9223372036854775807"; /* 2^63-1 */ - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -ERANGE); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 19); str = "9223372036854775808"; /* 2^63 */ - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -ERANGE); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 19); str = "10E"; - res = qemu_strtosz(str, &endptr); - g_assert_cmpint(res, ==, -ERANGE); + err = qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 3); } static void test_qemu_strtosz_metric(void) { const char *str = "12345k"; + int err; char *endptr = NULL; - int64_t res; + int64_t res = 0xbaadf00d; - res = qemu_strtosz_metric(str, &endptr); + err = qemu_strtosz_metric(str, &endptr, &res); + g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 12345000); g_assert(endptr == str + 6); } -- cgit v1.1 From f46bfdbfc8f95cf65d7818ef68a801e063c40332 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:07 +0100 Subject: util/cutils: Change qemu_strtosz*() from int64_t to uint64_t This will permit its use in parse_option_size(). Cc: Dr. David Alan Gilbert Cc: Eduardo Habkost (maintainer:X86) Cc: Kevin Wolf (supporter:Block layer core) Cc: Max Reitz (supporter:Block layer core) Cc: qemu-block@nongnu.org (open list:Block layer core) Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Dr. David Alan Gilbert Message-Id: <1487708048-2131-24-git-send-email-armbru@redhat.com> --- tests/test-cutils.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/tests/test-cutils.c b/tests/test-cutils.c index d0137de..f64a49b 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1374,7 +1374,7 @@ static void test_qemu_strtosz_simple(void) const char *str; char *endptr = NULL; int err; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; str = "0"; err = qemu_strtosz(str, &endptr, &res); @@ -1412,17 +1412,17 @@ static void test_qemu_strtosz_simple(void) g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */ g_assert(endptr == str + 16); - str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */ + str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */ err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0x7ffffffffffffc00); - g_assert(endptr == str + 19); + g_assert_cmpint(res, ==, 0xfffffffffffff800); + g_assert(endptr == str + 20); - str = "9223372036854775295"; /* 0x7ffffffffffffdff */ + str = "18446744073709550591"; /* 0xfffffffffffffbff */ err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, 0); - g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */ - g_assert(endptr == str + 19); + g_assert_cmpint(res, ==, 0xfffffffffffff800); /* rounded to 53 bits */ + g_assert(endptr == str + 20); /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */ @@ -1440,7 +1440,7 @@ static void test_qemu_strtosz_units(void) const char *e = "1E"; int err; char *endptr = NULL; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; /* default is M */ err = qemu_strtosz_MiB(none, &endptr, &res); @@ -1489,7 +1489,7 @@ static void test_qemu_strtosz_float(void) const char *str = "12.345M"; int err; char *endptr = NULL; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, 0); @@ -1502,7 +1502,7 @@ static void test_qemu_strtosz_invalid(void) const char *str; char *endptr = NULL; int err; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; str = ""; err = qemu_strtosz(str, &endptr, &res); @@ -1525,7 +1525,7 @@ static void test_qemu_strtosz_trailing(void) const char *str; char *endptr = NULL; int err; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; str = "123xxx"; err = qemu_strtosz_MiB(str, &endptr, &res); @@ -1550,29 +1550,29 @@ static void test_qemu_strtosz_erange(void) const char *str; char *endptr = NULL; int err; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; str = "-1"; err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 2); - str = "9223372036854775296"; /* 0x7ffffffffffffe00 */ + str = "18446744073709550592"; /* 0xfffffffffffffc00 */ err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert(endptr == str + 19); + g_assert(endptr == str + 20); - str = "9223372036854775807"; /* 2^63-1 */ + str = "18446744073709551615"; /* 2^64-1 */ err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert(endptr == str + 19); + g_assert(endptr == str + 20); - str = "9223372036854775808"; /* 2^63 */ + str = "18446744073709551616"; /* 2^64 */ err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, -ERANGE); - g_assert(endptr == str + 19); + g_assert(endptr == str + 20); - str = "10E"; + str = "20E"; err = qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, ==, -ERANGE); g_assert(endptr == str + 3); @@ -1583,7 +1583,7 @@ static void test_qemu_strtosz_metric(void) const char *str = "12345k"; int err; char *endptr = NULL; - int64_t res = 0xbaadf00d; + uint64_t res = 0xbaadf00d; err = qemu_strtosz_metric(str, &endptr, &res); g_assert_cmpint(err, ==, 0); -- cgit v1.1 From 75cdcd1553e74b5edc58aed23e3b2da8dabb1876 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 21 Feb 2017 21:14:08 +0100 Subject: option: Fix checking of sizes for overflow and trailing crap parse_option_size()'s checking for overflow and trailing crap is wrong. Has always been that way. qemu_strtosz() gets it right, so use that. This adds support for size suffixes 'P', 'E', and ignores case for all suffixes, not just 'k'. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <1487708048-2131-25-git-send-email-armbru@redhat.com> --- tests/test-qemu-opts.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 8b92f7b..c46ef31 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -702,10 +702,9 @@ static void test_opts_parse_size(void) g_assert(!opts); opts = qemu_opts_parse(&opts_list_02, "size1=18446744073709550592", /* fffffffffffffc00 */ - false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), ==, 1); - g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0); + false, &err); + error_free_or_abort(&err); + g_assert(!opts); /* Suffixes */ opts = qemu_opts_parse(&opts_list_02, "size1=8b,size2=1.5k,size3=2M", @@ -723,19 +722,17 @@ static void test_opts_parse_size(void) /* Beyond limit with suffix */ opts = qemu_opts_parse(&opts_list_02, "size1=16777216T", - false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), ==, 1); - g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0); + false, &err); + error_free_or_abort(&err); + g_assert(!opts); /* Trailing crap */ opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err); error_free_or_abort(&err); g_assert(!opts); - opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &error_abort); - /* BUG: should reject */ - g_assert_cmpuint(opts_count(opts), ==, 1); - g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 16 * G_BYTE); + opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &err); + error_free_or_abort(&err); + g_assert(!opts); qemu_opts_reset(&opts_list_02); } -- cgit v1.1