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