linux-user/syscall: Replace g_malloc0 + memcpy with g_memdup
[qemu/ar7.git] / tests / test-visitor-serialization.c
blob634563bae4c2748cd9206420a16bd13c56b0645c
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 <glib.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <float.h>
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 {
32 PTYPE_STRING = 0,
33 PTYPE_BOOLEAN,
34 PTYPE_NUMBER,
35 PTYPE_INTEGER,
36 PTYPE_U8,
37 PTYPE_U16,
38 PTYPE_U32,
39 PTYPE_U64,
40 PTYPE_S8,
41 PTYPE_S16,
42 PTYPE_S32,
43 PTYPE_S64,
44 PTYPE_EOL,
47 typedef struct PrimitiveType {
48 union {
49 const char *string;
50 bool boolean;
51 double number;
52 int64_t integer;
53 uint8_t u8;
54 uint16_t u16;
55 uint32_t u32;
56 uint64_t u64;
57 int8_t s8;
58 int16_t s16;
59 int32_t s32;
60 int64_t s64;
61 intmax_t max;
62 } value;
63 enum PrimitiveTypeKind type;
64 const char *description;
65 } PrimitiveType;
67 typedef struct PrimitiveList {
68 union {
69 strList *strings;
70 boolList *booleans;
71 numberList *numbers;
72 intList *integers;
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;
81 } value;
82 enum PrimitiveTypeKind type;
83 const char *description;
84 } PrimitiveList;
86 /* test helpers */
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;
102 switch(pt->type) {
103 case PTYPE_STRING:
104 visit_type_str(v, (char **)&pt->value.string, NULL, errp);
105 break;
106 case PTYPE_BOOLEAN:
107 visit_type_bool(v, &pt->value.boolean, NULL, errp);
108 break;
109 case PTYPE_NUMBER:
110 visit_type_number(v, &pt->value.number, NULL, errp);
111 break;
112 case PTYPE_INTEGER:
113 visit_type_int(v, &pt->value.integer, NULL, errp);
114 break;
115 case PTYPE_U8:
116 visit_type_uint8(v, &pt->value.u8, NULL, errp);
117 break;
118 case PTYPE_U16:
119 visit_type_uint16(v, &pt->value.u16, NULL, errp);
120 break;
121 case PTYPE_U32:
122 visit_type_uint32(v, &pt->value.u32, NULL, errp);
123 break;
124 case PTYPE_U64:
125 visit_type_uint64(v, &pt->value.u64, NULL, errp);
126 break;
127 case PTYPE_S8:
128 visit_type_int8(v, &pt->value.s8, NULL, errp);
129 break;
130 case PTYPE_S16:
131 visit_type_int16(v, &pt->value.s16, NULL, errp);
132 break;
133 case PTYPE_S32:
134 visit_type_int32(v, &pt->value.s32, NULL, errp);
135 break;
136 case PTYPE_S64:
137 visit_type_int64(v, &pt->value.s64, NULL, errp);
138 break;
139 case PTYPE_EOL:
140 g_assert_not_reached();
144 static void visit_primitive_list(Visitor *v, void **native, Error **errp)
146 PrimitiveList *pl = *native;
147 switch (pl->type) {
148 case PTYPE_STRING:
149 visit_type_strList(v, &pl->value.strings, NULL, errp);
150 break;
151 case PTYPE_BOOLEAN:
152 visit_type_boolList(v, &pl->value.booleans, NULL, errp);
153 break;
154 case PTYPE_NUMBER:
155 visit_type_numberList(v, &pl->value.numbers, NULL, errp);
156 break;
157 case PTYPE_INTEGER:
158 visit_type_intList(v, &pl->value.integers, NULL, errp);
159 break;
160 case PTYPE_S8:
161 visit_type_int8List(v, &pl->value.s8_integers, NULL, errp);
162 break;
163 case PTYPE_S16:
164 visit_type_int16List(v, &pl->value.s16_integers, NULL, errp);
165 break;
166 case PTYPE_S32:
167 visit_type_int32List(v, &pl->value.s32_integers, NULL, errp);
168 break;
169 case PTYPE_S64:
170 visit_type_int64List(v, &pl->value.s64_integers, NULL, errp);
171 break;
172 case PTYPE_U8:
173 visit_type_uint8List(v, &pl->value.u8_integers, NULL, errp);
174 break;
175 case PTYPE_U16:
176 visit_type_uint16List(v, &pl->value.u16_integers, NULL, errp);
177 break;
178 case PTYPE_U32:
179 visit_type_uint32List(v, &pl->value.u32_integers, NULL, errp);
180 break;
181 case PTYPE_U64:
182 visit_type_uint64List(v, &pl->value.u64_integers, NULL, errp);
183 break;
184 default:
185 g_assert_not_reached();
189 typedef struct TestStruct
191 int64_t integer;
192 bool boolean;
193 char *string;
194 } TestStruct;
196 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
197 const char *name, Error **errp)
199 Error *err = NULL;
201 visit_start_struct(v, (void **)obj, NULL, name, sizeof(TestStruct), &err);
202 if (err) {
203 goto out;
206 visit_type_int(v, &(*obj)->integer, "integer", &err);
207 if (err) {
208 goto out_end;
210 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
211 if (err) {
212 goto out_end;
214 visit_type_str(v, &(*obj)->string, "string", &err);
216 out_end:
217 error_propagate(errp, err);
218 err = NULL;
219 visit_end_struct(v, &err);
220 out:
221 error_propagate(errp, err);
224 static TestStruct *struct_create(void)
226 TestStruct *ts = g_malloc0(sizeof(*ts));
227 ts->integer = -42;
228 ts->boolean = true;
229 ts->string = strdup("test string");
230 return ts;
233 static void struct_compare(TestStruct *ts1, TestStruct *ts2)
235 g_assert(ts1);
236 g_assert(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)
244 g_free(ts->string);
245 g_free(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");
270 return udnp;
273 static void nested_struct_compare(UserDefTwo *udnp1, UserDefTwo *udnp2)
275 g_assert(udnp1);
276 g_assert(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);
309 /* test cases */
311 typedef enum VisitorCapabilities {
312 VCAP_PRIMITIVES = 1,
313 VCAP_STRUCTURES = 2,
314 VCAP_LISTS = 4,
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);
324 const char *type;
325 VisitorCapabilities caps;
326 } SerializeOps;
328 typedef struct TestArgs {
329 const SerializeOps *ops;
330 void *test_data;
331 } TestArgs;
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));
339 Error *err = NULL;
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);
365 } else {
366 g_assert_cmpint(pt->value.max, ==, pt_copy->value.max);
369 ops->cleanup(serialize_data);
370 g_free(args);
371 g_free(pt_copy);
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;
382 Error *err = NULL;
383 void *serialize_data;
384 void *cur_head = NULL;
385 int i;
387 pl.type = pl_copy.type = pt->type;
389 /* build up our list of primitive types */
390 for (i = 0; i < 32; i++) {
391 switch (pl.type) {
392 case PTYPE_STRING: {
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;
397 } else {
398 tmp->next = pl.value.strings;
399 pl.value.strings = tmp;
401 break;
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;
408 } else {
409 tmp->next = pl.value.integers;
410 pl.value.integers = tmp;
412 break;
414 case PTYPE_S8: {
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;
419 } else {
420 tmp->next = pl.value.s8_integers;
421 pl.value.s8_integers = tmp;
423 break;
425 case PTYPE_S16: {
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;
430 } else {
431 tmp->next = pl.value.s16_integers;
432 pl.value.s16_integers = tmp;
434 break;
436 case PTYPE_S32: {
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;
441 } else {
442 tmp->next = pl.value.s32_integers;
443 pl.value.s32_integers = tmp;
445 break;
447 case PTYPE_S64: {
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;
452 } else {
453 tmp->next = pl.value.s64_integers;
454 pl.value.s64_integers = tmp;
456 break;
458 case PTYPE_U8: {
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;
463 } else {
464 tmp->next = pl.value.u8_integers;
465 pl.value.u8_integers = tmp;
467 break;
469 case PTYPE_U16: {
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;
474 } else {
475 tmp->next = pl.value.u16_integers;
476 pl.value.u16_integers = tmp;
478 break;
480 case PTYPE_U32: {
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;
485 } else {
486 tmp->next = pl.value.u32_integers;
487 pl.value.u32_integers = tmp;
489 break;
491 case PTYPE_U64: {
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;
496 } else {
497 tmp->next = pl.value.u64_integers;
498 pl.value.u64_integers = tmp;
500 break;
502 case PTYPE_NUMBER: {
503 numberList *tmp = g_new0(numberList, 1);
504 tmp->value = pt->value.number;
505 if (pl.value.numbers == NULL) {
506 pl.value.numbers = tmp;
507 } else {
508 tmp->next = pl.value.numbers;
509 pl.value.numbers = tmp;
511 break;
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;
518 } else {
519 tmp->next = pl.value.booleans;
520 pl.value.booleans = tmp;
522 break;
524 default:
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);
533 i = 0;
535 /* compare our deserialized list of primitives to the original */
536 do {
537 switch (pl_copy.type) {
538 case PTYPE_STRING: {
539 strList *ptr;
540 if (cur_head) {
541 ptr = cur_head;
542 cur_head = ptr->next;
543 } else {
544 cur_head = ptr = pl_copy.value.strings;
546 g_assert_cmpstr(pt->value.string, ==, ptr->value);
547 break;
549 case PTYPE_INTEGER: {
550 intList *ptr;
551 if (cur_head) {
552 ptr = cur_head;
553 cur_head = ptr->next;
554 } else {
555 cur_head = ptr = pl_copy.value.integers;
557 g_assert_cmpint(pt->value.integer, ==, ptr->value);
558 break;
560 case PTYPE_S8: {
561 int8List *ptr;
562 if (cur_head) {
563 ptr = cur_head;
564 cur_head = ptr->next;
565 } else {
566 cur_head = ptr = pl_copy.value.s8_integers;
568 g_assert_cmpint(pt->value.s8, ==, ptr->value);
569 break;
571 case PTYPE_S16: {
572 int16List *ptr;
573 if (cur_head) {
574 ptr = cur_head;
575 cur_head = ptr->next;
576 } else {
577 cur_head = ptr = pl_copy.value.s16_integers;
579 g_assert_cmpint(pt->value.s16, ==, ptr->value);
580 break;
582 case PTYPE_S32: {
583 int32List *ptr;
584 if (cur_head) {
585 ptr = cur_head;
586 cur_head = ptr->next;
587 } else {
588 cur_head = ptr = pl_copy.value.s32_integers;
590 g_assert_cmpint(pt->value.s32, ==, ptr->value);
591 break;
593 case PTYPE_S64: {
594 int64List *ptr;
595 if (cur_head) {
596 ptr = cur_head;
597 cur_head = ptr->next;
598 } else {
599 cur_head = ptr = pl_copy.value.s64_integers;
601 g_assert_cmpint(pt->value.s64, ==, ptr->value);
602 break;
604 case PTYPE_U8: {
605 uint8List *ptr;
606 if (cur_head) {
607 ptr = cur_head;
608 cur_head = ptr->next;
609 } else {
610 cur_head = ptr = pl_copy.value.u8_integers;
612 g_assert_cmpint(pt->value.u8, ==, ptr->value);
613 break;
615 case PTYPE_U16: {
616 uint16List *ptr;
617 if (cur_head) {
618 ptr = cur_head;
619 cur_head = ptr->next;
620 } else {
621 cur_head = ptr = pl_copy.value.u16_integers;
623 g_assert_cmpint(pt->value.u16, ==, ptr->value);
624 break;
626 case PTYPE_U32: {
627 uint32List *ptr;
628 if (cur_head) {
629 ptr = cur_head;
630 cur_head = ptr->next;
631 } else {
632 cur_head = ptr = pl_copy.value.u32_integers;
634 g_assert_cmpint(pt->value.u32, ==, ptr->value);
635 break;
637 case PTYPE_U64: {
638 uint64List *ptr;
639 if (cur_head) {
640 ptr = cur_head;
641 cur_head = ptr->next;
642 } else {
643 cur_head = ptr = pl_copy.value.u64_integers;
645 g_assert_cmpint(pt->value.u64, ==, ptr->value);
646 break;
648 case PTYPE_NUMBER: {
649 numberList *ptr;
650 GString *double_expected = g_string_new("");
651 GString *double_actual = g_string_new("");
652 if (cur_head) {
653 ptr = cur_head;
654 cur_head = ptr->next;
655 } else {
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
660 * formatted values
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);
667 break;
669 case PTYPE_BOOLEAN: {
670 boolList *ptr;
671 if (cur_head) {
672 ptr = cur_head;
673 cur_head = ptr->next;
674 } else {
675 cur_head = ptr = pl_copy.value.booleans;
677 g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
678 break;
680 default:
681 g_assert_not_reached();
683 i++;
684 } while (cur_head);
686 g_assert_cmpint(i, ==, 33);
688 ops->cleanup(serialize_data);
689 dealloc_helper(&pl, visit_primitive_list, &err);
690 g_assert(!err);
691 dealloc_helper(&pl_copy, visit_primitive_list, &err);
692 g_assert(!err);
693 g_free(args);
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;
702 Error *err = 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);
711 struct_cleanup(ts);
712 struct_cleanup(ts_copy);
714 ops->cleanup(serialize_data);
715 g_free(args);
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;
724 Error *err = 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,
729 &err);
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);
738 g_free(args);
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;
746 Error *err = NULL;
747 void *serialize_data;
748 int i = 0;
750 for (i = 0; i < 8; i++) {
751 tmp = g_new0(UserDefTwoList, 1);
752 tmp->value = nested_struct_create();
753 tmp->next = listp;
754 listp = tmp;
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);
763 tmp = listp;
764 tmp_copy = listp_copy;
765 while (listp_copy) {
766 g_assert(listp);
767 nested_struct_compare(listp->value, listp_copy->value);
768 listp = listp->next;
769 listp_copy = listp_copy->next;
772 qapi_free_UserDefTwoList(tmp);
773 qapi_free_UserDefTwoList(tmp_copy);
775 ops->cleanup(serialize_data);
776 g_free(args);
779 static PrimitiveType pt_values[] = {
780 /* string tests */
782 .description = "string_empty",
783 .type = PTYPE_STRING,
784 .value.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",
811 /* boolean tests */
813 .description = "boolean_true1",
814 .type = PTYPE_BOOLEAN,
815 .value.boolean = true,
818 .description = "boolean_true2",
819 .type = PTYPE_BOOLEAN,
820 .value.boolean = 8,
823 .description = "boolean_true3",
824 .type = PTYPE_BOOLEAN,
825 .value.boolean = -1,
828 .description = "boolean_false1",
829 .type = PTYPE_BOOLEAN,
830 .value.boolean = false,
833 .description = "boolean_false2",
834 .type = PTYPE_BOOLEAN,
835 .value.boolean = 0,
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
840 * beyond that.
843 .description = "number_sanity1",
844 .type = PTYPE_NUMBER,
845 .value.number = -1,
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,
866 .value.integer = -1,
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,
883 /* uint8 tests */
885 .description = "uint8_sanity1",
886 .type = PTYPE_U8,
887 .value.u8 = 1,
890 .description = "uint8_sanity2",
891 .type = PTYPE_U8,
892 .value.u8 = UINT8_MAX / 2 + 1,
895 .description = "uint8_min",
896 .type = PTYPE_U8,
897 .value.u8 = 0,
900 .description = "uint8_max",
901 .type = PTYPE_U8,
902 .value.u8 = UINT8_MAX,
904 /* uint16 tests */
906 .description = "uint16_sanity1",
907 .type = PTYPE_U16,
908 .value.u16 = 1,
911 .description = "uint16_sanity2",
912 .type = PTYPE_U16,
913 .value.u16 = UINT16_MAX / 2 + 1,
916 .description = "uint16_min",
917 .type = PTYPE_U16,
918 .value.u16 = 0,
921 .description = "uint16_max",
922 .type = PTYPE_U16,
923 .value.u16 = UINT16_MAX,
925 /* uint32 tests */
927 .description = "uint32_sanity1",
928 .type = PTYPE_U32,
929 .value.u32 = 1,
932 .description = "uint32_sanity2",
933 .type = PTYPE_U32,
934 .value.u32 = UINT32_MAX / 2 + 1,
937 .description = "uint32_min",
938 .type = PTYPE_U32,
939 .value.u32 = 0,
942 .description = "uint32_max",
943 .type = PTYPE_U32,
944 .value.u32 = UINT32_MAX,
946 /* uint64 tests */
948 .description = "uint64_sanity1",
949 .type = PTYPE_U64,
950 .value.u64 = 1,
953 .description = "uint64_sanity2",
954 .type = PTYPE_U64,
955 .value.u64 = UINT64_MAX / 2 + 1,
958 .description = "uint64_min",
959 .type = PTYPE_U64,
960 .value.u64 = 0,
963 .description = "uint64_max",
964 .type = PTYPE_U64,
965 .value.u64 = UINT64_MAX,
967 /* int8 tests */
969 .description = "int8_sanity1",
970 .type = PTYPE_S8,
971 .value.s8 = -1,
974 .description = "int8_sanity2",
975 .type = PTYPE_S8,
976 .value.s8 = INT8_MAX / 2 + 1,
979 .description = "int8_min",
980 .type = PTYPE_S8,
981 .value.s8 = INT8_MIN,
984 .description = "int8_max",
985 .type = PTYPE_S8,
986 .value.s8 = INT8_MAX,
988 /* int16 tests */
990 .description = "int16_sanity1",
991 .type = PTYPE_S16,
992 .value.s16 = -1,
995 .description = "int16_sanity2",
996 .type = PTYPE_S16,
997 .value.s16 = INT16_MAX / 2 + 1,
1000 .description = "int16_min",
1001 .type = PTYPE_S16,
1002 .value.s16 = INT16_MIN,
1005 .description = "int16_max",
1006 .type = PTYPE_S16,
1007 .value.s16 = INT16_MAX,
1009 /* int32 tests */
1011 .description = "int32_sanity1",
1012 .type = PTYPE_S32,
1013 .value.s32 = -1,
1016 .description = "int32_sanity2",
1017 .type = PTYPE_S32,
1018 .value.s32 = INT32_MAX / 2 + 1,
1021 .description = "int32_min",
1022 .type = PTYPE_S32,
1023 .value.s32 = INT32_MIN,
1026 .description = "int32_max",
1027 .type = PTYPE_S32,
1028 .value.s32 = INT32_MAX,
1030 /* int64 tests */
1032 .description = "int64_sanity1",
1033 .type = PTYPE_S64,
1034 .value.s64 = -1,
1037 .description = "int64_sanity2",
1038 .type = PTYPE_S64,
1039 .value.s64 = INT64_MAX / 2 + 1,
1042 .description = "int64_min",
1043 .type = PTYPE_S64,
1044 .value.s64 = INT64_MIN,
1047 .description = "int64_max",
1048 .type = PTYPE_S64,
1049 .value.s64 = INT64_MAX,
1051 { .type = PTYPE_EOL }
1054 /* visitor-specific op implementations */
1056 typedef struct QmpSerializeData {
1057 QmpOutputVisitor *qov;
1058 QmpInputVisitor *qiv;
1059 } QmpSerializeData;
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);
1068 *datap = d;
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);
1095 g_free(d);
1098 typedef struct StringSerializeData {
1099 char *string;
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);
1111 *datap = d;
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);
1130 g_free(d->string);
1131 g_free(d);
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[] = {
1142 .type = "QMP",
1143 .serialize = qmp_serialize,
1144 .deserialize = qmp_deserialize,
1145 .cleanup = qmp_cleanup,
1146 .caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
1147 VCAP_PRIMITIVE_LISTS
1150 .type = "String",
1151 .serialize = string_serialize,
1152 .deserialize = string_deserialize,
1153 .cleanup = string_cleanup,
1154 .caps = VCAP_PRIMITIVES
1156 { NULL }
1159 static void add_visitor_type(const SerializeOps *ops)
1161 char testname_prefix[128];
1162 char testname[128];
1163 TestArgs *args;
1164 int i = 0;
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));
1173 args->ops = ops;
1174 args->test_data = &pt_values[i];
1175 g_test_add_data_func(testname, args, test_primitives);
1176 i++;
1180 if (ops->caps & VCAP_STRUCTURES) {
1181 sprintf(testname, "%s/struct", testname_prefix);
1182 args = g_malloc0(sizeof(*args));
1183 args->ops = ops;
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));
1189 args->ops = ops;
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));
1197 args->ops = ops;
1198 args->test_data = NULL;
1199 g_test_add_data_func(testname, args, test_nested_struct_list);
1202 if (ops->caps & VCAP_PRIMITIVE_LISTS) {
1203 i = 0;
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));
1208 args->ops = ops;
1209 args->test_data = &pt_values[i];
1210 g_test_add_data_func(testname, args, test_primitive_lists);
1211 i++;
1216 int main(int argc, char **argv)
1218 int i = 0;
1220 g_test_init(&argc, &argv, NULL);
1222 while (visitors[i].type != NULL) {
1223 add_visitor_type(&visitors[i]);
1224 i++;
1227 g_test_run();
1229 return 0;