]> git.proxmox.com Git - mirror_qemu.git/blame - tests/unit/test-visitor-serialization.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / tests / unit / 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 16
2d496105 17#include "test-qapi-visit.h"
da34e65c 18#include "qapi/error.h"
c7eb39cb 19#include "qapi/qmp/qjson.h"
fc81fa1e 20#include "qapi/qmp/qstring.h"
b3db211f
DB
21#include "qapi/qobject-input-visitor.h"
22#include "qapi/qobject-output-visitor.h"
0d30b0a2
MR
23#include "qapi/string-input-visitor.h"
24#include "qapi/string-output-visitor.h"
8addacdd
MR
25#include "qapi/dealloc-visitor.h"
26
27enum 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,
41};
2d496105
MR
42
43typedef 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;
2d496105 57 } value;
8addacdd 58 enum PrimitiveTypeKind type;
2d496105
MR
59 const char *description;
60} PrimitiveType;
61
8addacdd
MR
62typedef 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;
80
2d496105
MR
81/* test helpers */
82
8addacdd
MR
83typedef void (*VisitorFunc)(Visitor *v, void **native, Error **errp);
84
85static void dealloc_helper(void *native_in, VisitorFunc visit, Error **errp)
86{
2c0ef9f4 87 Visitor *v = qapi_dealloc_visitor_new();
8addacdd 88
2c0ef9f4 89 visit(v, &native_in, errp);
8addacdd 90
2c0ef9f4 91 visit_free(v);
8addacdd
MR
92}
93
2d496105
MR
94static void visit_primitive_type(Visitor *v, void **native, Error **errp)
95{
96 PrimitiveType *pt = *native;
97 switch(pt->type) {
98 case PTYPE_STRING:
51e72bc1 99 visit_type_str(v, NULL, (char **)&pt->value.string, errp);
2d496105
MR
100 break;
101 case PTYPE_BOOLEAN:
51e72bc1 102 visit_type_bool(v, NULL, &pt->value.boolean, errp);
2d496105
MR
103 break;
104 case PTYPE_NUMBER:
51e72bc1 105 visit_type_number(v, NULL, &pt->value.number, errp);
2d496105
MR
106 break;
107 case PTYPE_INTEGER:
51e72bc1 108 visit_type_int(v, NULL, &pt->value.integer, errp);
2d496105
MR
109 break;
110 case PTYPE_U8:
51e72bc1 111 visit_type_uint8(v, NULL, &pt->value.u8, errp);
2d496105
MR
112 break;
113 case PTYPE_U16:
51e72bc1 114 visit_type_uint16(v, NULL, &pt->value.u16, errp);
2d496105
MR
115 break;
116 case PTYPE_U32:
51e72bc1 117 visit_type_uint32(v, NULL, &pt->value.u32, errp);
2d496105
MR
118 break;
119 case PTYPE_U64:
51e72bc1 120 visit_type_uint64(v, NULL, &pt->value.u64, errp);
2d496105
MR
121 break;
122 case PTYPE_S8:
51e72bc1 123 visit_type_int8(v, NULL, &pt->value.s8, errp);
2d496105
MR
124 break;
125 case PTYPE_S16:
51e72bc1 126 visit_type_int16(v, NULL, &pt->value.s16, errp);
2d496105
MR
127 break;
128 case PTYPE_S32:
51e72bc1 129 visit_type_int32(v, NULL, &pt->value.s32, errp);
2d496105
MR
130 break;
131 case PTYPE_S64:
51e72bc1 132 visit_type_int64(v, NULL, &pt->value.s64, errp);
2d496105
MR
133 break;
134 case PTYPE_EOL:
dfc6f865 135 g_assert_not_reached();
2d496105
MR
136 }
137}
138
8addacdd
MR
139static void visit_primitive_list(Visitor *v, void **native, Error **errp)
140{
141 PrimitiveList *pl = *native;
142 switch (pl->type) {
143 case PTYPE_STRING:
51e72bc1 144 visit_type_strList(v, NULL, &pl->value.strings, errp);
8addacdd
MR
145 break;
146 case PTYPE_BOOLEAN:
51e72bc1 147 visit_type_boolList(v, NULL, &pl->value.booleans, errp);
8addacdd
MR
148 break;
149 case PTYPE_NUMBER:
51e72bc1 150 visit_type_numberList(v, NULL, &pl->value.numbers, errp);
8addacdd
MR
151 break;
152 case PTYPE_INTEGER:
51e72bc1 153 visit_type_intList(v, NULL, &pl->value.integers, errp);
8addacdd
MR
154 break;
155 case PTYPE_S8:
51e72bc1 156 visit_type_int8List(v, NULL, &pl->value.s8_integers, errp);
8addacdd
MR
157 break;
158 case PTYPE_S16:
51e72bc1 159 visit_type_int16List(v, NULL, &pl->value.s16_integers, errp);
8addacdd
MR
160 break;
161 case PTYPE_S32:
51e72bc1 162 visit_type_int32List(v, NULL, &pl->value.s32_integers, errp);
8addacdd
MR
163 break;
164 case PTYPE_S64:
51e72bc1 165 visit_type_int64List(v, NULL, &pl->value.s64_integers, errp);
8addacdd
MR
166 break;
167 case PTYPE_U8:
51e72bc1 168 visit_type_uint8List(v, NULL, &pl->value.u8_integers, errp);
8addacdd
MR
169 break;
170 case PTYPE_U16:
51e72bc1 171 visit_type_uint16List(v, NULL, &pl->value.u16_integers, errp);
8addacdd
MR
172 break;
173 case PTYPE_U32:
51e72bc1 174 visit_type_uint32List(v, NULL, &pl->value.u32_integers, errp);
8addacdd
MR
175 break;
176 case PTYPE_U64:
51e72bc1 177 visit_type_uint64List(v, NULL, &pl->value.u64_integers, errp);
8addacdd
MR
178 break;
179 default:
dfc6f865 180 g_assert_not_reached();
8addacdd
MR
181 }
182}
183
2d496105
MR
184
185static TestStruct *struct_create(void)
186{
187 TestStruct *ts = g_malloc0(sizeof(*ts));
188 ts->integer = -42;
189 ts->boolean = true;
190 ts->string = strdup("test string");
191 return ts;
192}
193
194static void struct_compare(TestStruct *ts1, TestStruct *ts2)
195{
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);
201}
202
203static void struct_cleanup(TestStruct *ts)
204{
205 g_free(ts->string);
206 g_free(ts);
207}
208
209static void visit_struct(Visitor *v, void **native, Error **errp)
210{
51e72bc1 211 visit_type_TestStruct(v, NULL, (TestStruct **)native, errp);
2d496105
MR
212}
213
b6fcf32d 214static UserDefTwo *nested_struct_create(void)
2d496105 215{
b6fcf32d 216 UserDefTwo *udnp = g_malloc0(sizeof(*udnp));
2d496105 217 udnp->string0 = strdup("test_string0");
6446a592
EB
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);
ddf21908 222 udnp->dict1->dict2->userdef->integer = 42;
6446a592
EB
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));
6446a592 226 udnp->dict1->dict3->userdef = g_new0(UserDefOne, 1);
ddf21908 227 udnp->dict1->dict3->userdef->integer = 43;
6446a592
EB
228 udnp->dict1->dict3->userdef->string = strdup("test_string");
229 udnp->dict1->dict3->string = strdup("test_string3");
2d496105
MR
230 return udnp;
231}
232
b6fcf32d 233static void nested_struct_compare(UserDefTwo *udnp1, UserDefTwo *udnp2)
2d496105
MR
234{
235 g_assert(udnp1);
236 g_assert(udnp2);
237 g_assert_cmpstr(udnp1->string0, ==, udnp2->string0);
6446a592 238 g_assert_cmpstr(udnp1->dict1->string1, ==, udnp2->dict1->string1);
ddf21908
EB
239 g_assert_cmpint(udnp1->dict1->dict2->userdef->integer, ==,
240 udnp2->dict1->dict2->userdef->integer);
6446a592
EB
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);
4b2fc7db 245 g_assert(!udnp1->dict1->dict3 == !udnp2->dict1->dict3);
ddf21908
EB
246 g_assert_cmpint(udnp1->dict1->dict3->userdef->integer, ==,
247 udnp2->dict1->dict3->userdef->integer);
6446a592
EB
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);
2d496105
MR
252}
253
b6fcf32d 254static void nested_struct_cleanup(UserDefTwo *udnp)
2d496105 255{
b6fcf32d 256 qapi_free_UserDefTwo(udnp);
2d496105
MR
257}
258
259static void visit_nested_struct(Visitor *v, void **native, Error **errp)
260{
51e72bc1 261 visit_type_UserDefTwo(v, NULL, (UserDefTwo **)native, errp);
2d496105
MR
262}
263
264static void visit_nested_struct_list(Visitor *v, void **native, Error **errp)
265{
51e72bc1 266 visit_type_UserDefTwoList(v, NULL, (UserDefTwoList **)native, errp);
2d496105
MR
267}
268
269/* test cases */
270
2d496105
MR
271typedef enum VisitorCapabilities {
272 VCAP_PRIMITIVES = 1,
273 VCAP_STRUCTURES = 2,
274 VCAP_LISTS = 4,
8addacdd 275 VCAP_PRIMITIVE_LISTS = 8,
2d496105
MR
276} VisitorCapabilities;
277
278typedef 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;
287
288typedef struct TestArgs {
289 const SerializeOps *ops;
290 void *test_data;
291} TestArgs;
292
2d496105
MR
293static void test_primitives(gconstpointer opaque)
294{
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));
2d496105 299 void *serialize_data;
2d496105
MR
300
301 pt_copy->type = pt->type;
3f66f764
EB
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);
2d496105 305
2d496105 306 g_assert(pt_copy != NULL);
28f1c1f6
MA
307 switch (pt->type) {
308 case PTYPE_STRING:
2d496105 309 g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string);
2bd01ac1 310 g_free((char *)pt_copy->value.string);
28f1c1f6
MA
311 break;
312 case PTYPE_BOOLEAN:
313 g_assert_cmpint(pt->value.boolean, ==, pt->value.boolean);
314 break;
315 case PTYPE_NUMBER:
2a02c139 316 g_assert_cmpfloat(pt->value.number, ==, pt_copy->value.number);
28f1c1f6
MA
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();
2d496105
MR
347 }
348
349 ops->cleanup(serialize_data);
350 g_free(args);
2bd01ac1 351 g_free(pt_copy);
2d496105
MR
352}
353
8addacdd
MR
354static void test_primitive_lists(gconstpointer opaque)
355{
356 TestArgs *args = (TestArgs *) opaque;
357 const SerializeOps *ops = args->ops;
358 PrimitiveType *pt = args->test_data;
748bfb4e
SW
359 PrimitiveList pl = { .value = { NULL } };
360 PrimitiveList pl_copy = { .value = { NULL } };
8addacdd 361 PrimitiveList *pl_copy_ptr = &pl_copy;
8addacdd
MR
362 void *serialize_data;
363 void *cur_head = NULL;
364 int i;
365
366 pl.type = pl_copy.type = pt->type;
367
368 /* build up our list of primitive types */
369 for (i = 0; i < 32; i++) {
370 switch (pl.type) {
371 case PTYPE_STRING: {
54aa3de7 372 QAPI_LIST_PREPEND(pl.value.strings, g_strdup(pt->value.string));
8addacdd
MR
373 break;
374 }
375 case PTYPE_INTEGER: {
54aa3de7 376 QAPI_LIST_PREPEND(pl.value.integers, pt->value.integer);
8addacdd
MR
377 break;
378 }
379 case PTYPE_S8: {
54aa3de7 380 QAPI_LIST_PREPEND(pl.value.s8_integers, pt->value.s8);
8addacdd
MR
381 break;
382 }
383 case PTYPE_S16: {
54aa3de7 384 QAPI_LIST_PREPEND(pl.value.s16_integers, pt->value.s16);
8addacdd
MR
385 break;
386 }
387 case PTYPE_S32: {
54aa3de7 388 QAPI_LIST_PREPEND(pl.value.s32_integers, pt->value.s32);
8addacdd
MR
389 break;
390 }
391 case PTYPE_S64: {
54aa3de7 392 QAPI_LIST_PREPEND(pl.value.s64_integers, pt->value.s64);
8addacdd
MR
393 break;
394 }
395 case PTYPE_U8: {
54aa3de7 396 QAPI_LIST_PREPEND(pl.value.u8_integers, pt->value.u8);
8addacdd
MR
397 break;
398 }
399 case PTYPE_U16: {
54aa3de7 400 QAPI_LIST_PREPEND(pl.value.u16_integers, pt->value.u16);
8addacdd
MR
401 break;
402 }
403 case PTYPE_U32: {
54aa3de7 404 QAPI_LIST_PREPEND(pl.value.u32_integers, pt->value.u32);
8addacdd
MR
405 break;
406 }
407 case PTYPE_U64: {
54aa3de7 408 QAPI_LIST_PREPEND(pl.value.u64_integers, pt->value.u64);
8addacdd
MR
409 break;
410 }
411 case PTYPE_NUMBER: {
54aa3de7 412 QAPI_LIST_PREPEND(pl.value.numbers, pt->value.number);
8addacdd
MR
413 break;
414 }
415 case PTYPE_BOOLEAN: {
54aa3de7 416 QAPI_LIST_PREPEND(pl.value.booleans, pt->value.boolean);
8addacdd
MR
417 break;
418 }
419 default:
dfc6f865 420 g_assert_not_reached();
8addacdd
MR
421 }
422 }
423
3f66f764
EB
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);
8addacdd 428
ac9e723f
PB
429
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();
469 }
8addacdd
MR
470
471 /* compare our deserialized list of primitives to the original */
ac9e723f
PB
472 i = 0;
473 while (cur_head) {
8addacdd
MR
474 switch (pl_copy.type) {
475 case PTYPE_STRING: {
ac9e723f
PB
476 strList *ptr = cur_head;
477 cur_head = ptr->next;
8addacdd
MR
478 g_assert_cmpstr(pt->value.string, ==, ptr->value);
479 break;
480 }
481 case PTYPE_INTEGER: {
ac9e723f
PB
482 intList *ptr = cur_head;
483 cur_head = ptr->next;
8addacdd
MR
484 g_assert_cmpint(pt->value.integer, ==, ptr->value);
485 break;
486 }
487 case PTYPE_S8: {
ac9e723f
PB
488 int8List *ptr = cur_head;
489 cur_head = ptr->next;
8addacdd
MR
490 g_assert_cmpint(pt->value.s8, ==, ptr->value);
491 break;
492 }
493 case PTYPE_S16: {
ac9e723f
PB
494 int16List *ptr = cur_head;
495 cur_head = ptr->next;
8addacdd
MR
496 g_assert_cmpint(pt->value.s16, ==, ptr->value);
497 break;
498 }
499 case PTYPE_S32: {
ac9e723f
PB
500 int32List *ptr = cur_head;
501 cur_head = ptr->next;
8addacdd
MR
502 g_assert_cmpint(pt->value.s32, ==, ptr->value);
503 break;
504 }
505 case PTYPE_S64: {
ac9e723f
PB
506 int64List *ptr = cur_head;
507 cur_head = ptr->next;
8addacdd
MR
508 g_assert_cmpint(pt->value.s64, ==, ptr->value);
509 break;
510 }
511 case PTYPE_U8: {
ac9e723f
PB
512 uint8List *ptr = cur_head;
513 cur_head = ptr->next;
8addacdd
MR
514 g_assert_cmpint(pt->value.u8, ==, ptr->value);
515 break;
516 }
517 case PTYPE_U16: {
ac9e723f
PB
518 uint16List *ptr = cur_head;
519 cur_head = ptr->next;
8addacdd
MR
520 g_assert_cmpint(pt->value.u16, ==, ptr->value);
521 break;
522 }
523 case PTYPE_U32: {
ac9e723f
PB
524 uint32List *ptr = cur_head;
525 cur_head = ptr->next;
8addacdd
MR
526 g_assert_cmpint(pt->value.u32, ==, ptr->value);
527 break;
528 }
529 case PTYPE_U64: {
ac9e723f
PB
530 uint64List *ptr = cur_head;
531 cur_head = ptr->next;
8addacdd
MR
532 g_assert_cmpint(pt->value.u64, ==, ptr->value);
533 break;
534 }
535 case PTYPE_NUMBER: {
8addacdd
MR
536 GString *double_expected = g_string_new("");
537 GString *double_actual = g_string_new("");
ac9e723f
PB
538 numberList *ptr = cur_head;
539 cur_head = ptr->next;
8addacdd
MR
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
543 */
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;
550 }
551 case PTYPE_BOOLEAN: {
ac9e723f
PB
552 boolList *ptr = cur_head;
553 cur_head = ptr->next;
8addacdd
MR
554 g_assert_cmpint(!!pt->value.boolean, ==, !!ptr->value);
555 break;
556 }
557 default:
dfc6f865 558 g_assert_not_reached();
8addacdd
MR
559 }
560 i++;
ac9e723f 561 }
8addacdd 562
ac9e723f 563 g_assert_cmpint(i, ==, 32);
8addacdd
MR
564
565 ops->cleanup(serialize_data);
3f66f764
EB
566 dealloc_helper(&pl, visit_primitive_list, &error_abort);
567 dealloc_helper(&pl_copy, visit_primitive_list, &error_abort);
8addacdd
MR
568 g_free(args);
569}
570
2d496105
MR
571static void test_struct(gconstpointer opaque)
572{
573 TestArgs *args = (TestArgs *) opaque;
574 const SerializeOps *ops = args->ops;
575 TestStruct *ts = struct_create();
576 TestStruct *ts_copy = NULL;
2d496105
MR
577 void *serialize_data;
578
3f66f764
EB
579 ops->serialize(ts, &serialize_data, visit_struct, &error_abort);
580 ops->deserialize((void **)&ts_copy, serialize_data, visit_struct,
581 &error_abort);
2d496105 582
2d496105
MR
583 struct_compare(ts, ts_copy);
584
585 struct_cleanup(ts);
586 struct_cleanup(ts_copy);
587
588 ops->cleanup(serialize_data);
589 g_free(args);
590}
591
592static void test_nested_struct(gconstpointer opaque)
593{
594 TestArgs *args = (TestArgs *) opaque;
595 const SerializeOps *ops = args->ops;
b6fcf32d
EB
596 UserDefTwo *udnp = nested_struct_create();
597 UserDefTwo *udnp_copy = NULL;
2d496105 598 void *serialize_data;
b6fcf32d 599
3f66f764 600 ops->serialize(udnp, &serialize_data, visit_nested_struct, &error_abort);
b6fcf32d 601 ops->deserialize((void **)&udnp_copy, serialize_data, visit_nested_struct,
3f66f764 602 &error_abort);
2d496105 603
2d496105
MR
604 nested_struct_compare(udnp, udnp_copy);
605
606 nested_struct_cleanup(udnp);
607 nested_struct_cleanup(udnp_copy);
608
609 ops->cleanup(serialize_data);
610 g_free(args);
611}
612
613static void test_nested_struct_list(gconstpointer opaque)
614{
615 TestArgs *args = (TestArgs *) opaque;
616 const SerializeOps *ops = args->ops;
b6fcf32d 617 UserDefTwoList *listp = NULL, *tmp, *tmp_copy, *listp_copy = NULL;
2d496105
MR
618 void *serialize_data;
619 int i = 0;
620
621 for (i = 0; i < 8; i++) {
54aa3de7 622 QAPI_LIST_PREPEND(listp, nested_struct_create());
2d496105 623 }
b6fcf32d 624
3f66f764
EB
625 ops->serialize(listp, &serialize_data, visit_nested_struct_list,
626 &error_abort);
2d496105 627 ops->deserialize((void **)&listp_copy, serialize_data,
3f66f764 628 visit_nested_struct_list, &error_abort);
2d496105
MR
629
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;
637 }
638
b6fcf32d
EB
639 qapi_free_UserDefTwoList(tmp);
640 qapi_free_UserDefTwoList(tmp_copy);
2d496105
MR
641
642 ops->cleanup(serialize_data);
643 g_free(args);
644}
645
748bfb4e 646static PrimitiveType pt_values[] = {
2d496105
MR
647 /* string tests */
648 {
649 .description = "string_empty",
650 .type = PTYPE_STRING,
651 .value.string = "",
652 },
653 {
654 .description = "string_whitespace",
655 .type = PTYPE_STRING,
656 .value.string = "a b c\td",
657 },
658 {
659 .description = "string_newlines",
660 .type = PTYPE_STRING,
661 .value.string = "a\nb\n",
662 },
663 {
664 .description = "string_commas",
665 .type = PTYPE_STRING,
666 .value.string = "a,b, c,d",
667 },
668 {
669 .description = "string_single_quoted",
670 .type = PTYPE_STRING,
671 .value.string = "'a b',cd",
672 },
673 {
674 .description = "string_double_quoted",
675 .type = PTYPE_STRING,
676 .value.string = "\"a b\",cd",
677 },
678 /* boolean tests */
679 {
680 .description = "boolean_true1",
681 .type = PTYPE_BOOLEAN,
682 .value.boolean = true,
683 },
684 {
685 .description = "boolean_true2",
686 .type = PTYPE_BOOLEAN,
687 .value.boolean = 8,
688 },
689 {
690 .description = "boolean_true3",
691 .type = PTYPE_BOOLEAN,
692 .value.boolean = -1,
693 },
694 {
695 .description = "boolean_false1",
696 .type = PTYPE_BOOLEAN,
697 .value.boolean = false,
698 },
699 {
700 .description = "boolean_false2",
701 .type = PTYPE_BOOLEAN,
702 .value.boolean = 0,
703 },
704 /* number tests (double) */
2d496105
MR
705 {
706 .description = "number_sanity1",
707 .type = PTYPE_NUMBER,
708 .value.number = -1,
709 },
710 {
711 .description = "number_sanity2",
712 .type = PTYPE_NUMBER,
2a02c139 713 .value.number = 3.141593,
2d496105
MR
714 },
715 {
716 .description = "number_min",
717 .type = PTYPE_NUMBER,
718 .value.number = DBL_MIN,
719 },
720 {
721 .description = "number_max",
722 .type = PTYPE_NUMBER,
723 .value.number = DBL_MAX,
724 },
725 /* integer tests (int64) */
726 {
727 .description = "integer_sanity1",
728 .type = PTYPE_INTEGER,
729 .value.integer = -1,
730 },
731 {
732 .description = "integer_sanity2",
733 .type = PTYPE_INTEGER,
734 .value.integer = INT64_MAX / 2 + 1,
735 },
736 {
737 .description = "integer_min",
738 .type = PTYPE_INTEGER,
739 .value.integer = INT64_MIN,
740 },
741 {
742 .description = "integer_max",
743 .type = PTYPE_INTEGER,
744 .value.integer = INT64_MAX,
745 },
746 /* uint8 tests */
747 {
748 .description = "uint8_sanity1",
749 .type = PTYPE_U8,
750 .value.u8 = 1,
751 },
752 {
753 .description = "uint8_sanity2",
754 .type = PTYPE_U8,
755 .value.u8 = UINT8_MAX / 2 + 1,
756 },
757 {
758 .description = "uint8_min",
759 .type = PTYPE_U8,
760 .value.u8 = 0,
761 },
762 {
763 .description = "uint8_max",
764 .type = PTYPE_U8,
765 .value.u8 = UINT8_MAX,
766 },
767 /* uint16 tests */
768 {
769 .description = "uint16_sanity1",
770 .type = PTYPE_U16,
771 .value.u16 = 1,
772 },
773 {
774 .description = "uint16_sanity2",
775 .type = PTYPE_U16,
776 .value.u16 = UINT16_MAX / 2 + 1,
777 },
778 {
779 .description = "uint16_min",
780 .type = PTYPE_U16,
781 .value.u16 = 0,
782 },
783 {
784 .description = "uint16_max",
785 .type = PTYPE_U16,
786 .value.u16 = UINT16_MAX,
787 },
788 /* uint32 tests */
789 {
790 .description = "uint32_sanity1",
791 .type = PTYPE_U32,
792 .value.u32 = 1,
793 },
794 {
795 .description = "uint32_sanity2",
796 .type = PTYPE_U32,
797 .value.u32 = UINT32_MAX / 2 + 1,
798 },
799 {
800 .description = "uint32_min",
801 .type = PTYPE_U32,
802 .value.u32 = 0,
803 },
804 {
805 .description = "uint32_max",
806 .type = PTYPE_U32,
807 .value.u32 = UINT32_MAX,
808 },
809 /* uint64 tests */
810 {
811 .description = "uint64_sanity1",
812 .type = PTYPE_U64,
813 .value.u64 = 1,
814 },
815 {
816 .description = "uint64_sanity2",
817 .type = PTYPE_U64,
818 .value.u64 = UINT64_MAX / 2 + 1,
819 },
820 {
821 .description = "uint64_min",
822 .type = PTYPE_U64,
823 .value.u64 = 0,
824 },
825 {
826 .description = "uint64_max",
827 .type = PTYPE_U64,
828 .value.u64 = UINT64_MAX,
829 },
830 /* int8 tests */
831 {
832 .description = "int8_sanity1",
833 .type = PTYPE_S8,
834 .value.s8 = -1,
835 },
836 {
837 .description = "int8_sanity2",
838 .type = PTYPE_S8,
839 .value.s8 = INT8_MAX / 2 + 1,
840 },
841 {
842 .description = "int8_min",
843 .type = PTYPE_S8,
844 .value.s8 = INT8_MIN,
845 },
846 {
847 .description = "int8_max",
848 .type = PTYPE_S8,
849 .value.s8 = INT8_MAX,
850 },
851 /* int16 tests */
852 {
853 .description = "int16_sanity1",
854 .type = PTYPE_S16,
855 .value.s16 = -1,
856 },
857 {
858 .description = "int16_sanity2",
859 .type = PTYPE_S16,
860 .value.s16 = INT16_MAX / 2 + 1,
861 },
862 {
863 .description = "int16_min",
864 .type = PTYPE_S16,
865 .value.s16 = INT16_MIN,
866 },
867 {
868 .description = "int16_max",
869 .type = PTYPE_S16,
870 .value.s16 = INT16_MAX,
871 },
872 /* int32 tests */
873 {
874 .description = "int32_sanity1",
875 .type = PTYPE_S32,
876 .value.s32 = -1,
877 },
878 {
879 .description = "int32_sanity2",
880 .type = PTYPE_S32,
881 .value.s32 = INT32_MAX / 2 + 1,
882 },
883 {
884 .description = "int32_min",
885 .type = PTYPE_S32,
886 .value.s32 = INT32_MIN,
887 },
888 {
889 .description = "int32_max",
890 .type = PTYPE_S32,
891 .value.s32 = INT32_MAX,
892 },
893 /* int64 tests */
894 {
895 .description = "int64_sanity1",
896 .type = PTYPE_S64,
897 .value.s64 = -1,
898 },
899 {
900 .description = "int64_sanity2",
901 .type = PTYPE_S64,
902 .value.s64 = INT64_MAX / 2 + 1,
903 },
904 {
905 .description = "int64_min",
906 .type = PTYPE_S64,
907 .value.s64 = INT64_MIN,
908 },
909 {
910 .description = "int64_max",
911 .type = PTYPE_S64,
912 .value.s64 = INT64_MAX,
913 },
914 { .type = PTYPE_EOL }
915};
916
917/* visitor-specific op implementations */
918
919typedef struct QmpSerializeData {
3b098d56
EB
920 Visitor *qov;
921 QObject *obj;
b70ce101 922 Visitor *qiv;
2d496105
MR
923} QmpSerializeData;
924
925static void qmp_serialize(void *native_in, void **datap,
926 VisitorFunc visit, Error **errp)
927{
928 QmpSerializeData *d = g_malloc0(sizeof(*d));
929
7d5e199a 930 d->qov = qobject_output_visitor_new(&d->obj);
3b098d56 931 visit(d->qov, &native_in, errp);
2d496105
MR
932 *datap = d;
933}
934
935static void qmp_deserialize(void **native_out, void *datap,
936 VisitorFunc visit, Error **errp)
937{
938 QmpSerializeData *d = datap;
eab3a467 939 GString *output_json;
ad7f375d
MR
940 QObject *obj_orig, *obj;
941
3b098d56
EB
942 visit_complete(d->qov, &d->obj);
943 obj_orig = d->obj;
ad7f375d 944 output_json = qobject_to_json(obj_orig);
eab3a467 945 obj = qobject_from_json(output_json->str, &error_abort);
2d496105 946
eab3a467 947 g_string_free(output_json, true);
048abb7b 948 d->qiv = qobject_input_visitor_new(obj);
cb3e7f08
MAL
949 qobject_unref(obj_orig);
950 qobject_unref(obj);
b70ce101 951 visit(d->qiv, native_out, errp);
2d496105
MR
952}
953
954static void qmp_cleanup(void *datap)
955{
956 QmpSerializeData *d = datap;
3b098d56 957 visit_free(d->qov);
b70ce101 958 visit_free(d->qiv);
2bd01ac1
SB
959
960 g_free(d);
2d496105
MR
961}
962
0d30b0a2 963typedef struct StringSerializeData {
2bd01ac1 964 char *string;
3b098d56 965 Visitor *sov;
7a0525c7 966 Visitor *siv;
0d30b0a2
MR
967} StringSerializeData;
968
969static void string_serialize(void *native_in, void **datap,
970 VisitorFunc visit, Error **errp)
971{
972 StringSerializeData *d = g_malloc0(sizeof(*d));
973
3b098d56
EB
974 d->sov = string_output_visitor_new(false, &d->string);
975 visit(d->sov, &native_in, errp);
0d30b0a2
MR
976 *datap = d;
977}
978
979static void string_deserialize(void **native_out, void *datap,
980 VisitorFunc visit, Error **errp)
981{
982 StringSerializeData *d = datap;
983
3b098d56 984 visit_complete(d->sov, &d->string);
2bd01ac1 985 d->siv = string_input_visitor_new(d->string);
7a0525c7 986 visit(d->siv, native_out, errp);
0d30b0a2
MR
987}
988
989static void string_cleanup(void *datap)
990{
991 StringSerializeData *d = datap;
2bd01ac1 992
3b098d56 993 visit_free(d->sov);
7a0525c7 994 visit_free(d->siv);
2bd01ac1
SB
995 g_free(d->string);
996 g_free(d);
0d30b0a2
MR
997}
998
2d496105
MR
999/* visitor registration, test harness */
1000
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
1004 */
1005static const SerializeOps visitors[] = {
1006 {
1007 .type = "QMP",
1008 .serialize = qmp_serialize,
1009 .deserialize = qmp_deserialize,
1010 .cleanup = qmp_cleanup,
8addacdd
MR
1011 .caps = VCAP_PRIMITIVES | VCAP_STRUCTURES | VCAP_LISTS |
1012 VCAP_PRIMITIVE_LISTS
2d496105 1013 },
0d30b0a2
MR
1014 {
1015 .type = "String",
1016 .serialize = string_serialize,
1017 .deserialize = string_deserialize,
1018 .cleanup = string_cleanup,
1019 .caps = VCAP_PRIMITIVES
1020 },
2d496105
MR
1021 { NULL }
1022};
1023
1024static void add_visitor_type(const SerializeOps *ops)
1025{
fdf235ba 1026 char testname_prefix[32];
2d496105
MR
1027 char testname[128];
1028 TestArgs *args;
1029 int i = 0;
1030
1031 sprintf(testname_prefix, "/visitor/serialization/%s", ops->type);
1032
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++;
1042 }
1043 }
1044
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);
1051
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);
1057 }
1058
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);
1065 }
8addacdd
MR
1066
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++;
1077 }
1078 }
2d496105
MR
1079}
1080
1081int main(int argc, char **argv)
1082{
1083 int i = 0;
1084
1085 g_test_init(&argc, &argv, NULL);
1086
1087 while (visitors[i].type != NULL) {
1088 add_visitor_type(&visitors[i]);
1089 i++;
1090 }
1091
1092 g_test_run();
1093
1094 return 0;
1095}