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.
19 #include "qemu-common.h"
20 #include "test-qapi-types.h"
21 #include "test-qapi-visit.h"
22 #include "qapi/qmp/types.h"
23 #include "qapi/qmp-input-visitor.h"
24 #include "qapi/qmp-output-visitor.h"
25 #include "qapi/string-input-visitor.h"
26 #include "qapi/string-output-visitor.h"
27 #include "qapi-types.h"
28 #include "qapi-visit.h"
29 #include "qapi/dealloc-visitor.h"
31 enum PrimitiveTypeKind
{
47 typedef struct PrimitiveType
{
63 enum PrimitiveTypeKind type
;
64 const char *description
;
67 typedef struct PrimitiveList
{
73 int8List
*s8_integers
;
74 int16List
*s16_integers
;
75 int32List
*s32_integers
;
76 int64List
*s64_integers
;
77 uint8List
*u8_integers
;
78 uint16List
*u16_integers
;
79 uint32List
*u32_integers
;
80 uint64List
*u64_integers
;
82 enum PrimitiveTypeKind type
;
83 const char *description
;
88 typedef void (*VisitorFunc
)(Visitor
*v
, void **native
, Error
**errp
);
90 static void dealloc_helper(void *native_in
, VisitorFunc visit
, Error
**errp
)
92 QapiDeallocVisitor
*qdv
= qapi_dealloc_visitor_new();
94 visit(qapi_dealloc_get_visitor(qdv
), &native_in
, errp
);
96 qapi_dealloc_visitor_cleanup(qdv
);
99 static void visit_primitive_type(Visitor
*v
, void **native
, Error
**errp
)
101 PrimitiveType
*pt
= *native
;
104 visit_type_str(v
, (char **)&pt
->value
.string
, NULL
, errp
);
107 visit_type_bool(v
, &pt
->value
.boolean
, NULL
, errp
);
110 visit_type_number(v
, &pt
->value
.number
, NULL
, errp
);
113 visit_type_int(v
, &pt
->value
.integer
, NULL
, errp
);
116 visit_type_uint8(v
, &pt
->value
.u8
, NULL
, errp
);
119 visit_type_uint16(v
, &pt
->value
.u16
, NULL
, errp
);
122 visit_type_uint32(v
, &pt
->value
.u32
, NULL
, errp
);
125 visit_type_uint64(v
, &pt
->value
.u64
, NULL
, errp
);
128 visit_type_int8(v
, &pt
->value
.s8
, NULL
, errp
);
131 visit_type_int16(v
, &pt
->value
.s16
, NULL
, errp
);
134 visit_type_int32(v
, &pt
->value
.s32
, NULL
, errp
);
137 visit_type_int64(v
, &pt
->value
.s64
, NULL
, errp
);
140 g_assert_not_reached();
144 static void visit_primitive_list(Visitor
*v
, void **native
, Error
**errp
)
146 PrimitiveList
*pl
= *native
;
149 visit_type_strList(v
, &pl
->value
.strings
, NULL
, errp
);
152 visit_type_boolList(v
, &pl
->value
.booleans
, NULL
, errp
);
155 visit_type_numberList(v
, &pl
->value
.numbers
, NULL
, errp
);
158 visit_type_intList(v
, &pl
->value
.integers
, NULL
, errp
);
161 visit_type_int8List(v
, &pl
->value
.s8_integers
, NULL
, errp
);
164 visit_type_int16List(v
, &pl
->value
.s16_integers
, NULL
, errp
);
167 visit_type_int32List(v
, &pl
->value
.s32_integers
, NULL
, errp
);
170 visit_type_int64List(v
, &pl
->value
.s64_integers
, NULL
, errp
);
173 visit_type_uint8List(v
, &pl
->value
.u8_integers
, NULL
, errp
);
176 visit_type_uint16List(v
, &pl
->value
.u16_integers
, NULL
, errp
);
179 visit_type_uint32List(v
, &pl
->value
.u32_integers
, NULL
, errp
);
182 visit_type_uint64List(v
, &pl
->value
.u64_integers
, NULL
, errp
);
185 g_assert_not_reached();
189 typedef struct TestStruct
196 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
197 const char *name
, Error
**errp
)
201 visit_start_struct(v
, (void **)obj
, NULL
, name
, sizeof(TestStruct
), &err
);
206 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
210 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
214 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
217 error_propagate(errp
, err
);
219 visit_end_struct(v
, &err
);
221 error_propagate(errp
, err
);
224 static TestStruct
*struct_create(void)
226 TestStruct
*ts
= g_malloc0(sizeof(*ts
));
229 ts
->string
= strdup("test string");
233 static void struct_compare(TestStruct
*ts1
, TestStruct
*ts2
)
237 g_assert_cmpint(ts1
->integer
, ==, ts2
->integer
);
238 g_assert(ts1
->boolean
== ts2
->boolean
);
239 g_assert_cmpstr(ts1
->string
, ==, ts2
->string
);
242 static void struct_cleanup(TestStruct
*ts
)
248 static void visit_struct(Visitor
*v
, void **native
, Error
**errp
)
250 visit_type_TestStruct(v
, (TestStruct
**)native
, NULL
, errp
);
253 static UserDefTwo
*nested_struct_create(void)
255 UserDefTwo
*udnp
= g_malloc0(sizeof(*udnp
));
256 udnp
->string0
= strdup("test_string0");
257 udnp
->dict1
= g_malloc0(sizeof(*udnp
->dict1
));
258 udnp
->dict1
->string1
= strdup("test_string1");
259 udnp
->dict1
->dict2
= g_malloc0(sizeof(*udnp
->dict1
->dict2
));
260 udnp
->dict1
->dict2
->userdef
= g_new0(UserDefOne
, 1);
261 udnp
->dict1
->dict2
->userdef
->integer
= 42;
262 udnp
->dict1
->dict2
->userdef
->string
= strdup("test_string");
263 udnp
->dict1
->dict2
->string
= strdup("test_string2");
264 udnp
->dict1
->dict3
= g_malloc0(sizeof(*udnp
->dict1
->dict3
));
265 udnp
->dict1
->has_dict3
= true;
266 udnp
->dict1
->dict3
->userdef
= g_new0(UserDefOne
, 1);
267 udnp
->dict1
->dict3
->userdef
->integer
= 43;
268 udnp
->dict1
->dict3
->userdef
->string
= strdup("test_string");
269 udnp
->dict1
->dict3
->string
= strdup("test_string3");
273 static void nested_struct_compare(UserDefTwo
*udnp1
, UserDefTwo
*udnp2
)
277 g_assert_cmpstr(udnp1
->string0
, ==, udnp2
->string0
);
278 g_assert_cmpstr(udnp1
->dict1
->string1
, ==, udnp2
->dict1
->string1
);
279 g_assert_cmpint(udnp1
->dict1
->dict2
->userdef
->integer
, ==,
280 udnp2
->dict1
->dict2
->userdef
->integer
);
281 g_assert_cmpstr(udnp1
->dict1
->dict2
->userdef
->string
, ==,
282 udnp2
->dict1
->dict2
->userdef
->string
);
283 g_assert_cmpstr(udnp1
->dict1
->dict2
->string
, ==,
284 udnp2
->dict1
->dict2
->string
);
285 g_assert(udnp1
->dict1
->has_dict3
== udnp2
->dict1
->has_dict3
);
286 g_assert_cmpint(udnp1
->dict1
->dict3
->userdef
->integer
, ==,
287 udnp2
->dict1
->dict3
->userdef
->integer
);
288 g_assert_cmpstr(udnp1
->dict1
->dict3
->userdef
->string
, ==,
289 udnp2
->dict1
->dict3
->userdef
->string
);
290 g_assert_cmpstr(udnp1
->dict1
->dict3
->string
, ==,
291 udnp2
->dict1
->dict3
->string
);
294 static void nested_struct_cleanup(UserDefTwo
*udnp
)
296 qapi_free_UserDefTwo(udnp
);
299 static void visit_nested_struct(Visitor
*v
, void **native
, Error
**errp
)
301 visit_type_UserDefTwo(v
, (UserDefTwo
**)native
, NULL
, errp
);
304 static void visit_nested_struct_list(Visitor
*v
, void **native
, Error
**errp
)
306 visit_type_UserDefTwoList(v
, (UserDefTwoList
**)native
, NULL
, errp
);
311 typedef enum VisitorCapabilities
{
315 VCAP_PRIMITIVE_LISTS
= 8,
316 } VisitorCapabilities
;
318 typedef struct SerializeOps
{
319 void (*serialize
)(void *native_in
, void **datap
,
320 VisitorFunc visit
, Error
**errp
);
321 void (*deserialize
)(void **native_out
, void *datap
,
322 VisitorFunc visit
, Error
**errp
);
323 void (*cleanup
)(void *datap
);
325 VisitorCapabilities caps
;
328 typedef struct TestArgs
{
329 const SerializeOps
*ops
;
333 static void test_primitives(gconstpointer opaque
)
335 TestArgs
*args
= (TestArgs
*) opaque
;
336 const SerializeOps
*ops
= args
->ops
;
337 PrimitiveType
*pt
= args
->test_data
;
338 PrimitiveType
*pt_copy
= g_malloc0(sizeof(*pt_copy
));
340 void *serialize_data
;
342 pt_copy
->type
= pt
->type
;
343 ops
->serialize(pt
, &serialize_data
, visit_primitive_type
, &err
);
344 ops
->deserialize((void **)&pt_copy
, serialize_data
, visit_primitive_type
, &err
);
346 g_assert(err
== NULL
);
347 g_assert(pt_copy
!= NULL
);
348 if (pt
->type
== PTYPE_STRING
) {
349 g_assert_cmpstr(pt
->value
.string
, ==, pt_copy
->value
.string
);
350 g_free((char *)pt_copy
->value
.string
);
351 } else if (pt
->type
== PTYPE_NUMBER
) {
352 GString
*double_expected
= g_string_new("");
353 GString
*double_actual
= g_string_new("");
354 /* we serialize with %f for our reference visitors, so rather than fuzzy
355 * floating math to test "equality", just compare the formatted values
357 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
358 g_string_printf(double_actual
, "%.6f", pt_copy
->value
.number
);
359 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
361 g_string_free(double_expected
, true);
362 g_string_free(double_actual
, true);
363 } else if (pt
->type
== PTYPE_BOOLEAN
) {
364 g_assert_cmpint(!!pt
->value
.max
, ==, !!pt
->value
.max
);
366 g_assert_cmpint(pt
->value
.max
, ==, pt_copy
->value
.max
);
369 ops
->cleanup(serialize_data
);
374 static void test_primitive_lists(gconstpointer opaque
)
376 TestArgs
*args
= (TestArgs
*) opaque
;
377 const SerializeOps
*ops
= args
->ops
;
378 PrimitiveType
*pt
= args
->test_data
;
379 PrimitiveList pl
= { .value
= { NULL
} };
380 PrimitiveList pl_copy
= { .value
= { NULL
} };
381 PrimitiveList
*pl_copy_ptr
= &pl_copy
;
383 void *serialize_data
;
384 void *cur_head
= NULL
;
387 pl
.type
= pl_copy
.type
= pt
->type
;
389 /* build up our list of primitive types */
390 for (i
= 0; i
< 32; i
++) {
393 strList
*tmp
= g_new0(strList
, 1);
394 tmp
->value
= g_strdup(pt
->value
.string
);
395 if (pl
.value
.strings
== NULL
) {
396 pl
.value
.strings
= tmp
;
398 tmp
->next
= pl
.value
.strings
;
399 pl
.value
.strings
= tmp
;
403 case PTYPE_INTEGER
: {
404 intList
*tmp
= g_new0(intList
, 1);
405 tmp
->value
= pt
->value
.integer
;
406 if (pl
.value
.integers
== NULL
) {
407 pl
.value
.integers
= tmp
;
409 tmp
->next
= pl
.value
.integers
;
410 pl
.value
.integers
= tmp
;
415 int8List
*tmp
= g_new0(int8List
, 1);
416 tmp
->value
= pt
->value
.s8
;
417 if (pl
.value
.s8_integers
== NULL
) {
418 pl
.value
.s8_integers
= tmp
;
420 tmp
->next
= pl
.value
.s8_integers
;
421 pl
.value
.s8_integers
= tmp
;
426 int16List
*tmp
= g_new0(int16List
, 1);
427 tmp
->value
= pt
->value
.s16
;
428 if (pl
.value
.s16_integers
== NULL
) {
429 pl
.value
.s16_integers
= tmp
;
431 tmp
->next
= pl
.value
.s16_integers
;
432 pl
.value
.s16_integers
= tmp
;
437 int32List
*tmp
= g_new0(int32List
, 1);
438 tmp
->value
= pt
->value
.s32
;
439 if (pl
.value
.s32_integers
== NULL
) {
440 pl
.value
.s32_integers
= tmp
;
442 tmp
->next
= pl
.value
.s32_integers
;
443 pl
.value
.s32_integers
= tmp
;
448 int64List
*tmp
= g_new0(int64List
, 1);
449 tmp
->value
= pt
->value
.s64
;
450 if (pl
.value
.s64_integers
== NULL
) {
451 pl
.value
.s64_integers
= tmp
;
453 tmp
->next
= pl
.value
.s64_integers
;
454 pl
.value
.s64_integers
= tmp
;
459 uint8List
*tmp
= g_new0(uint8List
, 1);
460 tmp
->value
= pt
->value
.u8
;
461 if (pl
.value
.u8_integers
== NULL
) {
462 pl
.value
.u8_integers
= tmp
;
464 tmp
->next
= pl
.value
.u8_integers
;
465 pl
.value
.u8_integers
= tmp
;
470 uint16List
*tmp
= g_new0(uint16List
, 1);
471 tmp
->value
= pt
->value
.u16
;
472 if (pl
.value
.u16_integers
== NULL
) {
473 pl
.value
.u16_integers
= tmp
;
475 tmp
->next
= pl
.value
.u16_integers
;
476 pl
.value
.u16_integers
= tmp
;
481 uint32List
*tmp
= g_new0(uint32List
, 1);
482 tmp
->value
= pt
->value
.u32
;
483 if (pl
.value
.u32_integers
== NULL
) {
484 pl
.value
.u32_integers
= tmp
;
486 tmp
->next
= pl
.value
.u32_integers
;
487 pl
.value
.u32_integers
= tmp
;
492 uint64List
*tmp
= g_new0(uint64List
, 1);
493 tmp
->value
= pt
->value
.u64
;
494 if (pl
.value
.u64_integers
== NULL
) {
495 pl
.value
.u64_integers
= tmp
;
497 tmp
->next
= pl
.value
.u64_integers
;
498 pl
.value
.u64_integers
= tmp
;
503 numberList
*tmp
= g_new0(numberList
, 1);
504 tmp
->value
= pt
->value
.number
;
505 if (pl
.value
.numbers
== NULL
) {
506 pl
.value
.numbers
= tmp
;
508 tmp
->next
= pl
.value
.numbers
;
509 pl
.value
.numbers
= tmp
;
513 case PTYPE_BOOLEAN
: {
514 boolList
*tmp
= g_new0(boolList
, 1);
515 tmp
->value
= pt
->value
.boolean
;
516 if (pl
.value
.booleans
== NULL
) {
517 pl
.value
.booleans
= tmp
;
519 tmp
->next
= pl
.value
.booleans
;
520 pl
.value
.booleans
= tmp
;
525 g_assert_not_reached();
529 ops
->serialize((void **)&pl
, &serialize_data
, visit_primitive_list
, &err
);
530 ops
->deserialize((void **)&pl_copy_ptr
, serialize_data
, visit_primitive_list
, &err
);
532 g_assert(err
== NULL
);
535 /* compare our deserialized list of primitives to the original */
537 switch (pl_copy
.type
) {
542 cur_head
= ptr
->next
;
544 cur_head
= ptr
= pl_copy
.value
.strings
;
546 g_assert_cmpstr(pt
->value
.string
, ==, ptr
->value
);
549 case PTYPE_INTEGER
: {
553 cur_head
= ptr
->next
;
555 cur_head
= ptr
= pl_copy
.value
.integers
;
557 g_assert_cmpint(pt
->value
.integer
, ==, ptr
->value
);
564 cur_head
= ptr
->next
;
566 cur_head
= ptr
= pl_copy
.value
.s8_integers
;
568 g_assert_cmpint(pt
->value
.s8
, ==, ptr
->value
);
575 cur_head
= ptr
->next
;
577 cur_head
= ptr
= pl_copy
.value
.s16_integers
;
579 g_assert_cmpint(pt
->value
.s16
, ==, ptr
->value
);
586 cur_head
= ptr
->next
;
588 cur_head
= ptr
= pl_copy
.value
.s32_integers
;
590 g_assert_cmpint(pt
->value
.s32
, ==, ptr
->value
);
597 cur_head
= ptr
->next
;
599 cur_head
= ptr
= pl_copy
.value
.s64_integers
;
601 g_assert_cmpint(pt
->value
.s64
, ==, ptr
->value
);
608 cur_head
= ptr
->next
;
610 cur_head
= ptr
= pl_copy
.value
.u8_integers
;
612 g_assert_cmpint(pt
->value
.u8
, ==, ptr
->value
);
619 cur_head
= ptr
->next
;
621 cur_head
= ptr
= pl_copy
.value
.u16_integers
;
623 g_assert_cmpint(pt
->value
.u16
, ==, ptr
->value
);
630 cur_head
= ptr
->next
;
632 cur_head
= ptr
= pl_copy
.value
.u32_integers
;
634 g_assert_cmpint(pt
->value
.u32
, ==, ptr
->value
);
641 cur_head
= ptr
->next
;
643 cur_head
= ptr
= pl_copy
.value
.u64_integers
;
645 g_assert_cmpint(pt
->value
.u64
, ==, ptr
->value
);
650 GString
*double_expected
= g_string_new("");
651 GString
*double_actual
= g_string_new("");
654 cur_head
= ptr
->next
;
656 cur_head
= ptr
= pl_copy
.value
.numbers
;
658 /* we serialize with %f for our reference visitors, so rather than
659 * fuzzy floating math to test "equality", just compare the
662 g_string_printf(double_expected
, "%.6f", pt
->value
.number
);
663 g_string_printf(double_actual
, "%.6f", ptr
->value
);
664 g_assert_cmpstr(double_actual
->str
, ==, double_expected
->str
);
665 g_string_free(double_expected
, true);
666 g_string_free(double_actual
, true);
669 case PTYPE_BOOLEAN
: {
673 cur_head
= ptr
->next
;
675 cur_head
= ptr
= pl_copy
.value
.booleans
;
677 g_assert_cmpint(!!pt
->value
.boolean
, ==, !!ptr
->value
);
681 g_assert_not_reached();
686 g_assert_cmpint(i
, ==, 33);
688 ops
->cleanup(serialize_data
);
689 dealloc_helper(&pl
, visit_primitive_list
, &err
);
691 dealloc_helper(&pl_copy
, visit_primitive_list
, &err
);
696 static void test_struct(gconstpointer opaque
)
698 TestArgs
*args
= (TestArgs
*) opaque
;
699 const SerializeOps
*ops
= args
->ops
;
700 TestStruct
*ts
= struct_create();
701 TestStruct
*ts_copy
= NULL
;
703 void *serialize_data
;
705 ops
->serialize(ts
, &serialize_data
, visit_struct
, &err
);
706 ops
->deserialize((void **)&ts_copy
, serialize_data
, visit_struct
, &err
);
708 g_assert(err
== NULL
);
709 struct_compare(ts
, ts_copy
);
712 struct_cleanup(ts_copy
);
714 ops
->cleanup(serialize_data
);
718 static void test_nested_struct(gconstpointer opaque
)
720 TestArgs
*args
= (TestArgs
*) opaque
;
721 const SerializeOps
*ops
= args
->ops
;
722 UserDefTwo
*udnp
= nested_struct_create();
723 UserDefTwo
*udnp_copy
= NULL
;
725 void *serialize_data
;
727 ops
->serialize(udnp
, &serialize_data
, visit_nested_struct
, &err
);
728 ops
->deserialize((void **)&udnp_copy
, serialize_data
, visit_nested_struct
,
731 g_assert(err
== NULL
);
732 nested_struct_compare(udnp
, udnp_copy
);
734 nested_struct_cleanup(udnp
);
735 nested_struct_cleanup(udnp_copy
);
737 ops
->cleanup(serialize_data
);
741 static void test_nested_struct_list(gconstpointer opaque
)
743 TestArgs
*args
= (TestArgs
*) opaque
;
744 const SerializeOps
*ops
= args
->ops
;
745 UserDefTwoList
*listp
= NULL
, *tmp
, *tmp_copy
, *listp_copy
= NULL
;
747 void *serialize_data
;
750 for (i
= 0; i
< 8; i
++) {
751 tmp
= g_new0(UserDefTwoList
, 1);
752 tmp
->value
= nested_struct_create();
757 ops
->serialize(listp
, &serialize_data
, visit_nested_struct_list
, &err
);
758 ops
->deserialize((void **)&listp_copy
, serialize_data
,
759 visit_nested_struct_list
, &err
);
761 g_assert(err
== NULL
);
764 tmp_copy
= listp_copy
;
767 nested_struct_compare(listp
->value
, listp_copy
->value
);
769 listp_copy
= listp_copy
->next
;
772 qapi_free_UserDefTwoList(tmp
);
773 qapi_free_UserDefTwoList(tmp_copy
);
775 ops
->cleanup(serialize_data
);
779 static PrimitiveType pt_values
[] = {
782 .description
= "string_empty",
783 .type
= PTYPE_STRING
,
787 .description
= "string_whitespace",
788 .type
= PTYPE_STRING
,
789 .value
.string
= "a b c\td",
792 .description
= "string_newlines",
793 .type
= PTYPE_STRING
,
794 .value
.string
= "a\nb\n",
797 .description
= "string_commas",
798 .type
= PTYPE_STRING
,
799 .value
.string
= "a,b, c,d",
802 .description
= "string_single_quoted",
803 .type
= PTYPE_STRING
,
804 .value
.string
= "'a b',cd",
807 .description
= "string_double_quoted",
808 .type
= PTYPE_STRING
,
809 .value
.string
= "\"a b\",cd",
813 .description
= "boolean_true1",
814 .type
= PTYPE_BOOLEAN
,
815 .value
.boolean
= true,
818 .description
= "boolean_true2",
819 .type
= PTYPE_BOOLEAN
,
823 .description
= "boolean_true3",
824 .type
= PTYPE_BOOLEAN
,
828 .description
= "boolean_false1",
829 .type
= PTYPE_BOOLEAN
,
830 .value
.boolean
= false,
833 .description
= "boolean_false2",
834 .type
= PTYPE_BOOLEAN
,
837 /* number tests (double) */
838 /* note: we format these to %.6f before comparing, since that's how
839 * we serialize them and it doesn't make sense to check precision
843 .description
= "number_sanity1",
844 .type
= PTYPE_NUMBER
,
848 .description
= "number_sanity2",
849 .type
= PTYPE_NUMBER
,
850 .value
.number
= 3.14159265,
853 .description
= "number_min",
854 .type
= PTYPE_NUMBER
,
855 .value
.number
= DBL_MIN
,
858 .description
= "number_max",
859 .type
= PTYPE_NUMBER
,
860 .value
.number
= DBL_MAX
,
862 /* integer tests (int64) */
864 .description
= "integer_sanity1",
865 .type
= PTYPE_INTEGER
,
869 .description
= "integer_sanity2",
870 .type
= PTYPE_INTEGER
,
871 .value
.integer
= INT64_MAX
/ 2 + 1,
874 .description
= "integer_min",
875 .type
= PTYPE_INTEGER
,
876 .value
.integer
= INT64_MIN
,
879 .description
= "integer_max",
880 .type
= PTYPE_INTEGER
,
881 .value
.integer
= INT64_MAX
,
885 .description
= "uint8_sanity1",
890 .description
= "uint8_sanity2",
892 .value
.u8
= UINT8_MAX
/ 2 + 1,
895 .description
= "uint8_min",
900 .description
= "uint8_max",
902 .value
.u8
= UINT8_MAX
,
906 .description
= "uint16_sanity1",
911 .description
= "uint16_sanity2",
913 .value
.u16
= UINT16_MAX
/ 2 + 1,
916 .description
= "uint16_min",
921 .description
= "uint16_max",
923 .value
.u16
= UINT16_MAX
,
927 .description
= "uint32_sanity1",
932 .description
= "uint32_sanity2",
934 .value
.u32
= UINT32_MAX
/ 2 + 1,
937 .description
= "uint32_min",
942 .description
= "uint32_max",
944 .value
.u32
= UINT32_MAX
,
948 .description
= "uint64_sanity1",
953 .description
= "uint64_sanity2",
955 .value
.u64
= UINT64_MAX
/ 2 + 1,
958 .description
= "uint64_min",
963 .description
= "uint64_max",
965 .value
.u64
= UINT64_MAX
,
969 .description
= "int8_sanity1",
974 .description
= "int8_sanity2",
976 .value
.s8
= INT8_MAX
/ 2 + 1,
979 .description
= "int8_min",
981 .value
.s8
= INT8_MIN
,
984 .description
= "int8_max",
986 .value
.s8
= INT8_MAX
,
990 .description
= "int16_sanity1",
995 .description
= "int16_sanity2",
997 .value
.s16
= INT16_MAX
/ 2 + 1,
1000 .description
= "int16_min",
1002 .value
.s16
= INT16_MIN
,
1005 .description
= "int16_max",
1007 .value
.s16
= INT16_MAX
,
1011 .description
= "int32_sanity1",
1016 .description
= "int32_sanity2",
1018 .value
.s32
= INT32_MAX
/ 2 + 1,
1021 .description
= "int32_min",
1023 .value
.s32
= INT32_MIN
,
1026 .description
= "int32_max",
1028 .value
.s32
= INT32_MAX
,
1032 .description
= "int64_sanity1",
1037 .description
= "int64_sanity2",
1039 .value
.s64
= INT64_MAX
/ 2 + 1,
1042 .description
= "int64_min",
1044 .value
.s64
= INT64_MIN
,
1047 .description
= "int64_max",
1049 .value
.s64
= INT64_MAX
,
1051 { .type
= PTYPE_EOL
}
1054 /* visitor-specific op implementations */
1056 typedef struct QmpSerializeData
{
1057 QmpOutputVisitor
*qov
;
1058 QmpInputVisitor
*qiv
;
1061 static void qmp_serialize(void *native_in
, void **datap
,
1062 VisitorFunc visit
, Error
**errp
)
1064 QmpSerializeData
*d
= g_malloc0(sizeof(*d
));
1066 d
->qov
= qmp_output_visitor_new();
1067 visit(qmp_output_get_visitor(d
->qov
), &native_in
, errp
);
1071 static void qmp_deserialize(void **native_out
, void *datap
,
1072 VisitorFunc visit
, Error
**errp
)
1074 QmpSerializeData
*d
= datap
;
1075 QString
*output_json
;
1076 QObject
*obj_orig
, *obj
;
1078 obj_orig
= qmp_output_get_qobject(d
->qov
);
1079 output_json
= qobject_to_json(obj_orig
);
1080 obj
= qobject_from_json(qstring_get_str(output_json
));
1082 QDECREF(output_json
);
1083 d
->qiv
= qmp_input_visitor_new(obj
);
1084 qobject_decref(obj_orig
);
1085 qobject_decref(obj
);
1086 visit(qmp_input_get_visitor(d
->qiv
), native_out
, errp
);
1089 static void qmp_cleanup(void *datap
)
1091 QmpSerializeData
*d
= datap
;
1092 qmp_output_visitor_cleanup(d
->qov
);
1093 qmp_input_visitor_cleanup(d
->qiv
);
1098 typedef struct StringSerializeData
{
1100 StringOutputVisitor
*sov
;
1101 StringInputVisitor
*siv
;
1102 } StringSerializeData
;
1104 static void string_serialize(void *native_in
, void **datap
,
1105 VisitorFunc visit
, Error
**errp
)
1107 StringSerializeData
*d
= g_malloc0(sizeof(*d
));
1109 d
->sov
= string_output_visitor_new(false);
1110 visit(string_output_get_visitor(d
->sov
), &native_in
, errp
);
1114 static void string_deserialize(void **native_out
, void *datap
,
1115 VisitorFunc visit
, Error
**errp
)
1117 StringSerializeData
*d
= datap
;
1119 d
->string
= string_output_get_string(d
->sov
);
1120 d
->siv
= string_input_visitor_new(d
->string
);
1121 visit(string_input_get_visitor(d
->siv
), native_out
, errp
);
1124 static void string_cleanup(void *datap
)
1126 StringSerializeData
*d
= datap
;
1128 string_output_visitor_cleanup(d
->sov
);
1129 string_input_visitor_cleanup(d
->siv
);
1134 /* visitor registration, test harness */
1136 /* note: to function interchangeably as a serialization mechanism your
1137 * visitor test implementation should pass the test cases for all visitor
1138 * capabilities: primitives, structures, and lists
1140 static const SerializeOps visitors
[] = {
1143 .serialize
= qmp_serialize
,
1144 .deserialize
= qmp_deserialize
,
1145 .cleanup
= qmp_cleanup
,
1146 .caps
= VCAP_PRIMITIVES
| VCAP_STRUCTURES
| VCAP_LISTS
|
1147 VCAP_PRIMITIVE_LISTS
1151 .serialize
= string_serialize
,
1152 .deserialize
= string_deserialize
,
1153 .cleanup
= string_cleanup
,
1154 .caps
= VCAP_PRIMITIVES
1159 static void add_visitor_type(const SerializeOps
*ops
)
1161 char testname_prefix
[128];
1166 sprintf(testname_prefix
, "/visitor/serialization/%s", ops
->type
);
1168 if (ops
->caps
& VCAP_PRIMITIVES
) {
1169 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1170 sprintf(testname
, "%s/primitives/%s", testname_prefix
,
1171 pt_values
[i
].description
);
1172 args
= g_malloc0(sizeof(*args
));
1174 args
->test_data
= &pt_values
[i
];
1175 g_test_add_data_func(testname
, args
, test_primitives
);
1180 if (ops
->caps
& VCAP_STRUCTURES
) {
1181 sprintf(testname
, "%s/struct", testname_prefix
);
1182 args
= g_malloc0(sizeof(*args
));
1184 args
->test_data
= NULL
;
1185 g_test_add_data_func(testname
, args
, test_struct
);
1187 sprintf(testname
, "%s/nested_struct", testname_prefix
);
1188 args
= g_malloc0(sizeof(*args
));
1190 args
->test_data
= NULL
;
1191 g_test_add_data_func(testname
, args
, test_nested_struct
);
1194 if (ops
->caps
& VCAP_LISTS
) {
1195 sprintf(testname
, "%s/nested_struct_list", testname_prefix
);
1196 args
= g_malloc0(sizeof(*args
));
1198 args
->test_data
= NULL
;
1199 g_test_add_data_func(testname
, args
, test_nested_struct_list
);
1202 if (ops
->caps
& VCAP_PRIMITIVE_LISTS
) {
1204 while (pt_values
[i
].type
!= PTYPE_EOL
) {
1205 sprintf(testname
, "%s/primitive_list/%s", testname_prefix
,
1206 pt_values
[i
].description
);
1207 args
= g_malloc0(sizeof(*args
));
1209 args
->test_data
= &pt_values
[i
];
1210 g_test_add_data_func(testname
, args
, test_primitive_lists
);
1216 int main(int argc
, char **argv
)
1220 g_test_init(&argc
, &argv
, NULL
);
1222 while (visitors
[i
].type
!= NULL
) {
1223 add_visitor_type(&visitors
[i
]);