tests/avocado: Disable the test_sbsaref_edk2_firmware by default
[qemu/kevin.git] / tests / unit / test-visitor-serialization.c
blobc2056c3eaa834f7311e30ea225c07340ea4f3dbf
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 "test-qapi-visit.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qapi/qobject-input-visitor.h"
22 #include "qapi/qobject-output-visitor.h"
23 #include "qapi/string-input-visitor.h"
24 #include "qapi/string-output-visitor.h"
25 #include "qapi/dealloc-visitor.h"
27 enum PrimitiveTypeKind {
28 PTYPE_STRING = 0,
29 PTYPE_BOOLEAN,
30 PTYPE_NUMBER,
31 PTYPE_INTEGER,
32 PTYPE_U8,
33 PTYPE_U16,
34 PTYPE_U32,
35 PTYPE_U64,
36 PTYPE_S8,
37 PTYPE_S16,
38 PTYPE_S32,
39 PTYPE_S64,
40 PTYPE_EOL,
43 typedef struct PrimitiveType {
44 union {
45 const char *string;
46 bool boolean;
47 double number;
48 int64_t integer;
49 uint8_t u8;
50 uint16_t u16;
51 uint32_t u32;
52 uint64_t u64;
53 int8_t s8;
54 int16_t s16;
55 int32_t s32;
56 int64_t s64;
57 } value;
58 enum PrimitiveTypeKind type;
59 const char *description;
60 } PrimitiveType;
62 typedef struct PrimitiveList {
63 union {
64 strList *strings;
65 boolList *booleans;
66 numberList *numbers;
67 intList *integers;
68 int8List *s8_integers;
69 int16List *s16_integers;
70 int32List *s32_integers;
71 int64List *s64_integers;
72 uint8List *u8_integers;
73 uint16List *u16_integers;
74 uint32List *u32_integers;
75 uint64List *u64_integers;
76 } value;
77 enum PrimitiveTypeKind type;
78 const char *description;
79 } PrimitiveList;
81 /* test helpers */
83 typedef void (*VisitorFunc)(Visitor *v, void **native, Error **errp);
85 static void dealloc_helper(void *native_in, VisitorFunc visit, Error **errp)
87 Visitor *v = qapi_dealloc_visitor_new();
89 visit(v, &native_in, errp);
91 visit_free(v);
94 static void visit_primitive_type(Visitor *v, void **native, Error **errp)
96 PrimitiveType *pt = *native;
97 switch(pt->type) {
98 case PTYPE_STRING:
99 visit_type_str(v, NULL, (char **)&pt->value.string, errp);
100 break;
101 case PTYPE_BOOLEAN:
102 visit_type_bool(v, NULL, &pt->value.boolean, errp);
103 break;
104 case PTYPE_NUMBER:
105 visit_type_number(v, NULL, &pt->value.number, errp);
106 break;
107 case PTYPE_INTEGER:
108 visit_type_int(v, NULL, &pt->value.integer, errp);
109 break;
110 case PTYPE_U8:
111 visit_type_uint8(v, NULL, &pt->value.u8, errp);
112 break;
113 case PTYPE_U16:
114 visit_type_uint16(v, NULL, &pt->value.u16, errp);
115 break;
116 case PTYPE_U32:
117 visit_type_uint32(v, NULL, &pt->value.u32, errp);
118 break;
119 case PTYPE_U64:
120 visit_type_uint64(v, NULL, &pt->value.u64, errp);
121 break;
122 case PTYPE_S8:
123 visit_type_int8(v, NULL, &pt->value.s8, errp);
124 break;
125 case PTYPE_S16:
126 visit_type_int16(v, NULL, &pt->value.s16, errp);
127 break;
128 case PTYPE_S32:
129 visit_type_int32(v, NULL, &pt->value.s32, errp);
130 break;
131 case PTYPE_S64:
132 visit_type_int64(v, NULL, &pt->value.s64, errp);
133 break;
134 case PTYPE_EOL:
135 g_assert_not_reached();
139 static void visit_primitive_list(Visitor *v, void **native, Error **errp)
141 PrimitiveList *pl = *native;
142 switch (pl->type) {
143 case PTYPE_STRING:
144 visit_type_strList(v, NULL, &pl->value.strings, errp);
145 break;
146 case PTYPE_BOOLEAN:
147 visit_type_boolList(v, NULL, &pl->value.booleans, errp);
148 break;
149 case PTYPE_NUMBER:
150 visit_type_numberList(v, NULL, &pl->value.numbers, errp);
151 break;
152 case PTYPE_INTEGER:
153 visit_type_intList(v, NULL, &pl->value.integers, errp);
154 break;
155 case PTYPE_S8:
156 visit_type_int8List(v, NULL, &pl->value.s8_integers, errp);
157 break;
158 case PTYPE_S16:
159 visit_type_int16List(v, NULL, &pl->value.s16_integers, errp);
160 break;
161 case PTYPE_S32:
162 visit_type_int32List(v, NULL, &pl->value.s32_integers, errp);
163 break;
164 case PTYPE_S64:
165 visit_type_int64List(v, NULL, &pl->value.s64_integers, errp);
166 break;
167 case PTYPE_U8:
168 visit_type_uint8List(v, NULL, &pl->value.u8_integers, errp);
169 break;
170 case PTYPE_U16:
171 visit_type_uint16List(v, NULL, &pl->value.u16_integers, errp);
172 break;
173 case PTYPE_U32:
174 visit_type_uint32List(v, NULL, &pl->value.u32_integers, errp);
175 break;
176 case PTYPE_U64:
177 visit_type_uint64List(v, NULL, &pl->value.u64_integers, errp);
178 break;
179 default:
180 g_assert_not_reached();
185 static TestStruct *struct_create(void)
187 TestStruct *ts = g_malloc0(sizeof(*ts));
188 ts->integer = -42;
189 ts->boolean = true;
190 ts->string = strdup("test string");
191 return ts;
194 static void struct_compare(TestStruct *ts1, TestStruct *ts2)
196 g_assert(ts1);
197 g_assert(ts2);
198 g_assert_cmpint(ts1->integer, ==, ts2->integer);
199 g_assert(ts1->boolean == ts2->boolean);
200 g_assert_cmpstr(ts1->string, ==, ts2->string);
203 static void struct_cleanup(TestStruct *ts)
205 g_free(ts->string);
206 g_free(ts);
209 static void visit_struct(Visitor *v, void **native, Error **errp)
211 visit_type_TestStruct(v, NULL, (TestStruct **)native, errp);
214 static UserDefTwo *nested_struct_create(void)
216 UserDefTwo *udnp = g_malloc0(sizeof(*udnp));
217 udnp->string0 = strdup("test_string0");
218 udnp->dict1 = g_malloc0(sizeof(*udnp->dict1));
219 udnp->dict1->string1 = strdup("test_string1");
220 udnp->dict1->dict2 = g_malloc0(sizeof(*udnp->dict1->dict2));
221 udnp->dict1->dict2->userdef = g_new0(UserDefOne, 1);
222 udnp->dict1->dict2->userdef->integer = 42;
223 udnp->dict1->dict2->userdef->string = strdup("test_string");
224 udnp->dict1->dict2->string = strdup("test_string2");
225 udnp->dict1->dict3 = g_malloc0(sizeof(*udnp->dict1->dict3));
226 udnp->dict1->dict3->userdef = g_new0(UserDefOne, 1);
227 udnp->dict1->dict3->userdef->integer = 43;
228 udnp->dict1->dict3->userdef->string = strdup("test_string");
229 udnp->dict1->dict3->string = strdup("test_string3");
230 return udnp;
233 static void nested_struct_compare(UserDefTwo *udnp1, UserDefTwo *udnp2)
235 g_assert(udnp1);
236 g_assert(udnp2);
237 g_assert_cmpstr(udnp1->string0, ==, udnp2->string0);
238 g_assert_cmpstr(udnp1->dict1->string1, ==, udnp2->dict1->string1);
239 g_assert_cmpint(udnp1->dict1->dict2->userdef->integer, ==,
240 udnp2->dict1->dict2->userdef->integer);
241 g_assert_cmpstr(udnp1->dict1->dict2->userdef->string, ==,
242 udnp2->dict1->dict2->userdef->string);
243 g_assert_cmpstr(udnp1->dict1->dict2->string, ==,
244 udnp2->dict1->dict2->string);
245 g_assert(!udnp1->dict1->dict3 == !udnp2->dict1->dict3);
246 g_assert_cmpint(udnp1->dict1->dict3->userdef->integer, ==,
247 udnp2->dict1->dict3->userdef->integer);
248 g_assert_cmpstr(udnp1->dict1->dict3->userdef->string, ==,
249 udnp2->dict1->dict3->userdef->string);
250 g_assert_cmpstr(udnp1->dict1->dict3->string, ==,
251 udnp2->dict1->dict3->string);
254 static void nested_struct_cleanup(UserDefTwo *udnp)
256 qapi_free_UserDefTwo(udnp);
259 static void visit_nested_struct(Visitor *v, void **native, Error **errp)
261 visit_type_UserDefTwo(v, NULL, (UserDefTwo **)native, errp);
264 static void visit_nested_struct_list(Visitor *v, void **native, Error **errp)
266 visit_type_UserDefTwoList(v, NULL, (UserDefTwoList **)native, errp);
269 /* test cases */
271 typedef enum VisitorCapabilities {
272 VCAP_PRIMITIVES = 1,
273 VCAP_STRUCTURES = 2,
274 VCAP_LISTS = 4,
275 VCAP_PRIMITIVE_LISTS = 8,
276 } VisitorCapabilities;
278 typedef struct SerializeOps {
279 void (*serialize)(void *native_in, void **datap,
280 VisitorFunc visit, Error **errp);
281 void (*deserialize)(void **native_out, void *datap,
282 VisitorFunc visit, Error **errp);
283 void (*cleanup)(void *datap);
284 const char *type;
285 VisitorCapabilities caps;
286 } SerializeOps;
288 typedef struct TestArgs {
289 const SerializeOps *ops;
290 void *test_data;
291 } TestArgs;
293 static void test_primitives(gconstpointer opaque)
295 TestArgs *args = (TestArgs *) opaque;
296 const SerializeOps *ops = args->ops;
297 PrimitiveType *pt = args->test_data;
298 PrimitiveType *pt_copy = g_malloc0(sizeof(*pt_copy));
299 void *serialize_data;
301 pt_copy->type = pt->type;
302 ops->serialize(pt, &serialize_data, visit_primitive_type, &error_abort);
303 ops->deserialize((void **)&pt_copy, serialize_data, visit_primitive_type,
304 &error_abort);
306 g_assert(pt_copy != NULL);
307 switch (pt->type) {
308 case PTYPE_STRING:
309 g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
310 g_free((char *)pt_copy->value.string);
311 break;
312 case PTYPE_BOOLEAN:
313 g_assert_cmpint(pt->value.boolean, ==, pt->value.boolean);
314 break;
315 case PTYPE_NUMBER:
316 g_assert_cmpfloat(pt->value.number, ==, pt_copy->value.number);
317 break;
318 case PTYPE_INTEGER:
319 g_assert_cmpint(pt->value.integer, ==, pt_copy->value.integer);
320 break;
321 case PTYPE_U8:
322 g_assert_cmpuint(pt->value.u8, ==, pt_copy->value.u8);
323 break;
324 case PTYPE_U16:
325 g_assert_cmpuint(pt->value.u16, ==, pt_copy->value.u16);
326 break;
327 case PTYPE_U32:
328 g_assert_cmpuint(pt->value.u32, ==, pt_copy->value.u32);
329 break;
330 case PTYPE_U64:
331 g_assert_cmpuint(pt->value.u64, ==, pt_copy->value.u64);
332 break;
333 case PTYPE_S8:
334 g_assert_cmpint(pt->value.s8, ==, pt_copy->value.s8);
335 break;
336 case PTYPE_S16:
337 g_assert_cmpint(pt->value.s16, ==, pt_copy->value.s16);
338 break;
339 case PTYPE_S32:
340 g_assert_cmpint(pt->value.s32, ==, pt_copy->value.s32);
341 break;
342 case PTYPE_S64:
343 g_assert_cmpint(pt->value.s64, ==, pt_copy->value.s64);
344 break;
345 case PTYPE_EOL:
346 g_assert_not_reached();
349 ops->cleanup(serialize_data);
350 g_free(args);
351 g_free(pt_copy);
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 = { NULL } };
360 PrimitiveList pl_copy = { .value = { NULL } };
361 PrimitiveList *pl_copy_ptr = &pl_copy;
362 void *serialize_data;
363 void *cur_head = NULL;
364 int i;
366 pl.type = pl_copy.type = pt->type;
368 /* build up our list of primitive types */
369 for (i = 0; i < 32; i++) {
370 switch (pl.type) {
371 case PTYPE_STRING: {
372 QAPI_LIST_PREPEND(pl.value.strings, g_strdup(pt->value.string));
373 break;
375 case PTYPE_INTEGER: {
376 QAPI_LIST_PREPEND(pl.value.integers, pt->value.integer);
377 break;
379 case PTYPE_S8: {
380 QAPI_LIST_PREPEND(pl.value.s8_integers, pt->value.s8);
381 break;
383 case PTYPE_S16: {
384 QAPI_LIST_PREPEND(pl.value.s16_integers, pt->value.s16);
385 break;
387 case PTYPE_S32: {
388 QAPI_LIST_PREPEND(pl.value.s32_integers, pt->value.s32);
389 break;
391 case PTYPE_S64: {
392 QAPI_LIST_PREPEND(pl.value.s64_integers, pt->value.s64);
393 break;
395 case PTYPE_U8: {
396 QAPI_LIST_PREPEND(pl.value.u8_integers, pt->value.u8);
397 break;
399 case PTYPE_U16: {
400 QAPI_LIST_PREPEND(pl.value.u16_integers, pt->value.u16);
401 break;
403 case PTYPE_U32: {
404 QAPI_LIST_PREPEND(pl.value.u32_integers, pt->value.u32);
405 break;
407 case PTYPE_U64: {
408 QAPI_LIST_PREPEND(pl.value.u64_integers, pt->value.u64);
409 break;
411 case PTYPE_NUMBER: {
412 QAPI_LIST_PREPEND(pl.value.numbers, pt->value.number);
413 break;
415 case PTYPE_BOOLEAN: {
416 QAPI_LIST_PREPEND(pl.value.booleans, pt->value.boolean);
417 break;
419 default:
420 g_assert_not_reached();
424 ops->serialize((void **)&pl, &serialize_data, visit_primitive_list,
425 &error_abort);
426 ops->deserialize((void **)&pl_copy_ptr, serialize_data,
427 visit_primitive_list, &error_abort);
430 switch (pl_copy.type) {
431 case PTYPE_STRING:
432 cur_head = pl_copy.value.strings;
433 break;
434 case PTYPE_INTEGER:
435 cur_head = pl_copy.value.integers;
436 break;
437 case PTYPE_S8:
438 cur_head = pl_copy.value.s8_integers;
439 break;
440 case PTYPE_S16:
441 cur_head = pl_copy.value.s16_integers;
442 break;
443 case PTYPE_S32:
444 cur_head = pl_copy.value.s32_integers;
445 break;
446 case PTYPE_S64:
447 cur_head = pl_copy.value.s64_integers;
448 break;
449 case PTYPE_U8:
450 cur_head = pl_copy.value.u8_integers;
451 break;
452 case PTYPE_U16:
453 cur_head = pl_copy.value.u16_integers;
454 break;
455 case PTYPE_U32:
456 cur_head = pl_copy.value.u32_integers;
457 break;
458 case PTYPE_U64:
459 cur_head = pl_copy.value.u64_integers;
460 break;
461 case PTYPE_NUMBER:
462 cur_head = pl_copy.value.numbers;
463 break;
464 case PTYPE_BOOLEAN:
465 cur_head = pl_copy.value.booleans;
466 break;
467 default:
468 g_assert_not_reached();
471 /* compare our deserialized list of primitives to the original */
472 i = 0;
473 while (cur_head) {
474 switch (pl_copy.type) {
475 case PTYPE_STRING: {
476 strList *ptr = cur_head;
477 cur_head = ptr->next;
478 g_assert_cmpstr(pt->value.string, ==, ptr->value);
479 break;
481 case PTYPE_INTEGER: {
482 intList *ptr = cur_head;
483 cur_head = ptr->next;
484 g_assert_cmpint(pt->value.integer, ==, ptr->value);
485 break;
487 case PTYPE_S8: {
488 int8List *ptr = cur_head;
489 cur_head = ptr->next;
490 g_assert_cmpint(pt->value.s8, ==, ptr->value);
491 break;
493 case PTYPE_S16: {
494 int16List *ptr = cur_head;
495 cur_head = ptr->next;
496 g_assert_cmpint(pt->value.s16, ==, ptr->value);
497 break;
499 case PTYPE_S32: {
500 int32List *ptr = cur_head;
501 cur_head = ptr->next;
502 g_assert_cmpint(pt->value.s32, ==, ptr->value);
503 break;
505 case PTYPE_S64: {
506 int64List *ptr = cur_head;
507 cur_head = ptr->next;
508 g_assert_cmpint(pt->value.s64, ==, ptr->value);
509 break;
511 case PTYPE_U8: {
512 uint8List *ptr = cur_head;
513 cur_head = ptr->next;
514 g_assert_cmpint(pt->value.u8, ==, ptr->value);
515 break;
517 case PTYPE_U16: {
518 uint16List *ptr = cur_head;
519 cur_head = ptr->next;
520 g_assert_cmpint(pt->value.u16, ==, ptr->value);
521 break;
523 case PTYPE_U32: {
524 uint32List *ptr = cur_head;
525 cur_head = ptr->next;
526 g_assert_cmpint(pt->value.u32, ==, ptr->value);
527 break;
529 case PTYPE_U64: {
530 uint64List *ptr = cur_head;
531 cur_head = ptr->next;
532 g_assert_cmpint(pt->value.u64, ==, ptr->value);
533 break;
535 case PTYPE_NUMBER: {
536 GString *double_expected = g_string_new("");
537 GString *double_actual = g_string_new("");
538 numberList *ptr = cur_head;
539 cur_head = ptr->next;
540 /* we serialize with %f for our reference visitors, so rather than
541 * fuzzy floating math to test "equality", just compare the
542 * formatted values
544 g_string_printf(double_expected, "%.6f", pt->value.number);
545 g_string_printf(double_actual, "%.6f", ptr->value);
546 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
547 g_string_free(double_expected, true);
548 g_string_free(double_actual, true);
549 break;
551 case PTYPE_BOOLEAN: {
552 boolList *ptr = cur_head;
553 cur_head = ptr->next;
554 g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
555 break;
557 default:
558 g_assert_not_reached();
560 i++;
563 g_assert_cmpint(i, ==, 32);
565 ops->cleanup(serialize_data);
566 dealloc_helper(&pl, visit_primitive_list, &error_abort);
567 dealloc_helper(&pl_copy, visit_primitive_list, &error_abort);
568 g_free(args);
571 static void test_struct(gconstpointer opaque)
573 TestArgs *args = (TestArgs *) opaque;
574 const SerializeOps *ops = args->ops;
575 TestStruct *ts = struct_create();
576 TestStruct *ts_copy = NULL;
577 void *serialize_data;
579 ops->serialize(ts, &serialize_data, visit_struct, &error_abort);
580 ops->deserialize((void **)&ts_copy, serialize_data, visit_struct,
581 &error_abort);
583 struct_compare(ts, ts_copy);
585 struct_cleanup(ts);
586 struct_cleanup(ts_copy);
588 ops->cleanup(serialize_data);
589 g_free(args);
592 static void test_nested_struct(gconstpointer opaque)
594 TestArgs *args = (TestArgs *) opaque;
595 const SerializeOps *ops = args->ops;
596 UserDefTwo *udnp = nested_struct_create();
597 UserDefTwo *udnp_copy = NULL;
598 void *serialize_data;
600 ops->serialize(udnp, &serialize_data, visit_nested_struct, &error_abort);
601 ops->deserialize((void **)&udnp_copy, serialize_data, visit_nested_struct,
602 &error_abort);
604 nested_struct_compare(udnp, udnp_copy);
606 nested_struct_cleanup(udnp);
607 nested_struct_cleanup(udnp_copy);
609 ops->cleanup(serialize_data);
610 g_free(args);
613 static void test_nested_struct_list(gconstpointer opaque)
615 TestArgs *args = (TestArgs *) opaque;
616 const SerializeOps *ops = args->ops;
617 UserDefTwoList *listp = NULL, *tmp, *tmp_copy, *listp_copy = NULL;
618 void *serialize_data;
619 int i = 0;
621 for (i = 0; i < 8; i++) {
622 QAPI_LIST_PREPEND(listp, nested_struct_create());
625 ops->serialize(listp, &serialize_data, visit_nested_struct_list,
626 &error_abort);
627 ops->deserialize((void **)&listp_copy, serialize_data,
628 visit_nested_struct_list, &error_abort);
630 tmp = listp;
631 tmp_copy = listp_copy;
632 while (listp_copy) {
633 g_assert(listp);
634 nested_struct_compare(listp->value, listp_copy->value);
635 listp = listp->next;
636 listp_copy = listp_copy->next;
639 qapi_free_UserDefTwoList(tmp);
640 qapi_free_UserDefTwoList(tmp_copy);
642 ops->cleanup(serialize_data);
643 g_free(args);
646 static PrimitiveType pt_values[] = {
647 /* string tests */
649 .description = "string_empty",
650 .type = PTYPE_STRING,
651 .value.string = "",
654 .description = "string_whitespace",
655 .type = PTYPE_STRING,
656 .value.string = "a b c\td",
659 .description = "string_newlines",
660 .type = PTYPE_STRING,
661 .value.string = "a\nb\n",
664 .description = "string_commas",
665 .type = PTYPE_STRING,
666 .value.string = "a,b, c,d",
669 .description = "string_single_quoted",
670 .type = PTYPE_STRING,
671 .value.string = "'a b',cd",
674 .description = "string_double_quoted",
675 .type = PTYPE_STRING,
676 .value.string = "\"a b\",cd",
678 /* boolean tests */
680 .description = "boolean_true1",
681 .type = PTYPE_BOOLEAN,
682 .value.boolean = true,
685 .description = "boolean_true2",
686 .type = PTYPE_BOOLEAN,
687 .value.boolean = 8,
690 .description = "boolean_true3",
691 .type = PTYPE_BOOLEAN,
692 .value.boolean = -1,
695 .description = "boolean_false1",
696 .type = PTYPE_BOOLEAN,
697 .value.boolean = false,
700 .description = "boolean_false2",
701 .type = PTYPE_BOOLEAN,
702 .value.boolean = 0,
704 /* number tests (double) */
706 .description = "number_sanity1",
707 .type = PTYPE_NUMBER,
708 .value.number = -1,
711 .description = "number_sanity2",
712 .type = PTYPE_NUMBER,
713 .value.number = 3.141593,
716 .description = "number_min",
717 .type = PTYPE_NUMBER,
718 .value.number = DBL_MIN,
721 .description = "number_max",
722 .type = PTYPE_NUMBER,
723 .value.number = DBL_MAX,
725 /* integer tests (int64) */
727 .description = "integer_sanity1",
728 .type = PTYPE_INTEGER,
729 .value.integer = -1,
732 .description = "integer_sanity2",
733 .type = PTYPE_INTEGER,
734 .value.integer = INT64_MAX / 2 + 1,
737 .description = "integer_min",
738 .type = PTYPE_INTEGER,
739 .value.integer = INT64_MIN,
742 .description = "integer_max",
743 .type = PTYPE_INTEGER,
744 .value.integer = INT64_MAX,
746 /* uint8 tests */
748 .description = "uint8_sanity1",
749 .type = PTYPE_U8,
750 .value.u8 = 1,
753 .description = "uint8_sanity2",
754 .type = PTYPE_U8,
755 .value.u8 = UINT8_MAX / 2 + 1,
758 .description = "uint8_min",
759 .type = PTYPE_U8,
760 .value.u8 = 0,
763 .description = "uint8_max",
764 .type = PTYPE_U8,
765 .value.u8 = UINT8_MAX,
767 /* uint16 tests */
769 .description = "uint16_sanity1",
770 .type = PTYPE_U16,
771 .value.u16 = 1,
774 .description = "uint16_sanity2",
775 .type = PTYPE_U16,
776 .value.u16 = UINT16_MAX / 2 + 1,
779 .description = "uint16_min",
780 .type = PTYPE_U16,
781 .value.u16 = 0,
784 .description = "uint16_max",
785 .type = PTYPE_U16,
786 .value.u16 = UINT16_MAX,
788 /* uint32 tests */
790 .description = "uint32_sanity1",
791 .type = PTYPE_U32,
792 .value.u32 = 1,
795 .description = "uint32_sanity2",
796 .type = PTYPE_U32,
797 .value.u32 = UINT32_MAX / 2 + 1,
800 .description = "uint32_min",
801 .type = PTYPE_U32,
802 .value.u32 = 0,
805 .description = "uint32_max",
806 .type = PTYPE_U32,
807 .value.u32 = UINT32_MAX,
809 /* uint64 tests */
811 .description = "uint64_sanity1",
812 .type = PTYPE_U64,
813 .value.u64 = 1,
816 .description = "uint64_sanity2",
817 .type = PTYPE_U64,
818 .value.u64 = UINT64_MAX / 2 + 1,
821 .description = "uint64_min",
822 .type = PTYPE_U64,
823 .value.u64 = 0,
826 .description = "uint64_max",
827 .type = PTYPE_U64,
828 .value.u64 = UINT64_MAX,
830 /* int8 tests */
832 .description = "int8_sanity1",
833 .type = PTYPE_S8,
834 .value.s8 = -1,
837 .description = "int8_sanity2",
838 .type = PTYPE_S8,
839 .value.s8 = INT8_MAX / 2 + 1,
842 .description = "int8_min",
843 .type = PTYPE_S8,
844 .value.s8 = INT8_MIN,
847 .description = "int8_max",
848 .type = PTYPE_S8,
849 .value.s8 = INT8_MAX,
851 /* int16 tests */
853 .description = "int16_sanity1",
854 .type = PTYPE_S16,
855 .value.s16 = -1,
858 .description = "int16_sanity2",
859 .type = PTYPE_S16,
860 .value.s16 = INT16_MAX / 2 + 1,
863 .description = "int16_min",
864 .type = PTYPE_S16,
865 .value.s16 = INT16_MIN,
868 .description = "int16_max",
869 .type = PTYPE_S16,
870 .value.s16 = INT16_MAX,
872 /* int32 tests */
874 .description = "int32_sanity1",
875 .type = PTYPE_S32,
876 .value.s32 = -1,
879 .description = "int32_sanity2",
880 .type = PTYPE_S32,
881 .value.s32 = INT32_MAX / 2 + 1,
884 .description = "int32_min",
885 .type = PTYPE_S32,
886 .value.s32 = INT32_MIN,
889 .description = "int32_max",
890 .type = PTYPE_S32,
891 .value.s32 = INT32_MAX,
893 /* int64 tests */
895 .description = "int64_sanity1",
896 .type = PTYPE_S64,
897 .value.s64 = -1,
900 .description = "int64_sanity2",
901 .type = PTYPE_S64,
902 .value.s64 = INT64_MAX / 2 + 1,
905 .description = "int64_min",
906 .type = PTYPE_S64,
907 .value.s64 = INT64_MIN,
910 .description = "int64_max",
911 .type = PTYPE_S64,
912 .value.s64 = INT64_MAX,
914 { .type = PTYPE_EOL }
917 /* visitor-specific op implementations */
919 typedef struct QmpSerializeData {
920 Visitor *qov;
921 QObject *obj;
922 Visitor *qiv;
923 } QmpSerializeData;
925 static void qmp_serialize(void *native_in, void **datap,
926 VisitorFunc visit, Error **errp)
928 QmpSerializeData *d = g_malloc0(sizeof(*d));
930 d->qov = qobject_output_visitor_new(&d->obj);
931 visit(d->qov, &native_in, errp);
932 *datap = d;
935 static void qmp_deserialize(void **native_out, void *datap,
936 VisitorFunc visit, Error **errp)
938 QmpSerializeData *d = datap;
939 GString *output_json;
940 QObject *obj_orig, *obj;
942 visit_complete(d->qov, &d->obj);
943 obj_orig = d->obj;
944 output_json = qobject_to_json(obj_orig);
945 obj = qobject_from_json(output_json->str, &error_abort);
947 g_string_free(output_json, true);
948 d->qiv = qobject_input_visitor_new(obj);
949 qobject_unref(obj_orig);
950 qobject_unref(obj);
951 visit(d->qiv, native_out, errp);
954 static void qmp_cleanup(void *datap)
956 QmpSerializeData *d = datap;
957 visit_free(d->qov);
958 visit_free(d->qiv);
960 g_free(d);
963 typedef struct StringSerializeData {
964 char *string;
965 Visitor *sov;
966 Visitor *siv;
967 } StringSerializeData;
969 static void string_serialize(void *native_in, void **datap,
970 VisitorFunc visit, Error **errp)
972 StringSerializeData *d = g_malloc0(sizeof(*d));
974 d->sov = string_output_visitor_new(false, &d->string);
975 visit(d->sov, &native_in, errp);
976 *datap = d;
979 static void string_deserialize(void **native_out, void *datap,
980 VisitorFunc visit, Error **errp)
982 StringSerializeData *d = datap;
984 visit_complete(d->sov, &d->string);
985 d->siv = string_input_visitor_new(d->string);
986 visit(d->siv, native_out, errp);
989 static void string_cleanup(void *datap)
991 StringSerializeData *d = datap;
993 visit_free(d->sov);
994 visit_free(d->siv);
995 g_free(d->string);
996 g_free(d);
999 /* visitor registration, test harness */
1001 /* note: to function interchangeably as a serialization mechanism your
1002 * visitor test implementation should pass the test cases for all visitor
1003 * capabilities: primitives, structures, and lists
1005 static const SerializeOps visitors[] = {
1007 .type = "QMP",
1008 .serialize = qmp_serialize,
1009 .deserialize = qmp_deserialize,
1010 .cleanup = qmp_cleanup,
1011 .caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
1012 VCAP_PRIMITIVE_LISTS
1015 .type = "String",
1016 .serialize = string_serialize,
1017 .deserialize = string_deserialize,
1018 .cleanup = string_cleanup,
1019 .caps = VCAP_PRIMITIVES
1021 { NULL }
1024 static void add_visitor_type(const SerializeOps *ops)
1026 char testname_prefix[32];
1027 char testname[128];
1028 TestArgs *args;
1029 int i = 0;
1031 sprintf(testname_prefix, "/visitor/serialization/%s", ops->type);
1033 if (ops->caps & VCAP_PRIMITIVES) {
1034 while (pt_values[i].type != PTYPE_EOL) {
1035 sprintf(testname, "%s/primitives/%s", testname_prefix,
1036 pt_values[i].description);
1037 args = g_malloc0(sizeof(*args));
1038 args->ops = ops;
1039 args->test_data = &pt_values[i];
1040 g_test_add_data_func(testname, args, test_primitives);
1041 i++;
1045 if (ops->caps & VCAP_STRUCTURES) {
1046 sprintf(testname, "%s/struct", testname_prefix);
1047 args = g_malloc0(sizeof(*args));
1048 args->ops = ops;
1049 args->test_data = NULL;
1050 g_test_add_data_func(testname, args, test_struct);
1052 sprintf(testname, "%s/nested_struct", testname_prefix);
1053 args = g_malloc0(sizeof(*args));
1054 args->ops = ops;
1055 args->test_data = NULL;
1056 g_test_add_data_func(testname, args, test_nested_struct);
1059 if (ops->caps & VCAP_LISTS) {
1060 sprintf(testname, "%s/nested_struct_list", testname_prefix);
1061 args = g_malloc0(sizeof(*args));
1062 args->ops = ops;
1063 args->test_data = NULL;
1064 g_test_add_data_func(testname, args, test_nested_struct_list);
1067 if (ops->caps & VCAP_PRIMITIVE_LISTS) {
1068 i = 0;
1069 while (pt_values[i].type != PTYPE_EOL) {
1070 sprintf(testname, "%s/primitive_list/%s", testname_prefix,
1071 pt_values[i].description);
1072 args = g_malloc0(sizeof(*args));
1073 args->ops = ops;
1074 args->test_data = &pt_values[i];
1075 g_test_add_data_func(testname, args, test_primitive_lists);
1076 i++;
1081 int main(int argc, char **argv)
1083 int i = 0;
1085 g_test_init(&argc, &argv, NULL);
1087 while (visitors[i].type != NULL) {
1088 add_visitor_type(&visitors[i]);
1089 i++;
1092 g_test_run();
1094 return 0;