]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-visitor-serialization.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / tests / test-visitor-serialization.c
CommitLineData
2d496105
MR
1/*
2 * Unit-tests for visitor-based serialization
3 *
6446a592 4 * Copyright (C) 2014-2015 Red Hat, Inc.
2d496105
MR
5 * Copyright IBM, Corp. 2012
6 *
7 * Authors:
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 *
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.
12 */
13
681c28a3 14#include "qemu/osdep.h"
2d496105 15#include <float.h>
79ee7df8
PB
16
17#include "qemu-common.h"
2d496105
MR
18#include "test-qapi-types.h"
19#include "test-qapi-visit.h"
da34e65c 20#include "qapi/error.h"
7b1b5d19 21#include "qapi/qmp/types.h"
2d496105
MR
22#include "qapi/qmp-input-visitor.h"
23#include "qapi/qmp-output-visitor.h"
0d30b0a2
MR
24#include "qapi/string-input-visitor.h"
25#include "qapi/string-output-visitor.h"
8addacdd
MR
26#include "qapi-types.h"
27#include "qapi-visit.h"
28#include "qapi/dealloc-visitor.h"
29
30enum PrimitiveTypeKind {
31 PTYPE_STRING = 0,
32 PTYPE_BOOLEAN,
33 PTYPE_NUMBER,
34 PTYPE_INTEGER,
35 PTYPE_U8,
36 PTYPE_U16,
37 PTYPE_U32,
38 PTYPE_U64,
39 PTYPE_S8,
40 PTYPE_S16,
41 PTYPE_S32,
42 PTYPE_S64,
43 PTYPE_EOL,
44};
2d496105
MR
45
46typedef struct PrimitiveType {
47 union {
48 const char *string;
49 bool boolean;
50 double number;
51 int64_t integer;
52 uint8_t u8;
53 uint16_t u16;
54 uint32_t u32;
55 uint64_t u64;
56 int8_t s8;
57 int16_t s16;
58 int32_t s32;
59 int64_t s64;
60 intmax_t max;
61 } value;
8addacdd 62 enum PrimitiveTypeKind type;
2d496105
MR
63 const char *description;
64} PrimitiveType;
65
8addacdd
MR
66typedef struct PrimitiveList {
67 union {
68 strList *strings;
69 boolList *booleans;
70 numberList *numbers;
71 intList *integers;
72 int8List *s8_integers;
73 int16List *s16_integers;
74 int32List *s32_integers;
75 int64List *s64_integers;
76 uint8List *u8_integers;
77 uint16List *u16_integers;
78 uint32List *u32_integers;
79 uint64List *u64_integers;
80 } value;
81 enum PrimitiveTypeKind type;
82 const char *description;
83} PrimitiveList;
84
2d496105
MR
85/* test helpers */
86
8addacdd
MR
87typedef void (*VisitorFunc)(Visitor *v, void **native, Error **errp);
88
89static void dealloc_helper(void *native_in, VisitorFunc visit, Error **errp)
90{
91 QapiDeallocVisitor *qdv = qapi_dealloc_visitor_new();
92
93 visit(qapi_dealloc_get_visitor(qdv), &native_in, errp);
94
95 qapi_dealloc_visitor_cleanup(qdv);
96}
97
2d496105
MR
98static void visit_primitive_type(Visitor *v, void **native, Error **errp)
99{
100 PrimitiveType *pt = *native;
101 switch(pt->type) {
102 case PTYPE_STRING:
51e72bc1 103 visit_type_str(v, NULL, (char **)&pt->value.string, errp);
2d496105
MR
104 break;
105 case PTYPE_BOOLEAN:
51e72bc1 106 visit_type_bool(v, NULL, &pt->value.boolean, errp);
2d496105
MR
107 break;
108 case PTYPE_NUMBER:
51e72bc1 109 visit_type_number(v, NULL, &pt->value.number, errp);
2d496105
MR
110 break;
111 case PTYPE_INTEGER:
51e72bc1 112 visit_type_int(v, NULL, &pt->value.integer, errp);
2d496105
MR
113 break;
114 case PTYPE_U8:
51e72bc1 115 visit_type_uint8(v, NULL, &pt->value.u8, errp);
2d496105
MR
116 break;
117 case PTYPE_U16:
51e72bc1 118 visit_type_uint16(v, NULL, &pt->value.u16, errp);
2d496105
MR
119 break;
120 case PTYPE_U32:
51e72bc1 121 visit_type_uint32(v, NULL, &pt->value.u32, errp);
2d496105
MR
122 break;
123 case PTYPE_U64:
51e72bc1 124 visit_type_uint64(v, NULL, &pt->value.u64, errp);
2d496105
MR
125 break;
126 case PTYPE_S8:
51e72bc1 127 visit_type_int8(v, NULL, &pt->value.s8, errp);
2d496105
MR
128 break;
129 case PTYPE_S16:
51e72bc1 130 visit_type_int16(v, NULL, &pt->value.s16, errp);
2d496105
MR
131 break;
132 case PTYPE_S32:
51e72bc1 133 visit_type_int32(v, NULL, &pt->value.s32, errp);
2d496105
MR
134 break;
135 case PTYPE_S64:
51e72bc1 136 visit_type_int64(v, NULL, &pt->value.s64, errp);
2d496105
MR
137 break;
138 case PTYPE_EOL:
dfc6f865 139 g_assert_not_reached();
2d496105
MR
140 }
141}
142
8addacdd
MR
143static void visit_primitive_list(Visitor *v, void **native, Error **errp)
144{
145 PrimitiveList *pl = *native;
146 switch (pl->type) {
147 case PTYPE_STRING:
51e72bc1 148 visit_type_strList(v, NULL, &pl->value.strings, errp);
8addacdd
MR
149 break;
150 case PTYPE_BOOLEAN:
51e72bc1 151 visit_type_boolList(v, NULL, &pl->value.booleans, errp);
8addacdd
MR
152 break;
153 case PTYPE_NUMBER:
51e72bc1 154 visit_type_numberList(v, NULL, &pl->value.numbers, errp);
8addacdd
MR
155 break;
156 case PTYPE_INTEGER:
51e72bc1 157 visit_type_intList(v, NULL, &pl->value.integers, errp);
8addacdd
MR
158 break;
159 case PTYPE_S8:
51e72bc1 160 visit_type_int8List(v, NULL, &pl->value.s8_integers, errp);
8addacdd
MR
161 break;
162 case PTYPE_S16:
51e72bc1 163 visit_type_int16List(v, NULL, &pl->value.s16_integers, errp);
8addacdd
MR
164 break;
165 case PTYPE_S32:
51e72bc1 166 visit_type_int32List(v, NULL, &pl->value.s32_integers, errp);
8addacdd
MR
167 break;
168 case PTYPE_S64:
51e72bc1 169 visit_type_int64List(v, NULL, &pl->value.s64_integers, errp);
8addacdd
MR
170 break;
171 case PTYPE_U8:
51e72bc1 172 visit_type_uint8List(v, NULL, &pl->value.u8_integers, errp);
8addacdd
MR
173 break;
174 case PTYPE_U16:
51e72bc1 175 visit_type_uint16List(v, NULL, &pl->value.u16_integers, errp);
8addacdd
MR
176 break;
177 case PTYPE_U32:
51e72bc1 178 visit_type_uint32List(v, NULL, &pl->value.u32_integers, errp);
8addacdd
MR
179 break;
180 case PTYPE_U64:
51e72bc1 181 visit_type_uint64List(v, NULL, &pl->value.u64_integers, errp);
8addacdd
MR
182 break;
183 default:
dfc6f865 184 g_assert_not_reached();
8addacdd
MR
185 }
186}
187
2d496105
MR
188
189static TestStruct *struct_create(void)
190{
191 TestStruct *ts = g_malloc0(sizeof(*ts));
192 ts->integer = -42;
193 ts->boolean = true;
194 ts->string = strdup("test string");
195 return ts;
196}
197
198static void struct_compare(TestStruct *ts1, TestStruct *ts2)
199{
200 g_assert(ts1);
201 g_assert(ts2);
202 g_assert_cmpint(ts1->integer, ==, ts2->integer);
203 g_assert(ts1->boolean == ts2->boolean);
204 g_assert_cmpstr(ts1->string, ==, ts2->string);
205}
206
207static void struct_cleanup(TestStruct *ts)
208{
209 g_free(ts->string);
210 g_free(ts);
211}
212
213static void visit_struct(Visitor *v, void **native, Error **errp)
214{
51e72bc1 215 visit_type_TestStruct(v, NULL, (TestStruct **)native, errp);
2d496105
MR
216}
217
b6fcf32d 218static UserDefTwo *nested_struct_create(void)
2d496105 219{
b6fcf32d 220 UserDefTwo *udnp = g_malloc0(sizeof(*udnp));
2d496105 221 udnp->string0 = strdup("test_string0");
6446a592
EB
222 udnp->dict1 = g_malloc0(sizeof(*udnp->dict1));
223 udnp->dict1->string1 = strdup("test_string1");
224 udnp->dict1->dict2 = g_malloc0(sizeof(*udnp->dict1->dict2));
225 udnp->dict1->dict2->userdef = g_new0(UserDefOne, 1);
ddf21908 226 udnp->dict1->dict2->userdef->integer = 42;
6446a592
EB
227 udnp->dict1->dict2->userdef->string = strdup("test_string");
228 udnp->dict1->dict2->string = strdup("test_string2");
229 udnp->dict1->dict3 = g_malloc0(sizeof(*udnp->dict1->dict3));
230 udnp->dict1->has_dict3 = true;
231 udnp->dict1->dict3->userdef = g_new0(UserDefOne, 1);
ddf21908 232 udnp->dict1->dict3->userdef->integer = 43;
6446a592
EB
233 udnp->dict1->dict3->userdef->string = strdup("test_string");
234 udnp->dict1->dict3->string = strdup("test_string3");
2d496105
MR
235 return udnp;
236}
237
b6fcf32d 238static void nested_struct_compare(UserDefTwo *udnp1, UserDefTwo *udnp2)
2d496105
MR
239{
240 g_assert(udnp1);
241 g_assert(udnp2);
242 g_assert_cmpstr(udnp1->string0, ==, udnp2->string0);
6446a592 243 g_assert_cmpstr(udnp1->dict1->string1, ==, udnp2->dict1->string1);
ddf21908
EB
244 g_assert_cmpint(udnp1->dict1->dict2->userdef->integer, ==,
245 udnp2->dict1->dict2->userdef->integer);
6446a592
EB
246 g_assert_cmpstr(udnp1->dict1->dict2->userdef->string, ==,
247 udnp2->dict1->dict2->userdef->string);
248 g_assert_cmpstr(udnp1->dict1->dict2->string, ==,
249 udnp2->dict1->dict2->string);
250 g_assert(udnp1->dict1->has_dict3 == udnp2->dict1->has_dict3);
ddf21908
EB
251 g_assert_cmpint(udnp1->dict1->dict3->userdef->integer, ==,
252 udnp2->dict1->dict3->userdef->integer);
6446a592
EB
253 g_assert_cmpstr(udnp1->dict1->dict3->userdef->string, ==,
254 udnp2->dict1->dict3->userdef->string);
255 g_assert_cmpstr(udnp1->dict1->dict3->string, ==,
256 udnp2->dict1->dict3->string);
2d496105
MR
257}
258
b6fcf32d 259static void nested_struct_cleanup(UserDefTwo *udnp)
2d496105 260{
b6fcf32d 261 qapi_free_UserDefTwo(udnp);
2d496105
MR
262}
263
264static void visit_nested_struct(Visitor *v, void **native, Error **errp)
265{
51e72bc1 266 visit_type_UserDefTwo(v, NULL, (UserDefTwo **)native, errp);
2d496105
MR
267}
268
269static void visit_nested_struct_list(Visitor *v, void **native, Error **errp)
270{
51e72bc1 271 visit_type_UserDefTwoList(v, NULL, (UserDefTwoList **)native, errp);
2d496105
MR
272}
273
274/* test cases */
275
2d496105
MR
276typedef enum VisitorCapabilities {
277 VCAP_PRIMITIVES = 1,
278 VCAP_STRUCTURES = 2,
279 VCAP_LISTS = 4,
8addacdd 280 VCAP_PRIMITIVE_LISTS = 8,
2d496105
MR
281} VisitorCapabilities;
282
283typedef struct SerializeOps {
284 void (*serialize)(void *native_in, void **datap,
285 VisitorFunc visit, Error **errp);
286 void (*deserialize)(void **native_out, void *datap,
287 VisitorFunc visit, Error **errp);
288 void (*cleanup)(void *datap);
289 const char *type;
290 VisitorCapabilities caps;
291} SerializeOps;
292
293typedef struct TestArgs {
294 const SerializeOps *ops;
295 void *test_data;
296} TestArgs;
297
2d496105
MR
298static void test_primitives(gconstpointer opaque)
299{
300 TestArgs *args = (TestArgs *) opaque;
301 const SerializeOps *ops = args->ops;
302 PrimitiveType *pt = args->test_data;
303 PrimitiveType *pt_copy = g_malloc0(sizeof(*pt_copy));
2d496105 304 void *serialize_data;
2d496105
MR
305
306 pt_copy->type = pt->type;
3f66f764
EB
307 ops->serialize(pt, &serialize_data, visit_primitive_type, &error_abort);
308 ops->deserialize((void **)&pt_copy, serialize_data, visit_primitive_type,
309 &error_abort);
2d496105 310
2d496105
MR
311 g_assert(pt_copy != NULL);
312 if (pt->type == PTYPE_STRING) {
313 g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
2bd01ac1 314 g_free((char *)pt_copy->value.string);
2d496105 315 } else if (pt->type == PTYPE_NUMBER) {
089f26bb
MR
316 GString *double_expected = g_string_new("");
317 GString *double_actual = g_string_new("");
2d496105
MR
318 /* we serialize with %f for our reference visitors, so rather than fuzzy
319 * floating math to test "equality", just compare the formatted values
320 */
089f26bb
MR
321 g_string_printf(double_expected, "%.6f", pt->value.number);
322 g_string_printf(double_actual, "%.6f", pt_copy->value.number);
323 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
324
325 g_string_free(double_expected, true);
326 g_string_free(double_actual, true);
2d496105
MR
327 } else if (pt->type == PTYPE_BOOLEAN) {
328 g_assert_cmpint(!!pt->value.max, ==, !!pt->value.max);
329 } else {
330 g_assert_cmpint(pt->value.max, ==, pt_copy->value.max);
331 }
332
333 ops->cleanup(serialize_data);
334 g_free(args);
2bd01ac1 335 g_free(pt_copy);
2d496105
MR
336}
337
8addacdd
MR
338static void test_primitive_lists(gconstpointer opaque)
339{
340 TestArgs *args = (TestArgs *) opaque;
341 const SerializeOps *ops = args->ops;
342 PrimitiveType *pt = args->test_data;
748bfb4e
SW
343 PrimitiveList pl = { .value = { NULL } };
344 PrimitiveList pl_copy = { .value = { NULL } };
8addacdd 345 PrimitiveList *pl_copy_ptr = &pl_copy;
8addacdd
MR
346 void *serialize_data;
347 void *cur_head = NULL;
348 int i;
349
350 pl.type = pl_copy.type = pt->type;
351
352 /* build up our list of primitive types */
353 for (i = 0; i < 32; i++) {
354 switch (pl.type) {
355 case PTYPE_STRING: {
356 strList *tmp = g_new0(strList, 1);
357 tmp->value = g_strdup(pt->value.string);
358 if (pl.value.strings == NULL) {
359 pl.value.strings = tmp;
360 } else {
361 tmp->next = pl.value.strings;
362 pl.value.strings = tmp;
363 }
364 break;
365 }
366 case PTYPE_INTEGER: {
367 intList *tmp = g_new0(intList, 1);
368 tmp->value = pt->value.integer;
369 if (pl.value.integers == NULL) {
370 pl.value.integers = tmp;
371 } else {
372 tmp->next = pl.value.integers;
373 pl.value.integers = tmp;
374 }
375 break;
376 }
377 case PTYPE_S8: {
378 int8List *tmp = g_new0(int8List, 1);
379 tmp->value = pt->value.s8;
380 if (pl.value.s8_integers == NULL) {
381 pl.value.s8_integers = tmp;
382 } else {
383 tmp->next = pl.value.s8_integers;
384 pl.value.s8_integers = tmp;
385 }
386 break;
387 }
388 case PTYPE_S16: {
389 int16List *tmp = g_new0(int16List, 1);
390 tmp->value = pt->value.s16;
391 if (pl.value.s16_integers == NULL) {
392 pl.value.s16_integers = tmp;
393 } else {
394 tmp->next = pl.value.s16_integers;
395 pl.value.s16_integers = tmp;
396 }
397 break;
398 }
399 case PTYPE_S32: {
400 int32List *tmp = g_new0(int32List, 1);
401 tmp->value = pt->value.s32;
402 if (pl.value.s32_integers == NULL) {
403 pl.value.s32_integers = tmp;
404 } else {
405 tmp->next = pl.value.s32_integers;
406 pl.value.s32_integers = tmp;
407 }
408 break;
409 }
410 case PTYPE_S64: {
411 int64List *tmp = g_new0(int64List, 1);
412 tmp->value = pt->value.s64;
413 if (pl.value.s64_integers == NULL) {
414 pl.value.s64_integers = tmp;
415 } else {
416 tmp->next = pl.value.s64_integers;
417 pl.value.s64_integers = tmp;
418 }
419 break;
420 }
421 case PTYPE_U8: {
422 uint8List *tmp = g_new0(uint8List, 1);
423 tmp->value = pt->value.u8;
424 if (pl.value.u8_integers == NULL) {
425 pl.value.u8_integers = tmp;
426 } else {
427 tmp->next = pl.value.u8_integers;
428 pl.value.u8_integers = tmp;
429 }
430 break;
431 }
432 case PTYPE_U16: {
433 uint16List *tmp = g_new0(uint16List, 1);
434 tmp->value = pt->value.u16;
435 if (pl.value.u16_integers == NULL) {
436 pl.value.u16_integers = tmp;
437 } else {
438 tmp->next = pl.value.u16_integers;
439 pl.value.u16_integers = tmp;
440 }
441 break;
442 }
443 case PTYPE_U32: {
444 uint32List *tmp = g_new0(uint32List, 1);
445 tmp->value = pt->value.u32;
446 if (pl.value.u32_integers == NULL) {
447 pl.value.u32_integers = tmp;
448 } else {
449 tmp->next = pl.value.u32_integers;
450 pl.value.u32_integers = tmp;
451 }
452 break;
453 }
454 case PTYPE_U64: {
455 uint64List *tmp = g_new0(uint64List, 1);
456 tmp->value = pt->value.u64;
457 if (pl.value.u64_integers == NULL) {
458 pl.value.u64_integers = tmp;
459 } else {
460 tmp->next = pl.value.u64_integers;
461 pl.value.u64_integers = tmp;
462 }
463 break;
464 }
465 case PTYPE_NUMBER: {
466 numberList *tmp = g_new0(numberList, 1);
467 tmp->value = pt->value.number;
468 if (pl.value.numbers == NULL) {
469 pl.value.numbers = tmp;
470 } else {
471 tmp->next = pl.value.numbers;
472 pl.value.numbers = tmp;
473 }
474 break;
475 }
476 case PTYPE_BOOLEAN: {
477 boolList *tmp = g_new0(boolList, 1);
478 tmp->value = pt->value.boolean;
479 if (pl.value.booleans == NULL) {
480 pl.value.booleans = tmp;
481 } else {
482 tmp->next = pl.value.booleans;
483 pl.value.booleans = tmp;
484 }
485 break;
486 }
487 default:
dfc6f865 488 g_assert_not_reached();
8addacdd
MR
489 }
490 }
491
3f66f764
EB
492 ops->serialize((void **)&pl, &serialize_data, visit_primitive_list,
493 &error_abort);
494 ops->deserialize((void **)&pl_copy_ptr, serialize_data,
495 visit_primitive_list, &error_abort);
8addacdd 496
8addacdd
MR
497 i = 0;
498
499 /* compare our deserialized list of primitives to the original */
500 do {
501 switch (pl_copy.type) {
502 case PTYPE_STRING: {
503 strList *ptr;
504 if (cur_head) {
505 ptr = cur_head;
506 cur_head = ptr->next;
507 } else {
508 cur_head = ptr = pl_copy.value.strings;
509 }
510 g_assert_cmpstr(pt->value.string, ==, ptr->value);
511 break;
512 }
513 case PTYPE_INTEGER: {
514 intList *ptr;
515 if (cur_head) {
516 ptr = cur_head;
517 cur_head = ptr->next;
518 } else {
519 cur_head = ptr = pl_copy.value.integers;
520 }
521 g_assert_cmpint(pt->value.integer, ==, ptr->value);
522 break;
523 }
524 case PTYPE_S8: {
525 int8List *ptr;
526 if (cur_head) {
527 ptr = cur_head;
528 cur_head = ptr->next;
529 } else {
530 cur_head = ptr = pl_copy.value.s8_integers;
531 }
532 g_assert_cmpint(pt->value.s8, ==, ptr->value);
533 break;
534 }
535 case PTYPE_S16: {
536 int16List *ptr;
537 if (cur_head) {
538 ptr = cur_head;
539 cur_head = ptr->next;
540 } else {
541 cur_head = ptr = pl_copy.value.s16_integers;
542 }
543 g_assert_cmpint(pt->value.s16, ==, ptr->value);
544 break;
545 }
546 case PTYPE_S32: {
547 int32List *ptr;
548 if (cur_head) {
549 ptr = cur_head;
550 cur_head = ptr->next;
551 } else {
552 cur_head = ptr = pl_copy.value.s32_integers;
553 }
554 g_assert_cmpint(pt->value.s32, ==, ptr->value);
555 break;
556 }
557 case PTYPE_S64: {
558 int64List *ptr;
559 if (cur_head) {
560 ptr = cur_head;
561 cur_head = ptr->next;
562 } else {
563 cur_head = ptr = pl_copy.value.s64_integers;
564 }
565 g_assert_cmpint(pt->value.s64, ==, ptr->value);
566 break;
567 }
568 case PTYPE_U8: {
569 uint8List *ptr;
570 if (cur_head) {
571 ptr = cur_head;
572 cur_head = ptr->next;
573 } else {
574 cur_head = ptr = pl_copy.value.u8_integers;
575 }
576 g_assert_cmpint(pt->value.u8, ==, ptr->value);
577 break;
578 }
579 case PTYPE_U16: {
580 uint16List *ptr;
581 if (cur_head) {
582 ptr = cur_head;
583 cur_head = ptr->next;
584 } else {
585 cur_head = ptr = pl_copy.value.u16_integers;
586 }
587 g_assert_cmpint(pt->value.u16, ==, ptr->value);
588 break;
589 }
590 case PTYPE_U32: {
591 uint32List *ptr;
592 if (cur_head) {
593 ptr = cur_head;
594 cur_head = ptr->next;
595 } else {
596 cur_head = ptr = pl_copy.value.u32_integers;
597 }
598 g_assert_cmpint(pt->value.u32, ==, ptr->value);
599 break;
600 }
601 case PTYPE_U64: {
602 uint64List *ptr;
603 if (cur_head) {
604 ptr = cur_head;
605 cur_head = ptr->next;
606 } else {
607 cur_head = ptr = pl_copy.value.u64_integers;
608 }
609 g_assert_cmpint(pt->value.u64, ==, ptr->value);
610 break;
611 }
612 case PTYPE_NUMBER: {
613 numberList *ptr;
614 GString *double_expected = g_string_new("");
615 GString *double_actual = g_string_new("");
616 if (cur_head) {
617 ptr = cur_head;
618 cur_head = ptr->next;
619 } else {
620 cur_head = ptr = pl_copy.value.numbers;
621 }
622 /* we serialize with %f for our reference visitors, so rather than
623 * fuzzy floating math to test "equality", just compare the
624 * formatted values
625 */
626 g_string_printf(double_expected, "%.6f", pt->value.number);
627 g_string_printf(double_actual, "%.6f", ptr->value);
628 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
629 g_string_free(double_expected, true);
630 g_string_free(double_actual, true);
631 break;
632 }
633 case PTYPE_BOOLEAN: {
634 boolList *ptr;
635 if (cur_head) {
636 ptr = cur_head;
637 cur_head = ptr->next;
638 } else {
639 cur_head = ptr = pl_copy.value.booleans;
640 }
641 g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
642 break;
643 }
644 default:
dfc6f865 645 g_assert_not_reached();
8addacdd
MR
646 }
647 i++;
648 } while (cur_head);
649
650 g_assert_cmpint(i, ==, 33);
651
652 ops->cleanup(serialize_data);
3f66f764
EB
653 dealloc_helper(&pl, visit_primitive_list, &error_abort);
654 dealloc_helper(&pl_copy, visit_primitive_list, &error_abort);
8addacdd
MR
655 g_free(args);
656}
657
2d496105
MR
658static void test_struct(gconstpointer opaque)
659{
660 TestArgs *args = (TestArgs *) opaque;
661 const SerializeOps *ops = args->ops;
662 TestStruct *ts = struct_create();
663 TestStruct *ts_copy = NULL;
2d496105
MR
664 void *serialize_data;
665
3f66f764
EB
666 ops->serialize(ts, &serialize_data, visit_struct, &error_abort);
667 ops->deserialize((void **)&ts_copy, serialize_data, visit_struct,
668 &error_abort);
2d496105 669
2d496105
MR
670 struct_compare(ts, ts_copy);
671
672 struct_cleanup(ts);
673 struct_cleanup(ts_copy);
674
675 ops->cleanup(serialize_data);
676 g_free(args);
677}
678
679static void test_nested_struct(gconstpointer opaque)
680{
681 TestArgs *args = (TestArgs *) opaque;
682 const SerializeOps *ops = args->ops;
b6fcf32d
EB
683 UserDefTwo *udnp = nested_struct_create();
684 UserDefTwo *udnp_copy = NULL;
2d496105 685 void *serialize_data;
b6fcf32d 686
3f66f764 687 ops->serialize(udnp, &serialize_data, visit_nested_struct, &error_abort);
b6fcf32d 688 ops->deserialize((void **)&udnp_copy, serialize_data, visit_nested_struct,
3f66f764 689 &error_abort);
2d496105 690
2d496105
MR
691 nested_struct_compare(udnp, udnp_copy);
692
693 nested_struct_cleanup(udnp);
694 nested_struct_cleanup(udnp_copy);
695
696 ops->cleanup(serialize_data);
697 g_free(args);
698}
699
700static void test_nested_struct_list(gconstpointer opaque)
701{
702 TestArgs *args = (TestArgs *) opaque;
703 const SerializeOps *ops = args->ops;
b6fcf32d 704 UserDefTwoList *listp = NULL, *tmp, *tmp_copy, *listp_copy = NULL;
2d496105
MR
705 void *serialize_data;
706 int i = 0;
707
708 for (i = 0; i < 8; i++) {
b6fcf32d 709 tmp = g_new0(UserDefTwoList, 1);
2d496105
MR
710 tmp->value = nested_struct_create();
711 tmp->next = listp;
712 listp = tmp;
713 }
b6fcf32d 714
3f66f764
EB
715 ops->serialize(listp, &serialize_data, visit_nested_struct_list,
716 &error_abort);
2d496105 717 ops->deserialize((void **)&listp_copy, serialize_data,
3f66f764 718 visit_nested_struct_list, &error_abort);
2d496105
MR
719
720 tmp = listp;
721 tmp_copy = listp_copy;
722 while (listp_copy) {
723 g_assert(listp);
724 nested_struct_compare(listp->value, listp_copy->value);
725 listp = listp->next;
726 listp_copy = listp_copy->next;
727 }
728
b6fcf32d
EB
729 qapi_free_UserDefTwoList(tmp);
730 qapi_free_UserDefTwoList(tmp_copy);
2d496105
MR
731
732 ops->cleanup(serialize_data);
733 g_free(args);
734}
735
748bfb4e 736static PrimitiveType pt_values[] = {
2d496105
MR
737 /* string tests */
738 {
739 .description = "string_empty",
740 .type = PTYPE_STRING,
741 .value.string = "",
742 },
743 {
744 .description = "string_whitespace",
745 .type = PTYPE_STRING,
746 .value.string = "a b c\td",
747 },
748 {
749 .description = "string_newlines",
750 .type = PTYPE_STRING,
751 .value.string = "a\nb\n",
752 },
753 {
754 .description = "string_commas",
755 .type = PTYPE_STRING,
756 .value.string = "a,b, c,d",
757 },
758 {
759 .description = "string_single_quoted",
760 .type = PTYPE_STRING,
761 .value.string = "'a b',cd",
762 },
763 {
764 .description = "string_double_quoted",
765 .type = PTYPE_STRING,
766 .value.string = "\"a b\",cd",
767 },
768 /* boolean tests */
769 {
770 .description = "boolean_true1",
771 .type = PTYPE_BOOLEAN,
772 .value.boolean = true,
773 },
774 {
775 .description = "boolean_true2",
776 .type = PTYPE_BOOLEAN,
777 .value.boolean = 8,
778 },
779 {
780 .description = "boolean_true3",
781 .type = PTYPE_BOOLEAN,
782 .value.boolean = -1,
783 },
784 {
785 .description = "boolean_false1",
786 .type = PTYPE_BOOLEAN,
787 .value.boolean = false,
788 },
789 {
790 .description = "boolean_false2",
791 .type = PTYPE_BOOLEAN,
792 .value.boolean = 0,
793 },
794 /* number tests (double) */
795 /* note: we format these to %.6f before comparing, since that's how
796 * we serialize them and it doesn't make sense to check precision
797 * beyond that.
798 */
799 {
800 .description = "number_sanity1",
801 .type = PTYPE_NUMBER,
802 .value.number = -1,
803 },
804 {
805 .description = "number_sanity2",
806 .type = PTYPE_NUMBER,
807 .value.number = 3.14159265,
808 },
809 {
810 .description = "number_min",
811 .type = PTYPE_NUMBER,
812 .value.number = DBL_MIN,
813 },
814 {
815 .description = "number_max",
816 .type = PTYPE_NUMBER,
817 .value.number = DBL_MAX,
818 },
819 /* integer tests (int64) */
820 {
821 .description = "integer_sanity1",
822 .type = PTYPE_INTEGER,
823 .value.integer = -1,
824 },
825 {
826 .description = "integer_sanity2",
827 .type = PTYPE_INTEGER,
828 .value.integer = INT64_MAX / 2 + 1,
829 },
830 {
831 .description = "integer_min",
832 .type = PTYPE_INTEGER,
833 .value.integer = INT64_MIN,
834 },
835 {
836 .description = "integer_max",
837 .type = PTYPE_INTEGER,
838 .value.integer = INT64_MAX,
839 },
840 /* uint8 tests */
841 {
842 .description = "uint8_sanity1",
843 .type = PTYPE_U8,
844 .value.u8 = 1,
845 },
846 {
847 .description = "uint8_sanity2",
848 .type = PTYPE_U8,
849 .value.u8 = UINT8_MAX / 2 + 1,
850 },
851 {
852 .description = "uint8_min",
853 .type = PTYPE_U8,
854 .value.u8 = 0,
855 },
856 {
857 .description = "uint8_max",
858 .type = PTYPE_U8,
859 .value.u8 = UINT8_MAX,
860 },
861 /* uint16 tests */
862 {
863 .description = "uint16_sanity1",
864 .type = PTYPE_U16,
865 .value.u16 = 1,
866 },
867 {
868 .description = "uint16_sanity2",
869 .type = PTYPE_U16,
870 .value.u16 = UINT16_MAX / 2 + 1,
871 },
872 {
873 .description = "uint16_min",
874 .type = PTYPE_U16,
875 .value.u16 = 0,
876 },
877 {
878 .description = "uint16_max",
879 .type = PTYPE_U16,
880 .value.u16 = UINT16_MAX,
881 },
882 /* uint32 tests */
883 {
884 .description = "uint32_sanity1",
885 .type = PTYPE_U32,
886 .value.u32 = 1,
887 },
888 {
889 .description = "uint32_sanity2",
890 .type = PTYPE_U32,
891 .value.u32 = UINT32_MAX / 2 + 1,
892 },
893 {
894 .description = "uint32_min",
895 .type = PTYPE_U32,
896 .value.u32 = 0,
897 },
898 {
899 .description = "uint32_max",
900 .type = PTYPE_U32,
901 .value.u32 = UINT32_MAX,
902 },
903 /* uint64 tests */
904 {
905 .description = "uint64_sanity1",
906 .type = PTYPE_U64,
907 .value.u64 = 1,
908 },
909 {
910 .description = "uint64_sanity2",
911 .type = PTYPE_U64,
912 .value.u64 = UINT64_MAX / 2 + 1,
913 },
914 {
915 .description = "uint64_min",
916 .type = PTYPE_U64,
917 .value.u64 = 0,
918 },
919 {
920 .description = "uint64_max",
921 .type = PTYPE_U64,
922 .value.u64 = UINT64_MAX,
923 },
924 /* int8 tests */
925 {
926 .description = "int8_sanity1",
927 .type = PTYPE_S8,
928 .value.s8 = -1,
929 },
930 {
931 .description = "int8_sanity2",
932 .type = PTYPE_S8,
933 .value.s8 = INT8_MAX / 2 + 1,
934 },
935 {
936 .description = "int8_min",
937 .type = PTYPE_S8,
938 .value.s8 = INT8_MIN,
939 },
940 {
941 .description = "int8_max",
942 .type = PTYPE_S8,
943 .value.s8 = INT8_MAX,
944 },
945 /* int16 tests */
946 {
947 .description = "int16_sanity1",
948 .type = PTYPE_S16,
949 .value.s16 = -1,
950 },
951 {
952 .description = "int16_sanity2",
953 .type = PTYPE_S16,
954 .value.s16 = INT16_MAX / 2 + 1,
955 },
956 {
957 .description = "int16_min",
958 .type = PTYPE_S16,
959 .value.s16 = INT16_MIN,
960 },
961 {
962 .description = "int16_max",
963 .type = PTYPE_S16,
964 .value.s16 = INT16_MAX,
965 },
966 /* int32 tests */
967 {
968 .description = "int32_sanity1",
969 .type = PTYPE_S32,
970 .value.s32 = -1,
971 },
972 {
973 .description = "int32_sanity2",
974 .type = PTYPE_S32,
975 .value.s32 = INT32_MAX / 2 + 1,
976 },
977 {
978 .description = "int32_min",
979 .type = PTYPE_S32,
980 .value.s32 = INT32_MIN,
981 },
982 {
983 .description = "int32_max",
984 .type = PTYPE_S32,
985 .value.s32 = INT32_MAX,
986 },
987 /* int64 tests */
988 {
989 .description = "int64_sanity1",
990 .type = PTYPE_S64,
991 .value.s64 = -1,
992 },
993 {
994 .description = "int64_sanity2",
995 .type = PTYPE_S64,
996 .value.s64 = INT64_MAX / 2 + 1,
997 },
998 {
999 .description = "int64_min",
1000 .type = PTYPE_S64,
1001 .value.s64 = INT64_MIN,
1002 },
1003 {
1004 .description = "int64_max",
1005 .type = PTYPE_S64,
1006 .value.s64 = INT64_MAX,
1007 },
1008 { .type = PTYPE_EOL }
1009};
1010
1011/* visitor-specific op implementations */
1012
1013typedef struct QmpSerializeData {
1014 QmpOutputVisitor *qov;
1015 QmpInputVisitor *qiv;
1016} QmpSerializeData;
1017
1018static void qmp_serialize(void *native_in, void **datap,
1019 VisitorFunc visit, Error **errp)
1020{
1021 QmpSerializeData *d = g_malloc0(sizeof(*d));
1022
1023 d->qov = qmp_output_visitor_new();
1024 visit(qmp_output_get_visitor(d->qov), &native_in, errp);
1025 *datap = d;
1026}
1027
1028static void qmp_deserialize(void **native_out, void *datap,
1029 VisitorFunc visit, Error **errp)
1030{
1031 QmpSerializeData *d = datap;
ad7f375d
MR
1032 QString *output_json;
1033 QObject *obj_orig, *obj;
1034
1035 obj_orig = qmp_output_get_qobject(d->qov);
1036 output_json = qobject_to_json(obj_orig);
1037 obj = qobject_from_json(qstring_get_str(output_json));
2d496105
MR
1038
1039 QDECREF(output_json);
240f64b6 1040 d->qiv = qmp_input_visitor_new(obj, true);
ad7f375d 1041 qobject_decref(obj_orig);
2bd01ac1 1042 qobject_decref(obj);
2d496105
MR
1043 visit(qmp_input_get_visitor(d->qiv), native_out, errp);
1044}
1045
1046static void qmp_cleanup(void *datap)
1047{
1048 QmpSerializeData *d = datap;
1049 qmp_output_visitor_cleanup(d->qov);
1050 qmp_input_visitor_cleanup(d->qiv);
2bd01ac1
SB
1051
1052 g_free(d);
2d496105
MR
1053}
1054
0d30b0a2 1055typedef struct StringSerializeData {
2bd01ac1 1056 char *string;
0d30b0a2
MR
1057 StringOutputVisitor *sov;
1058 StringInputVisitor *siv;
1059} StringSerializeData;
1060
1061static void string_serialize(void *native_in, void **datap,
1062 VisitorFunc visit, Error **errp)
1063{
1064 StringSerializeData *d = g_malloc0(sizeof(*d));
1065
0b7593e0 1066 d->sov = string_output_visitor_new(false);
0d30b0a2
MR
1067 visit(string_output_get_visitor(d->sov), &native_in, errp);
1068 *datap = d;
1069}
1070
1071static void string_deserialize(void **native_out, void *datap,
1072 VisitorFunc visit, Error **errp)
1073{
1074 StringSerializeData *d = datap;
1075
2bd01ac1
SB
1076 d->string = string_output_get_string(d->sov);
1077 d->siv = string_input_visitor_new(d->string);
0d30b0a2
MR
1078 visit(string_input_get_visitor(d->siv), native_out, errp);
1079}
1080
1081static void string_cleanup(void *datap)
1082{
1083 StringSerializeData *d = datap;
2bd01ac1 1084
0d30b0a2
MR
1085 string_output_visitor_cleanup(d->sov);
1086 string_input_visitor_cleanup(d->siv);
2bd01ac1
SB
1087 g_free(d->string);
1088 g_free(d);
0d30b0a2
MR
1089}
1090
2d496105
MR
1091/* visitor registration, test harness */
1092
1093/* note: to function interchangeably as a serialization mechanism your
1094 * visitor test implementation should pass the test cases for all visitor
1095 * capabilities: primitives, structures, and lists
1096 */
1097static const SerializeOps visitors[] = {
1098 {
1099 .type = "QMP",
1100 .serialize = qmp_serialize,
1101 .deserialize = qmp_deserialize,
1102 .cleanup = qmp_cleanup,
8addacdd
MR
1103 .caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
1104 VCAP_PRIMITIVE_LISTS
2d496105 1105 },
0d30b0a2
MR
1106 {
1107 .type = "String",
1108 .serialize = string_serialize,
1109 .deserialize = string_deserialize,
1110 .cleanup = string_cleanup,
1111 .caps = VCAP_PRIMITIVES
1112 },
2d496105
MR
1113 { NULL }
1114};
1115
1116static void add_visitor_type(const SerializeOps *ops)
1117{
1118 char testname_prefix[128];
1119 char testname[128];
1120 TestArgs *args;
1121 int i = 0;
1122
1123 sprintf(testname_prefix, "/visitor/serialization/%s", ops->type);
1124
1125 if (ops->caps & VCAP_PRIMITIVES) {
1126 while (pt_values[i].type != PTYPE_EOL) {
1127 sprintf(testname, "%s/primitives/%s", testname_prefix,
1128 pt_values[i].description);
1129 args = g_malloc0(sizeof(*args));
1130 args->ops = ops;
1131 args->test_data = &pt_values[i];
1132 g_test_add_data_func(testname, args, test_primitives);
1133 i++;
1134 }
1135 }
1136
1137 if (ops->caps & VCAP_STRUCTURES) {
1138 sprintf(testname, "%s/struct", testname_prefix);
1139 args = g_malloc0(sizeof(*args));
1140 args->ops = ops;
1141 args->test_data = NULL;
1142 g_test_add_data_func(testname, args, test_struct);
1143
1144 sprintf(testname, "%s/nested_struct", testname_prefix);
1145 args = g_malloc0(sizeof(*args));
1146 args->ops = ops;
1147 args->test_data = NULL;
1148 g_test_add_data_func(testname, args, test_nested_struct);
1149 }
1150
1151 if (ops->caps & VCAP_LISTS) {
1152 sprintf(testname, "%s/nested_struct_list", testname_prefix);
1153 args = g_malloc0(sizeof(*args));
1154 args->ops = ops;
1155 args->test_data = NULL;
1156 g_test_add_data_func(testname, args, test_nested_struct_list);
1157 }
8addacdd
MR
1158
1159 if (ops->caps & VCAP_PRIMITIVE_LISTS) {
1160 i = 0;
1161 while (pt_values[i].type != PTYPE_EOL) {
1162 sprintf(testname, "%s/primitive_list/%s", testname_prefix,
1163 pt_values[i].description);
1164 args = g_malloc0(sizeof(*args));
1165 args->ops = ops;
1166 args->test_data = &pt_values[i];
1167 g_test_add_data_func(testname, args, test_primitive_lists);
1168 i++;
1169 }
1170 }
2d496105
MR
1171}
1172
1173int main(int argc, char **argv)
1174{
1175 int i = 0;
1176
1177 g_test_init(&argc, &argv, NULL);
1178
1179 while (visitors[i].type != NULL) {
1180 add_visitor_type(&visitors[i]);
1181 i++;
1182 }
1183
1184 g_test_run();
1185
1186 return 0;
1187}