2 * Unit-tests for visitor-based serialization
4 * Copyright IBM, Corp. 2012
7 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
20 #include "qapi/qmp-input-visitor.h"
21 #include "qapi/qmp-output-visitor.h"
22 #include "qapi/string-input-visitor.h"
23 #include "qapi/string-output-visitor.h"
25 typedef struct PrimitiveType
{
56 const char *description
;
61 static void visit_primitive_type(Visitor
*v
, void **native
, Error
**errp
)
63 PrimitiveType
*pt
= *native
;
66 visit_type_str(v
, (char **)&pt
->value
.string
, NULL
, errp
);
69 visit_type_bool(v
, &pt
->value
.boolean
, NULL
, errp
);
72 visit_type_number(v
, &pt
->value
.number
, NULL
, errp
);
75 visit_type_int(v
, &pt
->value
.integer
, NULL
, errp
);
78 visit_type_uint8(v
, &pt
->value
.u8
, NULL
, errp
);
81 visit_type_uint16(v
, &pt
->value
.u16
, NULL
, errp
);
84 visit_type_uint32(v
, &pt
->value
.u32
, NULL
, errp
);
87 visit_type_uint64(v
, &pt
->value
.u64
, NULL
, errp
);
90 visit_type_int8(v
, &pt
->value
.s8
, NULL
, errp
);
93 visit_type_int16(v
, &pt
->value
.s16
, NULL
, errp
);
96 visit_type_int32(v
, &pt
->value
.s32
, NULL
, errp
);
99 visit_type_int64(v
, &pt
->value
.s64
, NULL
, errp
);
106 typedef struct TestStruct
113 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
114 const char *name
, Error
**errp
)
116 visit_start_struct(v
, (void **)obj
, NULL
, name
, sizeof(TestStruct
), errp
);
118 visit_type_int(v
, &(*obj
)->integer
, "integer", errp
);
119 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", errp
);
120 visit_type_str(v
, &(*obj
)->string
, "string", errp
);
122 visit_end_struct(v
, errp
);
125 static TestStruct
*struct_create(void)
127 TestStruct
*ts
= g_malloc0(sizeof(*ts
));
130 ts
->string
= strdup("test string");
134 static void struct_compare(TestStruct
*ts1
, TestStruct
*ts2
)
138 g_assert_cmpint(ts1
->integer
, ==, ts2
->integer
);
139 g_assert(ts1
->boolean
== ts2
->boolean
);
140 g_assert_cmpstr(ts1
->string
, ==, ts2
->string
);
143 static void struct_cleanup(TestStruct
*ts
)
149 static void visit_struct(Visitor
*v
, void **native
, Error
**errp
)
151 visit_type_TestStruct(v
, (TestStruct
**)native
, NULL
, errp
);
154 static UserDefNested
*nested_struct_create(void)
156 UserDefNested
*udnp
= g_malloc0(sizeof(*udnp
));
157 udnp
->string0
= strdup("test_string0");
158 udnp
->dict1
.string1
= strdup("test_string1");
159 udnp
->dict1
.dict2
.userdef1
= g_malloc0(sizeof(UserDefOne
));
160 udnp
->dict1
.dict2
.userdef1
->integer
= 42;
161 udnp
->dict1
.dict2
.userdef1
->string
= strdup("test_string");
162 udnp
->dict1
.dict2
.string2
= strdup("test_string2");
163 udnp
->dict1
.has_dict3
= true;
164 udnp
->dict1
.dict3
.userdef2
= g_malloc0(sizeof(UserDefOne
));
165 udnp
->dict1
.dict3
.userdef2
->integer
= 43;
166 udnp
->dict1
.dict3
.userdef2
->string
= strdup("test_string");
167 udnp
->dict1
.dict3
.string3
= strdup("test_string3");
171 static void nested_struct_compare(UserDefNested
*udnp1
, UserDefNested
*udnp2
)
175 g_assert_cmpstr(udnp1
->string0
, ==, udnp2
->string0
);
176 g_assert_cmpstr(udnp1
->dict1
.string1
, ==, udnp2
->dict1
.string1
);
177 g_assert_cmpint(udnp1
->dict1
.dict2
.userdef1
->integer
, ==,
178 udnp2
->dict1
.dict2
.userdef1
->integer
);
179 g_assert_cmpstr(udnp1
->dict1
.dict2
.userdef1
->string
, ==,
180 udnp2
->dict1
.dict2
.userdef1
->string
);
181 g_assert_cmpstr(udnp1
->dict1
.dict2
.string2
, ==, udnp2
->dict1
.dict2
.string2
);
182 g_assert(udnp1
->dict1
.has_dict3
== udnp2
->dict1
.has_dict3
);
183 g_assert_cmpint(udnp1
->dict1
.dict3
.userdef2
->integer
, ==,
184 udnp2
->dict1
.dict3
.userdef2
->integer
);
185 g_assert_cmpstr(udnp1
->dict1
.dict3
.userdef2
->string
, ==,
186 udnp2
->dict1
.dict3
.userdef2
->string
);
187 g_assert_cmpstr(udnp1
->dict1
.dict3
.string3
, ==, udnp2
->dict1
.dict3
.string3
);
190 static void nested_struct_cleanup(UserDefNested
*udnp
)
192 qapi_free_UserDefNested(udnp
);
195 static void visit_nested_struct(Visitor
*v
, void **native
, Error
**errp
)
197 visit_type_UserDefNested(v
, (UserDefNested
**)native
, NULL
, errp
);
200 static void visit_nested_struct_list(Visitor
*v
, void **native
, Error
**errp
)
202 visit_type_UserDefNestedList(v
, (UserDefNestedList
**)native
, NULL
, errp
);
207 typedef void (*VisitorFunc
)(Visitor
*v
, void **native
, Error
**errp
);
209 typedef enum VisitorCapabilities
{
213 } VisitorCapabilities
;
215 typedef struct SerializeOps
{
216 void (*serialize
)(void *native_in
, void **datap
,
217 VisitorFunc visit
, Error
**errp
);
218 void (*deserialize
)(void **native_out
, void *datap
,
219 VisitorFunc visit
, Error
**errp
);
220 void (*cleanup
)(void *datap
);
222 VisitorCapabilities caps
;
225 typedef struct TestArgs
{
226 const SerializeOps
*ops
;
230 #define FLOAT_STRING_PRECISION 6 /* corresponding to n in %.nf formatting */
231 static gsize
calc_float_string_storage(double value
)
233 int whole_value
= value
;
237 } while (whole_value
/= 10);
238 return i
+ 2 + FLOAT_STRING_PRECISION
;
241 static void test_primitives(gconstpointer opaque
)
243 TestArgs
*args
= (TestArgs
*) opaque
;
244 const SerializeOps
*ops
= args
->ops
;
245 PrimitiveType
*pt
= args
->test_data
;
246 PrimitiveType
*pt_copy
= g_malloc0(sizeof(*pt_copy
));
248 void *serialize_data
;
249 char *double1
, *double2
;
251 pt_copy
->type
= pt
->type
;
252 ops
->serialize(pt
, &serialize_data
, visit_primitive_type
, &err
);
253 ops
->deserialize((void **)&pt_copy
, serialize_data
, visit_primitive_type
, &err
);
255 g_assert(err
== NULL
);
256 g_assert(pt_copy
!= NULL
);
257 if (pt
->type
== PTYPE_STRING
) {
258 g_assert_cmpstr(pt
->value
.string
, ==, pt_copy
->value
.string
);
259 } else if (pt
->type
== PTYPE_NUMBER
) {
260 /* we serialize with %f for our reference visitors, so rather than fuzzy
261 * floating math to test "equality", just compare the formatted values
263 double1
= g_malloc0(calc_float_string_storage(pt
->value
.number
));
264 double2
= g_malloc0(calc_float_string_storage(pt_copy
->value
.number
));
265 g_assert_cmpstr(double1
, ==, double2
);
268 } else if (pt
->type
== PTYPE_BOOLEAN
) {
269 g_assert_cmpint(!!pt
->value
.max
, ==, !!pt
->value
.max
);
271 g_assert_cmpint(pt
->value
.max
, ==, pt_copy
->value
.max
);
274 ops
->cleanup(serialize_data
);
278 static void test_struct(gconstpointer opaque
)
280 TestArgs
*args
= (TestArgs
*) opaque
;
281 const SerializeOps
*ops
= args
->ops
;
282 TestStruct
*ts
= struct_create();
283 TestStruct
*ts_copy
= NULL
;
285 void *serialize_data
;
287 ops
->serialize(ts
, &serialize_data
, visit_struct
, &err
);
288 ops
->deserialize((void **)&ts_copy
, serialize_data
, visit_struct
, &err
);
290 g_assert(err
== NULL
);
291 struct_compare(ts
, ts_copy
);
294 struct_cleanup(ts_copy
);
296 ops
->cleanup(serialize_data
);
300 static void test_nested_struct(gconstpointer opaque
)
302 TestArgs
*args
= (TestArgs
*) opaque
;
303 const SerializeOps
*ops
= args
->ops
;
304 UserDefNested
*udnp
= nested_struct_create();
305 UserDefNested
*udnp_copy
= NULL
;
307 void *serialize_data
;
309 ops
->serialize(udnp
, &serialize_data
, visit_nested_struct
, &err
);
310 ops
->deserialize((void **)&udnp_copy
, serialize_data
, visit_nested_struct
, &err
);
312 g_assert(err
== NULL
);
313 nested_struct_compare(udnp
, udnp_copy
);
315 nested_struct_cleanup(udnp
);
316 nested_struct_cleanup(udnp_copy
);
318 ops
->cleanup(serialize_data
);
322 static void test_nested_struct_list(gconstpointer opaque
)
324 TestArgs
*args
= (TestArgs
*) opaque
;
325 const SerializeOps
*ops
= args
->ops
;
326 UserDefNestedList
*listp
= NULL
, *tmp
, *tmp_copy
, *listp_copy
= NULL
;
328 void *serialize_data
;
331 for (i
= 0; i
< 8; i
++) {
332 tmp
= g_malloc0(sizeof(UserDefNestedList
));
333 tmp
->value
= nested_struct_create();
338 ops
->serialize(listp
, &serialize_data
, visit_nested_struct_list
, &err
);
339 ops
->deserialize((void **)&listp_copy
, serialize_data
,
340 visit_nested_struct_list
, &err
);
342 g_assert(err
== NULL
);
345 tmp_copy
= listp_copy
;
348 nested_struct_compare(listp
->value
, listp_copy
->value
);
350 listp_copy
= listp_copy
->next
;
353 qapi_free_UserDefNestedList(tmp
);
354 qapi_free_UserDefNestedList(tmp_copy
);
356 ops
->cleanup(serialize_data
);
360 PrimitiveType pt_values
[] = {
363 .description
= "string_empty",
364 .type
= PTYPE_STRING
,
368 .description
= "string_whitespace",
369 .type
= PTYPE_STRING
,
370 .value
.string
= "a b c\td",
373 .description
= "string_newlines",
374 .type
= PTYPE_STRING
,
375 .value
.string
= "a\nb\n",
378 .description
= "string_commas",
379 .type
= PTYPE_STRING
,
380 .value
.string
= "a,b, c,d",
383 .description
= "string_single_quoted",
384 .type
= PTYPE_STRING
,
385 .value
.string
= "'a b',cd",
388 .description
= "string_double_quoted",
389 .type
= PTYPE_STRING
,
390 .value
.string
= "\"a b\",cd",
394 .description
= "boolean_true1",
395 .type
= PTYPE_BOOLEAN
,
396 .value
.boolean
= true,
399 .description
= "boolean_true2",
400 .type
= PTYPE_BOOLEAN
,
404 .description
= "boolean_true3",
405 .type
= PTYPE_BOOLEAN
,
409 .description
= "boolean_false1",
410 .type
= PTYPE_BOOLEAN
,
411 .value
.boolean
= false,
414 .description
= "boolean_false2",
415 .type
= PTYPE_BOOLEAN
,
418 /* number tests (double) */
419 /* note: we format these to %.6f before comparing, since that's how
420 * we serialize them and it doesn't make sense to check precision
424 .description
= "number_sanity1",
425 .type
= PTYPE_NUMBER
,
429 .description
= "number_sanity2",
430 .type
= PTYPE_NUMBER
,
431 .value
.number
= 3.14159265,
434 .description
= "number_min",
435 .type
= PTYPE_NUMBER
,
436 .value
.number
= DBL_MIN
,
439 .description
= "number_max",
440 .type
= PTYPE_NUMBER
,
441 .value
.number
= DBL_MAX
,
443 /* integer tests (int64) */
445 .description
= "integer_sanity1",
446 .type
= PTYPE_INTEGER
,
450 .description
= "integer_sanity2",
451 .type
= PTYPE_INTEGER
,
452 .value
.integer
= INT64_MAX
/ 2 + 1,
455 .description
= "integer_min",
456 .type
= PTYPE_INTEGER
,
457 .value
.integer
= INT64_MIN
,
460 .description
= "integer_max",
461 .type
= PTYPE_INTEGER
,
462 .value
.integer
= INT64_MAX
,
466 .description
= "uint8_sanity1",
471 .description
= "uint8_sanity2",
473 .value
.u8
= UINT8_MAX
/ 2 + 1,
476 .description
= "uint8_min",
481 .description
= "uint8_max",
483 .value
.u8
= UINT8_MAX
,
487 .description
= "uint16_sanity1",
492 .description
= "uint16_sanity2",
494 .value
.u16
= UINT16_MAX
/ 2 + 1,
497 .description
= "uint16_min",
502 .description
= "uint16_max",
504 .value
.u16
= UINT16_MAX
,
508 .description
= "uint32_sanity1",
513 .description
= "uint32_sanity2",
515 .value
.u32
= UINT32_MAX
/ 2 + 1,
518 .description
= "uint32_min",
523 .description
= "uint32_max",
525 .value
.u32
= UINT32_MAX
,
529 .description
= "uint64_sanity1",
534 .description
= "uint64_sanity2",
536 .value
.u64
= UINT64_MAX
/ 2 + 1,
539 .description
= "uint64_min",
544 .description
= "uint64_max",
546 .value
.u64
= UINT64_MAX
,
550 .description
= "int8_sanity1",
555 .description
= "int8_sanity2",
557 .value
.s8
= INT8_MAX
/ 2 + 1,
560 .description
= "int8_min",
562 .value
.s8
= INT8_MIN
,
565 .description
= "int8_max",
567 .value
.s8
= INT8_MAX
,
571 .description
= "int16_sanity1",
576 .description
= "int16_sanity2",
578 .value
.s16
= INT16_MAX
/ 2 + 1,
581 .description
= "int16_min",
583 .value
.s16
= INT16_MIN
,
586 .description
= "int16_max",
588 .value
.s16
= INT16_MAX
,
592 .description
= "int32_sanity1",
597 .description
= "int32_sanity2",
599 .value
.s32
= INT32_MAX
/ 2 + 1,
602 .description
= "int32_min",
604 .value
.s32
= INT32_MIN
,
607 .description
= "int32_max",
609 .value
.s32
= INT32_MAX
,
613 .description
= "int64_sanity1",
618 .description
= "int64_sanity2",
620 .value
.s64
= INT64_MAX
/ 2 + 1,
623 .description
= "int64_min",
625 .value
.s64
= INT64_MIN
,
628 .description
= "int64_max",
630 .value
.s64
= INT64_MAX
,
632 { .type
= PTYPE_EOL
}
635 /* visitor-specific op implementations */
637 typedef struct QmpSerializeData
{
638 QmpOutputVisitor
*qov
;
639 QmpInputVisitor
*qiv
;
642 static void qmp_serialize(void *native_in
, void **datap
,
643 VisitorFunc visit
, Error
**errp
)
645 QmpSerializeData
*d
= g_malloc0(sizeof(*d
));
647 d
->qov
= qmp_output_visitor_new();
648 visit(qmp_output_get_visitor(d
->qov
), &native_in
, errp
);
652 static void qmp_deserialize(void **native_out
, void *datap
,
653 VisitorFunc visit
, Error
**errp
)
655 QmpSerializeData
*d
= datap
;
656 QString
*output_json
= qobject_to_json(qmp_output_get_qobject(d
->qov
));
657 QObject
*obj
= qobject_from_json(qstring_get_str(output_json
));
659 QDECREF(output_json
);
660 d
->qiv
= qmp_input_visitor_new(obj
);
661 visit(qmp_input_get_visitor(d
->qiv
), native_out
, errp
);
664 static void qmp_cleanup(void *datap
)
666 QmpSerializeData
*d
= datap
;
667 qmp_output_visitor_cleanup(d
->qov
);
668 qmp_input_visitor_cleanup(d
->qiv
);
671 typedef struct StringSerializeData
{
672 StringOutputVisitor
*sov
;
673 StringInputVisitor
*siv
;
674 } StringSerializeData
;
676 static void string_serialize(void *native_in
, void **datap
,
677 VisitorFunc visit
, Error
**errp
)
679 StringSerializeData
*d
= g_malloc0(sizeof(*d
));
681 d
->sov
= string_output_visitor_new();
682 visit(string_output_get_visitor(d
->sov
), &native_in
, errp
);
686 static void string_deserialize(void **native_out
, void *datap
,
687 VisitorFunc visit
, Error
**errp
)
689 StringSerializeData
*d
= datap
;
691 d
->siv
= string_input_visitor_new(string_output_get_string(d
->sov
));
692 visit(string_input_get_visitor(d
->siv
), native_out
, errp
);
695 static void string_cleanup(void *datap
)
697 StringSerializeData
*d
= datap
;
698 string_output_visitor_cleanup(d
->sov
);
699 string_input_visitor_cleanup(d
->siv
);
702 /* visitor registration, test harness */
704 /* note: to function interchangeably as a serialization mechanism your
705 * visitor test implementation should pass the test cases for all visitor
706 * capabilities: primitives, structures, and lists
708 static const SerializeOps visitors
[] = {
711 .serialize
= qmp_serialize
,
712 .deserialize
= qmp_deserialize
,
713 .cleanup
= qmp_cleanup
,
714 .caps
= VCAP_PRIMITIVES
| VCAP_STRUCTURES
| VCAP_LISTS
718 .serialize
= string_serialize
,
719 .deserialize
= string_deserialize
,
720 .cleanup
= string_cleanup
,
721 .caps
= VCAP_PRIMITIVES
726 static void add_visitor_type(const SerializeOps
*ops
)
728 char testname_prefix
[128];
733 sprintf(testname_prefix
, "/visitor/serialization/%s", ops
->type
);
735 if (ops
->caps
& VCAP_PRIMITIVES
) {
736 while (pt_values
[i
].type
!= PTYPE_EOL
) {
737 sprintf(testname
, "%s/primitives/%s", testname_prefix
,
738 pt_values
[i
].description
);
739 args
= g_malloc0(sizeof(*args
));
741 args
->test_data
= &pt_values
[i
];
742 g_test_add_data_func(testname
, args
, test_primitives
);
747 if (ops
->caps
& VCAP_STRUCTURES
) {
748 sprintf(testname
, "%s/struct", testname_prefix
);
749 args
= g_malloc0(sizeof(*args
));
751 args
->test_data
= NULL
;
752 g_test_add_data_func(testname
, args
, test_struct
);
754 sprintf(testname
, "%s/nested_struct", testname_prefix
);
755 args
= g_malloc0(sizeof(*args
));
757 args
->test_data
= NULL
;
758 g_test_add_data_func(testname
, args
, test_nested_struct
);
761 if (ops
->caps
& VCAP_LISTS
) {
762 sprintf(testname
, "%s/nested_struct_list", testname_prefix
);
763 args
= g_malloc0(sizeof(*args
));
765 args
->test_data
= NULL
;
766 g_test_add_data_func(testname
, args
, test_nested_struct_list
);
770 int main(int argc
, char **argv
)
774 g_test_init(&argc
, &argv
, NULL
);
776 while (visitors
[i
].type
!= NULL
) {
777 add_visitor_type(&visitors
[i
]);