qapi: Use QAPI_LIST_PREPEND() where possible
[qemu/ar7.git] / tests / test-visitor-serialization.c
blob12275e56d86274e0cdc341a4549f74f3ab765724
1 /*
2 * Unit-tests for visitor-based serialization
4 * Copyright (C) 2014-2015 Red Hat, Inc.
5 * Copyright IBM, Corp. 2012
7 * Authors:
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"
15 #include <float.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 {
29 PTYPE_STRING = 0,
30 PTYPE_BOOLEAN,
31 PTYPE_NUMBER,
32 PTYPE_INTEGER,
33 PTYPE_U8,
34 PTYPE_U16,
35 PTYPE_U32,
36 PTYPE_U64,
37 PTYPE_S8,
38 PTYPE_S16,
39 PTYPE_S32,
40 PTYPE_S64,
41 PTYPE_EOL,
44 typedef struct PrimitiveType {
45 union {
46 const char *string;
47 bool boolean;
48 double number;
49 int64_t integer;
50 uint8_t u8;
51 uint16_t u16;
52 uint32_t u32;
53 uint64_t u64;
54 int8_t s8;
55 int16_t s16;
56 int32_t s32;
57 int64_t s64;
58 intmax_t max;
59 } value;
60 enum PrimitiveTypeKind type;
61 const char *description;
62 } PrimitiveType;
64 typedef struct PrimitiveList {
65 union {
66 strList *strings;
67 boolList *booleans;
68 numberList *numbers;
69 intList *integers;
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;
78 } value;
79 enum PrimitiveTypeKind type;
80 const char *description;
81 } PrimitiveList;
83 /* test helpers */
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);
93 visit_free(v);
96 static void visit_primitive_type(Visitor *v, void **native, Error **errp)
98 PrimitiveType *pt = *native;
99 switch(pt->type) {
100 case PTYPE_STRING:
101 visit_type_str(v, NULL, (char **)&pt->value.string, errp);
102 break;
103 case PTYPE_BOOLEAN:
104 visit_type_bool(v, NULL, &pt->value.boolean, errp);
105 break;
106 case PTYPE_NUMBER:
107 visit_type_number(v, NULL, &pt->value.number, errp);
108 break;
109 case PTYPE_INTEGER:
110 visit_type_int(v, NULL, &pt->value.integer, errp);
111 break;
112 case PTYPE_U8:
113 visit_type_uint8(v, NULL, &pt->value.u8, errp);
114 break;
115 case PTYPE_U16:
116 visit_type_uint16(v, NULL, &pt->value.u16, errp);
117 break;
118 case PTYPE_U32:
119 visit_type_uint32(v, NULL, &pt->value.u32, errp);
120 break;
121 case PTYPE_U64:
122 visit_type_uint64(v, NULL, &pt->value.u64, errp);
123 break;
124 case PTYPE_S8:
125 visit_type_int8(v, NULL, &pt->value.s8, errp);
126 break;
127 case PTYPE_S16:
128 visit_type_int16(v, NULL, &pt->value.s16, errp);
129 break;
130 case PTYPE_S32:
131 visit_type_int32(v, NULL, &pt->value.s32, errp);
132 break;
133 case PTYPE_S64:
134 visit_type_int64(v, NULL, &pt->value.s64, errp);
135 break;
136 case PTYPE_EOL:
137 g_assert_not_reached();
141 static void visit_primitive_list(Visitor *v, void **native, Error **errp)
143 PrimitiveList *pl = *native;
144 switch (pl->type) {
145 case PTYPE_STRING:
146 visit_type_strList(v, NULL, &pl->value.strings, errp);
147 break;
148 case PTYPE_BOOLEAN:
149 visit_type_boolList(v, NULL, &pl->value.booleans, errp);
150 break;
151 case PTYPE_NUMBER:
152 visit_type_numberList(v, NULL, &pl->value.numbers, errp);
153 break;
154 case PTYPE_INTEGER:
155 visit_type_intList(v, NULL, &pl->value.integers, errp);
156 break;
157 case PTYPE_S8:
158 visit_type_int8List(v, NULL, &pl->value.s8_integers, errp);
159 break;
160 case PTYPE_S16:
161 visit_type_int16List(v, NULL, &pl->value.s16_integers, errp);
162 break;
163 case PTYPE_S32:
164 visit_type_int32List(v, NULL, &pl->value.s32_integers, errp);
165 break;
166 case PTYPE_S64:
167 visit_type_int64List(v, NULL, &pl->value.s64_integers, errp);
168 break;
169 case PTYPE_U8:
170 visit_type_uint8List(v, NULL, &pl->value.u8_integers, errp);
171 break;
172 case PTYPE_U16:
173 visit_type_uint16List(v, NULL, &pl->value.u16_integers, errp);
174 break;
175 case PTYPE_U32:
176 visit_type_uint32List(v, NULL, &pl->value.u32_integers, errp);
177 break;
178 case PTYPE_U64:
179 visit_type_uint64List(v, NULL, &pl->value.u64_integers, errp);
180 break;
181 default:
182 g_assert_not_reached();
187 static TestStruct *struct_create(void)
189 TestStruct *ts = g_malloc0(sizeof(*ts));
190 ts->integer = -42;
191 ts->boolean = true;
192 ts->string = strdup("test string");
193 return ts;
196 static void struct_compare(TestStruct *ts1, TestStruct *ts2)
198 g_assert(ts1);
199 g_assert(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)
207 g_free(ts->string);
208 g_free(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");
233 return udnp;
236 static void nested_struct_compare(UserDefTwo *udnp1, UserDefTwo *udnp2)
238 g_assert(udnp1);
239 g_assert(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);
272 /* test cases */
274 typedef enum VisitorCapabilities {
275 VCAP_PRIMITIVES = 1,
276 VCAP_STRUCTURES = 2,
277 VCAP_LISTS = 4,
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);
287 const char *type;
288 VisitorCapabilities caps;
289 } SerializeOps;
291 typedef struct TestArgs {
292 const SerializeOps *ops;
293 void *test_data;
294 } TestArgs;
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,
307 &error_abort);
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);
327 } else {
328 g_assert_cmpint(pt->value.max, ==, pt_copy->value.max);
331 ops->cleanup(serialize_data);
332 g_free(args);
333 g_free(pt_copy);
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;
346 int i;
348 pl.type = pl_copy.type = pt->type;
350 /* build up our list of primitive types */
351 for (i = 0; i < 32; i++) {
352 switch (pl.type) {
353 case PTYPE_STRING: {
354 QAPI_LIST_PREPEND(pl.value.strings, g_strdup(pt->value.string));
355 break;
357 case PTYPE_INTEGER: {
358 QAPI_LIST_PREPEND(pl.value.integers, pt->value.integer);
359 break;
361 case PTYPE_S8: {
362 QAPI_LIST_PREPEND(pl.value.s8_integers, pt->value.s8);
363 break;
365 case PTYPE_S16: {
366 QAPI_LIST_PREPEND(pl.value.s16_integers, pt->value.s16);
367 break;
369 case PTYPE_S32: {
370 QAPI_LIST_PREPEND(pl.value.s32_integers, pt->value.s32);
371 break;
373 case PTYPE_S64: {
374 QAPI_LIST_PREPEND(pl.value.s64_integers, pt->value.s64);
375 break;
377 case PTYPE_U8: {
378 QAPI_LIST_PREPEND(pl.value.u8_integers, pt->value.u8);
379 break;
381 case PTYPE_U16: {
382 QAPI_LIST_PREPEND(pl.value.u16_integers, pt->value.u16);
383 break;
385 case PTYPE_U32: {
386 QAPI_LIST_PREPEND(pl.value.u32_integers, pt->value.u32);
387 break;
389 case PTYPE_U64: {
390 QAPI_LIST_PREPEND(pl.value.u64_integers, pt->value.u64);
391 break;
393 case PTYPE_NUMBER: {
394 QAPI_LIST_PREPEND(pl.value.numbers, pt->value.number);
395 break;
397 case PTYPE_BOOLEAN: {
398 QAPI_LIST_PREPEND(pl.value.booleans, pt->value.boolean);
399 break;
401 default:
402 g_assert_not_reached();
406 ops->serialize((void **)&pl, &serialize_data, visit_primitive_list,
407 &error_abort);
408 ops->deserialize((void **)&pl_copy_ptr, serialize_data,
409 visit_primitive_list, &error_abort);
411 i = 0;
413 /* compare our deserialized list of primitives to the original */
414 do {
415 switch (pl_copy.type) {
416 case PTYPE_STRING: {
417 strList *ptr;
418 if (cur_head) {
419 ptr = cur_head;
420 cur_head = ptr->next;
421 } else {
422 cur_head = ptr = pl_copy.value.strings;
424 g_assert_cmpstr(pt->value.string, ==, ptr->value);
425 break;
427 case PTYPE_INTEGER: {
428 intList *ptr;
429 if (cur_head) {
430 ptr = cur_head;
431 cur_head = ptr->next;
432 } else {
433 cur_head = ptr = pl_copy.value.integers;
435 g_assert_cmpint(pt->value.integer, ==, ptr->value);
436 break;
438 case PTYPE_S8: {
439 int8List *ptr;
440 if (cur_head) {
441 ptr = cur_head;
442 cur_head = ptr->next;
443 } else {
444 cur_head = ptr = pl_copy.value.s8_integers;
446 g_assert_cmpint(pt->value.s8, ==, ptr->value);
447 break;
449 case PTYPE_S16: {
450 int16List *ptr;
451 if (cur_head) {
452 ptr = cur_head;
453 cur_head = ptr->next;
454 } else {
455 cur_head = ptr = pl_copy.value.s16_integers;
457 g_assert_cmpint(pt->value.s16, ==, ptr->value);
458 break;
460 case PTYPE_S32: {
461 int32List *ptr;
462 if (cur_head) {
463 ptr = cur_head;
464 cur_head = ptr->next;
465 } else {
466 cur_head = ptr = pl_copy.value.s32_integers;
468 g_assert_cmpint(pt->value.s32, ==, ptr->value);
469 break;
471 case PTYPE_S64: {
472 int64List *ptr;
473 if (cur_head) {
474 ptr = cur_head;
475 cur_head = ptr->next;
476 } else {
477 cur_head = ptr = pl_copy.value.s64_integers;
479 g_assert_cmpint(pt->value.s64, ==, ptr->value);
480 break;
482 case PTYPE_U8: {
483 uint8List *ptr;
484 if (cur_head) {
485 ptr = cur_head;
486 cur_head = ptr->next;
487 } else {
488 cur_head = ptr = pl_copy.value.u8_integers;
490 g_assert_cmpint(pt->value.u8, ==, ptr->value);
491 break;
493 case PTYPE_U16: {
494 uint16List *ptr;
495 if (cur_head) {
496 ptr = cur_head;
497 cur_head = ptr->next;
498 } else {
499 cur_head = ptr = pl_copy.value.u16_integers;
501 g_assert_cmpint(pt->value.u16, ==, ptr->value);
502 break;
504 case PTYPE_U32: {
505 uint32List *ptr;
506 if (cur_head) {
507 ptr = cur_head;
508 cur_head = ptr->next;
509 } else {
510 cur_head = ptr = pl_copy.value.u32_integers;
512 g_assert_cmpint(pt->value.u32, ==, ptr->value);
513 break;
515 case PTYPE_U64: {
516 uint64List *ptr;
517 if (cur_head) {
518 ptr = cur_head;
519 cur_head = ptr->next;
520 } else {
521 cur_head = ptr = pl_copy.value.u64_integers;
523 g_assert_cmpint(pt->value.u64, ==, ptr->value);
524 break;
526 case PTYPE_NUMBER: {
527 numberList *ptr;
528 GString *double_expected = g_string_new("");
529 GString *double_actual = g_string_new("");
530 if (cur_head) {
531 ptr = cur_head;
532 cur_head = ptr->next;
533 } else {
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
538 * formatted values
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);
545 break;
547 case PTYPE_BOOLEAN: {
548 boolList *ptr;
549 if (cur_head) {
550 ptr = cur_head;
551 cur_head = ptr->next;
552 } else {
553 cur_head = ptr = pl_copy.value.booleans;
555 g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
556 break;
558 default:
559 g_assert_not_reached();
561 i++;
562 } while (cur_head);
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);
569 g_free(args);
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,
582 &error_abort);
584 struct_compare(ts, ts_copy);
586 struct_cleanup(ts);
587 struct_cleanup(ts_copy);
589 ops->cleanup(serialize_data);
590 g_free(args);
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,
603 &error_abort);
605 nested_struct_compare(udnp, udnp_copy);
607 nested_struct_cleanup(udnp);
608 nested_struct_cleanup(udnp_copy);
610 ops->cleanup(serialize_data);
611 g_free(args);
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;
620 int i = 0;
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,
627 &error_abort);
628 ops->deserialize((void **)&listp_copy, serialize_data,
629 visit_nested_struct_list, &error_abort);
631 tmp = listp;
632 tmp_copy = listp_copy;
633 while (listp_copy) {
634 g_assert(listp);
635 nested_struct_compare(listp->value, listp_copy->value);
636 listp = listp->next;
637 listp_copy = listp_copy->next;
640 qapi_free_UserDefTwoList(tmp);
641 qapi_free_UserDefTwoList(tmp_copy);
643 ops->cleanup(serialize_data);
644 g_free(args);
647 static PrimitiveType pt_values[] = {
648 /* string tests */
650 .description = "string_empty",
651 .type = PTYPE_STRING,
652 .value.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",
679 /* boolean tests */
681 .description = "boolean_true1",
682 .type = PTYPE_BOOLEAN,
683 .value.boolean = true,
686 .description = "boolean_true2",
687 .type = PTYPE_BOOLEAN,
688 .value.boolean = 8,
691 .description = "boolean_true3",
692 .type = PTYPE_BOOLEAN,
693 .value.boolean = -1,
696 .description = "boolean_false1",
697 .type = PTYPE_BOOLEAN,
698 .value.boolean = false,
701 .description = "boolean_false2",
702 .type = PTYPE_BOOLEAN,
703 .value.boolean = 0,
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
708 * beyond that.
711 .description = "number_sanity1",
712 .type = PTYPE_NUMBER,
713 .value.number = -1,
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,
734 .value.integer = -1,
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,
751 /* uint8 tests */
753 .description = "uint8_sanity1",
754 .type = PTYPE_U8,
755 .value.u8 = 1,
758 .description = "uint8_sanity2",
759 .type = PTYPE_U8,
760 .value.u8 = UINT8_MAX / 2 + 1,
763 .description = "uint8_min",
764 .type = PTYPE_U8,
765 .value.u8 = 0,
768 .description = "uint8_max",
769 .type = PTYPE_U8,
770 .value.u8 = UINT8_MAX,
772 /* uint16 tests */
774 .description = "uint16_sanity1",
775 .type = PTYPE_U16,
776 .value.u16 = 1,
779 .description = "uint16_sanity2",
780 .type = PTYPE_U16,
781 .value.u16 = UINT16_MAX / 2 + 1,
784 .description = "uint16_min",
785 .type = PTYPE_U16,
786 .value.u16 = 0,
789 .description = "uint16_max",
790 .type = PTYPE_U16,
791 .value.u16 = UINT16_MAX,
793 /* uint32 tests */
795 .description = "uint32_sanity1",
796 .type = PTYPE_U32,
797 .value.u32 = 1,
800 .description = "uint32_sanity2",
801 .type = PTYPE_U32,
802 .value.u32 = UINT32_MAX / 2 + 1,
805 .description = "uint32_min",
806 .type = PTYPE_U32,
807 .value.u32 = 0,
810 .description = "uint32_max",
811 .type = PTYPE_U32,
812 .value.u32 = UINT32_MAX,
814 /* uint64 tests */
816 .description = "uint64_sanity1",
817 .type = PTYPE_U64,
818 .value.u64 = 1,
821 .description = "uint64_sanity2",
822 .type = PTYPE_U64,
823 .value.u64 = UINT64_MAX / 2 + 1,
826 .description = "uint64_min",
827 .type = PTYPE_U64,
828 .value.u64 = 0,
831 .description = "uint64_max",
832 .type = PTYPE_U64,
833 .value.u64 = UINT64_MAX,
835 /* int8 tests */
837 .description = "int8_sanity1",
838 .type = PTYPE_S8,
839 .value.s8 = -1,
842 .description = "int8_sanity2",
843 .type = PTYPE_S8,
844 .value.s8 = INT8_MAX / 2 + 1,
847 .description = "int8_min",
848 .type = PTYPE_S8,
849 .value.s8 = INT8_MIN,
852 .description = "int8_max",
853 .type = PTYPE_S8,
854 .value.s8 = INT8_MAX,
856 /* int16 tests */
858 .description = "int16_sanity1",
859 .type = PTYPE_S16,
860 .value.s16 = -1,
863 .description = "int16_sanity2",
864 .type = PTYPE_S16,
865 .value.s16 = INT16_MAX / 2 + 1,
868 .description = "int16_min",
869 .type = PTYPE_S16,
870 .value.s16 = INT16_MIN,
873 .description = "int16_max",
874 .type = PTYPE_S16,
875 .value.s16 = INT16_MAX,
877 /* int32 tests */
879 .description = "int32_sanity1",
880 .type = PTYPE_S32,
881 .value.s32 = -1,
884 .description = "int32_sanity2",
885 .type = PTYPE_S32,
886 .value.s32 = INT32_MAX / 2 + 1,
889 .description = "int32_min",
890 .type = PTYPE_S32,
891 .value.s32 = INT32_MIN,
894 .description = "int32_max",
895 .type = PTYPE_S32,
896 .value.s32 = INT32_MAX,
898 /* int64 tests */
900 .description = "int64_sanity1",
901 .type = PTYPE_S64,
902 .value.s64 = -1,
905 .description = "int64_sanity2",
906 .type = PTYPE_S64,
907 .value.s64 = INT64_MAX / 2 + 1,
910 .description = "int64_min",
911 .type = PTYPE_S64,
912 .value.s64 = INT64_MIN,
915 .description = "int64_max",
916 .type = PTYPE_S64,
917 .value.s64 = INT64_MAX,
919 { .type = PTYPE_EOL }
922 /* visitor-specific op implementations */
924 typedef struct QmpSerializeData {
925 Visitor *qov;
926 QObject *obj;
927 Visitor *qiv;
928 } 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);
937 *datap = d;
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);
948 obj_orig = 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);
955 qobject_unref(obj);
956 visit(d->qiv, native_out, errp);
959 static void qmp_cleanup(void *datap)
961 QmpSerializeData *d = datap;
962 visit_free(d->qov);
963 visit_free(d->qiv);
965 g_free(d);
968 typedef struct StringSerializeData {
969 char *string;
970 Visitor *sov;
971 Visitor *siv;
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);
981 *datap = d;
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;
998 visit_free(d->sov);
999 visit_free(d->siv);
1000 g_free(d->string);
1001 g_free(d);
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[] = {
1012 .type = "QMP",
1013 .serialize = qmp_serialize,
1014 .deserialize = qmp_deserialize,
1015 .cleanup = qmp_cleanup,
1016 .caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
1017 VCAP_PRIMITIVE_LISTS
1020 .type = "String",
1021 .serialize = string_serialize,
1022 .deserialize = string_deserialize,
1023 .cleanup = string_cleanup,
1024 .caps = VCAP_PRIMITIVES
1026 { NULL }
1029 static void add_visitor_type(const SerializeOps *ops)
1031 char testname_prefix[32];
1032 char testname[128];
1033 TestArgs *args;
1034 int i = 0;
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));
1043 args->ops = ops;
1044 args->test_data = &pt_values[i];
1045 g_test_add_data_func(testname, args, test_primitives);
1046 i++;
1050 if (ops->caps & VCAP_STRUCTURES) {
1051 sprintf(testname, "%s/struct", testname_prefix);
1052 args = g_malloc0(sizeof(*args));
1053 args->ops = ops;
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));
1059 args->ops = ops;
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));
1067 args->ops = ops;
1068 args->test_data = NULL;
1069 g_test_add_data_func(testname, args, test_nested_struct_list);
1072 if (ops->caps & VCAP_PRIMITIVE_LISTS) {
1073 i = 0;
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));
1078 args->ops = ops;
1079 args->test_data = &pt_values[i];
1080 g_test_add_data_func(testname, args, test_primitive_lists);
1081 i++;
1086 int main(int argc, char **argv)
1088 int i = 0;
1090 g_test_init(&argc, &argv, NULL);
1092 while (visitors[i].type != NULL) {
1093 add_visitor_type(&visitors[i]);
1094 i++;
1097 g_test_run();
1099 return 0;