visitor: Fix uint64 parsing for scsi-disk wwn
cover visitor: Fix uint64 parsing for scsi-disk wwn
Hello,
This series fixes uint64 handling in the string input visitor. It was inspired by a patch from Lin Ma.
Test coverage is provided for string input visitor, QOM, scsi-disk levels.
Regards,
Andreas
Andreas Färber (7):
string-input-visitor: Fix uint64 parsing
test-string-input-visitor: Add int test case
test-string-input-visitor: Add uint64 test
tests: Add QOM property unit tests
tests: Add scsi-disk test
cutils: Normalize qemu_strto[u]ll() signature
string-input-visitor: Use qemu_strto[u]ll()
MAINTAINERS | 2 + include/qemu-common.h | 4 +- qapi/string-input-visitor.c | 68 +++++++++++++++++---- tests/Makefile | 6 ++ tests/check-qom-props.c | 120 ++++++++++++++++++++++++++++++++++++++ tests/scsi-disk-test.c | 83 ++++++++++++++++++++++++++ tests/test-cutils.c | 76 ++++++++++++------------ tests/test-string-input-visitor.c | 31 ++++++++++ util/cutils.c | 4 +- 9 files changed, 342 insertions(+), 52 deletions(-) create mode 100644 tests/check-qom-props.c create mode 100644 tests/scsi-disk-test.c
Signature · 1 line
2.1.4
PATCH 1/7 string-input-visitor: Fix uint64 parsing
All integers would get parsed by strtoll(), not handling the case of UINT64 properties with the most significient bit set.
Implement a .type_uint64 visitor callback, reusing the existing parse_str() code through a new argument, using strtoull().
As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
--- qapi/string-input-visitor.c | 57 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-)
qapi/string-input-visitor.c +52 -5
@@ -37,7 +37,7 @@ static void free_range(void *range, void *dummy)g_free (range); } -static void parse_str (StringInputVisitor *siv,Error **errp) +static void parse_str (StringInputVisitor *siv,bool u64,Error **errp) {char *str = (char *) siv->string;long long start, end;
@@ -50,7 +50,11 @@ static void parse_str(StringInputVisitor *siv, Error **errp) do { errno = 0; - start = strtoll(str, &endptr, 0); + if (u64) { + start = strtoull(str, &endptr, 0); + } else { + start = strtoll(str, &endptr, 0); + } if (errno == 0 && endptr > str) { if (*endptr == '\0') { cur = g_malloc0(sizeof(*cur));
@@ -60,7 +64,7 @@ static void parse_str(StringInputVisitor *siv, Error **errp) range_compare); cur =NULL ; str =NULL ; - }else if (*endptr =='-' ) { + }else if (*endptr =='-' && !u64) { str = endptr +1 ; errno =0 ; end =strtoll (str, &endptr,0 );
@@ -122,7 +126,7 @@ start_list(Visitor *v, const char *name, Error **errp) {StringInputVisitor *siv =DO_UPCAST (StringInputVisitor, visitor, v); -parse_str (siv, errp); +parse_str (siv, false, errp); siv->cur_range =g_list_first (siv->ranges);if (siv->cur_range) {
@@ -190,7 +194,7 @@ static void parse_type_int(Visitor *v, int64_t *obj, const char *name,return ; } -parse_str (siv, errp); +parse_str (siv, false, errp);if (!siv->ranges) { goto error;
@@ -221,6 +225,48 @@ error: "an int64 value or range"); } +static void parse_type_uint64 (Visitor *v,uint64_t *obj,const char *name, +Error **errp) +{ +StringInputVisitor *siv =DO_UPCAST (StringInputVisitor, visitor, v); + +if (!siv->string) { +error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" , +"integer" ); +return ; + } + +parse_str (siv, true, errp); + +if (!siv->ranges) { + goto error; + } + +if (!siv->cur_range) { +Range *r; + + siv->cur_range =g_list_first (siv->ranges); +if (!siv->cur_range) { + goto error; + } + + r = siv->cur_range->data; +if (!r) { + goto error; + } + + siv->cur = r->begin; + } + + *obj = siv->cur; + siv->cur++; +return ; + +error: +error_setg (errp,QERR_INVALID_PARAMETER_VALUE , name, +"a uint64 value or range" ); +} +static void parse_type_size (Visitor *v,uint64_t *obj,const char *name,Error **errp) {
@@ -332,6 +378,7 @@ StringInputVisitor *string_input_visitor_new(const char *str) v->visitor.type_enum = input_type_enum; v->visitor.type_int = parse_type_int; + v->visitor.type_uint64 = parse_type_uint64; v->visitor.type_size = parse_type_size; v->visitor.type_bool = parse_type_bool; v->visitor.type_str = parse_type_str;
Signature · 1 line
2.1.4
Re: [Qemu-devel] [PATCH 1/7] string-input-visitor: Fix uint64 parsing
Quoted 16 lines · Andreas Färber
All integers would get parsed by
strtoll(), not handling the case of UINT64 properties with the most significient bit set.Implement a .type_uint64 visitor callback, reusing the existing
parse_str()code through a new argument, usingstrtoull().As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
Signed-off-by Andreas Färber <[email protected]>--- qapi/string-input-visitor.c | 57 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-)
qapi/string-input-visitor.c static void parse_str(StringInputVisitor *siv, Error **errp) · line 54
Quoted 6 lines
qapi/string-input-visitor.c +2
-1@@ -50,7 +50,11 @@ static void parse_str(StringInputVisitor *siv, Error **errp) do { errno = 0; - start = strtoll(str, &endptr, 0); + if (u64) { + start = strtoull(str, &endptr, 0);
accepts the range [-ULLONG_MAX, ULLONG_MAX] (with 2s complement wraparound). Do you really want -1 being a synonym for ULLONG_MAX, or do you want to explicitly reject leading '-' when parsing unsigned (arguments can be made for both behaviors; in fact, libvirt has two separate wrappers for parsing uint64_t depending on which behavior is wanted)
qapi/string-input-visitor.c static void parse_str(StringInputVisitor *siv, Error **errp) · line 56
Quoted 2 lines
qapi/string-input-visitor.c +2
-0+ }else { + start =strtoll (str, &endptr,0 );
accepts the range [LLONG_MIN, LLONG_MAX] (that is, roughly half the range of the unsigned version)
qapi/string-input-visitor.c static void parse_str(StringInputVisitor *siv, Error **errp) · line 67
Quoted 10 lines
qapi/string-input-visitor.c +2
-1+ }if (errno ==0 && endptr > str) {if (*endptr =='\0' ) { cur =g_malloc0 (sizeof (*cur));@@ -60,7 +64,7 @@ static void parse_str(StringInputVisitor *siv, Error **errp) range_compare); cur =NULL ; str =NULL ; - }else if (*endptr =='-' ) { + }else if (*endptr =='-' && !u64) {
Why do you not want to handle ranges when using unsigned numbers?
qapi/string-input-visitor.c error: · line 237
Quoted 11 lines
qapi/string-input-visitor.c +10
-0+static void parse_type_uint64 (Visitor *v,uint64_t *obj,const char *name, +Error **errp) +{ +StringInputVisitor *siv =DO_UPCAST (StringInputVisitor, visitor, v); + +if (!siv->string) { +error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" , +"integer" ); +return ; + }
...
That's a lot of copy-and-paste. Can't you make parse_type_int64() and parse_type_uint64() both call into a single helper method, that contains the guts of the existing parse_type_int64() and adds a single parameter for the one place where the two functions differ on their call to parse_str()?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 1/7] string-input-visitor: Fix uint64 parsing
Am 25.09.2015 um 16:49 schrieb Eric Blake:
Quoted 31 lines · Andreas Färber
Quoted 16 lines
All integers would get parsed by
strtoll(), not handling the case of UINT64 properties with the most significient bit set.Implement a .type_uint64 visitor callback, reusing the existing
parse_str()code through a new argument, usingstrtoull().As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
Signed-off-by Andreas Färber <[email protected]>--- qapi/string-input-visitor.c | 57 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-)qapi/string-input-visitor.c static void parse_str(StringInputVisitor *siv, Error **errp) · line 54
Quoted 6 lines
qapi/string-input-visitor.c +2
-1@@ -50,7 +50,11 @@ static void parse_str(StringInputVisitor *siv, Error **errp) do { errno = 0; - start = strtoll(str, &endptr, 0); + if (u64) { + start = strtoull(str, &endptr, 0);accepts the range [-ULLONG_MAX,
ULLONG_MAX] (with 2s complement wraparound). Do you really want -1 being a synonym forULLONG_MAX, or do you want to explicitly reject leading '-' when parsing unsigned (arguments can be made for both behaviors; in fact, libvirt has two separate wrappers for parsinguint64_tdepending on which behavior is wanted)
Quoted 5 lines
qapi/string-input-visitor.c static void parse_str(StringInputVisitor *siv, Error **errp) · line 56
Quoted 2 lines
qapi/string-input-visitor.c +2
-0+ }else { + start =strtoll (str, &endptr,0 );accepts the range [
LLONG_MIN,LLONG_MAX] (that is, roughly half the range of the unsigned version)
No one has further commented on this, so I take it no further changes are required here for now.
Quoted 4 lines
Quoted 4 lines
qapi/string-input-visitor.c +1
-0+ }if (errno ==0 && endptr > str) {if (*endptr =='\0' ) { cur =g_malloc0 (sizeof (*cur));
Quoted 8 lines
qapi/string-input-visitor.c static void parse_str(StringInputVisitor *siv, Error **errp) · line 67
Quoted 6 lines
qapi/string-input-visitor.c +1
-1@@ -60,7 +64,7 @@ static void parse_str(StringInputVisitor *siv, Error **errp) range_compare); cur =NULL ; str =NULL ; - }else if (*endptr =='-' ) { + }else if (*endptr =='-' && !u64) {Why do you not want to handle ranges when using unsigned numbers?
For some reason I must've read this as handling negative numbers, which we wouldn't have for unsigned numbers...
However, since there is only one .start_list() callback, which passes !u64 to retain previous behavior, we would never actually run into this code path today. I've reverted my change and duplicated the strtoull() handling instead nonetheless.
Quoted 13 lines
Quoted 11 lines
qapi/string-input-visitor.c +10
-0+static void parse_type_uint64 (Visitor *v,uint64_t *obj,const char *name, +Error **errp) +{ +StringInputVisitor *siv =DO_UPCAST (StringInputVisitor, visitor, v); + +if (!siv->string) { +error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" , +"integer" ); +return ; + }...
Quoted 5 lines
That's a lot of copy-and-paste. Can't you make
parse_type_int64()andparse_type_uint64()both call into a single helper method, that contains the guts of the existingparse_type_int64()and adds a single parameter for the one place where the two functions differ on their call toparse_str()?
I don't see how. They have different signatures, and there's a lot of gotos that differ in the error message. I'm all for sharing code but it seems more work refactoring that code for reuse than duplication saved. If you have a concrete suggestion how to improve it, please share a diff or let's do that as follow-up.
Regards,
Andreas
Signature · 2 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Felix Imendörffer, Jane Smithard, Graham Norton; HRB 21284 (AG Nürnberg)
Re: [Qemu-devel] [PATCH 1/7] string-input-visitor: Fix uint64 parsing
In reply to Andreas FärberQuoted 2 lines · Andreas Färber
All integers would get parsed by
strtoll(), not handling the case of UINT64 properties with the most significient bit set.
This mess is part of a bigger mess, I'm afraid.
The major ways integers get parsed are:
* QMP: parse_literal() in qmp/qobject/json-parser.c
This is what parses QMP off the wire.
RFC 7159 does not prescribe range or precision of JSON numbers. Our implementation accepts the union of int64_t and double.
If the lexer recognizes a floating-point number, we convert it with strtod() and represent it as double.
If the lexer recognizes a decimal integer, and strtoll() can convert it, we represent it in int64_t. Else, we convert it with strtod() and represent it as double. Unclean: code assumes int64_t is long long.
Yes, that means QMP can't currently support the full range of QAPI's uint64 type.
* QemuOpts: parse_option_number() in util/qemu-option.c
This is what parses key=value,... strings for command line and other places.
QemuOpts can be used in two ways. If you fill out QemuOptDesc desc[], it rejects unknown keys and parses values of known keys. If you leave it empty, it accepts all keys, and doesn't parse values. Either way, it also stores raw string values.
QemuOpts' parser only supports unsigned numbers, in decimal, octal and hex. Error checking is very poor. In particular, it considers negative input valid, and silently casts it to uint64_t. I wouldn't be surprised if some code depended on that.
* String input visitor: parse_str() in qapi/string-input-visitor.c
This appears to be used only by QOM so far:
- object_property_get_enum()
- object_property_get_uint16List()
- object_property_parse()
parse_str() appears to parse some fancy list syntax. Comes from commit 659268f. The commit message is useless. I can't see offhand how this interacts with the visitor core.
Anyway, if we ignore the fancy crap and just look at the parsing of a single integer, we see that it supports int64_t in decimal, octal and hex, it fails to check for ERANGE, and assumes int64_t is long long.
* Options visitor: opts_type_int() in opts qapi/opts-visitor.c
This one is for converting QemuOpts to QAPI-defined C types. It uses the raw string values, not the parsed ones. The QemuOpts parser is neither needed nor wanted here. You should use the options visitor with an empty desc[] array to bypass it. Example: numa.c.
We got fancy list syntax again. This one looks like I could understand it with a bit of effort. But let's look just at the parsing of a single integer. It supports uint64_t in decimal, octal and hex, and *surprise* checks for errors carefully.
Fixing just a part of a mess can be okay. I just don't want to lever the bigger mess unmentioned.
Quoted 2 lines
Implement a .type_uint64 visitor callback, reusing the existing
parse_str()code through a new argument, usingstrtoull().
I'm afraid you're leaving the bug in the visitor core unfixed.
The (essentially undocumented) Visitor abstraction has the following methods for integers:
* Mandatory: type_int()
Interface uses int64_t for the value. The implementation should ensure it fits into int64_t.
* Optional: type_int{8,16,32}()
These use int{8,16,32}_t for the value.
If present, it should ensure the value fits into the data type.
If missing, the core falls back to type_int() plus appropriate range checking.
* Optional: type_int64()
Same interface as type_int().
If present, it should ensure the value fits into int64_t.
If missing, the core falls back to type_int().
Aside: setting type_int64() would be useful only when you want to distinguish QAPI types int and int64. So far, nobody does. In fact, nobody uses QAPI type int64! I'm tempted to define QAPI type int as a mere alias for int64 and drop the redundant stuff.
* Optional: type_uint{8,16,32}()
These use uint{8,16,32}_t for the value.
If present, it should ensure the value fits into the data type.
If missing, the core falls back to type_int() plus appropriate range checking.
* Optional: type_uint64()
Now it gets interesting. Interface uses uint64_t for the value.
If present, it should ensure the value fits into uint64_t.
If missing, the core falls back to type_int(). No range checking. If type_int() performs range checking as it should, then uint64_t values not representable in int64_t get rejected (wrong), and negative values representable in int64_t get cast to uint64_t (also wrong).
I think we need to make type_uint64() mandatory, and drop the fallback.
* Optional: type_size()
Same interface as type_uint64().
If present, it should ensure the value fits into uint64_t.
If missing, the core first tries falling back to type_uint64() and then to type_int(). Falling back to type_int() is as wrong here as it is in type_uint64().
Quoted 1 line
As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
I'm not sure I get this sentence.
Quoted 2 lines
Signed-off-by Andreas Färber <[email protected]>
On the actual patch, I have nothing to add over Eric's review right now.
Re: [Qemu-devel] [PATCH 1/7] string-input-visitor: Fix uint64 parsing
Am 30.09.2015 um 15:19 schrieb Markus Armbruster:
Quoted 4 lines
Quoted 1 line · Andreas Färber
As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
I'm not sure I get this sentence.
This patch causes checkpatch warnings. I intentionally do not address them in this bug-fix patch, but instead in a later patch in the series.
Andreas
Signature · 2 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Felix Imendörffer, Jane Smithard, Graham Norton; HRB 21284 (AG Nürnberg)
Re: [Qemu-devel] [PATCH 1/7] string-input-visitor: Fix uint64 parsing
Quoted 8 lines · Andreas Färber
Am 30.09.2015 um 15:19 schrieb Markus Armbruster:
Quoted 4 lines
Quoted 1 line · Andreas Färber
As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
I'm not sure I get this sentence.
This patch causes checkpatch warnings. I intentionally do not address them in this bug-fix patch, but instead in a later patch in the series.
Maybe:
As this is a bug fix, the patch intentionally ignores checkpatch warnings to prefer the use of qemu_strto[u]ll() to minimize size; a later patch will further address that issue.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 1/7] string-input-visitor: Fix uint64 parsing
In reply to Markus ArmbrusterQuoted 3 lines · Markus Armbruster
The (essentially undocumented) Visitor abstraction has the following methods for integers:
I proposed documentation at:
https://lists.gnu.org/archive/html/qemu-devel/2015-09/msg05434.html
Quoted 6 lines
* Mandatory:
type_int()Interface uses
int64_tfor the value. The implementation should ensure it fits intoint64_t.
Quoted 8 lines
* Optional: type_int{8,16,32}()
These use int{8,16,32}_t for the value.
If present, it should ensure the value fits into the data type.
If missing, the core falls back to
type_int()plus appropriate range checking.
No one implements them. In fact, as part of preparing my documentation, I actually proposed simplifying the visitor callback interface to drop them:
https://lists.gnu.org/archive/html/qemu-devel/2015-09/msg05432.html
Quoted 5 lines
* Optional:
type_int64()Same interface as
type_int().
Quoted 8 lines
If present, it should ensure the value fits into
int64_t.If missing, the core falls back to
type_int().Aside: setting
type_int64()would be useful only when you want to distinguish QAPI types int and int64. So far, nobody does. In fact, nobody uses QAPI type int64! I'm tempted to define QAPI type int as a mere alias for int64 and drop the redundant stuff.
Already part of my proposal.
Quoted 9 lines
* Optional: type_uint{8,16,32}()
These use uint{8,16,32}_t for the value.
If present, it should ensure the value fits into the data type.
If missing, the core falls back to
type_int()plus appropriate range checking.
Also unused, and simplified above.
Quoted 7 lines
* Optional:
type_uint64()Now it gets interesting. Interface uses
uint64_tfor the value.If present, it should ensure the value fits into
uint64_t.
Quoted 7 lines
If missing, the core falls back to
type_int(). No range checking. Iftype_int()performs range checking as it should, thenuint64_tvalues not representable inint64_tget rejected (wrong), and negative values representable inint64_tget cast touint64_t(also wrong).I think we need to make
type_uint64()mandatory, and drop the fallback.
Probably a good idea, although not done in my proposed patches.
Quoted 10 lines
* Optional:
type_size()Same interface as
type_uint64().If present, it should ensure the value fits into
uint64_t.If missing, the core first tries falling back to
type_uint64()and then totype_int(). Falling back totype_int()is as wrong here as it is intype_uint64().
Provided by the QemuOpts parser to allow '1k' to mean 1024, and so on.
Quoted 10 lines
Quoted 2 lines
As a bug fix, ignore warnings about preference of qemu_strto[u]ll().
I'm not sure I get this sentence.
Quoted 2 lines
Signed-off-by Andreas Färber <[email protected]>On the actual patch, I have nothing to add over Eric's review right now.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 2/7 test-string-input-visitor: Add int test case
In reply to Andreas FärberIn addition to -42 also parse the maximum int64.
--- tests/test-string-input-visitor.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tests/test-string-input-visitor.c +8 -0
@@ -62,6 +62,14 @@ static void test_visitor_in_int(TestInputVisitorData *data,visit_type_int (v, &res,NULL , &err);g_assert (!err); g_assert_cmpint(res, ==, value); +visitor_input_teardown (data, unused); + + value =INT64_MAX ; + v =visitor_input_test_init (data,g_strdup_printf ("%" PRId64, value)); + +visit_type_int (v, &res,NULL , &err); +g_assert (!err); + g_assert_cmpint(res, ==, value); } static void test_visitor_in_intList(TestInputVisitorData *data,
Signature · 1 line
2.1.4
Re: [Qemu-devel] [PATCH 2/7] test-string-input-visitor: Add int test case
Quoted 6 lines · Andreas Färber
In addition to -42 also parse the maximum int64.
Signed-off-by Andreas Färber <[email protected]>--- tests/test-string-input-visitor.c | 8 ++++++++ 1 file changed, 8 insertions(+)
Shouldn't we also add tests of one-beyond-range, to have coverage of expected error behavior when a number is too large in magnitude?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 3/7 test-string-input-visitor: Add uint64 test
In reply to Andreas FärberTest parsing of decimal and hexadecimal uint64 numbers with most significient bit set.
--- tests/test-string-input-visitor.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
tests/test-string-input-visitor.c +23 -0
@@ -72,6 +72,27 @@ static void test_visitor_in_int(TestInputVisitorData *data, g_assert_cmpint(res, ==, value); } +static void test_visitor_in_uint64 (TestInputVisitorData *data, +const void *unused) +{ +uint64_t res =0 , value =UINT64_MAX ; +Error *err =NULL ; +Visitor *v; + + v =visitor_input_test_init (data,g_strdup_printf ("%" PRIu64, value)); + +visit_type_uint64 (v, &res,NULL , &err); +g_assert (!err); + g_assert_cmpint(res, ==, value); +visitor_input_teardown (data, unused); + + v =visitor_input_test_init (data,g_strdup_printf ("0x%" PRIx64, value)); + +visit_type_uint64 (v, &res,NULL , &err); +g_assert (!err); + g_assert_cmpint(res, ==, value); +} +static void test_visitor_in_intList (TestInputVisitorData *data,const void *unused) {
@@ -271,6 +292,8 @@ int main(int argc, char **argv)input_visitor_test_add ("/string-visitor/input/int" , &in_visitor_data, test_visitor_in_int); +input_visitor_test_add ("/string-visitor/input/uint64" , + &in_visitor_data, test_visitor_in_uint64);input_visitor_test_add ("/string-visitor/input/intList" , &in_visitor_data, test_visitor_in_intList); input_visitor_test_add("/string-visitor/input/bool",
Signature · 1 line
2.1.4
Re: [Qemu-devel] [PATCH 3/7] test-string-input-visitor: Add uint64 test
Quoted 2 lines · Andreas Färber
Test parsing of decimal and hexadecimal uint64 numbers with most significient bit set.
s/significient/significant/
Quoted 5 lines
Signed-off-by Andreas Färber <[email protected]>--- tests/test-string-input-visitor.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
It would also be nice to add tests of one-beyond-range to make sure we are properly diagnosing range issues. And we need to make up our mind on whether "-1" is a parse error or accepted as a synonym for ULLONG_MAX when doing an unsigned parse.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 4/7 tests: Add QOM property unit tests
In reply to Andreas FärberAdd a test for parsing and setting a uint64 property.
--- MAINTAINERS | 1 + tests/Makefile | 3 ++ tests/check-qom-props.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/check-qom-props.c
MAINTAINERS +1 -0
@@ -1032,6 +1032,7 @@ F: include/qom/ X: include/qom/cpu.h F: qom/ X: qom/cpu.c +F: tests/check-qom-props.c F: tests/qom-test.c QMP
tests/Makefile +3 -0
@@ -72,6 +72,8 @@ check-unit-y += tests/check-qom-interface$(EXESUF) gcov-files-check-qom-interface-y = qom/object.c check-unit-y += tests/check-qom-proplist$(EXESUF) gcov-files-check-qom-proplist-y = qom/object.c +check-unit-y += tests/check-qom-props$(EXESUF) +gcov-files-check-qom-props-y = qom/object.c check-unit-y += tests/test-qemu-opts$(EXESUF) gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c check-unit-y += tests/test-write-threshold$(EXESUF)
@@ -303,6 +305,7 @@ tests/check-qfloat$(EXESUF): tests/check-qfloat.o $(test-util-obj-y) tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y) tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y) tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y) +tests/check-qom-props$(EXESUF): tests/check-qom-props.o $(test-qom-obj-y) tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y) tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y) tests/test-rfifolock$(EXESUF): tests/test-rfifolock.o $(test-util-obj-y)
tests/check-qom-props.c new file +120 -0
120 lines not on this page · raw message
Signature · 1 line
2.1.4
Re: [Qemu-devel] [PATCH 4/7] tests: Add QOM property unit tests
Quoted 13 lines · Andreas Färber
Add a test for parsing and setting a uint64 property.
Signed-off-by Andreas Färber <[email protected]>--- MAINTAINERS | 1 + tests/Makefile | 3 ++ tests/check-qom-props.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/check-qom-props.ctests/check-qom-props.c new file +2
-0+static void test_dummy_uint64 (void ) +{
tests/check-qom-props.c line 96
Quoted 10 lines
tests/check-qom-props.c new file +10
-0+Error *err =NULL ; +char *str; +DummyObject *dobj =DUMMY_OBJECT (object_new (TYPE_DUMMY )); + +g_assert (dobj->u64val ==0 ); + + str =g_strdup_printf ("%" PRIu64,UINT64_MAX ); +object_property_parse (OBJECT (dobj), str,"u64val" , &err); +g_free (str); +g_assert (!err);
Use &error_abort, then you don't need the g_assert(!err).
tests/check-qom-props.c line 106
Quoted 10 lines
tests/check-qom-props.c new file +10
-0+ g_assert_cmpint(dobj->u64val, ==, UINT64_MAX); + + dobj->u64val =0 ; + str =g_strdup_printf ("0x%" PRIx64,UINT64_MAX ); +object_property_parse (OBJECT (dobj), str,"u64val" , &err); +g_free (str); +g_assert (!err); + g_assert_cmpint(dobj->u64val, ==, UINT64_MAX); + +object_unref (OBJECT (dobj));
As with other patches in this series, intentionally testing the behavior of -1, and of (ULLONG_MAX+1), would be good.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 4/7] tests: Add QOM property unit tests
In reply to Andreas FärberQuoted 9 lines · Andreas Färber
Add a test for parsing and setting a uint64 property.
Signed-off-by Andreas Färber <[email protected]>--- MAINTAINERS | 1 + tests/Makefile | 3 ++ tests/check-qom-props.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/check-qom-props.c
FWIW, back in commit a31bdae5a76ecc060c1eb8a66be1896072c1e8b2 I added a check-qom-proplist.c unit test, and later extended it with test for enums. Perhaps your addition is best placed in there, or we could rename that test to check-qom-props.c so we just have one file for all property unit testing
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Re: [Qemu-devel] [PATCH 4/7] tests: Add QOM property unit tests
Am 25.09.2015 um 17:01 schrieb Daniel P. Berrange:
Quoted 11 lines · Andreas Färber
Quoted 9 lines
Add a test for parsing and setting a uint64 property.
Signed-off-by Andreas Färber <[email protected]>--- MAINTAINERS | 1 + tests/Makefile | 3 ++ tests/check-qom-props.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 tests/check-qom-props.c
Quoted 3 lines
FWIW, back in commit
a31bdae5a76ecc060c1eb8a66be1896072c1e8b2I added acheck-qom-proplist.cunit test, and later extended it with test for enums.
That's where your copyright and authorship came from.
Quoted 3 lines
Perhaps your addition is best placed in there, or we could rename that test to
check-qom-props.cso we just have one file for all property unit testing
I can only guess that my dislike was "/qom/proplist" as my test has nothing to do with lists. Otherwise it looks similar enough. I'll rename it and merge the two.
Regards,
Andreas
Signature · 2 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Felix Imendörffer, Jane Smithard, Graham Norton; HRB 21284 (AG Nürnberg)
PATCH 5/7 tests: Add scsi-disk test
In reply to Andreas FärberTest scsi-{disk,hd,cd} wwn properties for correct 64-bit parsing.
For now piggyback on virtio-scsi.
--- MAINTAINERS | 1 + tests/Makefile | 3 ++ tests/scsi-disk-test.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 tests/scsi-disk-test.c
MAINTAINERS +1 -0
@@ -688,6 +688,7 @@ M: Paolo Bonzini <[email protected]> S: Supported F: include/hw/scsi* F: hw/scsi/* +F: tests/scsi-disk-test.c T: git git://github.com/bonzini/qemu.git scsi-next LSI53C895A
tests/Makefile +3 -0
@@ -106,6 +106,8 @@ check-qtest-virtio-y += tests/virtio-rng-test$(EXESUF) gcov-files-virtio-y += hw/virtio/virtio-rng.c check-qtest-virtio-y += tests/virtio-scsi-test$(EXESUF) gcov-files-virtio-y += i386-softmmu/hw/scsi/virtio-scsi.c +check-qtest-virtio-y += tests/scsi-disk-test$(EXESUF) +gcov-files-virtio-y += i386-softmmu/hw/scsi/scsi-disk.c ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy) check-qtest-virtio-y += tests/virtio-9p-test$(EXESUF) gcov-files-virtio-y += hw/9pfs/virtio-9p.c
@@ -435,6 +437,7 @@ tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-usb-obj-y) tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o $(libqos-usb-obj-y) tests/pc-cpu-test$(EXESUF): tests/pc-cpu-test.o tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o $(qtest-obj-y) +tests/scsi-disk-test$(EXESUF): tests/scsi-disk-test.o tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y) tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y)
tests/scsi-disk-test.c new file +83 -0
83 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH 6/7 cutils: Normalize qemu_strto[u]ll() signature
In reply to Andreas FärberInstead of using int64_t for qemu_strtoll() and uiint64_t for qemu_strtoull(), use long long and unsigned long long as their name implies.
The only affected callers are our test cases.
This prepares for following checkpatch's recommendation of using it more, by making it easier to switch from POSIX to QEMU versions. Remaining difference is const-ness.
--- include/qemu-common.h | 4 +-- tests/test-cutils.c | 76 +++++++++++++++++++++++++-------------------------- util/cutils.c | 4 +-- 3 files changed, 42 insertions(+), 42 deletions(-)
include/qemu-common.h +2 -2
@@ -208,9 +208,9 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,int qemu_strtoul (const char *nptr,const char **endptr,int base,unsigned long *result);int qemu_strtoll (const char *nptr,const char **endptr,int base, -int64_t *result); +long long *result);int qemu_strtoull (const char *nptr,const char **endptr,int base, -uint64_t *result); +unsigned long long *result);int parse_uint (const char *s,unsigned long long *value,char **endptr,int base);
tests/test-cutils.c +38 -38
304 lines not on this page · raw message
util/cutils.c +2 -2
@@ -459,7 +459,7 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base,* See qemu_strtol() documentation for more info. */ int qemu_strtoll (const char *nptr,const char **endptr,int base, -int64_t *result) +long long *result) {char *p;int err =0 ;
@@ -482,7 +482,7 @@ int qemu_strtoll(const char *nptr, const char **endptr, int base,* See qemu_strtol() documentation for more info. */ int qemu_strtoull (const char *nptr,const char **endptr,int base, -uint64_t *result) +unsigned long long *result) {char *p;int err =0 ;
Signature · 1 line
2.1.4
Re: [Qemu-devel] [PATCH 6/7] cutils: Normalize qemu_strto[u]ll() signature
Quoted 9 lines · Andreas Färber
Instead of using
int64_tforqemu_strtoll()anduiint64_tforqemu_strtoull(), use long long and unsigned long long as their name implies.The only affected callers are our test cases.
This prepares for following checkpatch's recommendation of using it more, by making it easier to switch from POSIX to QEMU versions. Remaining difference is const-ness.
Do we actually use long long and unsigned long long anywhere?
Paolo
Re: [Qemu-devel] [PATCH 6/7] cutils: Normalize qemu_strto[u]ll() signature
Am 25.09.2015 um 14:42 schrieb Paolo Bonzini:
Quoted 5 lines · Andreas Färber
Quoted 4 lines
Instead of using
int64_tforqemu_strtoll()anduiint64_tforqemu_strtoull(), use long long and unsigned long long as their name implies.
Quoted 7 lines
Quoted 5 lines
The only affected callers are our test cases.
This prepares for following checkpatch's recommendation of using it more, by making it easier to switch from POSIX to QEMU versions. Remaining difference is const-ness.
Do we actually use long long and unsigned long long anywhere?
The next patch does, because checkpatch asks for it. :)
qemu_strtoull() has some special handling for Windows apparently.
Cheers,
Andreas
Signature · 2 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Felix Imendörffer, Jane Smithard, Graham Norton; HRB 21284 (AG Nürnberg)
Re: [Qemu-devel] [PATCH 6/7] cutils: Normalize qemu_strto[u]ll() signature
Quoted 5 lines · Andreas Färber
Quoted 1 line
Do we actually use long long and unsigned long long anywhere?
The next patch does, because checkpatch asks for it. :)
qemu_strtoull()has some special handling for Windows apparently.
No, I really mean the types. :) The qemu_strtoll/qemu_strtoull functions use {,u}int64_t because they are much more used than long long and unsigned long long.
Paolo
Re: [Qemu-devel] [PATCH 6/7] cutils: Normalize qemu_strto[u]ll() signature
Am 25.09.2015 um 14:56 schrieb Paolo Bonzini:
Quoted 10 lines · Andreas Färber
Quoted 5 lines
Quoted 1 line
Do we actually use long long and unsigned long long anywhere?
The next patch does, because checkpatch asks for it. :)
qemu_strtoull()has some special handling for Windows apparently.No, I really mean the types. :) The qemu_strtoll/qemu_strtoull functions use {,u}int64_t because they are much more used than long long and unsigned long long.
Well, my answer still stands: The next patch has code using long long.
Problem is that uint64_t foo = strtoull(...) works, while qemu_strtoull(..., &foo) causes a pointer mismatch warning treated as error. I could've converted those to uint64_t (assuming the type is not needed for something else), but I rather wanted to keep changes small.
If we want functions using [u]int64_t, we should name them ...strto[u]64, not mixing C and POSIX types.
But I assumed there may be some controversy, so I intentionally put this after the actual bug fixes and test cases, they can easily be dropped. :)
Cheers,
Andreas
Signature · 2 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Felix Imendörffer, Jane Smithard, Graham Norton; HRB 21284 (AG Nürnberg)
Re: [Qemu-devel] [PATCH 6/7] cutils: Normalize qemu_strto[u]ll() signature
Quoted 6 lines · Andreas Färber
Quoted 4 lines
Quoted 4 lines
No, I really mean the types. :) The qemu_strtoll/qemu_strtoull functions use {,u}int64_t because they are much more used than long long and unsigned long long.
Well, my answer still stands: The next patch has code using long long.
Quoted 7 lines
Problem is that
uint64_tfoo = strtoull(...) works, whileqemu_strtoull(..., &foo)causes a pointer mismatch warning treated as error. I could've converted those touint64_t(assuming the type is not needed for something else), but I rather wanted to keep changes small.If we want functions using [u]int64_t, we should name them ...strto[u]64, not mixing C and POSIX types.
Good point. Yes, I would prefer the function to be renamed and using uint64_t in string-input-visitor.
Paolo
Quoted 2 lines
But I assumed there may be some controversy, so I intentionally put this after the actual bug fixes and test cases, they can easily be dropped. :)
Re: [Qemu-devel] [PATCH 6/7] cutils: Normalize qemu_strto[u]ll() signature
In reply to Andreas FärberQuoted 1 line · Andreas Färber
Instead of using
int64_tforqemu_strtoll()anduiint64_tfor
s/uiint64/uint64/
Quoted 9 lines
qemu_strtoull(), use long long and unsigned long long as their name implies.The only affected callers are our test cases.
This prepares for following checkpatch's recommendation of using it more, by making it easier to switch from POSIX to QEMU versions. Remaining difference is const-ness.
Quoted 6 lines
Signed-off-by Andreas Färber <[email protected]>--- include/qemu-common.h | 4 +-- tests/test-cutils.c | 76 +++++++++++++++++++++++++-------------------------- util/cutils.c | 4 +-- 3 files changed, 42 insertions(+), 42 deletions(-)
I concur with Paolo's suggestion to instead rename the functions to make it obvious that we are parsing [u]int64, since that is more likely to match the type that the bulk of callers will already have on hand.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 7/7 string-input-visitor: Use qemu_strto[u]ll()
In reply to Andreas FärberSwitch from strtoll() and strtoull() to qemu_strtoll() and qemu_strtoull(), as recommended by checkpatch.
--- qapi/string-input-visitor.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
qapi/string-input-visitor.c +8 -7
@@ -39,10 +39,11 @@ static void free_range(void *range, void *dummy)static void parse_str (StringInputVisitor *siv,bool u64,Error **errp) { -char *str = (char *) siv->string; +const char *str = siv->string;long long start, end;Range *cur; -char *endptr; +const char *endptr; +int ret;if (siv->ranges) {return ;
@@ -51,11 +52,11 @@ static void parse_str(StringInputVisitor *siv, bool u64, Error **errp) do { errno = 0; if (u64) { - start = strtoull(str, &endptr, 0); + ret = qemu_strtoull(str, &endptr, 0, (unsigned long long *)&start); } else { - start = strtoll(str, &endptr, 0); + ret = qemu_strtoll(str, &endptr, 0, &start); } - if (errno == 0 && endptr > str) { + if (ret == 0 && endptr > str) { if (*endptr == '\0') { cur = g_malloc0(sizeof(*cur)); cur->begin = start;
@@ -67,8 +68,8 @@ static void parse_str(StringInputVisitor *siv, bool u64, Error **errp) }else if (*endptr =='-' && !u64) { str = endptr +1 ; errno =0 ; - end =strtoll (str, &endptr,0 ); -if (errno ==0 && endptr > str && start <= end && + ret =qemu_strtoll (str, &endptr,0 , &end); +if (ret ==0 && endptr > str && start <= end && (start >INT64_MAX -65536 || end < start +65536 )) {if (*endptr =='\0' ) {
Signature · 1 line
2.1.4