2 * Unit-tests for visitor-based serialization
4 * Copyright IBM, Corp. 2012
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)
198 visit_start_struct(v, (void **)obj, NULL, name, sizeof(TestStruct), errp);
200 visit_type_int(v, &(*obj)->integer, "integer", errp);
201 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
202 visit_type_str(v, &(*obj)->string, "string", errp);
204 visit_end_struct(v, errp);
207 static TestStruct *struct_create(void)
209 TestStruct *ts = g_malloc0(sizeof(*ts));
212 ts->string = strdup("test string");
216 static void struct_compare(TestStruct *ts1, TestStruct *ts2)
220 g_assert_cmpint(ts1->integer, ==, ts2->integer);
221 g_assert(ts1->boolean == ts2->boolean);
222 g_assert_cmpstr(ts1->string, ==, ts2->string);
225 static void struct_cleanup(TestStruct *ts)
231 static void visit_struct(Visitor *v, void **native, Error **errp)
233 visit_type_TestStruct(v, (TestStruct **)native, NULL, errp);
236 static UserDefNested *nested_struct_create(void)
238 UserDefNested *udnp = g_malloc0(sizeof(*udnp));
239 udnp->string0 = strdup("test_string0");
240 udnp->dict1.string1 = strdup("test_string1");
241 udnp->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
242 udnp->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
243 udnp->dict1.dict2.userdef1->base->integer = 42;
244 udnp->dict1.dict2.userdef1->string = strdup("test_string");
245 udnp->dict1.dict2.string2 = strdup("test_string2");
246 udnp->dict1.has_dict3 = true;
247 udnp->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
248 udnp->dict1.dict3.userdef2->base = g_new0(UserDefZero, 1);
249 udnp->dict1.dict3.userdef2->base->integer = 43;
250 udnp->dict1.dict3.userdef2->string = strdup("test_string");
251 udnp->dict1.dict3.string3 = strdup("test_string3");
255 static void nested_struct_compare(UserDefNested *udnp1, UserDefNested *udnp2)
259 g_assert_cmpstr(udnp1->string0, ==, udnp2->string0);
260 g_assert_cmpstr(udnp1->dict1.string1, ==, udnp2->dict1.string1);
261 g_assert_cmpint(udnp1->dict1.dict2.userdef1->base->integer, ==,
262 udnp2->dict1.dict2.userdef1->base->integer);
263 g_assert_cmpstr(udnp1->dict1.dict2.userdef1->string, ==,
264 udnp2->dict1.dict2.userdef1->string);
265 g_assert_cmpstr(udnp1->dict1.dict2.string2, ==, udnp2->dict1.dict2.string2);
266 g_assert(udnp1->dict1.has_dict3 == udnp2->dict1.has_dict3);
267 g_assert_cmpint(udnp1->dict1.dict3.userdef2->base->integer, ==,
268 udnp2->dict1.dict3.userdef2->base->integer);
269 g_assert_cmpstr(udnp1->dict1.dict3.userdef2->string, ==,
270 udnp2->dict1.dict3.userdef2->string);
271 g_assert_cmpstr(udnp1->dict1.dict3.string3, ==, udnp2->dict1.dict3.string3);
274 static void nested_struct_cleanup(UserDefNested *udnp)
276 qapi_free_UserDefNested(udnp);
279 static void visit_nested_struct(Visitor *v, void **native, Error **errp)
281 visit_type_UserDefNested(v, (UserDefNested **)native, NULL, errp);
284 static void visit_nested_struct_list(Visitor *v, void **native, Error **errp)
286 visit_type_UserDefNestedList(v, (UserDefNestedList **)native, NULL, errp);
291 typedef enum VisitorCapabilities {
295 VCAP_PRIMITIVE_LISTS = 8,
296 } VisitorCapabilities;
298 typedef struct SerializeOps {
299 void (*serialize)(void *native_in, void **datap,
300 VisitorFunc visit, Error **errp);
301 void (*deserialize)(void **native_out, void *datap,
302 VisitorFunc visit, Error **errp);
303 void (*cleanup)(void *datap);
305 VisitorCapabilities caps;
308 typedef struct TestArgs {
309 const SerializeOps *ops;
313 static void test_primitives(gconstpointer opaque)
315 TestArgs *args = (TestArgs *) opaque;
316 const SerializeOps *ops = args->ops;
317 PrimitiveType *pt = args->test_data;
318 PrimitiveType *pt_copy = g_malloc0(sizeof(*pt_copy));
320 void *serialize_data;
322 pt_copy->type = pt->type;
323 ops->serialize(pt, &serialize_data, visit_primitive_type, &err);
324 ops->deserialize((void **)&pt_copy, serialize_data, visit_primitive_type, &err);
326 g_assert(err == NULL);
327 g_assert(pt_copy != NULL);
328 if (pt->type == PTYPE_STRING) {
329 g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
330 g_free((char *)pt_copy->value.string);
331 } else if (pt->type == PTYPE_NUMBER) {
332 GString *double_expected = g_string_new("");
333 GString *double_actual = g_string_new("");
334 /* we serialize with %f for our reference visitors, so rather than fuzzy
335 * floating math to test "equality", just compare the formatted values
337 g_string_printf(double_expected, "%.6f", pt->value.number);
338 g_string_printf(double_actual, "%.6f", pt_copy->value.number);
339 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
341 g_string_free(double_expected, true);
342 g_string_free(double_actual, true);
343 } else if (pt->type == PTYPE_BOOLEAN) {
344 g_assert_cmpint(!!pt->value.max, ==, !!pt->value.max);
346 g_assert_cmpint(pt->value.max, ==, pt_copy->value.max);
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 = { 0 } };
360 PrimitiveList pl_copy = { .value = { 0 } };
361 PrimitiveList *pl_copy_ptr = &pl_copy;
363 void *serialize_data;
364 void *cur_head = NULL;
367 pl.type = pl_copy.type = pt->type;
369 /* build up our list of primitive types */
370 for (i = 0; i < 32; i++) {
373 strList *tmp = g_new0(strList, 1);
374 tmp->value = g_strdup(pt->value.string);
375 if (pl.value.strings == NULL) {
376 pl.value.strings = tmp;
378 tmp->next = pl.value.strings;
379 pl.value.strings = tmp;
383 case PTYPE_INTEGER: {
384 intList *tmp = g_new0(intList, 1);
385 tmp->value = pt->value.integer;
386 if (pl.value.integers == NULL) {
387 pl.value.integers = tmp;
389 tmp->next = pl.value.integers;
390 pl.value.integers = tmp;
395 int8List *tmp = g_new0(int8List, 1);
396 tmp->value = pt->value.s8;
397 if (pl.value.s8_integers == NULL) {
398 pl.value.s8_integers = tmp;
400 tmp->next = pl.value.s8_integers;
401 pl.value.s8_integers = tmp;
406 int16List *tmp = g_new0(int16List, 1);
407 tmp->value = pt->value.s16;
408 if (pl.value.s16_integers == NULL) {
409 pl.value.s16_integers = tmp;
411 tmp->next = pl.value.s16_integers;
412 pl.value.s16_integers = tmp;
417 int32List *tmp = g_new0(int32List, 1);
418 tmp->value = pt->value.s32;
419 if (pl.value.s32_integers == NULL) {
420 pl.value.s32_integers = tmp;
422 tmp->next = pl.value.s32_integers;
423 pl.value.s32_integers = tmp;
428 int64List *tmp = g_new0(int64List, 1);
429 tmp->value = pt->value.s64;
430 if (pl.value.s64_integers == NULL) {
431 pl.value.s64_integers = tmp;
433 tmp->next = pl.value.s64_integers;
434 pl.value.s64_integers = tmp;
439 uint8List *tmp = g_new0(uint8List, 1);
440 tmp->value = pt->value.u8;
441 if (pl.value.u8_integers == NULL) {
442 pl.value.u8_integers = tmp;
444 tmp->next = pl.value.u8_integers;
445 pl.value.u8_integers = tmp;
450 uint16List *tmp = g_new0(uint16List, 1);
451 tmp->value = pt->value.u16;
452 if (pl.value.u16_integers == NULL) {
453 pl.value.u16_integers = tmp;
455 tmp->next = pl.value.u16_integers;
456 pl.value.u16_integers = tmp;
461 uint32List *tmp = g_new0(uint32List, 1);
462 tmp->value = pt->value.u32;
463 if (pl.value.u32_integers == NULL) {
464 pl.value.u32_integers = tmp;
466 tmp->next = pl.value.u32_integers;
467 pl.value.u32_integers = tmp;
472 uint64List *tmp = g_new0(uint64List, 1);
473 tmp->value = pt->value.u64;
474 if (pl.value.u64_integers == NULL) {
475 pl.value.u64_integers = tmp;
477 tmp->next = pl.value.u64_integers;
478 pl.value.u64_integers = tmp;
483 numberList *tmp = g_new0(numberList, 1);
484 tmp->value = pt->value.number;
485 if (pl.value.numbers == NULL) {
486 pl.value.numbers = tmp;
488 tmp->next = pl.value.numbers;
489 pl.value.numbers = tmp;
493 case PTYPE_BOOLEAN: {
494 boolList *tmp = g_new0(boolList, 1);
495 tmp->value = pt->value.boolean;
496 if (pl.value.booleans == NULL) {
497 pl.value.booleans = tmp;
499 tmp->next = pl.value.booleans;
500 pl.value.booleans = tmp;
505 g_assert_not_reached();
509 ops->serialize((void **)&pl, &serialize_data, visit_primitive_list, &err);
510 ops->deserialize((void **)&pl_copy_ptr, serialize_data, visit_primitive_list, &err);
512 g_assert(err == NULL);
515 /* compare our deserialized list of primitives to the original */
517 switch (pl_copy.type) {
522 cur_head = ptr->next;
524 cur_head = ptr = pl_copy.value.strings;
526 g_assert_cmpstr(pt->value.string, ==, ptr->value);
529 case PTYPE_INTEGER: {
533 cur_head = ptr->next;
535 cur_head = ptr = pl_copy.value.integers;
537 g_assert_cmpint(pt->value.integer, ==, ptr->value);
544 cur_head = ptr->next;
546 cur_head = ptr = pl_copy.value.s8_integers;
548 g_assert_cmpint(pt->value.s8, ==, ptr->value);
555 cur_head = ptr->next;
557 cur_head = ptr = pl_copy.value.s16_integers;
559 g_assert_cmpint(pt->value.s16, ==, ptr->value);
566 cur_head = ptr->next;
568 cur_head = ptr = pl_copy.value.s32_integers;
570 g_assert_cmpint(pt->value.s32, ==, ptr->value);
577 cur_head = ptr->next;
579 cur_head = ptr = pl_copy.value.s64_integers;
581 g_assert_cmpint(pt->value.s64, ==, ptr->value);
588 cur_head = ptr->next;
590 cur_head = ptr = pl_copy.value.u8_integers;
592 g_assert_cmpint(pt->value.u8, ==, ptr->value);
599 cur_head = ptr->next;
601 cur_head = ptr = pl_copy.value.u16_integers;
603 g_assert_cmpint(pt->value.u16, ==, ptr->value);
610 cur_head = ptr->next;
612 cur_head = ptr = pl_copy.value.u32_integers;
614 g_assert_cmpint(pt->value.u32, ==, ptr->value);
621 cur_head = ptr->next;
623 cur_head = ptr = pl_copy.value.u64_integers;
625 g_assert_cmpint(pt->value.u64, ==, ptr->value);
630 GString *double_expected = g_string_new("");
631 GString *double_actual = g_string_new("");
634 cur_head = ptr->next;
636 cur_head = ptr = pl_copy.value.numbers;
638 /* we serialize with %f for our reference visitors, so rather than
639 * fuzzy floating math to test "equality", just compare the
642 g_string_printf(double_expected, "%.6f", pt->value.number);
643 g_string_printf(double_actual, "%.6f", ptr->value);
644 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
645 g_string_free(double_expected, true);
646 g_string_free(double_actual, true);
649 case PTYPE_BOOLEAN: {
653 cur_head = ptr->next;
655 cur_head = ptr = pl_copy.value.booleans;
657 g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
661 g_assert_not_reached();
666 g_assert_cmpint(i, ==, 33);
668 ops->cleanup(serialize_data);
669 dealloc_helper(&pl, visit_primitive_list, &err);
671 dealloc_helper(&pl_copy, visit_primitive_list, &err);
676 static void test_struct(gconstpointer opaque)
678 TestArgs *args = (TestArgs *) opaque;
679 const SerializeOps *ops = args->ops;
680 TestStruct *ts = struct_create();
681 TestStruct *ts_copy = NULL;
683 void *serialize_data;
685 ops->serialize(ts, &serialize_data, visit_struct, &err);
686 ops->deserialize((void **)&ts_copy, serialize_data, visit_struct, &err);
688 g_assert(err == NULL);
689 struct_compare(ts, ts_copy);
692 struct_cleanup(ts_copy);
694 ops->cleanup(serialize_data);
698 static void test_nested_struct(gconstpointer opaque)
700 TestArgs *args = (TestArgs *) opaque;
701 const SerializeOps *ops = args->ops;
702 UserDefNested *udnp = nested_struct_create();
703 UserDefNested *udnp_copy = NULL;
705 void *serialize_data;
707 ops->serialize(udnp, &serialize_data, visit_nested_struct, &err);
708 ops->deserialize((void **)&udnp_copy, serialize_data, visit_nested_struct, &err);
710 g_assert(err == NULL);
711 nested_struct_compare(udnp, udnp_copy);
713 nested_struct_cleanup(udnp);
714 nested_struct_cleanup(udnp_copy);
716 ops->cleanup(serialize_data);
720 static void test_nested_struct_list(gconstpointer opaque)
722 TestArgs *args = (TestArgs *) opaque;
723 const SerializeOps *ops = args->ops;
724 UserDefNestedList *listp = NULL, *tmp, *tmp_copy, *listp_copy = NULL;
726 void *serialize_data;
729 for (i = 0; i < 8; i++) {
730 tmp = g_malloc0(sizeof(UserDefNestedList));
731 tmp->value = nested_struct_create();
736 ops->serialize(listp, &serialize_data, visit_nested_struct_list, &err);
737 ops->deserialize((void **)&listp_copy, serialize_data,
738 visit_nested_struct_list, &err);
740 g_assert(err == NULL);
743 tmp_copy = listp_copy;
746 nested_struct_compare(listp->value, listp_copy->value);
748 listp_copy = listp_copy->next;
751 qapi_free_UserDefNestedList(tmp);
752 qapi_free_UserDefNestedList(tmp_copy);
754 ops->cleanup(serialize_data);
758 PrimitiveType pt_values[] = {
761 .description = "string_empty",
762 .type = PTYPE_STRING,
766 .description = "string_whitespace",
767 .type = PTYPE_STRING,
768 .value.string = "a b c\td",
771 .description = "string_newlines",
772 .type = PTYPE_STRING,
773 .value.string = "a\nb\n",
776 .description = "string_commas",
777 .type = PTYPE_STRING,
778 .value.string = "a,b, c,d",
781 .description = "string_single_quoted",
782 .type = PTYPE_STRING,
783 .value.string = "'a b',cd",
786 .description = "string_double_quoted",
787 .type = PTYPE_STRING,
788 .value.string = "\"a b\",cd",
792 .description = "boolean_true1",
793 .type = PTYPE_BOOLEAN,
794 .value.boolean = true,
797 .description = "boolean_true2",
798 .type = PTYPE_BOOLEAN,
802 .description = "boolean_true3",
803 .type = PTYPE_BOOLEAN,
807 .description = "boolean_false1",
808 .type = PTYPE_BOOLEAN,
809 .value.boolean = false,
812 .description = "boolean_false2",
813 .type = PTYPE_BOOLEAN,
816 /* number tests (double) */
817 /* note: we format these to %.6f before comparing, since that's how
818 * we serialize them and it doesn't make sense to check precision
822 .description = "number_sanity1",
823 .type = PTYPE_NUMBER,
827 .description = "number_sanity2",
828 .type = PTYPE_NUMBER,
829 .value.number = 3.14159265,
832 .description = "number_min",
833 .type = PTYPE_NUMBER,
834 .value.number = DBL_MIN,
837 .description = "number_max",
838 .type = PTYPE_NUMBER,
839 .value.number = DBL_MAX,
841 /* integer tests (int64) */
843 .description = "integer_sanity1",
844 .type = PTYPE_INTEGER,
848 .description = "integer_sanity2",
849 .type = PTYPE_INTEGER,
850 .value.integer = INT64_MAX / 2 + 1,
853 .description = "integer_min",
854 .type = PTYPE_INTEGER,
855 .value.integer = INT64_MIN,
858 .description = "integer_max",
859 .type = PTYPE_INTEGER,
860 .value.integer = INT64_MAX,
864 .description = "uint8_sanity1",
869 .description = "uint8_sanity2",
871 .value.u8 = UINT8_MAX / 2 + 1,
874 .description = "uint8_min",
879 .description = "uint8_max",
881 .value.u8 = UINT8_MAX,
885 .description = "uint16_sanity1",
890 .description = "uint16_sanity2",
892 .value.u16 = UINT16_MAX / 2 + 1,
895 .description = "uint16_min",
900 .description = "uint16_max",
902 .value.u16 = UINT16_MAX,
906 .description = "uint32_sanity1",
911 .description = "uint32_sanity2",
913 .value.u32 = UINT32_MAX / 2 + 1,
916 .description = "uint32_min",
921 .description = "uint32_max",
923 .value.u32 = UINT32_MAX,
927 .description = "uint64_sanity1",
932 .description = "uint64_sanity2",
934 .value.u64 = UINT64_MAX / 2 + 1,
937 .description = "uint64_min",
942 .description = "uint64_max",
944 .value.u64 = UINT64_MAX,
948 .description = "int8_sanity1",
953 .description = "int8_sanity2",
955 .value.s8 = INT8_MAX / 2 + 1,
958 .description = "int8_min",
960 .value.s8 = INT8_MIN,
963 .description = "int8_max",
965 .value.s8 = INT8_MAX,
969 .description = "int16_sanity1",
974 .description = "int16_sanity2",
976 .value.s16 = INT16_MAX / 2 + 1,
979 .description = "int16_min",
981 .value.s16 = INT16_MIN,
984 .description = "int16_max",
986 .value.s16 = INT16_MAX,
990 .description = "int32_sanity1",
995 .description = "int32_sanity2",
997 .value.s32 = INT32_MAX / 2 + 1,
1000 .description = "int32_min",
1002 .value.s32 = INT32_MIN,
1005 .description = "int32_max",
1007 .value.s32 = INT32_MAX,
1011 .description = "int64_sanity1",
1016 .description = "int64_sanity2",
1018 .value.s64 = INT64_MAX / 2 + 1,
1021 .description = "int64_min",
1023 .value.s64 = INT64_MIN,
1026 .description = "int64_max",
1028 .value.s64 = INT64_MAX,
1030 { .type = PTYPE_EOL }
1033 /* visitor-specific op implementations */
1035 typedef struct QmpSerializeData {
1036 QmpOutputVisitor *qov;
1037 QmpInputVisitor *qiv;
1040 static void qmp_serialize(void *native_in, void **datap,
1041 VisitorFunc visit, Error **errp)
1043 QmpSerializeData *d = g_malloc0(sizeof(*d));
1045 d->qov = qmp_output_visitor_new();
1046 visit(qmp_output_get_visitor(d->qov), &native_in, errp);
1050 static void qmp_deserialize(void **native_out, void *datap,
1051 VisitorFunc visit, Error **errp)
1053 QmpSerializeData *d = datap;
1054 QString *output_json;
1055 QObject *obj_orig, *obj;
1057 obj_orig = qmp_output_get_qobject(d->qov);
1058 output_json = qobject_to_json(obj_orig);
1059 obj = qobject_from_json(qstring_get_str(output_json));
1061 QDECREF(output_json);
1062 d->qiv = qmp_input_visitor_new(obj);
1063 qobject_decref(obj_orig);
1064 qobject_decref(obj);
1065 visit(qmp_input_get_visitor(d->qiv), native_out, errp);
1068 static void qmp_cleanup(void *datap)
1070 QmpSerializeData *d = datap;
1071 qmp_output_visitor_cleanup(d->qov);
1072 qmp_input_visitor_cleanup(d->qiv);
1077 typedef struct StringSerializeData {
1079 StringOutputVisitor *sov;
1080 StringInputVisitor *siv;
1081 } StringSerializeData;
1083 static void string_serialize(void *native_in, void **datap,
1084 VisitorFunc visit, Error **errp)
1086 StringSerializeData *d = g_malloc0(sizeof(*d));
1088 d->sov = string_output_visitor_new(false);
1089 visit(string_output_get_visitor(d->sov), &native_in, errp);
1093 static void string_deserialize(void **native_out, void *datap,
1094 VisitorFunc visit, Error **errp)
1096 StringSerializeData *d = datap;
1098 d->string = string_output_get_string(d->sov);
1099 d->siv = string_input_visitor_new(d->string);
1100 visit(string_input_get_visitor(d->siv), native_out, errp);
1103 static void string_cleanup(void *datap)
1105 StringSerializeData *d = datap;
1107 string_output_visitor_cleanup(d->sov);
1108 string_input_visitor_cleanup(d->siv);
1113 /* visitor registration, test harness */
1115 /* note: to function interchangeably as a serialization mechanism your
1116 * visitor test implementation should pass the test cases for all visitor
1117 * capabilities: primitives, structures, and lists
1119 static const SerializeOps visitors[] = {
1122 .serialize = qmp_serialize,
1123 .deserialize = qmp_deserialize,
1124 .cleanup = qmp_cleanup,
1125 .caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
1126 VCAP_PRIMITIVE_LISTS
1130 .serialize = string_serialize,
1131 .deserialize = string_deserialize,
1132 .cleanup = string_cleanup,
1133 .caps = VCAP_PRIMITIVES
1138 static void add_visitor_type(const SerializeOps *ops)
1140 char testname_prefix[128];
1145 sprintf(testname_prefix, "/visitor/serialization/%s", ops->type);
1147 if (ops->caps & VCAP_PRIMITIVES) {
1148 while (pt_values[i].type != PTYPE_EOL) {
1149 sprintf(testname, "%s/primitives/%s", testname_prefix,
1150 pt_values[i].description);
1151 args = g_malloc0(sizeof(*args));
1153 args->test_data = &pt_values[i];
1154 g_test_add_data_func(testname, args, test_primitives);
1159 if (ops->caps & VCAP_STRUCTURES) {
1160 sprintf(testname, "%s/struct", testname_prefix);
1161 args = g_malloc0(sizeof(*args));
1163 args->test_data = NULL;
1164 g_test_add_data_func(testname, args, test_struct);
1166 sprintf(testname, "%s/nested_struct", testname_prefix);
1167 args = g_malloc0(sizeof(*args));
1169 args->test_data = NULL;
1170 g_test_add_data_func(testname, args, test_nested_struct);
1173 if (ops->caps & VCAP_LISTS) {
1174 sprintf(testname, "%s/nested_struct_list", testname_prefix);
1175 args = g_malloc0(sizeof(*args));
1177 args->test_data = NULL;
1178 g_test_add_data_func(testname, args, test_nested_struct_list);
1181 if (ops->caps & VCAP_PRIMITIVE_LISTS) {
1183 while (pt_values[i].type != PTYPE_EOL) {
1184 sprintf(testname, "%s/primitive_list/%s", testname_prefix,
1185 pt_values[i].description);
1186 args = g_malloc0(sizeof(*args));
1188 args->test_data = &pt_values[i];
1189 g_test_add_data_func(testname, args, test_primitive_lists);
1195 int main(int argc, char **argv)
1199 g_test_init(&argc, &argv, NULL);
1201 while (visitors[i].type != NULL) {
1202 add_visitor_type(&visitors[i]);