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 strList
*tmp
= g_new0(strList
, 1);
355 tmp
->value
= g_strdup(pt
->value
.string
);
356 if (pl
.value
.strings
== NULL
) {
357 pl
.value
.strings
= tmp
;
359 tmp
->next
= pl
.value
.strings
;
360 pl
.value
.strings
= tmp
;
364 case PTYPE_INTEGER
: {
365 intList
*tmp
= g_new0(intList
, 1);
366 tmp
->value
= pt
->value
.integer
;
367 if (pl
.value
.integers
== NULL
) {
368 pl
.value
.integers
= tmp
;
370 tmp
->next
= pl
.value
.integers
;
371 pl
.value
.integers
= tmp
;
376 int8List
*tmp
= g_new0(int8List
, 1);
377 tmp
->value
= pt
->value
.s8
;
378 if (pl
.value
.s8_integers
== NULL
) {
379 pl
.value
.s8_integers
= tmp
;
381 tmp
->next
= pl
.value
.s8_integers
;
382 pl
.value
.s8_integers
= tmp
;
387 int16List
*tmp
= g_new0(int16List
, 1);
388 tmp
->value
= pt
->value
.s16
;
389 if (pl
.value
.s16_integers
== NULL
) {
390 pl
.value
.s16_integers
= tmp
;
392 tmp
->next
= pl
.value
.s16_integers
;
393 pl
.value
.s16_integers
= tmp
;
398 int32List
*tmp
= g_new0(int32List
, 1);
399 tmp
->value
= pt
->value
.s32
;
400 if (pl
.value
.s32_integers
== NULL
) {
401 pl
.value
.s32_integers
= tmp
;
403 tmp
->next
= pl
.value
.s32_integers
;
404 pl
.value
.s32_integers
= tmp
;
409 int64List
*tmp
= g_new0(int64List
, 1);
410 tmp
->value
= pt
->value
.s64
;
411 if (pl
.value
.s64_integers
== NULL
) {
412 pl
.value
.s64_integers
= tmp
;
414 tmp
->next
= pl
.value
.s64_integers
;
415 pl
.value
.s64_integers
= tmp
;
420 uint8List
*tmp
= g_new0(uint8List
, 1);
421 tmp
->value
= pt
->value
.u8
;
422 if (pl
.value
.u8_integers
== NULL
) {
423 pl
.value
.u8_integers
= tmp
;
425 tmp
->next
= pl
.value
.u8_integers
;
426 pl
.value
.u8_integers
= tmp
;
431 uint16List
*tmp
= g_new0(uint16List
, 1);
432 tmp
->value
= pt
->value
.u16
;
433 if (pl
.value
.u16_integers
== NULL
) {
434 pl
.value
.u16_integers
= tmp
;
436 tmp
->next
= pl
.value
.u16_integers
;
437 pl
.value
.u16_integers
= tmp
;
442 uint32List
*tmp
= g_new0(uint32List
, 1);
443 tmp
->value
= pt
->value
.u32
;
444 if (pl
.value
.u32_integers
== NULL
) {
445 pl
.value
.u32_integers
= tmp
;
447 tmp
->next
= pl
.value
.u32_integers
;
448 pl
.value
.u32_integers
= tmp
;
453 uint64List
*tmp
= g_new0(uint64List
, 1);
454 tmp
->value
= pt
->value
.u64
;
455 if (pl
.value
.u64_integers
== NULL
) {
456 pl
.value
.u64_integers
= tmp
;
458 tmp
->next
= pl
.value
.u64_integers
;
459 pl
.value
.u64_integers
= tmp
;
464 numberList
*tmp
= g_new0(numberList
, 1);
465 tmp
->value
= pt
->value
.number
;
466 if (pl
.value
.numbers
== NULL
) {
467 pl
.value
.numbers
= tmp
;
469 tmp
->next
= pl
.value
.numbers
;
470 pl
.value
.numbers
= tmp
;
474 case PTYPE_BOOLEAN
: {
475 boolList
*tmp
= g_new0(boolList
, 1);
476 tmp
->value
= pt
->value
.boolean
;
477 if (pl
.value
.booleans
== NULL
) {
478 pl
.value
.booleans
= tmp
;
480 tmp
->next
= pl
.value
.booleans
;
481 pl
.value
.booleans
= tmp
;
486 g_assert_not_reached();
490 ops
->serialize((void **)&pl
, &serialize_data
, visit_primitive_list
,
492 ops
->deserialize((void **)&pl_copy_ptr
, serialize_data
,
493 visit_primitive_list
, &error_abort
);
497 /* compare our deserialized list of primitives to the original */
499 switch (pl_copy
.type
) {
504 cur_head
= ptr
->next
;
506 cur_head
= ptr
= pl_copy
.value
.strings
;
508 g_assert_cmpstr(pt
->value
.string
, ==, ptr
->value
);
511 case PTYPE_INTEGER
: {
515 cur_head
= ptr
->next
;
517 cur_head
= ptr
= pl_copy
.value
.integers
;
519 g_assert_cmpint(pt
->value
.integer
, ==, ptr
->value
);
526 cur_head
= ptr
->next
;
528 cur_head
= ptr
= pl_copy
.value
.s8_integers
;
530 g_assert_cmpint(pt
->value
.s8
, ==, ptr
->value
);
537 cur_head
= ptr
->next
;
539 cur_head
= ptr
= pl_copy
.value
.s16_integers
;
541 g_assert_cmpint(pt
->value
.s16
, ==, ptr
->value
);
548 cur_head
= ptr
->next
;
550 cur_head
= ptr
= pl_copy
.value
.s32_integers
;
552 g_assert_cmpint(pt
->value
.s32
, ==, ptr
->value
);
559 cur_head
= ptr
->next
;
561 cur_head
= ptr
= pl_copy
.value
.s64_integers
;
563 g_assert_cmpint(pt
->value
.s64
, ==, ptr
->value
);
570 cur_head
= ptr
->next
;
572 cur_head
= ptr
= pl_copy
.value
.u8_integers
;
574 g_assert_cmpint(pt
->value
.u8
, ==, ptr
->value
);
581 cur_head
= ptr
->next
;
583 cur_head
= ptr
= pl_copy
.value
.u16_integers
;
585 g_assert_cmpint(pt
->value
.u16
, ==, ptr
->value
);
592 cur_head
= ptr
->next
;
594 cur_head
= ptr
= pl_copy
.value
.u32_integers
;
596 g_assert_cmpint(pt
->value
.u32
, ==, ptr
->value
);
603 cur_head
= ptr
->next
;
605 cur_head
= ptr
= pl_copy
.value
.u64_integers
;
607 g_assert_cmpint(pt
->value
.u64
, ==, ptr
->value
);
612 GString
*double_expected
= g_string_new("");
613 GString
*double_actual
= g_string_new("");
616 cur_head
= ptr
->next
;
618 cur_head
= ptr
= pl_copy
.value
.numbers
;
620 /* we serialize with %f for our reference visitors, so rather than
621 * fuzzy floating math to test "equality", just compare the
624 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
625 g_string_printf(double_actual
, "%.6f", ptr
->value
);
626 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
627 g_string_free(double_expected
, true);
628 g_string_free(double_actual
, true);
631 case PTYPE_BOOLEAN
: {
635 cur_head
= ptr
->next
;
637 cur_head
= ptr
= pl_copy
.value
.booleans
;
639 g_assert_cmpint(!!pt
->value
.boolean
, ==, !!ptr
->value
);
643 g_assert_not_reached();
648 g_assert_cmpint(i
, ==, 33);
650 ops
->cleanup(serialize_data
);
651 dealloc_helper(&pl
, visit_primitive_list
, &error_abort
);
652 dealloc_helper(&pl_copy
, visit_primitive_list
, &error_abort
);
656 static void test_struct(gconstpointer opaque
)
658 TestArgs
*args
= (TestArgs
*) opaque
;
659 const SerializeOps
*ops
= args
->ops
;
660 TestStruct
*ts
= struct_create();
661 TestStruct
*ts_copy
= NULL
;
662 void *serialize_data
;
664 ops
->serialize(ts
, &serialize_data
, visit_struct
, &error_abort
);
665 ops
->deserialize((void **)&ts_copy
, serialize_data
, visit_struct
,
668 struct_compare(ts
, ts_copy
);
671 struct_cleanup(ts_copy
);
673 ops
->cleanup(serialize_data
);
677 static void test_nested_struct(gconstpointer opaque
)
679 TestArgs
*args
= (TestArgs
*) opaque
;
680 const SerializeOps
*ops
= args
->ops
;
681 UserDefTwo
*udnp
= nested_struct_create();
682 UserDefTwo
*udnp_copy
= NULL
;
683 void *serialize_data
;
685 ops
->serialize(udnp
, &serialize_data
, visit_nested_struct
, &error_abort
);
686 ops
->deserialize((void **)&udnp_copy
, serialize_data
, visit_nested_struct
,
689 nested_struct_compare(udnp
, udnp_copy
);
691 nested_struct_cleanup(udnp
);
692 nested_struct_cleanup(udnp_copy
);
694 ops
->cleanup(serialize_data
);
698 static void test_nested_struct_list(gconstpointer opaque
)
700 TestArgs
*args
= (TestArgs
*) opaque
;
701 const SerializeOps
*ops
= args
->ops
;
702 UserDefTwoList
*listp
= NULL
, *tmp
, *tmp_copy
, *listp_copy
= NULL
;
703 void *serialize_data
;
706 for (i
= 0; i
< 8; i
++) {
707 tmp
= g_new0(UserDefTwoList
, 1);
708 tmp
->value
= nested_struct_create();
713 ops
->serialize(listp
, &serialize_data
, visit_nested_struct_list
,
715 ops
->deserialize((void **)&listp_copy
, serialize_data
,
716 visit_nested_struct_list
, &error_abort
);
719 tmp_copy
= listp_copy
;
722 nested_struct_compare(listp
->value
, listp_copy
->value
);
724 listp_copy
= listp_copy
->next
;
727 qapi_free_UserDefTwoList(tmp
);
728 qapi_free_UserDefTwoList(tmp_copy
);
730 ops
->cleanup(serialize_data
);
734 static PrimitiveType pt_values
[] = {
737 .description
= "string_empty",
738 .type
= PTYPE_STRING
,
742 .description
= "string_whitespace",
743 .type
= PTYPE_STRING
,
744 .value
.string
= "a b c\td",
747 .description
= "string_newlines",
748 .type
= PTYPE_STRING
,
749 .value
.string
= "a\nb\n",
752 .description
= "string_commas",
753 .type
= PTYPE_STRING
,
754 .value
.string
= "a,b, c,d",
757 .description
= "string_single_quoted",
758 .type
= PTYPE_STRING
,
759 .value
.string
= "'a b',cd",
762 .description
= "string_double_quoted",
763 .type
= PTYPE_STRING
,
764 .value
.string
= "\"a b\",cd",
768 .description
= "boolean_true1",
769 .type
= PTYPE_BOOLEAN
,
770 .value
.boolean
= true,
773 .description
= "boolean_true2",
774 .type
= PTYPE_BOOLEAN
,
778 .description
= "boolean_true3",
779 .type
= PTYPE_BOOLEAN
,
783 .description
= "boolean_false1",
784 .type
= PTYPE_BOOLEAN
,
785 .value
.boolean
= false,
788 .description
= "boolean_false2",
789 .type
= PTYPE_BOOLEAN
,
792 /* number tests (double) */
793 /* note: we format these to %.6f before comparing, since that's how
794 * we serialize them and it doesn't make sense to check precision
798 .description
= "number_sanity1",
799 .type
= PTYPE_NUMBER
,
803 .description
= "number_sanity2",
804 .type
= PTYPE_NUMBER
,
805 .value
.number
= 3.14159265,
808 .description
= "number_min",
809 .type
= PTYPE_NUMBER
,
810 .value
.number
= DBL_MIN
,
813 .description
= "number_max",
814 .type
= PTYPE_NUMBER
,
815 .value
.number
= DBL_MAX
,
817 /* integer tests (int64) */
819 .description
= "integer_sanity1",
820 .type
= PTYPE_INTEGER
,
824 .description
= "integer_sanity2",
825 .type
= PTYPE_INTEGER
,
826 .value
.integer
= INT64_MAX
/ 2 + 1,
829 .description
= "integer_min",
830 .type
= PTYPE_INTEGER
,
831 .value
.integer
= INT64_MIN
,
834 .description
= "integer_max",
835 .type
= PTYPE_INTEGER
,
836 .value
.integer
= INT64_MAX
,
840 .description
= "uint8_sanity1",
845 .description
= "uint8_sanity2",
847 .value
.u8
= UINT8_MAX
/ 2 + 1,
850 .description
= "uint8_min",
855 .description
= "uint8_max",
857 .value
.u8
= UINT8_MAX
,
861 .description
= "uint16_sanity1",
866 .description
= "uint16_sanity2",
868 .value
.u16
= UINT16_MAX
/ 2 + 1,
871 .description
= "uint16_min",
876 .description
= "uint16_max",
878 .value
.u16
= UINT16_MAX
,
882 .description
= "uint32_sanity1",
887 .description
= "uint32_sanity2",
889 .value
.u32
= UINT32_MAX
/ 2 + 1,
892 .description
= "uint32_min",
897 .description
= "uint32_max",
899 .value
.u32
= UINT32_MAX
,
903 .description
= "uint64_sanity1",
908 .description
= "uint64_sanity2",
910 .value
.u64
= UINT64_MAX
/ 2 + 1,
913 .description
= "uint64_min",
918 .description
= "uint64_max",
920 .value
.u64
= UINT64_MAX
,
924 .description
= "int8_sanity1",
929 .description
= "int8_sanity2",
931 .value
.s8
= INT8_MAX
/ 2 + 1,
934 .description
= "int8_min",
936 .value
.s8
= INT8_MIN
,
939 .description
= "int8_max",
941 .value
.s8
= INT8_MAX
,
945 .description
= "int16_sanity1",
950 .description
= "int16_sanity2",
952 .value
.s16
= INT16_MAX
/ 2 + 1,
955 .description
= "int16_min",
957 .value
.s16
= INT16_MIN
,
960 .description
= "int16_max",
962 .value
.s16
= INT16_MAX
,
966 .description
= "int32_sanity1",
971 .description
= "int32_sanity2",
973 .value
.s32
= INT32_MAX
/ 2 + 1,
976 .description
= "int32_min",
978 .value
.s32
= INT32_MIN
,
981 .description
= "int32_max",
983 .value
.s32
= INT32_MAX
,
987 .description
= "int64_sanity1",
992 .description
= "int64_sanity2",
994 .value
.s64
= INT64_MAX
/ 2 + 1,
997 .description
= "int64_min",
999 .value
.s64
= INT64_MIN
,
1002 .description
= "int64_max",
1004 .value
.s64
= INT64_MAX
,
1006 { .type
= PTYPE_EOL
}
1009 /* visitor-specific op implementations */
1011 typedef struct QmpSerializeData
{
1017 static void qmp_serialize(void *native_in
, void **datap
,
1018 VisitorFunc visit
, Error
**errp
)
1020 QmpSerializeData
*d
= g_malloc0(sizeof(*d
));
1022 d
->qov
= qobject_output_visitor_new(&d
->obj
);
1023 visit(d
->qov
, &native_in
, errp
);
1027 static void qmp_deserialize(void **native_out
, void *datap
,
1028 VisitorFunc visit
, Error
**errp
)
1030 QmpSerializeData
*d
= datap
;
1031 QString
*output_json
;
1032 QObject
*obj_orig
, *obj
;
1034 visit_complete(d
->qov
, &d
->obj
);
1036 output_json
= qobject_to_json(obj_orig
);
1037 obj
= qobject_from_json(qstring_get_str(output_json
), &error_abort
);
1039 QDECREF(output_json
);
1040 d
->qiv
= qobject_input_visitor_new(obj
);
1041 qobject_decref(obj_orig
);
1042 qobject_decref(obj
);
1043 visit(d
->qiv
, native_out
, errp
);
1046 static void qmp_cleanup(void *datap
)
1048 QmpSerializeData
*d
= datap
;
1055 typedef struct StringSerializeData
{
1059 } StringSerializeData
;
1061 static void string_serialize(void *native_in
, void **datap
,
1062 VisitorFunc visit
, Error
**errp
)
1064 StringSerializeData
*d
= g_malloc0(sizeof(*d
));
1066 d
->sov
= string_output_visitor_new(false, &d
->string
);
1067 visit(d
->sov
, &native_in
, errp
);
1071 static void string_deserialize(void **native_out
, void *datap
,
1072 VisitorFunc visit
, Error
**errp
)
1074 StringSerializeData
*d
= datap
;
1076 visit_complete(d
->sov
, &d
->string
);
1077 d
->siv
= string_input_visitor_new(d
->string
);
1078 visit(d
->siv
, native_out
, errp
);
1081 static void string_cleanup(void *datap
)
1083 StringSerializeData
*d
= datap
;
1091 /* visitor registration, test harness */
1093 /* note: to function interchangeably as a serialization mechanism your
1094 * visitor test implementation should pass the test cases for all visitor
1095 * capabilities: primitives, structures, and lists
1097 static const SerializeOps visitors
[] = {
1100 .serialize
= qmp_serialize
,
1101 .deserialize
= qmp_deserialize
,
1102 .cleanup
= qmp_cleanup
,
1103 .caps
= VCAP_PRIMITIVES
| VCAP_STRUCTURES
| VCAP_LISTS
|
1104 VCAP_PRIMITIVE_LISTS
1108 .serialize
= string_serialize
,
1109 .deserialize
= string_deserialize
,
1110 .cleanup
= string_cleanup
,
1111 .caps
= VCAP_PRIMITIVES
1116 static void add_visitor_type(const SerializeOps
*ops
)
1118 char testname_prefix
[128];
1123 sprintf(testname_prefix
, "/visitor/serialization/%s", ops
->type
);
1125 if (ops
->caps
& VCAP_PRIMITIVES
) {
1126 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1127 sprintf(testname
, "%s/primitives/%s", testname_prefix
,
1128 pt_values
[i
].description
);
1129 args
= g_malloc0(sizeof(*args
));
1131 args
->test_data
= &pt_values
[i
];
1132 g_test_add_data_func(testname
, args
, test_primitives
);
1137 if (ops
->caps
& VCAP_STRUCTURES
) {
1138 sprintf(testname
, "%s/struct", testname_prefix
);
1139 args
= g_malloc0(sizeof(*args
));
1141 args
->test_data
= NULL
;
1142 g_test_add_data_func(testname
, args
, test_struct
);
1144 sprintf(testname
, "%s/nested_struct", testname_prefix
);
1145 args
= g_malloc0(sizeof(*args
));
1147 args
->test_data
= NULL
;
1148 g_test_add_data_func(testname
, args
, test_nested_struct
);
1151 if (ops
->caps
& VCAP_LISTS
) {
1152 sprintf(testname
, "%s/nested_struct_list", testname_prefix
);
1153 args
= g_malloc0(sizeof(*args
));
1155 args
->test_data
= NULL
;
1156 g_test_add_data_func(testname
, args
, test_nested_struct_list
);
1159 if (ops
->caps
& VCAP_PRIMITIVE_LISTS
) {
1161 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1162 sprintf(testname
, "%s/primitive_list/%s", testname_prefix
,
1163 pt_values
[i
].description
);
1164 args
= g_malloc0(sizeof(*args
));
1166 args
->test_data
= &pt_values
[i
];
1167 g_test_add_data_func(testname
, args
, test_primitive_lists
);
1173 int main(int argc
, char **argv
)
1177 g_test_init(&argc
, &argv
, NULL
);
1179 while (visitors
[i
].type
!= NULL
) {
1180 add_visitor_type(&visitors
[i
]);