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 "qemu-common.h"
18 #include "test-qapi-visit.h"
19 #include "qapi/error.h"
20 #include "qapi/qmp/qjson.h"
21 #include "qapi/qmp/qstring.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qobject-output-visitor.h"
24 #include "qapi/string-input-visitor.h"
25 #include "qapi/string-output-visitor.h"
26 #include "qapi/dealloc-visitor.h"
28 enum PrimitiveTypeKind
{
44 typedef struct PrimitiveType
{
60 enum PrimitiveTypeKind type
;
61 const char *description
;
64 typedef struct PrimitiveList
{
70 int8List
*s8_integers
;
71 int16List
*s16_integers
;
72 int32List
*s32_integers
;
73 int64List
*s64_integers
;
74 uint8List
*u8_integers
;
75 uint16List
*u16_integers
;
76 uint32List
*u32_integers
;
77 uint64List
*u64_integers
;
79 enum PrimitiveTypeKind type
;
80 const char *description
;
85 typedef void (*VisitorFunc
)(Visitor
*v
, void **native
, Error
**errp
);
87 static void dealloc_helper(void *native_in
, VisitorFunc visit
, Error
**errp
)
89 Visitor
*v
= qapi_dealloc_visitor_new();
91 visit(v
, &native_in
, errp
);
96 static void visit_primitive_type(Visitor
*v
, void **native
, Error
**errp
)
98 PrimitiveType
*pt
= *native
;
101 visit_type_str(v
, NULL
, (char **)&pt
->value
.string
, errp
);
104 visit_type_bool(v
, NULL
, &pt
->value
.boolean
, errp
);
107 visit_type_number(v
, NULL
, &pt
->value
.number
, errp
);
110 visit_type_int(v
, NULL
, &pt
->value
.integer
, errp
);
113 visit_type_uint8(v
, NULL
, &pt
->value
.u8
, errp
);
116 visit_type_uint16(v
, NULL
, &pt
->value
.u16
, errp
);
119 visit_type_uint32(v
, NULL
, &pt
->value
.u32
, errp
);
122 visit_type_uint64(v
, NULL
, &pt
->value
.u64
, errp
);
125 visit_type_int8(v
, NULL
, &pt
->value
.s8
, errp
);
128 visit_type_int16(v
, NULL
, &pt
->value
.s16
, errp
);
131 visit_type_int32(v
, NULL
, &pt
->value
.s32
, errp
);
134 visit_type_int64(v
, NULL
, &pt
->value
.s64
, errp
);
137 g_assert_not_reached();
141 static void visit_primitive_list(Visitor
*v
, void **native
, Error
**errp
)
143 PrimitiveList
*pl
= *native
;
146 visit_type_strList(v
, NULL
, &pl
->value
.strings
, errp
);
149 visit_type_boolList(v
, NULL
, &pl
->value
.booleans
, errp
);
152 visit_type_numberList(v
, NULL
, &pl
->value
.numbers
, errp
);
155 visit_type_intList(v
, NULL
, &pl
->value
.integers
, errp
);
158 visit_type_int8List(v
, NULL
, &pl
->value
.s8_integers
, errp
);
161 visit_type_int16List(v
, NULL
, &pl
->value
.s16_integers
, errp
);
164 visit_type_int32List(v
, NULL
, &pl
->value
.s32_integers
, errp
);
167 visit_type_int64List(v
, NULL
, &pl
->value
.s64_integers
, errp
);
170 visit_type_uint8List(v
, NULL
, &pl
->value
.u8_integers
, errp
);
173 visit_type_uint16List(v
, NULL
, &pl
->value
.u16_integers
, errp
);
176 visit_type_uint32List(v
, NULL
, &pl
->value
.u32_integers
, errp
);
179 visit_type_uint64List(v
, NULL
, &pl
->value
.u64_integers
, errp
);
182 g_assert_not_reached();
187 static TestStruct
*struct_create(void)
189 TestStruct
*ts
= g_malloc0(sizeof(*ts
));
192 ts
->string
= strdup("test string");
196 static void struct_compare(TestStruct
*ts1
, TestStruct
*ts2
)
200 g_assert_cmpint(ts1
->integer
, ==, ts2
->integer
);
201 g_assert(ts1
->boolean
== ts2
->boolean
);
202 g_assert_cmpstr(ts1
->string
, ==, ts2
->string
);
205 static void struct_cleanup(TestStruct
*ts
)
211 static void visit_struct(Visitor
*v
, void **native
, Error
**errp
)
213 visit_type_TestStruct(v
, NULL
, (TestStruct
**)native
, errp
);
216 static UserDefTwo
*nested_struct_create(void)
218 UserDefTwo
*udnp
= g_malloc0(sizeof(*udnp
));
219 udnp
->string0
= strdup("test_string0");
220 udnp
->dict1
= g_malloc0(sizeof(*udnp
->dict1
));
221 udnp
->dict1
->string1
= strdup("test_string1");
222 udnp
->dict1
->dict2
= g_malloc0(sizeof(*udnp
->dict1
->dict2
));
223 udnp
->dict1
->dict2
->userdef
= g_new0(UserDefOne
, 1);
224 udnp
->dict1
->dict2
->userdef
->integer
= 42;
225 udnp
->dict1
->dict2
->userdef
->string
= strdup("test_string");
226 udnp
->dict1
->dict2
->string
= strdup("test_string2");
227 udnp
->dict1
->dict3
= g_malloc0(sizeof(*udnp
->dict1
->dict3
));
228 udnp
->dict1
->has_dict3
= true;
229 udnp
->dict1
->dict3
->userdef
= g_new0(UserDefOne
, 1);
230 udnp
->dict1
->dict3
->userdef
->integer
= 43;
231 udnp
->dict1
->dict3
->userdef
->string
= strdup("test_string");
232 udnp
->dict1
->dict3
->string
= strdup("test_string3");
236 static void nested_struct_compare(UserDefTwo
*udnp1
, UserDefTwo
*udnp2
)
240 g_assert_cmpstr(udnp1
->string0
, ==, udnp2
->string0
);
241 g_assert_cmpstr(udnp1
->dict1
->string1
, ==, udnp2
->dict1
->string1
);
242 g_assert_cmpint(udnp1
->dict1
->dict2
->userdef
->integer
, ==,
243 udnp2
->dict1
->dict2
->userdef
->integer
);
244 g_assert_cmpstr(udnp1
->dict1
->dict2
->userdef
->string
, ==,
245 udnp2
->dict1
->dict2
->userdef
->string
);
246 g_assert_cmpstr(udnp1
->dict1
->dict2
->string
, ==,
247 udnp2
->dict1
->dict2
->string
);
248 g_assert(udnp1
->dict1
->has_dict3
== udnp2
->dict1
->has_dict3
);
249 g_assert_cmpint(udnp1
->dict1
->dict3
->userdef
->integer
, ==,
250 udnp2
->dict1
->dict3
->userdef
->integer
);
251 g_assert_cmpstr(udnp1
->dict1
->dict3
->userdef
->string
, ==,
252 udnp2
->dict1
->dict3
->userdef
->string
);
253 g_assert_cmpstr(udnp1
->dict1
->dict3
->string
, ==,
254 udnp2
->dict1
->dict3
->string
);
257 static void nested_struct_cleanup(UserDefTwo
*udnp
)
259 qapi_free_UserDefTwo(udnp
);
262 static void visit_nested_struct(Visitor
*v
, void **native
, Error
**errp
)
264 visit_type_UserDefTwo(v
, NULL
, (UserDefTwo
**)native
, errp
);
267 static void visit_nested_struct_list(Visitor
*v
, void **native
, Error
**errp
)
269 visit_type_UserDefTwoList(v
, NULL
, (UserDefTwoList
**)native
, errp
);
274 typedef enum VisitorCapabilities
{
278 VCAP_PRIMITIVE_LISTS
= 8,
279 } VisitorCapabilities
;
281 typedef struct SerializeOps
{
282 void (*serialize
)(void *native_in
, void **datap
,
283 VisitorFunc visit
, Error
**errp
);
284 void (*deserialize
)(void **native_out
, void *datap
,
285 VisitorFunc visit
, Error
**errp
);
286 void (*cleanup
)(void *datap
);
288 VisitorCapabilities caps
;
291 typedef struct TestArgs
{
292 const SerializeOps
*ops
;
296 static void test_primitives(gconstpointer opaque
)
298 TestArgs
*args
= (TestArgs
*) opaque
;
299 const SerializeOps
*ops
= args
->ops
;
300 PrimitiveType
*pt
= args
->test_data
;
301 PrimitiveType
*pt_copy
= g_malloc0(sizeof(*pt_copy
));
302 void *serialize_data
;
304 pt_copy
->type
= pt
->type
;
305 ops
->serialize(pt
, &serialize_data
, visit_primitive_type
, &error_abort
);
306 ops
->deserialize((void **)&pt_copy
, serialize_data
, visit_primitive_type
,
309 g_assert(pt_copy
!= NULL
);
310 if (pt
->type
== PTYPE_STRING
) {
311 g_assert_cmpstr(pt
->value
.string
, ==, pt_copy
->value
.string
);
312 g_free((char *)pt_copy
->value
.string
);
313 } else if (pt
->type
== PTYPE_NUMBER
) {
314 GString
*double_expected
= g_string_new("");
315 GString
*double_actual
= g_string_new("");
316 /* we serialize with %f for our reference visitors, so rather than fuzzy
317 * floating math to test "equality", just compare the formatted values
319 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
320 g_string_printf(double_actual
, "%.6f", pt_copy
->value
.number
);
321 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
323 g_string_free(double_expected
, true);
324 g_string_free(double_actual
, true);
325 } else if (pt
->type
== PTYPE_BOOLEAN
) {
326 g_assert_cmpint(!!pt
->value
.max
, ==, !!pt
->value
.max
);
328 g_assert_cmpint(pt
->value
.max
, ==, pt_copy
->value
.max
);
331 ops
->cleanup(serialize_data
);
336 static void test_primitive_lists(gconstpointer opaque
)
338 TestArgs
*args
= (TestArgs
*) opaque
;
339 const SerializeOps
*ops
= args
->ops
;
340 PrimitiveType
*pt
= args
->test_data
;
341 PrimitiveList pl
= { .value
= { NULL
} };
342 PrimitiveList pl_copy
= { .value
= { NULL
} };
343 PrimitiveList
*pl_copy_ptr
= &pl_copy
;
344 void *serialize_data
;
345 void *cur_head
= NULL
;
348 pl
.type
= pl_copy
.type
= pt
->type
;
350 /* build up our list of primitive types */
351 for (i
= 0; i
< 32; i
++) {
354 QAPI_LIST_PREPEND(pl
.value
.strings
, g_strdup(pt
->value
.string
));
357 case PTYPE_INTEGER
: {
358 QAPI_LIST_PREPEND(pl
.value
.integers
, pt
->value
.integer
);
362 QAPI_LIST_PREPEND(pl
.value
.s8_integers
, pt
->value
.s8
);
366 QAPI_LIST_PREPEND(pl
.value
.s16_integers
, pt
->value
.s16
);
370 QAPI_LIST_PREPEND(pl
.value
.s32_integers
, pt
->value
.s32
);
374 QAPI_LIST_PREPEND(pl
.value
.s64_integers
, pt
->value
.s64
);
378 QAPI_LIST_PREPEND(pl
.value
.u8_integers
, pt
->value
.u8
);
382 QAPI_LIST_PREPEND(pl
.value
.u16_integers
, pt
->value
.u16
);
386 QAPI_LIST_PREPEND(pl
.value
.u32_integers
, pt
->value
.u32
);
390 QAPI_LIST_PREPEND(pl
.value
.u64_integers
, pt
->value
.u64
);
394 QAPI_LIST_PREPEND(pl
.value
.numbers
, pt
->value
.number
);
397 case PTYPE_BOOLEAN
: {
398 QAPI_LIST_PREPEND(pl
.value
.booleans
, pt
->value
.boolean
);
402 g_assert_not_reached();
406 ops
->serialize((void **)&pl
, &serialize_data
, visit_primitive_list
,
408 ops
->deserialize((void **)&pl_copy_ptr
, serialize_data
,
409 visit_primitive_list
, &error_abort
);
413 /* compare our deserialized list of primitives to the original */
415 switch (pl_copy
.type
) {
420 cur_head
= ptr
->next
;
422 cur_head
= ptr
= pl_copy
.value
.strings
;
424 g_assert_cmpstr(pt
->value
.string
, ==, ptr
->value
);
427 case PTYPE_INTEGER
: {
431 cur_head
= ptr
->next
;
433 cur_head
= ptr
= pl_copy
.value
.integers
;
435 g_assert_cmpint(pt
->value
.integer
, ==, ptr
->value
);
442 cur_head
= ptr
->next
;
444 cur_head
= ptr
= pl_copy
.value
.s8_integers
;
446 g_assert_cmpint(pt
->value
.s8
, ==, ptr
->value
);
453 cur_head
= ptr
->next
;
455 cur_head
= ptr
= pl_copy
.value
.s16_integers
;
457 g_assert_cmpint(pt
->value
.s16
, ==, ptr
->value
);
464 cur_head
= ptr
->next
;
466 cur_head
= ptr
= pl_copy
.value
.s32_integers
;
468 g_assert_cmpint(pt
->value
.s32
, ==, ptr
->value
);
475 cur_head
= ptr
->next
;
477 cur_head
= ptr
= pl_copy
.value
.s64_integers
;
479 g_assert_cmpint(pt
->value
.s64
, ==, ptr
->value
);
486 cur_head
= ptr
->next
;
488 cur_head
= ptr
= pl_copy
.value
.u8_integers
;
490 g_assert_cmpint(pt
->value
.u8
, ==, ptr
->value
);
497 cur_head
= ptr
->next
;
499 cur_head
= ptr
= pl_copy
.value
.u16_integers
;
501 g_assert_cmpint(pt
->value
.u16
, ==, ptr
->value
);
508 cur_head
= ptr
->next
;
510 cur_head
= ptr
= pl_copy
.value
.u32_integers
;
512 g_assert_cmpint(pt
->value
.u32
, ==, ptr
->value
);
519 cur_head
= ptr
->next
;
521 cur_head
= ptr
= pl_copy
.value
.u64_integers
;
523 g_assert_cmpint(pt
->value
.u64
, ==, ptr
->value
);
528 GString
*double_expected
= g_string_new("");
529 GString
*double_actual
= g_string_new("");
532 cur_head
= ptr
->next
;
534 cur_head
= ptr
= pl_copy
.value
.numbers
;
536 /* we serialize with %f for our reference visitors, so rather than
537 * fuzzy floating math to test "equality", just compare the
540 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
541 g_string_printf(double_actual
, "%.6f", ptr
->value
);
542 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
543 g_string_free(double_expected
, true);
544 g_string_free(double_actual
, true);
547 case PTYPE_BOOLEAN
: {
551 cur_head
= ptr
->next
;
553 cur_head
= ptr
= pl_copy
.value
.booleans
;
555 g_assert_cmpint(!!pt
->value
.boolean
, ==, !!ptr
->value
);
559 g_assert_not_reached();
564 g_assert_cmpint(i
, ==, 33);
566 ops
->cleanup(serialize_data
);
567 dealloc_helper(&pl
, visit_primitive_list
, &error_abort
);
568 dealloc_helper(&pl_copy
, visit_primitive_list
, &error_abort
);
572 static void test_struct(gconstpointer opaque
)
574 TestArgs
*args
= (TestArgs
*) opaque
;
575 const SerializeOps
*ops
= args
->ops
;
576 TestStruct
*ts
= struct_create();
577 TestStruct
*ts_copy
= NULL
;
578 void *serialize_data
;
580 ops
->serialize(ts
, &serialize_data
, visit_struct
, &error_abort
);
581 ops
->deserialize((void **)&ts_copy
, serialize_data
, visit_struct
,
584 struct_compare(ts
, ts_copy
);
587 struct_cleanup(ts_copy
);
589 ops
->cleanup(serialize_data
);
593 static void test_nested_struct(gconstpointer opaque
)
595 TestArgs
*args
= (TestArgs
*) opaque
;
596 const SerializeOps
*ops
= args
->ops
;
597 UserDefTwo
*udnp
= nested_struct_create();
598 UserDefTwo
*udnp_copy
= NULL
;
599 void *serialize_data
;
601 ops
->serialize(udnp
, &serialize_data
, visit_nested_struct
, &error_abort
);
602 ops
->deserialize((void **)&udnp_copy
, serialize_data
, visit_nested_struct
,
605 nested_struct_compare(udnp
, udnp_copy
);
607 nested_struct_cleanup(udnp
);
608 nested_struct_cleanup(udnp_copy
);
610 ops
->cleanup(serialize_data
);
614 static void test_nested_struct_list(gconstpointer opaque
)
616 TestArgs
*args
= (TestArgs
*) opaque
;
617 const SerializeOps
*ops
= args
->ops
;
618 UserDefTwoList
*listp
= NULL
, *tmp
, *tmp_copy
, *listp_copy
= NULL
;
619 void *serialize_data
;
622 for (i
= 0; i
< 8; i
++) {
623 QAPI_LIST_PREPEND(listp
, nested_struct_create());
626 ops
->serialize(listp
, &serialize_data
, visit_nested_struct_list
,
628 ops
->deserialize((void **)&listp_copy
, serialize_data
,
629 visit_nested_struct_list
, &error_abort
);
632 tmp_copy
= listp_copy
;
635 nested_struct_compare(listp
->value
, listp_copy
->value
);
637 listp_copy
= listp_copy
->next
;
640 qapi_free_UserDefTwoList(tmp
);
641 qapi_free_UserDefTwoList(tmp_copy
);
643 ops
->cleanup(serialize_data
);
647 static PrimitiveType pt_values
[] = {
650 .description
= "string_empty",
651 .type
= PTYPE_STRING
,
655 .description
= "string_whitespace",
656 .type
= PTYPE_STRING
,
657 .value
.string
= "a b c\td",
660 .description
= "string_newlines",
661 .type
= PTYPE_STRING
,
662 .value
.string
= "a\nb\n",
665 .description
= "string_commas",
666 .type
= PTYPE_STRING
,
667 .value
.string
= "a,b, c,d",
670 .description
= "string_single_quoted",
671 .type
= PTYPE_STRING
,
672 .value
.string
= "'a b',cd",
675 .description
= "string_double_quoted",
676 .type
= PTYPE_STRING
,
677 .value
.string
= "\"a b\",cd",
681 .description
= "boolean_true1",
682 .type
= PTYPE_BOOLEAN
,
683 .value
.boolean
= true,
686 .description
= "boolean_true2",
687 .type
= PTYPE_BOOLEAN
,
691 .description
= "boolean_true3",
692 .type
= PTYPE_BOOLEAN
,
696 .description
= "boolean_false1",
697 .type
= PTYPE_BOOLEAN
,
698 .value
.boolean
= false,
701 .description
= "boolean_false2",
702 .type
= PTYPE_BOOLEAN
,
705 /* number tests (double) */
706 /* note: we format these to %.6f before comparing, since that's how
707 * we serialize them and it doesn't make sense to check precision
711 .description
= "number_sanity1",
712 .type
= PTYPE_NUMBER
,
716 .description
= "number_sanity2",
717 .type
= PTYPE_NUMBER
,
718 .value
.number
= 3.14159265,
721 .description
= "number_min",
722 .type
= PTYPE_NUMBER
,
723 .value
.number
= DBL_MIN
,
726 .description
= "number_max",
727 .type
= PTYPE_NUMBER
,
728 .value
.number
= DBL_MAX
,
730 /* integer tests (int64) */
732 .description
= "integer_sanity1",
733 .type
= PTYPE_INTEGER
,
737 .description
= "integer_sanity2",
738 .type
= PTYPE_INTEGER
,
739 .value
.integer
= INT64_MAX
/ 2 + 1,
742 .description
= "integer_min",
743 .type
= PTYPE_INTEGER
,
744 .value
.integer
= INT64_MIN
,
747 .description
= "integer_max",
748 .type
= PTYPE_INTEGER
,
749 .value
.integer
= INT64_MAX
,
753 .description
= "uint8_sanity1",
758 .description
= "uint8_sanity2",
760 .value
.u8
= UINT8_MAX
/ 2 + 1,
763 .description
= "uint8_min",
768 .description
= "uint8_max",
770 .value
.u8
= UINT8_MAX
,
774 .description
= "uint16_sanity1",
779 .description
= "uint16_sanity2",
781 .value
.u16
= UINT16_MAX
/ 2 + 1,
784 .description
= "uint16_min",
789 .description
= "uint16_max",
791 .value
.u16
= UINT16_MAX
,
795 .description
= "uint32_sanity1",
800 .description
= "uint32_sanity2",
802 .value
.u32
= UINT32_MAX
/ 2 + 1,
805 .description
= "uint32_min",
810 .description
= "uint32_max",
812 .value
.u32
= UINT32_MAX
,
816 .description
= "uint64_sanity1",
821 .description
= "uint64_sanity2",
823 .value
.u64
= UINT64_MAX
/ 2 + 1,
826 .description
= "uint64_min",
831 .description
= "uint64_max",
833 .value
.u64
= UINT64_MAX
,
837 .description
= "int8_sanity1",
842 .description
= "int8_sanity2",
844 .value
.s8
= INT8_MAX
/ 2 + 1,
847 .description
= "int8_min",
849 .value
.s8
= INT8_MIN
,
852 .description
= "int8_max",
854 .value
.s8
= INT8_MAX
,
858 .description
= "int16_sanity1",
863 .description
= "int16_sanity2",
865 .value
.s16
= INT16_MAX
/ 2 + 1,
868 .description
= "int16_min",
870 .value
.s16
= INT16_MIN
,
873 .description
= "int16_max",
875 .value
.s16
= INT16_MAX
,
879 .description
= "int32_sanity1",
884 .description
= "int32_sanity2",
886 .value
.s32
= INT32_MAX
/ 2 + 1,
889 .description
= "int32_min",
891 .value
.s32
= INT32_MIN
,
894 .description
= "int32_max",
896 .value
.s32
= INT32_MAX
,
900 .description
= "int64_sanity1",
905 .description
= "int64_sanity2",
907 .value
.s64
= INT64_MAX
/ 2 + 1,
910 .description
= "int64_min",
912 .value
.s64
= INT64_MIN
,
915 .description
= "int64_max",
917 .value
.s64
= INT64_MAX
,
919 { .type
= PTYPE_EOL
}
922 /* visitor-specific op implementations */
924 typedef struct QmpSerializeData
{
930 static void qmp_serialize(void *native_in
, void **datap
,
931 VisitorFunc visit
, Error
**errp
)
933 QmpSerializeData
*d
= g_malloc0(sizeof(*d
));
935 d
->qov
= qobject_output_visitor_new(&d
->obj
);
936 visit(d
->qov
, &native_in
, errp
);
940 static void qmp_deserialize(void **native_out
, void *datap
,
941 VisitorFunc visit
, Error
**errp
)
943 QmpSerializeData
*d
= datap
;
944 QString
*output_json
;
945 QObject
*obj_orig
, *obj
;
947 visit_complete(d
->qov
, &d
->obj
);
949 output_json
= qobject_to_json(obj_orig
);
950 obj
= qobject_from_json(qstring_get_str(output_json
), &error_abort
);
952 qobject_unref(output_json
);
953 d
->qiv
= qobject_input_visitor_new(obj
);
954 qobject_unref(obj_orig
);
956 visit(d
->qiv
, native_out
, errp
);
959 static void qmp_cleanup(void *datap
)
961 QmpSerializeData
*d
= datap
;
968 typedef struct StringSerializeData
{
972 } StringSerializeData
;
974 static void string_serialize(void *native_in
, void **datap
,
975 VisitorFunc visit
, Error
**errp
)
977 StringSerializeData
*d
= g_malloc0(sizeof(*d
));
979 d
->sov
= string_output_visitor_new(false, &d
->string
);
980 visit(d
->sov
, &native_in
, errp
);
984 static void string_deserialize(void **native_out
, void *datap
,
985 VisitorFunc visit
, Error
**errp
)
987 StringSerializeData
*d
= datap
;
989 visit_complete(d
->sov
, &d
->string
);
990 d
->siv
= string_input_visitor_new(d
->string
);
991 visit(d
->siv
, native_out
, errp
);
994 static void string_cleanup(void *datap
)
996 StringSerializeData
*d
= datap
;
1004 /* visitor registration, test harness */
1006 /* note: to function interchangeably as a serialization mechanism your
1007 * visitor test implementation should pass the test cases for all visitor
1008 * capabilities: primitives, structures, and lists
1010 static const SerializeOps visitors
[] = {
1013 .serialize
= qmp_serialize
,
1014 .deserialize
= qmp_deserialize
,
1015 .cleanup
= qmp_cleanup
,
1016 .caps
= VCAP_PRIMITIVES
| VCAP_STRUCTURES
| VCAP_LISTS
|
1017 VCAP_PRIMITIVE_LISTS
1021 .serialize
= string_serialize
,
1022 .deserialize
= string_deserialize
,
1023 .cleanup
= string_cleanup
,
1024 .caps
= VCAP_PRIMITIVES
1029 static void add_visitor_type(const SerializeOps
*ops
)
1031 char testname_prefix
[32];
1036 sprintf(testname_prefix
, "/visitor/serialization/%s", ops
->type
);
1038 if (ops
->caps
& VCAP_PRIMITIVES
) {
1039 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1040 sprintf(testname
, "%s/primitives/%s", testname_prefix
,
1041 pt_values
[i
].description
);
1042 args
= g_malloc0(sizeof(*args
));
1044 args
->test_data
= &pt_values
[i
];
1045 g_test_add_data_func(testname
, args
, test_primitives
);
1050 if (ops
->caps
& VCAP_STRUCTURES
) {
1051 sprintf(testname
, "%s/struct", testname_prefix
);
1052 args
= g_malloc0(sizeof(*args
));
1054 args
->test_data
= NULL
;
1055 g_test_add_data_func(testname
, args
, test_struct
);
1057 sprintf(testname
, "%s/nested_struct", testname_prefix
);
1058 args
= g_malloc0(sizeof(*args
));
1060 args
->test_data
= NULL
;
1061 g_test_add_data_func(testname
, args
, test_nested_struct
);
1064 if (ops
->caps
& VCAP_LISTS
) {
1065 sprintf(testname
, "%s/nested_struct_list", testname_prefix
);
1066 args
= g_malloc0(sizeof(*args
));
1068 args
->test_data
= NULL
;
1069 g_test_add_data_func(testname
, args
, test_nested_struct_list
);
1072 if (ops
->caps
& VCAP_PRIMITIVE_LISTS
) {
1074 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1075 sprintf(testname
, "%s/primitive_list/%s", testname_prefix
,
1076 pt_values
[i
].description
);
1077 args
= g_malloc0(sizeof(*args
));
1079 args
->test_data
= &pt_values
[i
];
1080 g_test_add_data_func(testname
, args
, test_primitive_lists
);
1086 int main(int argc
, char **argv
)
1090 g_test_init(&argc
, &argv
, NULL
);
1092 while (visitors
[i
].type
!= NULL
) {
1093 add_visitor_type(&visitors
[i
]);