2 * Unit-tests for visitor-based serialization
4 * Copyright (C) 2014-2015 Red Hat, Inc.
5 * Copyright IBM, Corp. 2012
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
17 #include "test-qapi-visit.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qapi/qobject-input-visitor.h"
22 #include "qapi/qobject-output-visitor.h"
23 #include "qapi/string-input-visitor.h"
24 #include "qapi/string-output-visitor.h"
25 #include "qapi/dealloc-visitor.h"
27 enum PrimitiveTypeKind
{
43 typedef struct PrimitiveType
{
58 enum PrimitiveTypeKind type
;
59 const char *description
;
62 typedef struct PrimitiveList
{
68 int8List
*s8_integers
;
69 int16List
*s16_integers
;
70 int32List
*s32_integers
;
71 int64List
*s64_integers
;
72 uint8List
*u8_integers
;
73 uint16List
*u16_integers
;
74 uint32List
*u32_integers
;
75 uint64List
*u64_integers
;
77 enum PrimitiveTypeKind type
;
78 const char *description
;
83 typedef void (*VisitorFunc
)(Visitor
*v
, void **native
, Error
**errp
);
85 static void dealloc_helper(void *native_in
, VisitorFunc visit
, Error
**errp
)
87 Visitor
*v
= qapi_dealloc_visitor_new();
89 visit(v
, &native_in
, errp
);
94 static void visit_primitive_type(Visitor
*v
, void **native
, Error
**errp
)
96 PrimitiveType
*pt
= *native
;
99 visit_type_str(v
, NULL
, (char **)&pt
->value
.string
, errp
);
102 visit_type_bool(v
, NULL
, &pt
->value
.boolean
, errp
);
105 visit_type_number(v
, NULL
, &pt
->value
.number
, errp
);
108 visit_type_int(v
, NULL
, &pt
->value
.integer
, errp
);
111 visit_type_uint8(v
, NULL
, &pt
->value
.u8
, errp
);
114 visit_type_uint16(v
, NULL
, &pt
->value
.u16
, errp
);
117 visit_type_uint32(v
, NULL
, &pt
->value
.u32
, errp
);
120 visit_type_uint64(v
, NULL
, &pt
->value
.u64
, errp
);
123 visit_type_int8(v
, NULL
, &pt
->value
.s8
, errp
);
126 visit_type_int16(v
, NULL
, &pt
->value
.s16
, errp
);
129 visit_type_int32(v
, NULL
, &pt
->value
.s32
, errp
);
132 visit_type_int64(v
, NULL
, &pt
->value
.s64
, errp
);
135 g_assert_not_reached();
139 static void visit_primitive_list(Visitor
*v
, void **native
, Error
**errp
)
141 PrimitiveList
*pl
= *native
;
144 visit_type_strList(v
, NULL
, &pl
->value
.strings
, errp
);
147 visit_type_boolList(v
, NULL
, &pl
->value
.booleans
, errp
);
150 visit_type_numberList(v
, NULL
, &pl
->value
.numbers
, errp
);
153 visit_type_intList(v
, NULL
, &pl
->value
.integers
, errp
);
156 visit_type_int8List(v
, NULL
, &pl
->value
.s8_integers
, errp
);
159 visit_type_int16List(v
, NULL
, &pl
->value
.s16_integers
, errp
);
162 visit_type_int32List(v
, NULL
, &pl
->value
.s32_integers
, errp
);
165 visit_type_int64List(v
, NULL
, &pl
->value
.s64_integers
, errp
);
168 visit_type_uint8List(v
, NULL
, &pl
->value
.u8_integers
, errp
);
171 visit_type_uint16List(v
, NULL
, &pl
->value
.u16_integers
, errp
);
174 visit_type_uint32List(v
, NULL
, &pl
->value
.u32_integers
, errp
);
177 visit_type_uint64List(v
, NULL
, &pl
->value
.u64_integers
, errp
);
180 g_assert_not_reached();
185 static TestStruct
*struct_create(void)
187 TestStruct
*ts
= g_malloc0(sizeof(*ts
));
190 ts
->string
= strdup("test string");
194 static void struct_compare(TestStruct
*ts1
, TestStruct
*ts2
)
198 g_assert_cmpint(ts1
->integer
, ==, ts2
->integer
);
199 g_assert(ts1
->boolean
== ts2
->boolean
);
200 g_assert_cmpstr(ts1
->string
, ==, ts2
->string
);
203 static void struct_cleanup(TestStruct
*ts
)
209 static void visit_struct(Visitor
*v
, void **native
, Error
**errp
)
211 visit_type_TestStruct(v
, NULL
, (TestStruct
**)native
, errp
);
214 static UserDefTwo
*nested_struct_create(void)
216 UserDefTwo
*udnp
= g_malloc0(sizeof(*udnp
));
217 udnp
->string0
= strdup("test_string0");
218 udnp
->dict1
= g_malloc0(sizeof(*udnp
->dict1
));
219 udnp
->dict1
->string1
= strdup("test_string1");
220 udnp
->dict1
->dict2
= g_malloc0(sizeof(*udnp
->dict1
->dict2
));
221 udnp
->dict1
->dict2
->userdef
= g_new0(UserDefOne
, 1);
222 udnp
->dict1
->dict2
->userdef
->integer
= 42;
223 udnp
->dict1
->dict2
->userdef
->string
= strdup("test_string");
224 udnp
->dict1
->dict2
->string
= strdup("test_string2");
225 udnp
->dict1
->dict3
= g_malloc0(sizeof(*udnp
->dict1
->dict3
));
226 udnp
->dict1
->dict3
->userdef
= g_new0(UserDefOne
, 1);
227 udnp
->dict1
->dict3
->userdef
->integer
= 43;
228 udnp
->dict1
->dict3
->userdef
->string
= strdup("test_string");
229 udnp
->dict1
->dict3
->string
= strdup("test_string3");
233 static void nested_struct_compare(UserDefTwo
*udnp1
, UserDefTwo
*udnp2
)
237 g_assert_cmpstr(udnp1
->string0
, ==, udnp2
->string0
);
238 g_assert_cmpstr(udnp1
->dict1
->string1
, ==, udnp2
->dict1
->string1
);
239 g_assert_cmpint(udnp1
->dict1
->dict2
->userdef
->integer
, ==,
240 udnp2
->dict1
->dict2
->userdef
->integer
);
241 g_assert_cmpstr(udnp1
->dict1
->dict2
->userdef
->string
, ==,
242 udnp2
->dict1
->dict2
->userdef
->string
);
243 g_assert_cmpstr(udnp1
->dict1
->dict2
->string
, ==,
244 udnp2
->dict1
->dict2
->string
);
245 g_assert(!udnp1
->dict1
->dict3
== !udnp2
->dict1
->dict3
);
246 g_assert_cmpint(udnp1
->dict1
->dict3
->userdef
->integer
, ==,
247 udnp2
->dict1
->dict3
->userdef
->integer
);
248 g_assert_cmpstr(udnp1
->dict1
->dict3
->userdef
->string
, ==,
249 udnp2
->dict1
->dict3
->userdef
->string
);
250 g_assert_cmpstr(udnp1
->dict1
->dict3
->string
, ==,
251 udnp2
->dict1
->dict3
->string
);
254 static void nested_struct_cleanup(UserDefTwo
*udnp
)
256 qapi_free_UserDefTwo(udnp
);
259 static void visit_nested_struct(Visitor
*v
, void **native
, Error
**errp
)
261 visit_type_UserDefTwo(v
, NULL
, (UserDefTwo
**)native
, errp
);
264 static void visit_nested_struct_list(Visitor
*v
, void **native
, Error
**errp
)
266 visit_type_UserDefTwoList(v
, NULL
, (UserDefTwoList
**)native
, errp
);
271 typedef enum VisitorCapabilities
{
275 VCAP_PRIMITIVE_LISTS
= 8,
276 } VisitorCapabilities
;
278 typedef struct SerializeOps
{
279 void (*serialize
)(void *native_in
, void **datap
,
280 VisitorFunc visit
, Error
**errp
);
281 void (*deserialize
)(void **native_out
, void *datap
,
282 VisitorFunc visit
, Error
**errp
);
283 void (*cleanup
)(void *datap
);
285 VisitorCapabilities caps
;
288 typedef struct TestArgs
{
289 const SerializeOps
*ops
;
293 static void test_primitives(gconstpointer opaque
)
295 TestArgs
*args
= (TestArgs
*) opaque
;
296 const SerializeOps
*ops
= args
->ops
;
297 PrimitiveType
*pt
= args
->test_data
;
298 PrimitiveType
*pt_copy
= g_malloc0(sizeof(*pt_copy
));
299 void *serialize_data
;
301 pt_copy
->type
= pt
->type
;
302 ops
->serialize(pt
, &serialize_data
, visit_primitive_type
, &error_abort
);
303 ops
->deserialize((void **)&pt_copy
, serialize_data
, visit_primitive_type
,
306 g_assert(pt_copy
!= NULL
);
309 g_assert_cmpstr(pt
->value
.string
, ==, pt_copy
->value
.string
);
310 g_free((char *)pt_copy
->value
.string
);
313 g_assert_cmpint(pt
->value
.boolean
, ==, pt
->value
.boolean
);
316 g_assert_cmpfloat(pt
->value
.number
, ==, pt_copy
->value
.number
);
319 g_assert_cmpint(pt
->value
.integer
, ==, pt_copy
->value
.integer
);
322 g_assert_cmpuint(pt
->value
.u8
, ==, pt_copy
->value
.u8
);
325 g_assert_cmpuint(pt
->value
.u16
, ==, pt_copy
->value
.u16
);
328 g_assert_cmpuint(pt
->value
.u32
, ==, pt_copy
->value
.u32
);
331 g_assert_cmpuint(pt
->value
.u64
, ==, pt_copy
->value
.u64
);
334 g_assert_cmpint(pt
->value
.s8
, ==, pt_copy
->value
.s8
);
337 g_assert_cmpint(pt
->value
.s16
, ==, pt_copy
->value
.s16
);
340 g_assert_cmpint(pt
->value
.s32
, ==, pt_copy
->value
.s32
);
343 g_assert_cmpint(pt
->value
.s64
, ==, pt_copy
->value
.s64
);
346 g_assert_not_reached();
349 ops
->cleanup(serialize_data
);
354 static void test_primitive_lists(gconstpointer opaque
)
356 TestArgs
*args
= (TestArgs
*) opaque
;
357 const SerializeOps
*ops
= args
->ops
;
358 PrimitiveType
*pt
= args
->test_data
;
359 PrimitiveList pl
= { .value
= { NULL
} };
360 PrimitiveList pl_copy
= { .value
= { NULL
} };
361 PrimitiveList
*pl_copy_ptr
= &pl_copy
;
362 void *serialize_data
;
363 void *cur_head
= NULL
;
366 pl
.type
= pl_copy
.type
= pt
->type
;
368 /* build up our list of primitive types */
369 for (i
= 0; i
< 32; i
++) {
372 QAPI_LIST_PREPEND(pl
.value
.strings
, g_strdup(pt
->value
.string
));
375 case PTYPE_INTEGER
: {
376 QAPI_LIST_PREPEND(pl
.value
.integers
, pt
->value
.integer
);
380 QAPI_LIST_PREPEND(pl
.value
.s8_integers
, pt
->value
.s8
);
384 QAPI_LIST_PREPEND(pl
.value
.s16_integers
, pt
->value
.s16
);
388 QAPI_LIST_PREPEND(pl
.value
.s32_integers
, pt
->value
.s32
);
392 QAPI_LIST_PREPEND(pl
.value
.s64_integers
, pt
->value
.s64
);
396 QAPI_LIST_PREPEND(pl
.value
.u8_integers
, pt
->value
.u8
);
400 QAPI_LIST_PREPEND(pl
.value
.u16_integers
, pt
->value
.u16
);
404 QAPI_LIST_PREPEND(pl
.value
.u32_integers
, pt
->value
.u32
);
408 QAPI_LIST_PREPEND(pl
.value
.u64_integers
, pt
->value
.u64
);
412 QAPI_LIST_PREPEND(pl
.value
.numbers
, pt
->value
.number
);
415 case PTYPE_BOOLEAN
: {
416 QAPI_LIST_PREPEND(pl
.value
.booleans
, pt
->value
.boolean
);
420 g_assert_not_reached();
424 ops
->serialize((void **)&pl
, &serialize_data
, visit_primitive_list
,
426 ops
->deserialize((void **)&pl_copy_ptr
, serialize_data
,
427 visit_primitive_list
, &error_abort
);
430 switch (pl_copy
.type
) {
432 cur_head
= pl_copy
.value
.strings
;
435 cur_head
= pl_copy
.value
.integers
;
438 cur_head
= pl_copy
.value
.s8_integers
;
441 cur_head
= pl_copy
.value
.s16_integers
;
444 cur_head
= pl_copy
.value
.s32_integers
;
447 cur_head
= pl_copy
.value
.s64_integers
;
450 cur_head
= pl_copy
.value
.u8_integers
;
453 cur_head
= pl_copy
.value
.u16_integers
;
456 cur_head
= pl_copy
.value
.u32_integers
;
459 cur_head
= pl_copy
.value
.u64_integers
;
462 cur_head
= pl_copy
.value
.numbers
;
465 cur_head
= pl_copy
.value
.booleans
;
468 g_assert_not_reached();
471 /* compare our deserialized list of primitives to the original */
474 switch (pl_copy
.type
) {
476 strList
*ptr
= cur_head
;
477 cur_head
= ptr
->next
;
478 g_assert_cmpstr(pt
->value
.string
, ==, ptr
->value
);
481 case PTYPE_INTEGER
: {
482 intList
*ptr
= cur_head
;
483 cur_head
= ptr
->next
;
484 g_assert_cmpint(pt
->value
.integer
, ==, ptr
->value
);
488 int8List
*ptr
= cur_head
;
489 cur_head
= ptr
->next
;
490 g_assert_cmpint(pt
->value
.s8
, ==, ptr
->value
);
494 int16List
*ptr
= cur_head
;
495 cur_head
= ptr
->next
;
496 g_assert_cmpint(pt
->value
.s16
, ==, ptr
->value
);
500 int32List
*ptr
= cur_head
;
501 cur_head
= ptr
->next
;
502 g_assert_cmpint(pt
->value
.s32
, ==, ptr
->value
);
506 int64List
*ptr
= cur_head
;
507 cur_head
= ptr
->next
;
508 g_assert_cmpint(pt
->value
.s64
, ==, ptr
->value
);
512 uint8List
*ptr
= cur_head
;
513 cur_head
= ptr
->next
;
514 g_assert_cmpint(pt
->value
.u8
, ==, ptr
->value
);
518 uint16List
*ptr
= cur_head
;
519 cur_head
= ptr
->next
;
520 g_assert_cmpint(pt
->value
.u16
, ==, ptr
->value
);
524 uint32List
*ptr
= cur_head
;
525 cur_head
= ptr
->next
;
526 g_assert_cmpint(pt
->value
.u32
, ==, ptr
->value
);
530 uint64List
*ptr
= cur_head
;
531 cur_head
= ptr
->next
;
532 g_assert_cmpint(pt
->value
.u64
, ==, ptr
->value
);
536 GString
*double_expected
= g_string_new("");
537 GString
*double_actual
= g_string_new("");
538 numberList
*ptr
= cur_head
;
539 cur_head
= ptr
->next
;
540 /* we serialize with %f for our reference visitors, so rather than
541 * fuzzy floating math to test "equality", just compare the
544 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
545 g_string_printf(double_actual
, "%.6f", ptr
->value
);
546 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
547 g_string_free(double_expected
, true);
548 g_string_free(double_actual
, true);
551 case PTYPE_BOOLEAN
: {
552 boolList
*ptr
= cur_head
;
553 cur_head
= ptr
->next
;
554 g_assert_cmpint(!!pt
->value
.boolean
, ==, !!ptr
->value
);
558 g_assert_not_reached();
563 g_assert_cmpint(i
, ==, 32);
565 ops
->cleanup(serialize_data
);
566 dealloc_helper(&pl
, visit_primitive_list
, &error_abort
);
567 dealloc_helper(&pl_copy
, visit_primitive_list
, &error_abort
);
571 static void test_struct(gconstpointer opaque
)
573 TestArgs
*args
= (TestArgs
*) opaque
;
574 const SerializeOps
*ops
= args
->ops
;
575 TestStruct
*ts
= struct_create();
576 TestStruct
*ts_copy
= NULL
;
577 void *serialize_data
;
579 ops
->serialize(ts
, &serialize_data
, visit_struct
, &error_abort
);
580 ops
->deserialize((void **)&ts_copy
, serialize_data
, visit_struct
,
583 struct_compare(ts
, ts_copy
);
586 struct_cleanup(ts_copy
);
588 ops
->cleanup(serialize_data
);
592 static void test_nested_struct(gconstpointer opaque
)
594 TestArgs
*args
= (TestArgs
*) opaque
;
595 const SerializeOps
*ops
= args
->ops
;
596 UserDefTwo
*udnp
= nested_struct_create();
597 UserDefTwo
*udnp_copy
= NULL
;
598 void *serialize_data
;
600 ops
->serialize(udnp
, &serialize_data
, visit_nested_struct
, &error_abort
);
601 ops
->deserialize((void **)&udnp_copy
, serialize_data
, visit_nested_struct
,
604 nested_struct_compare(udnp
, udnp_copy
);
606 nested_struct_cleanup(udnp
);
607 nested_struct_cleanup(udnp_copy
);
609 ops
->cleanup(serialize_data
);
613 static void test_nested_struct_list(gconstpointer opaque
)
615 TestArgs
*args
= (TestArgs
*) opaque
;
616 const SerializeOps
*ops
= args
->ops
;
617 UserDefTwoList
*listp
= NULL
, *tmp
, *tmp_copy
, *listp_copy
= NULL
;
618 void *serialize_data
;
621 for (i
= 0; i
< 8; i
++) {
622 QAPI_LIST_PREPEND(listp
, nested_struct_create());
625 ops
->serialize(listp
, &serialize_data
, visit_nested_struct_list
,
627 ops
->deserialize((void **)&listp_copy
, serialize_data
,
628 visit_nested_struct_list
, &error_abort
);
631 tmp_copy
= listp_copy
;
634 nested_struct_compare(listp
->value
, listp_copy
->value
);
636 listp_copy
= listp_copy
->next
;
639 qapi_free_UserDefTwoList(tmp
);
640 qapi_free_UserDefTwoList(tmp_copy
);
642 ops
->cleanup(serialize_data
);
646 static PrimitiveType pt_values
[] = {
649 .description
= "string_empty",
650 .type
= PTYPE_STRING
,
654 .description
= "string_whitespace",
655 .type
= PTYPE_STRING
,
656 .value
.string
= "a b c\td",
659 .description
= "string_newlines",
660 .type
= PTYPE_STRING
,
661 .value
.string
= "a\nb\n",
664 .description
= "string_commas",
665 .type
= PTYPE_STRING
,
666 .value
.string
= "a,b, c,d",
669 .description
= "string_single_quoted",
670 .type
= PTYPE_STRING
,
671 .value
.string
= "'a b',cd",
674 .description
= "string_double_quoted",
675 .type
= PTYPE_STRING
,
676 .value
.string
= "\"a b\",cd",
680 .description
= "boolean_true1",
681 .type
= PTYPE_BOOLEAN
,
682 .value
.boolean
= true,
685 .description
= "boolean_true2",
686 .type
= PTYPE_BOOLEAN
,
690 .description
= "boolean_true3",
691 .type
= PTYPE_BOOLEAN
,
695 .description
= "boolean_false1",
696 .type
= PTYPE_BOOLEAN
,
697 .value
.boolean
= false,
700 .description
= "boolean_false2",
701 .type
= PTYPE_BOOLEAN
,
704 /* number tests (double) */
706 .description
= "number_sanity1",
707 .type
= PTYPE_NUMBER
,
711 .description
= "number_sanity2",
712 .type
= PTYPE_NUMBER
,
713 .value
.number
= 3.141593,
716 .description
= "number_min",
717 .type
= PTYPE_NUMBER
,
718 .value
.number
= DBL_MIN
,
721 .description
= "number_max",
722 .type
= PTYPE_NUMBER
,
723 .value
.number
= DBL_MAX
,
725 /* integer tests (int64) */
727 .description
= "integer_sanity1",
728 .type
= PTYPE_INTEGER
,
732 .description
= "integer_sanity2",
733 .type
= PTYPE_INTEGER
,
734 .value
.integer
= INT64_MAX
/ 2 + 1,
737 .description
= "integer_min",
738 .type
= PTYPE_INTEGER
,
739 .value
.integer
= INT64_MIN
,
742 .description
= "integer_max",
743 .type
= PTYPE_INTEGER
,
744 .value
.integer
= INT64_MAX
,
748 .description
= "uint8_sanity1",
753 .description
= "uint8_sanity2",
755 .value
.u8
= UINT8_MAX
/ 2 + 1,
758 .description
= "uint8_min",
763 .description
= "uint8_max",
765 .value
.u8
= UINT8_MAX
,
769 .description
= "uint16_sanity1",
774 .description
= "uint16_sanity2",
776 .value
.u16
= UINT16_MAX
/ 2 + 1,
779 .description
= "uint16_min",
784 .description
= "uint16_max",
786 .value
.u16
= UINT16_MAX
,
790 .description
= "uint32_sanity1",
795 .description
= "uint32_sanity2",
797 .value
.u32
= UINT32_MAX
/ 2 + 1,
800 .description
= "uint32_min",
805 .description
= "uint32_max",
807 .value
.u32
= UINT32_MAX
,
811 .description
= "uint64_sanity1",
816 .description
= "uint64_sanity2",
818 .value
.u64
= UINT64_MAX
/ 2 + 1,
821 .description
= "uint64_min",
826 .description
= "uint64_max",
828 .value
.u64
= UINT64_MAX
,
832 .description
= "int8_sanity1",
837 .description
= "int8_sanity2",
839 .value
.s8
= INT8_MAX
/ 2 + 1,
842 .description
= "int8_min",
844 .value
.s8
= INT8_MIN
,
847 .description
= "int8_max",
849 .value
.s8
= INT8_MAX
,
853 .description
= "int16_sanity1",
858 .description
= "int16_sanity2",
860 .value
.s16
= INT16_MAX
/ 2 + 1,
863 .description
= "int16_min",
865 .value
.s16
= INT16_MIN
,
868 .description
= "int16_max",
870 .value
.s16
= INT16_MAX
,
874 .description
= "int32_sanity1",
879 .description
= "int32_sanity2",
881 .value
.s32
= INT32_MAX
/ 2 + 1,
884 .description
= "int32_min",
886 .value
.s32
= INT32_MIN
,
889 .description
= "int32_max",
891 .value
.s32
= INT32_MAX
,
895 .description
= "int64_sanity1",
900 .description
= "int64_sanity2",
902 .value
.s64
= INT64_MAX
/ 2 + 1,
905 .description
= "int64_min",
907 .value
.s64
= INT64_MIN
,
910 .description
= "int64_max",
912 .value
.s64
= INT64_MAX
,
914 { .type
= PTYPE_EOL
}
917 /* visitor-specific op implementations */
919 typedef struct QmpSerializeData
{
925 static void qmp_serialize(void *native_in
, void **datap
,
926 VisitorFunc visit
, Error
**errp
)
928 QmpSerializeData
*d
= g_malloc0(sizeof(*d
));
930 d
->qov
= qobject_output_visitor_new(&d
->obj
);
931 visit(d
->qov
, &native_in
, errp
);
935 static void qmp_deserialize(void **native_out
, void *datap
,
936 VisitorFunc visit
, Error
**errp
)
938 QmpSerializeData
*d
= datap
;
939 GString
*output_json
;
940 QObject
*obj_orig
, *obj
;
942 visit_complete(d
->qov
, &d
->obj
);
944 output_json
= qobject_to_json(obj_orig
);
945 obj
= qobject_from_json(output_json
->str
, &error_abort
);
947 g_string_free(output_json
, true);
948 d
->qiv
= qobject_input_visitor_new(obj
);
949 qobject_unref(obj_orig
);
951 visit(d
->qiv
, native_out
, errp
);
954 static void qmp_cleanup(void *datap
)
956 QmpSerializeData
*d
= datap
;
963 typedef struct StringSerializeData
{
967 } StringSerializeData
;
969 static void string_serialize(void *native_in
, void **datap
,
970 VisitorFunc visit
, Error
**errp
)
972 StringSerializeData
*d
= g_malloc0(sizeof(*d
));
974 d
->sov
= string_output_visitor_new(false, &d
->string
);
975 visit(d
->sov
, &native_in
, errp
);
979 static void string_deserialize(void **native_out
, void *datap
,
980 VisitorFunc visit
, Error
**errp
)
982 StringSerializeData
*d
= datap
;
984 visit_complete(d
->sov
, &d
->string
);
985 d
->siv
= string_input_visitor_new(d
->string
);
986 visit(d
->siv
, native_out
, errp
);
989 static void string_cleanup(void *datap
)
991 StringSerializeData
*d
= datap
;
999 /* visitor registration, test harness */
1001 /* note: to function interchangeably as a serialization mechanism your
1002 * visitor test implementation should pass the test cases for all visitor
1003 * capabilities: primitives, structures, and lists
1005 static const SerializeOps visitors
[] = {
1008 .serialize
= qmp_serialize
,
1009 .deserialize
= qmp_deserialize
,
1010 .cleanup
= qmp_cleanup
,
1011 .caps
= VCAP_PRIMITIVES
| VCAP_STRUCTURES
| VCAP_LISTS
|
1012 VCAP_PRIMITIVE_LISTS
1016 .serialize
= string_serialize
,
1017 .deserialize
= string_deserialize
,
1018 .cleanup
= string_cleanup
,
1019 .caps
= VCAP_PRIMITIVES
1024 static void add_visitor_type(const SerializeOps
*ops
)
1026 char testname_prefix
[32];
1031 sprintf(testname_prefix
, "/visitor/serialization/%s", ops
->type
);
1033 if (ops
->caps
& VCAP_PRIMITIVES
) {
1034 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1035 sprintf(testname
, "%s/primitives/%s", testname_prefix
,
1036 pt_values
[i
].description
);
1037 args
= g_malloc0(sizeof(*args
));
1039 args
->test_data
= &pt_values
[i
];
1040 g_test_add_data_func(testname
, args
, test_primitives
);
1045 if (ops
->caps
& VCAP_STRUCTURES
) {
1046 sprintf(testname
, "%s/struct", testname_prefix
);
1047 args
= g_malloc0(sizeof(*args
));
1049 args
->test_data
= NULL
;
1050 g_test_add_data_func(testname
, args
, test_struct
);
1052 sprintf(testname
, "%s/nested_struct", testname_prefix
);
1053 args
= g_malloc0(sizeof(*args
));
1055 args
->test_data
= NULL
;
1056 g_test_add_data_func(testname
, args
, test_nested_struct
);
1059 if (ops
->caps
& VCAP_LISTS
) {
1060 sprintf(testname
, "%s/nested_struct_list", testname_prefix
);
1061 args
= g_malloc0(sizeof(*args
));
1063 args
->test_data
= NULL
;
1064 g_test_add_data_func(testname
, args
, test_nested_struct_list
);
1067 if (ops
->caps
& VCAP_PRIMITIVE_LISTS
) {
1069 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1070 sprintf(testname
, "%s/primitive_list/%s", testname_prefix
,
1071 pt_values
[i
].description
);
1072 args
= g_malloc0(sizeof(*args
));
1074 args
->test_data
= &pt_values
[i
];
1075 g_test_add_data_func(testname
, args
, test_primitive_lists
);
1081 int main(int argc
, char **argv
)
1085 g_test_init(&argc
, &argv
, NULL
);
1087 while (visitors
[i
].type
!= NULL
) {
1088 add_visitor_type(&visitors
[i
]);