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.
18 #include "qemu-common.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp-input-visitor.h"
23 #include "qapi/qmp-output-visitor.h"
24 #include "qapi/string-input-visitor.h"
25 #include "qapi/string-output-visitor.h"
26 #include "qapi-types.h"
27 #include "qapi-visit.h"
28 #include "qapi/dealloc-visitor.h"
30 enum PrimitiveTypeKind
{
46 typedef struct PrimitiveType
{
62 enum PrimitiveTypeKind type
;
63 const char *description
;
66 typedef struct PrimitiveList
{
72 int8List
*s8_integers
;
73 int16List
*s16_integers
;
74 int32List
*s32_integers
;
75 int64List
*s64_integers
;
76 uint8List
*u8_integers
;
77 uint16List
*u16_integers
;
78 uint32List
*u32_integers
;
79 uint64List
*u64_integers
;
81 enum PrimitiveTypeKind type
;
82 const char *description
;
87 typedef void (*VisitorFunc
)(Visitor
*v
, void **native
, Error
**errp
);
89 static void dealloc_helper(void *native_in
, VisitorFunc visit
, Error
**errp
)
91 QapiDeallocVisitor
*qdv
= qapi_dealloc_visitor_new();
93 visit(qapi_dealloc_get_visitor(qdv
), &native_in
, errp
);
95 qapi_dealloc_visitor_cleanup(qdv
);
98 static void visit_primitive_type(Visitor
*v
, void **native
, Error
**errp
)
100 PrimitiveType
*pt
= *native
;
103 visit_type_str(v
, (char **)&pt
->value
.string
, NULL
, errp
);
106 visit_type_bool(v
, &pt
->value
.boolean
, NULL
, errp
);
109 visit_type_number(v
, &pt
->value
.number
, NULL
, errp
);
112 visit_type_int(v
, &pt
->value
.integer
, NULL
, errp
);
115 visit_type_uint8(v
, &pt
->value
.u8
, NULL
, errp
);
118 visit_type_uint16(v
, &pt
->value
.u16
, NULL
, errp
);
121 visit_type_uint32(v
, &pt
->value
.u32
, NULL
, errp
);
124 visit_type_uint64(v
, &pt
->value
.u64
, NULL
, errp
);
127 visit_type_int8(v
, &pt
->value
.s8
, NULL
, errp
);
130 visit_type_int16(v
, &pt
->value
.s16
, NULL
, errp
);
133 visit_type_int32(v
, &pt
->value
.s32
, NULL
, errp
);
136 visit_type_int64(v
, &pt
->value
.s64
, NULL
, errp
);
139 g_assert_not_reached();
143 static void visit_primitive_list(Visitor
*v
, void **native
, Error
**errp
)
145 PrimitiveList
*pl
= *native
;
148 visit_type_strList(v
, &pl
->value
.strings
, NULL
, errp
);
151 visit_type_boolList(v
, &pl
->value
.booleans
, NULL
, errp
);
154 visit_type_numberList(v
, &pl
->value
.numbers
, NULL
, errp
);
157 visit_type_intList(v
, &pl
->value
.integers
, NULL
, errp
);
160 visit_type_int8List(v
, &pl
->value
.s8_integers
, NULL
, errp
);
163 visit_type_int16List(v
, &pl
->value
.s16_integers
, NULL
, errp
);
166 visit_type_int32List(v
, &pl
->value
.s32_integers
, NULL
, errp
);
169 visit_type_int64List(v
, &pl
->value
.s64_integers
, NULL
, errp
);
172 visit_type_uint8List(v
, &pl
->value
.u8_integers
, NULL
, errp
);
175 visit_type_uint16List(v
, &pl
->value
.u16_integers
, NULL
, errp
);
178 visit_type_uint32List(v
, &pl
->value
.u32_integers
, NULL
, errp
);
181 visit_type_uint64List(v
, &pl
->value
.u64_integers
, NULL
, errp
);
184 g_assert_not_reached();
188 typedef struct TestStruct
195 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
196 const char *name
, Error
**errp
)
200 visit_start_struct(v
, (void **)obj
, NULL
, name
, sizeof(TestStruct
), &err
);
205 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
209 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
213 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
216 error_propagate(errp
, err
);
218 visit_end_struct(v
, &err
);
220 error_propagate(errp
, err
);
223 static TestStruct
*struct_create(void)
225 TestStruct
*ts
= g_malloc0(sizeof(*ts
));
228 ts
->string
= strdup("test string");
232 static void struct_compare(TestStruct
*ts1
, TestStruct
*ts2
)
236 g_assert_cmpint(ts1
->integer
, ==, ts2
->integer
);
237 g_assert(ts1
->boolean
== ts2
->boolean
);
238 g_assert_cmpstr(ts1
->string
, ==, ts2
->string
);
241 static void struct_cleanup(TestStruct
*ts
)
247 static void visit_struct(Visitor
*v
, void **native
, Error
**errp
)
249 visit_type_TestStruct(v
, (TestStruct
**)native
, NULL
, errp
);
252 static UserDefNested
*nested_struct_create(void)
254 UserDefNested
*udnp
= g_malloc0(sizeof(*udnp
));
255 udnp
->string0
= strdup("test_string0");
256 udnp
->dict1
.string1
= strdup("test_string1");
257 udnp
->dict1
.dict2
.userdef1
= g_malloc0(sizeof(UserDefOne
));
258 udnp
->dict1
.dict2
.userdef1
->base
= g_new0(UserDefZero
, 1);
259 udnp
->dict1
.dict2
.userdef1
->base
->integer
= 42;
260 udnp
->dict1
.dict2
.userdef1
->string
= strdup("test_string");
261 udnp
->dict1
.dict2
.string2
= strdup("test_string2");
262 udnp
->dict1
.has_dict3
= true;
263 udnp
->dict1
.dict3
.userdef2
= g_malloc0(sizeof(UserDefOne
));
264 udnp
->dict1
.dict3
.userdef2
->base
= g_new0(UserDefZero
, 1);
265 udnp
->dict1
.dict3
.userdef2
->base
->integer
= 43;
266 udnp
->dict1
.dict3
.userdef2
->string
= strdup("test_string");
267 udnp
->dict1
.dict3
.string3
= strdup("test_string3");
271 static void nested_struct_compare(UserDefNested
*udnp1
, UserDefNested
*udnp2
)
275 g_assert_cmpstr(udnp1
->string0
, ==, udnp2
->string0
);
276 g_assert_cmpstr(udnp1
->dict1
.string1
, ==, udnp2
->dict1
.string1
);
277 g_assert_cmpint(udnp1
->dict1
.dict2
.userdef1
->base
->integer
, ==,
278 udnp2
->dict1
.dict2
.userdef1
->base
->integer
);
279 g_assert_cmpstr(udnp1
->dict1
.dict2
.userdef1
->string
, ==,
280 udnp2
->dict1
.dict2
.userdef1
->string
);
281 g_assert_cmpstr(udnp1
->dict1
.dict2
.string2
, ==, udnp2
->dict1
.dict2
.string2
);
282 g_assert(udnp1
->dict1
.has_dict3
== udnp2
->dict1
.has_dict3
);
283 g_assert_cmpint(udnp1
->dict1
.dict3
.userdef2
->base
->integer
, ==,
284 udnp2
->dict1
.dict3
.userdef2
->base
->integer
);
285 g_assert_cmpstr(udnp1
->dict1
.dict3
.userdef2
->string
, ==,
286 udnp2
->dict1
.dict3
.userdef2
->string
);
287 g_assert_cmpstr(udnp1
->dict1
.dict3
.string3
, ==, udnp2
->dict1
.dict3
.string3
);
290 static void nested_struct_cleanup(UserDefNested
*udnp
)
292 qapi_free_UserDefNested(udnp
);
295 static void visit_nested_struct(Visitor
*v
, void **native
, Error
**errp
)
297 visit_type_UserDefNested(v
, (UserDefNested
**)native
, NULL
, errp
);
300 static void visit_nested_struct_list(Visitor
*v
, void **native
, Error
**errp
)
302 visit_type_UserDefNestedList(v
, (UserDefNestedList
**)native
, NULL
, errp
);
307 typedef enum VisitorCapabilities
{
311 VCAP_PRIMITIVE_LISTS
= 8,
312 } VisitorCapabilities
;
314 typedef struct SerializeOps
{
315 void (*serialize
)(void *native_in
, void **datap
,
316 VisitorFunc visit
, Error
**errp
);
317 void (*deserialize
)(void **native_out
, void *datap
,
318 VisitorFunc visit
, Error
**errp
);
319 void (*cleanup
)(void *datap
);
321 VisitorCapabilities caps
;
324 typedef struct TestArgs
{
325 const SerializeOps
*ops
;
329 static void test_primitives(gconstpointer opaque
)
331 TestArgs
*args
= (TestArgs
*) opaque
;
332 const SerializeOps
*ops
= args
->ops
;
333 PrimitiveType
*pt
= args
->test_data
;
334 PrimitiveType
*pt_copy
= g_malloc0(sizeof(*pt_copy
));
336 void *serialize_data
;
338 pt_copy
->type
= pt
->type
;
339 ops
->serialize(pt
, &serialize_data
, visit_primitive_type
, &err
);
340 ops
->deserialize((void **)&pt_copy
, serialize_data
, visit_primitive_type
, &err
);
342 g_assert(err
== NULL
);
343 g_assert(pt_copy
!= NULL
);
344 if (pt
->type
== PTYPE_STRING
) {
345 g_assert_cmpstr(pt
->value
.string
, ==, pt_copy
->value
.string
);
346 g_free((char *)pt_copy
->value
.string
);
347 } else if (pt
->type
== PTYPE_NUMBER
) {
348 GString
*double_expected
= g_string_new("");
349 GString
*double_actual
= g_string_new("");
350 /* we serialize with %f for our reference visitors, so rather than fuzzy
351 * floating math to test "equality", just compare the formatted values
353 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
354 g_string_printf(double_actual
, "%.6f", pt_copy
->value
.number
);
355 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
357 g_string_free(double_expected
, true);
358 g_string_free(double_actual
, true);
359 } else if (pt
->type
== PTYPE_BOOLEAN
) {
360 g_assert_cmpint(!!pt
->value
.max
, ==, !!pt
->value
.max
);
362 g_assert_cmpint(pt
->value
.max
, ==, pt_copy
->value
.max
);
365 ops
->cleanup(serialize_data
);
370 static void test_primitive_lists(gconstpointer opaque
)
372 TestArgs
*args
= (TestArgs
*) opaque
;
373 const SerializeOps
*ops
= args
->ops
;
374 PrimitiveType
*pt
= args
->test_data
;
375 PrimitiveList pl
= { .value
= { NULL
} };
376 PrimitiveList pl_copy
= { .value
= { NULL
} };
377 PrimitiveList
*pl_copy_ptr
= &pl_copy
;
379 void *serialize_data
;
380 void *cur_head
= NULL
;
383 pl
.type
= pl_copy
.type
= pt
->type
;
385 /* build up our list of primitive types */
386 for (i
= 0; i
< 32; i
++) {
389 strList
*tmp
= g_new0(strList
, 1);
390 tmp
->value
= g_strdup(pt
->value
.string
);
391 if (pl
.value
.strings
== NULL
) {
392 pl
.value
.strings
= tmp
;
394 tmp
->next
= pl
.value
.strings
;
395 pl
.value
.strings
= tmp
;
399 case PTYPE_INTEGER
: {
400 intList
*tmp
= g_new0(intList
, 1);
401 tmp
->value
= pt
->value
.integer
;
402 if (pl
.value
.integers
== NULL
) {
403 pl
.value
.integers
= tmp
;
405 tmp
->next
= pl
.value
.integers
;
406 pl
.value
.integers
= tmp
;
411 int8List
*tmp
= g_new0(int8List
, 1);
412 tmp
->value
= pt
->value
.s8
;
413 if (pl
.value
.s8_integers
== NULL
) {
414 pl
.value
.s8_integers
= tmp
;
416 tmp
->next
= pl
.value
.s8_integers
;
417 pl
.value
.s8_integers
= tmp
;
422 int16List
*tmp
= g_new0(int16List
, 1);
423 tmp
->value
= pt
->value
.s16
;
424 if (pl
.value
.s16_integers
== NULL
) {
425 pl
.value
.s16_integers
= tmp
;
427 tmp
->next
= pl
.value
.s16_integers
;
428 pl
.value
.s16_integers
= tmp
;
433 int32List
*tmp
= g_new0(int32List
, 1);
434 tmp
->value
= pt
->value
.s32
;
435 if (pl
.value
.s32_integers
== NULL
) {
436 pl
.value
.s32_integers
= tmp
;
438 tmp
->next
= pl
.value
.s32_integers
;
439 pl
.value
.s32_integers
= tmp
;
444 int64List
*tmp
= g_new0(int64List
, 1);
445 tmp
->value
= pt
->value
.s64
;
446 if (pl
.value
.s64_integers
== NULL
) {
447 pl
.value
.s64_integers
= tmp
;
449 tmp
->next
= pl
.value
.s64_integers
;
450 pl
.value
.s64_integers
= tmp
;
455 uint8List
*tmp
= g_new0(uint8List
, 1);
456 tmp
->value
= pt
->value
.u8
;
457 if (pl
.value
.u8_integers
== NULL
) {
458 pl
.value
.u8_integers
= tmp
;
460 tmp
->next
= pl
.value
.u8_integers
;
461 pl
.value
.u8_integers
= tmp
;
466 uint16List
*tmp
= g_new0(uint16List
, 1);
467 tmp
->value
= pt
->value
.u16
;
468 if (pl
.value
.u16_integers
== NULL
) {
469 pl
.value
.u16_integers
= tmp
;
471 tmp
->next
= pl
.value
.u16_integers
;
472 pl
.value
.u16_integers
= tmp
;
477 uint32List
*tmp
= g_new0(uint32List
, 1);
478 tmp
->value
= pt
->value
.u32
;
479 if (pl
.value
.u32_integers
== NULL
) {
480 pl
.value
.u32_integers
= tmp
;
482 tmp
->next
= pl
.value
.u32_integers
;
483 pl
.value
.u32_integers
= tmp
;
488 uint64List
*tmp
= g_new0(uint64List
, 1);
489 tmp
->value
= pt
->value
.u64
;
490 if (pl
.value
.u64_integers
== NULL
) {
491 pl
.value
.u64_integers
= tmp
;
493 tmp
->next
= pl
.value
.u64_integers
;
494 pl
.value
.u64_integers
= tmp
;
499 numberList
*tmp
= g_new0(numberList
, 1);
500 tmp
->value
= pt
->value
.number
;
501 if (pl
.value
.numbers
== NULL
) {
502 pl
.value
.numbers
= tmp
;
504 tmp
->next
= pl
.value
.numbers
;
505 pl
.value
.numbers
= tmp
;
509 case PTYPE_BOOLEAN
: {
510 boolList
*tmp
= g_new0(boolList
, 1);
511 tmp
->value
= pt
->value
.boolean
;
512 if (pl
.value
.booleans
== NULL
) {
513 pl
.value
.booleans
= tmp
;
515 tmp
->next
= pl
.value
.booleans
;
516 pl
.value
.booleans
= tmp
;
521 g_assert_not_reached();
525 ops
->serialize((void **)&pl
, &serialize_data
, visit_primitive_list
, &err
);
526 ops
->deserialize((void **)&pl_copy_ptr
, serialize_data
, visit_primitive_list
, &err
);
528 g_assert(err
== NULL
);
531 /* compare our deserialized list of primitives to the original */
533 switch (pl_copy
.type
) {
538 cur_head
= ptr
->next
;
540 cur_head
= ptr
= pl_copy
.value
.strings
;
542 g_assert_cmpstr(pt
->value
.string
, ==, ptr
->value
);
545 case PTYPE_INTEGER
: {
549 cur_head
= ptr
->next
;
551 cur_head
= ptr
= pl_copy
.value
.integers
;
553 g_assert_cmpint(pt
->value
.integer
, ==, ptr
->value
);
560 cur_head
= ptr
->next
;
562 cur_head
= ptr
= pl_copy
.value
.s8_integers
;
564 g_assert_cmpint(pt
->value
.s8
, ==, ptr
->value
);
571 cur_head
= ptr
->next
;
573 cur_head
= ptr
= pl_copy
.value
.s16_integers
;
575 g_assert_cmpint(pt
->value
.s16
, ==, ptr
->value
);
582 cur_head
= ptr
->next
;
584 cur_head
= ptr
= pl_copy
.value
.s32_integers
;
586 g_assert_cmpint(pt
->value
.s32
, ==, ptr
->value
);
593 cur_head
= ptr
->next
;
595 cur_head
= ptr
= pl_copy
.value
.s64_integers
;
597 g_assert_cmpint(pt
->value
.s64
, ==, ptr
->value
);
604 cur_head
= ptr
->next
;
606 cur_head
= ptr
= pl_copy
.value
.u8_integers
;
608 g_assert_cmpint(pt
->value
.u8
, ==, ptr
->value
);
615 cur_head
= ptr
->next
;
617 cur_head
= ptr
= pl_copy
.value
.u16_integers
;
619 g_assert_cmpint(pt
->value
.u16
, ==, ptr
->value
);
626 cur_head
= ptr
->next
;
628 cur_head
= ptr
= pl_copy
.value
.u32_integers
;
630 g_assert_cmpint(pt
->value
.u32
, ==, ptr
->value
);
637 cur_head
= ptr
->next
;
639 cur_head
= ptr
= pl_copy
.value
.u64_integers
;
641 g_assert_cmpint(pt
->value
.u64
, ==, ptr
->value
);
646 GString
*double_expected
= g_string_new("");
647 GString
*double_actual
= g_string_new("");
650 cur_head
= ptr
->next
;
652 cur_head
= ptr
= pl_copy
.value
.numbers
;
654 /* we serialize with %f for our reference visitors, so rather than
655 * fuzzy floating math to test "equality", just compare the
658 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
659 g_string_printf(double_actual
, "%.6f", ptr
->value
);
660 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
661 g_string_free(double_expected
, true);
662 g_string_free(double_actual
, true);
665 case PTYPE_BOOLEAN
: {
669 cur_head
= ptr
->next
;
671 cur_head
= ptr
= pl_copy
.value
.booleans
;
673 g_assert_cmpint(!!pt
->value
.boolean
, ==, !!ptr
->value
);
677 g_assert_not_reached();
682 g_assert_cmpint(i
, ==, 33);
684 ops
->cleanup(serialize_data
);
685 dealloc_helper(&pl
, visit_primitive_list
, &err
);
687 dealloc_helper(&pl_copy
, visit_primitive_list
, &err
);
692 static void test_struct(gconstpointer opaque
)
694 TestArgs
*args
= (TestArgs
*) opaque
;
695 const SerializeOps
*ops
= args
->ops
;
696 TestStruct
*ts
= struct_create();
697 TestStruct
*ts_copy
= NULL
;
699 void *serialize_data
;
701 ops
->serialize(ts
, &serialize_data
, visit_struct
, &err
);
702 ops
->deserialize((void **)&ts_copy
, serialize_data
, visit_struct
, &err
);
704 g_assert(err
== NULL
);
705 struct_compare(ts
, ts_copy
);
708 struct_cleanup(ts_copy
);
710 ops
->cleanup(serialize_data
);
714 static void test_nested_struct(gconstpointer opaque
)
716 TestArgs
*args
= (TestArgs
*) opaque
;
717 const SerializeOps
*ops
= args
->ops
;
718 UserDefNested
*udnp
= nested_struct_create();
719 UserDefNested
*udnp_copy
= NULL
;
721 void *serialize_data
;
723 ops
->serialize(udnp
, &serialize_data
, visit_nested_struct
, &err
);
724 ops
->deserialize((void **)&udnp_copy
, serialize_data
, visit_nested_struct
, &err
);
726 g_assert(err
== NULL
);
727 nested_struct_compare(udnp
, udnp_copy
);
729 nested_struct_cleanup(udnp
);
730 nested_struct_cleanup(udnp_copy
);
732 ops
->cleanup(serialize_data
);
736 static void test_nested_struct_list(gconstpointer opaque
)
738 TestArgs
*args
= (TestArgs
*) opaque
;
739 const SerializeOps
*ops
= args
->ops
;
740 UserDefNestedList
*listp
= NULL
, *tmp
, *tmp_copy
, *listp_copy
= NULL
;
742 void *serialize_data
;
745 for (i
= 0; i
< 8; i
++) {
746 tmp
= g_malloc0(sizeof(UserDefNestedList
));
747 tmp
->value
= nested_struct_create();
752 ops
->serialize(listp
, &serialize_data
, visit_nested_struct_list
, &err
);
753 ops
->deserialize((void **)&listp_copy
, serialize_data
,
754 visit_nested_struct_list
, &err
);
756 g_assert(err
== NULL
);
759 tmp_copy
= listp_copy
;
762 nested_struct_compare(listp
->value
, listp_copy
->value
);
764 listp_copy
= listp_copy
->next
;
767 qapi_free_UserDefNestedList(tmp
);
768 qapi_free_UserDefNestedList(tmp_copy
);
770 ops
->cleanup(serialize_data
);
774 static PrimitiveType pt_values
[] = {
777 .description
= "string_empty",
778 .type
= PTYPE_STRING
,
782 .description
= "string_whitespace",
783 .type
= PTYPE_STRING
,
784 .value
.string
= "a b c\td",
787 .description
= "string_newlines",
788 .type
= PTYPE_STRING
,
789 .value
.string
= "a\nb\n",
792 .description
= "string_commas",
793 .type
= PTYPE_STRING
,
794 .value
.string
= "a,b, c,d",
797 .description
= "string_single_quoted",
798 .type
= PTYPE_STRING
,
799 .value
.string
= "'a b',cd",
802 .description
= "string_double_quoted",
803 .type
= PTYPE_STRING
,
804 .value
.string
= "\"a b\",cd",
808 .description
= "boolean_true1",
809 .type
= PTYPE_BOOLEAN
,
810 .value
.boolean
= true,
813 .description
= "boolean_true2",
814 .type
= PTYPE_BOOLEAN
,
818 .description
= "boolean_true3",
819 .type
= PTYPE_BOOLEAN
,
823 .description
= "boolean_false1",
824 .type
= PTYPE_BOOLEAN
,
825 .value
.boolean
= false,
828 .description
= "boolean_false2",
829 .type
= PTYPE_BOOLEAN
,
832 /* number tests (double) */
833 /* note: we format these to %.6f before comparing, since that's how
834 * we serialize them and it doesn't make sense to check precision
838 .description
= "number_sanity1",
839 .type
= PTYPE_NUMBER
,
843 .description
= "number_sanity2",
844 .type
= PTYPE_NUMBER
,
845 .value
.number
= 3.14159265,
848 .description
= "number_min",
849 .type
= PTYPE_NUMBER
,
850 .value
.number
= DBL_MIN
,
853 .description
= "number_max",
854 .type
= PTYPE_NUMBER
,
855 .value
.number
= DBL_MAX
,
857 /* integer tests (int64) */
859 .description
= "integer_sanity1",
860 .type
= PTYPE_INTEGER
,
864 .description
= "integer_sanity2",
865 .type
= PTYPE_INTEGER
,
866 .value
.integer
= INT64_MAX
/ 2 + 1,
869 .description
= "integer_min",
870 .type
= PTYPE_INTEGER
,
871 .value
.integer
= INT64_MIN
,
874 .description
= "integer_max",
875 .type
= PTYPE_INTEGER
,
876 .value
.integer
= INT64_MAX
,
880 .description
= "uint8_sanity1",
885 .description
= "uint8_sanity2",
887 .value
.u8
= UINT8_MAX
/ 2 + 1,
890 .description
= "uint8_min",
895 .description
= "uint8_max",
897 .value
.u8
= UINT8_MAX
,
901 .description
= "uint16_sanity1",
906 .description
= "uint16_sanity2",
908 .value
.u16
= UINT16_MAX
/ 2 + 1,
911 .description
= "uint16_min",
916 .description
= "uint16_max",
918 .value
.u16
= UINT16_MAX
,
922 .description
= "uint32_sanity1",
927 .description
= "uint32_sanity2",
929 .value
.u32
= UINT32_MAX
/ 2 + 1,
932 .description
= "uint32_min",
937 .description
= "uint32_max",
939 .value
.u32
= UINT32_MAX
,
943 .description
= "uint64_sanity1",
948 .description
= "uint64_sanity2",
950 .value
.u64
= UINT64_MAX
/ 2 + 1,
953 .description
= "uint64_min",
958 .description
= "uint64_max",
960 .value
.u64
= UINT64_MAX
,
964 .description
= "int8_sanity1",
969 .description
= "int8_sanity2",
971 .value
.s8
= INT8_MAX
/ 2 + 1,
974 .description
= "int8_min",
976 .value
.s8
= INT8_MIN
,
979 .description
= "int8_max",
981 .value
.s8
= INT8_MAX
,
985 .description
= "int16_sanity1",
990 .description
= "int16_sanity2",
992 .value
.s16
= INT16_MAX
/ 2 + 1,
995 .description
= "int16_min",
997 .value
.s16
= INT16_MIN
,
1000 .description
= "int16_max",
1002 .value
.s16
= INT16_MAX
,
1006 .description
= "int32_sanity1",
1011 .description
= "int32_sanity2",
1013 .value
.s32
= INT32_MAX
/ 2 + 1,
1016 .description
= "int32_min",
1018 .value
.s32
= INT32_MIN
,
1021 .description
= "int32_max",
1023 .value
.s32
= INT32_MAX
,
1027 .description
= "int64_sanity1",
1032 .description
= "int64_sanity2",
1034 .value
.s64
= INT64_MAX
/ 2 + 1,
1037 .description
= "int64_min",
1039 .value
.s64
= INT64_MIN
,
1042 .description
= "int64_max",
1044 .value
.s64
= INT64_MAX
,
1046 { .type
= PTYPE_EOL
}
1049 /* visitor-specific op implementations */
1051 typedef struct QmpSerializeData
{
1052 QmpOutputVisitor
*qov
;
1053 QmpInputVisitor
*qiv
;
1056 static void qmp_serialize(void *native_in
, void **datap
,
1057 VisitorFunc visit
, Error
**errp
)
1059 QmpSerializeData
*d
= g_malloc0(sizeof(*d
));
1061 d
->qov
= qmp_output_visitor_new();
1062 visit(qmp_output_get_visitor(d
->qov
), &native_in
, errp
);
1066 static void qmp_deserialize(void **native_out
, void *datap
,
1067 VisitorFunc visit
, Error
**errp
)
1069 QmpSerializeData
*d
= datap
;
1070 QString
*output_json
;
1071 QObject
*obj_orig
, *obj
;
1073 obj_orig
= qmp_output_get_qobject(d
->qov
);
1074 output_json
= qobject_to_json(obj_orig
);
1075 obj
= qobject_from_json(qstring_get_str(output_json
));
1077 QDECREF(output_json
);
1078 d
->qiv
= qmp_input_visitor_new(obj
);
1079 qobject_decref(obj_orig
);
1080 qobject_decref(obj
);
1081 visit(qmp_input_get_visitor(d
->qiv
), native_out
, errp
);
1084 static void qmp_cleanup(void *datap
)
1086 QmpSerializeData
*d
= datap
;
1087 qmp_output_visitor_cleanup(d
->qov
);
1088 qmp_input_visitor_cleanup(d
->qiv
);
1093 typedef struct StringSerializeData
{
1095 StringOutputVisitor
*sov
;
1096 StringInputVisitor
*siv
;
1097 } StringSerializeData
;
1099 static void string_serialize(void *native_in
, void **datap
,
1100 VisitorFunc visit
, Error
**errp
)
1102 StringSerializeData
*d
= g_malloc0(sizeof(*d
));
1104 d
->sov
= string_output_visitor_new(false);
1105 visit(string_output_get_visitor(d
->sov
), &native_in
, errp
);
1109 static void string_deserialize(void **native_out
, void *datap
,
1110 VisitorFunc visit
, Error
**errp
)
1112 StringSerializeData
*d
= datap
;
1114 d
->string
= string_output_get_string(d
->sov
);
1115 d
->siv
= string_input_visitor_new(d
->string
);
1116 visit(string_input_get_visitor(d
->siv
), native_out
, errp
);
1119 static void string_cleanup(void *datap
)
1121 StringSerializeData
*d
= datap
;
1123 string_output_visitor_cleanup(d
->sov
);
1124 string_input_visitor_cleanup(d
->siv
);
1129 /* visitor registration, test harness */
1131 /* note: to function interchangeably as a serialization mechanism your
1132 * visitor test implementation should pass the test cases for all visitor
1133 * capabilities: primitives, structures, and lists
1135 static const SerializeOps visitors
[] = {
1138 .serialize
= qmp_serialize
,
1139 .deserialize
= qmp_deserialize
,
1140 .cleanup
= qmp_cleanup
,
1141 .caps
= VCAP_PRIMITIVES
| VCAP_STRUCTURES
| VCAP_LISTS
|
1142 VCAP_PRIMITIVE_LISTS
1146 .serialize
= string_serialize
,
1147 .deserialize
= string_deserialize
,
1148 .cleanup
= string_cleanup
,
1149 .caps
= VCAP_PRIMITIVES
1154 static void add_visitor_type(const SerializeOps
*ops
)
1156 char testname_prefix
[128];
1161 sprintf(testname_prefix
, "/visitor/serialization/%s", ops
->type
);
1163 if (ops
->caps
& VCAP_PRIMITIVES
) {
1164 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1165 sprintf(testname
, "%s/primitives/%s", testname_prefix
,
1166 pt_values
[i
].description
);
1167 args
= g_malloc0(sizeof(*args
));
1169 args
->test_data
= &pt_values
[i
];
1170 g_test_add_data_func(testname
, args
, test_primitives
);
1175 if (ops
->caps
& VCAP_STRUCTURES
) {
1176 sprintf(testname
, "%s/struct", testname_prefix
);
1177 args
= g_malloc0(sizeof(*args
));
1179 args
->test_data
= NULL
;
1180 g_test_add_data_func(testname
, args
, test_struct
);
1182 sprintf(testname
, "%s/nested_struct", testname_prefix
);
1183 args
= g_malloc0(sizeof(*args
));
1185 args
->test_data
= NULL
;
1186 g_test_add_data_func(testname
, args
, test_nested_struct
);
1189 if (ops
->caps
& VCAP_LISTS
) {
1190 sprintf(testname
, "%s/nested_struct_list", testname_prefix
);
1191 args
= g_malloc0(sizeof(*args
));
1193 args
->test_data
= NULL
;
1194 g_test_add_data_func(testname
, args
, test_nested_struct_list
);
1197 if (ops
->caps
& VCAP_PRIMITIVE_LISTS
) {
1199 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1200 sprintf(testname
, "%s/primitive_list/%s", testname_prefix
,
1201 pt_values
[i
].description
);
1202 args
= g_malloc0(sizeof(*args
));
1204 args
->test_data
= &pt_values
[i
];
1205 g_test_add_data_func(testname
, args
, test_primitive_lists
);
1211 int main(int argc
, char **argv
)
1215 g_test_init(&argc
, &argv
, NULL
);
1217 while (visitors
[i
].type
!= NULL
) {
1218 add_visitor_type(&visitors
[i
]);