Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 1 | /* |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 2 | * QObject Input Visitor unit-tests (strict mode). |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 3 | * |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 4 | * Copyright (C) 2011-2012, 2015 Red Hat Inc. |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 8 | * Paolo Bonzini <pbonzini@redhat.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" |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 15 | |
Paolo Bonzini | 79ee7df | 2012-12-06 11:22:34 +0100 | [diff] [blame] | 16 | #include "qemu-common.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 18 | #include "qapi/qobject-input-visitor.h" |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 19 | #include "test-qapi-types.h" |
| 20 | #include "test-qapi-visit.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" |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 23 | #include "test-qmp-introspect.h" |
| 24 | #include "qmp-introspect.h" |
| 25 | #include "qapi-visit.h" |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 26 | |
| 27 | typedef struct TestInputVisitorData { |
| 28 | QObject *obj; |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 29 | Visitor *qiv; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 30 | } TestInputVisitorData; |
| 31 | |
| 32 | static void validate_teardown(TestInputVisitorData *data, |
| 33 | const void *unused) |
| 34 | { |
| 35 | qobject_decref(data->obj); |
| 36 | data->obj = NULL; |
| 37 | |
| 38 | if (data->qiv) { |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 39 | visit_free(data->qiv); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 40 | data->qiv = NULL; |
| 41 | } |
| 42 | } |
| 43 | |
Eric Blake | 0920a17 | 2015-11-05 23:35:28 -0700 | [diff] [blame] | 44 | /* The various test_init functions are provided instead of a test setup |
| 45 | function so that the JSON string used by the tests are kept in the test |
| 46 | functions (and not in main()). */ |
| 47 | static Visitor *validate_test_init_internal(TestInputVisitorData *data, |
| 48 | const char *json_string, |
| 49 | va_list *ap) |
| 50 | { |
Eric Blake | b18f114 | 2015-11-05 23:35:29 -0700 | [diff] [blame] | 51 | validate_teardown(data, NULL); |
| 52 | |
Eric Blake | 0920a17 | 2015-11-05 23:35:28 -0700 | [diff] [blame] | 53 | data->obj = qobject_from_jsonv(json_string, ap); |
| 54 | g_assert(data->obj); |
| 55 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 56 | data->qiv = qobject_input_visitor_new(data->obj, true); |
Eric Blake | 0920a17 | 2015-11-05 23:35:28 -0700 | [diff] [blame] | 57 | g_assert(data->qiv); |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 58 | return data->qiv; |
Eric Blake | 0920a17 | 2015-11-05 23:35:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 61 | static GCC_FMT_ATTR(2, 3) |
| 62 | Visitor *validate_test_init(TestInputVisitorData *data, |
| 63 | const char *json_string, ...) |
| 64 | { |
| 65 | Visitor *v; |
| 66 | va_list ap; |
| 67 | |
| 68 | va_start(ap, json_string); |
Eric Blake | 0920a17 | 2015-11-05 23:35:28 -0700 | [diff] [blame] | 69 | v = validate_test_init_internal(data, json_string, &ap); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 70 | va_end(ap); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 71 | return v; |
| 72 | } |
| 73 | |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 74 | /* similar to validate_test_init(), but does not expect a string |
| 75 | * literal/format json_string argument and so can be used for |
| 76 | * programatically generated strings (and we can't pass in programatically |
| 77 | * generated strings via %s format parameters since qobject_from_jsonv() |
| 78 | * will wrap those in double-quotes and treat the entire object as a |
| 79 | * string) |
| 80 | */ |
| 81 | static Visitor *validate_test_init_raw(TestInputVisitorData *data, |
| 82 | const char *json_string) |
| 83 | { |
Eric Blake | 0920a17 | 2015-11-05 23:35:28 -0700 | [diff] [blame] | 84 | return validate_test_init_internal(data, json_string, NULL); |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 87 | |
| 88 | static void test_validate_struct(TestInputVisitorData *data, |
| 89 | const void *unused) |
| 90 | { |
| 91 | TestStruct *p = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 92 | Visitor *v; |
| 93 | |
| 94 | v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); |
| 95 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 96 | visit_type_TestStruct(v, NULL, &p, &error_abort); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 97 | g_free(p->string); |
| 98 | g_free(p); |
| 99 | } |
| 100 | |
| 101 | static void test_validate_struct_nested(TestInputVisitorData *data, |
| 102 | const void *unused) |
| 103 | { |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 104 | UserDefTwo *udp = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 105 | Visitor *v; |
| 106 | |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 107 | v = validate_test_init(data, "{ 'string0': 'string0', " |
| 108 | "'dict1': { 'string1': 'string1', " |
| 109 | "'dict2': { 'userdef': { 'integer': 42, " |
| 110 | "'string': 'string' }, 'string': 'string2'}}}"); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 111 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 112 | visit_type_UserDefTwo(v, NULL, &udp, &error_abort); |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 113 | qapi_free_UserDefTwo(udp); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void test_validate_list(TestInputVisitorData *data, |
| 117 | const void *unused) |
| 118 | { |
| 119 | UserDefOneList *head = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 120 | Visitor *v; |
| 121 | |
| 122 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); |
| 123 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 124 | visit_type_UserDefOneList(v, NULL, &head, &error_abort); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 125 | qapi_free_UserDefOneList(head); |
| 126 | } |
| 127 | |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 128 | static void test_validate_union_native_list(TestInputVisitorData *data, |
| 129 | const void *unused) |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 130 | { |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 131 | UserDefNativeListUnion *tmp = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 132 | Visitor *v; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 133 | |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 134 | v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }"); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 135 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 136 | visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort); |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 137 | qapi_free_UserDefNativeListUnion(tmp); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 140 | static void test_validate_union_flat(TestInputVisitorData *data, |
| 141 | const void *unused) |
| 142 | { |
| 143 | UserDefFlatUnion *tmp = NULL; |
| 144 | Visitor *v; |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 145 | |
Wenchao Xia | 5223070 | 2014-03-04 18:44:39 -0800 | [diff] [blame] | 146 | v = validate_test_init(data, |
| 147 | "{ 'enum1': 'value1', " |
Markus Armbruster | 441cbac | 2015-09-16 13:06:10 +0200 | [diff] [blame] | 148 | "'integer': 41, " |
Wenchao Xia | 5223070 | 2014-03-04 18:44:39 -0800 | [diff] [blame] | 149 | "'string': 'str', " |
| 150 | "'boolean': true }"); |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 151 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 152 | visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 153 | qapi_free_UserDefFlatUnion(tmp); |
| 154 | } |
| 155 | |
Eric Blake | ab04526 | 2015-05-04 09:05:11 -0600 | [diff] [blame] | 156 | static void test_validate_alternate(TestInputVisitorData *data, |
| 157 | const void *unused) |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 158 | { |
Eric Blake | ab04526 | 2015-05-04 09:05:11 -0600 | [diff] [blame] | 159 | UserDefAlternate *tmp = NULL; |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 160 | Visitor *v; |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 161 | |
| 162 | v = validate_test_init(data, "42"); |
| 163 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 164 | visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); |
Eric Blake | ab04526 | 2015-05-04 09:05:11 -0600 | [diff] [blame] | 165 | qapi_free_UserDefAlternate(tmp); |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 168 | static void test_validate_fail_struct(TestInputVisitorData *data, |
| 169 | const void *unused) |
| 170 | { |
| 171 | TestStruct *p = NULL; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 172 | Error *err = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 173 | Visitor *v; |
| 174 | |
| 175 | v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); |
| 176 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 177 | visit_type_TestStruct(v, NULL, &p, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 178 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 179 | g_assert(!p); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void test_validate_fail_struct_nested(TestInputVisitorData *data, |
| 183 | const void *unused) |
| 184 | { |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 185 | UserDefTwo *udp = NULL; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 186 | Error *err = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 187 | Visitor *v; |
| 188 | |
| 189 | v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); |
| 190 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 191 | visit_type_UserDefTwo(v, NULL, &udp, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 192 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 193 | g_assert(!udp); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Markus Armbruster | bce3035 | 2016-10-04 17:23:50 +0200 | [diff] [blame] | 196 | static void test_validate_fail_struct_missing(TestInputVisitorData *data, |
| 197 | const void *unused) |
| 198 | { |
| 199 | Error *err = NULL; |
| 200 | Visitor *v; |
| 201 | QObject *any; |
| 202 | GenericAlternate *alt; |
| 203 | bool present; |
| 204 | int en; |
| 205 | int64_t i64; |
| 206 | uint32_t u32; |
| 207 | int8_t i8; |
| 208 | char *str; |
| 209 | double dbl; |
| 210 | |
| 211 | v = validate_test_init(data, "{}"); |
| 212 | visit_start_struct(v, NULL, NULL, 0, &error_abort); |
| 213 | visit_start_struct(v, "struct", NULL, 0, &err); |
| 214 | error_free_or_abort(&err); |
| 215 | visit_start_list(v, "list", NULL, 0, &err); |
| 216 | error_free_or_abort(&err); |
| 217 | visit_start_alternate(v, "alternate", &alt, sizeof(*alt), false, &err); |
| 218 | error_free_or_abort(&err); |
| 219 | visit_optional(v, "optional", &present); |
| 220 | g_assert(!present); |
| 221 | visit_type_enum(v, "enum", &en, EnumOne_lookup, &err); |
| 222 | error_free_or_abort(&err); |
| 223 | visit_type_int(v, "i64", &i64, &err); |
| 224 | error_free_or_abort(&err); |
| 225 | visit_type_uint32(v, "u32", &u32, &err); |
| 226 | error_free_or_abort(&err); |
| 227 | visit_type_int8(v, "i8", &i8, &err); |
| 228 | error_free_or_abort(&err); |
| 229 | visit_type_str(v, "i8", &str, &err); |
| 230 | error_free_or_abort(&err); |
| 231 | visit_type_number(v, "dbl", &dbl, &err); |
| 232 | error_free_or_abort(&err); |
| 233 | visit_type_any(v, "any", &any, &err); |
| 234 | error_free_or_abort(&err); |
| 235 | visit_type_null(v, "null", &err); |
| 236 | error_free_or_abort(&err); |
| 237 | visit_end_struct(v, NULL); |
| 238 | } |
| 239 | |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 240 | static void test_validate_fail_list(TestInputVisitorData *data, |
| 241 | const void *unused) |
| 242 | { |
| 243 | UserDefOneList *head = NULL; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 244 | Error *err = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 245 | Visitor *v; |
| 246 | |
| 247 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); |
| 248 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 249 | visit_type_UserDefOneList(v, NULL, &head, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 250 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 251 | g_assert(!head); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 254 | static void test_validate_fail_union_native_list(TestInputVisitorData *data, |
| 255 | const void *unused) |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 256 | { |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 257 | UserDefNativeListUnion *tmp = NULL; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 258 | Error *err = NULL; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 259 | Visitor *v; |
| 260 | |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 261 | v = validate_test_init(data, |
| 262 | "{ 'type': 'integer', 'data' : [ 'string' ] }"); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 263 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 264 | visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 265 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 266 | g_assert(!tmp); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 269 | static void test_validate_fail_union_flat(TestInputVisitorData *data, |
| 270 | const void *unused) |
| 271 | { |
| 272 | UserDefFlatUnion *tmp = NULL; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 273 | Error *err = NULL; |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 274 | Visitor *v; |
| 275 | |
| 276 | v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }"); |
| 277 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 278 | visit_type_UserDefFlatUnion(v, NULL, &tmp, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 279 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 280 | g_assert(!tmp); |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Michael Roth | cb55111 | 2014-09-18 15:36:42 -0500 | [diff] [blame] | 283 | static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data, |
| 284 | const void *unused) |
| 285 | { |
| 286 | UserDefFlatUnion2 *tmp = NULL; |
| 287 | Error *err = NULL; |
| 288 | Visitor *v; |
| 289 | |
| 290 | /* test situation where discriminator field ('enum1' here) is missing */ |
Markus Armbruster | 441cbac | 2015-09-16 13:06:10 +0200 | [diff] [blame] | 291 | v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); |
Michael Roth | cb55111 | 2014-09-18 15:36:42 -0500 | [diff] [blame] | 292 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 293 | visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 294 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 295 | g_assert(!tmp); |
Michael Roth | cb55111 | 2014-09-18 15:36:42 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Eric Blake | ab04526 | 2015-05-04 09:05:11 -0600 | [diff] [blame] | 298 | static void test_validate_fail_alternate(TestInputVisitorData *data, |
| 299 | const void *unused) |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 300 | { |
Eric Blake | e58d695 | 2016-04-28 15:45:10 -0600 | [diff] [blame] | 301 | UserDefAlternate *tmp; |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 302 | Visitor *v; |
Markus Armbruster | e940f54 | 2014-05-02 13:26:29 +0200 | [diff] [blame] | 303 | Error *err = NULL; |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 304 | |
| 305 | v = validate_test_init(data, "3.14"); |
| 306 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 307 | visit_type_UserDefAlternate(v, NULL, &tmp, &err); |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 308 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 309 | g_assert(!tmp); |
Markus Armbruster | 2c38b60 | 2014-03-01 08:40:30 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 312 | static void do_test_validate_qmp_introspect(TestInputVisitorData *data, |
| 313 | const char *schema_json) |
| 314 | { |
| 315 | SchemaInfoList *schema = NULL; |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 316 | Visitor *v; |
| 317 | |
| 318 | v = validate_test_init_raw(data, schema_json); |
| 319 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 320 | visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 321 | g_assert(schema); |
| 322 | |
| 323 | qapi_free_SchemaInfoList(schema); |
| 324 | } |
| 325 | |
| 326 | static void test_validate_qmp_introspect(TestInputVisitorData *data, |
| 327 | const void *unused) |
| 328 | { |
| 329 | do_test_validate_qmp_introspect(data, test_qmp_schema_json); |
| 330 | do_test_validate_qmp_introspect(data, qmp_schema_json); |
| 331 | } |
| 332 | |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 333 | static void validate_test_add(const char *testpath, |
| 334 | TestInputVisitorData *data, |
| 335 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) |
| 336 | { |
| 337 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, |
| 338 | validate_teardown); |
| 339 | } |
| 340 | |
| 341 | int main(int argc, char **argv) |
| 342 | { |
| 343 | TestInputVisitorData testdata; |
| 344 | |
| 345 | g_test_init(&argc, &argv, NULL); |
| 346 | |
| 347 | validate_test_add("/visitor/input-strict/pass/struct", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 348 | &testdata, test_validate_struct); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 349 | validate_test_add("/visitor/input-strict/pass/struct-nested", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 350 | &testdata, test_validate_struct_nested); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 351 | validate_test_add("/visitor/input-strict/pass/list", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 352 | &testdata, test_validate_list); |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 353 | validate_test_add("/visitor/input-strict/pass/union-flat", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 354 | &testdata, test_validate_union_flat); |
Eric Blake | ab04526 | 2015-05-04 09:05:11 -0600 | [diff] [blame] | 355 | validate_test_add("/visitor/input-strict/pass/alternate", |
| 356 | &testdata, test_validate_alternate); |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 357 | validate_test_add("/visitor/input-strict/pass/union-native-list", |
| 358 | &testdata, test_validate_union_native_list); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 359 | validate_test_add("/visitor/input-strict/fail/struct", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 360 | &testdata, test_validate_fail_struct); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 361 | validate_test_add("/visitor/input-strict/fail/struct-nested", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 362 | &testdata, test_validate_fail_struct_nested); |
Markus Armbruster | bce3035 | 2016-10-04 17:23:50 +0200 | [diff] [blame] | 363 | validate_test_add("/visitor/input-strict/fail/struct-missing", |
| 364 | &testdata, test_validate_fail_struct_missing); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 365 | validate_test_add("/visitor/input-strict/fail/list", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 366 | &testdata, test_validate_fail_list); |
Markus Armbruster | 2fc0043 | 2014-03-01 08:40:33 +0100 | [diff] [blame] | 367 | validate_test_add("/visitor/input-strict/fail/union-flat", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 368 | &testdata, test_validate_fail_union_flat); |
Michael Roth | cb55111 | 2014-09-18 15:36:42 -0500 | [diff] [blame] | 369 | validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator", |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 370 | &testdata, test_validate_fail_union_flat_no_discrim); |
Eric Blake | ab04526 | 2015-05-04 09:05:11 -0600 | [diff] [blame] | 371 | validate_test_add("/visitor/input-strict/fail/alternate", |
| 372 | &testdata, test_validate_fail_alternate); |
Eric Blake | 805017b | 2015-05-04 09:05:06 -0600 | [diff] [blame] | 373 | validate_test_add("/visitor/input-strict/fail/union-native-list", |
| 374 | &testdata, test_validate_fail_union_native_list); |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 375 | validate_test_add("/visitor/input-strict/pass/qmp-introspect", |
| 376 | &testdata, test_validate_qmp_introspect); |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 377 | |
| 378 | g_test_run(); |
| 379 | |
| 380 | return 0; |
| 381 | } |