Merge remote-tracking branch 'qmp/queue/qmp' into staging
[qemu/opensuse.git] / test-qmp-input-strict.c
blobf6df8cbe1edc92c017f8ac3e31dff76ac75cfa00
1 /*
2 * QMP Input Visitor unit-tests (strict mode).
4 * Copyright (C) 2011-2012 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include <glib.h>
15 #include <stdarg.h>
17 #include "qapi/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qemu-objects.h"
22 typedef struct TestInputVisitorData {
23 QObject *obj;
24 QmpInputVisitor *qiv;
25 } TestInputVisitorData;
27 static void validate_teardown(TestInputVisitorData *data,
28 const void *unused)
30 qobject_decref(data->obj);
31 data->obj = NULL;
33 if (data->qiv) {
34 qmp_input_visitor_cleanup(data->qiv);
35 data->qiv = NULL;
39 /* This is provided instead of a test setup function so that the JSON
40 string used by the tests are kept in the test functions (and not
41 int main()) */
42 static GCC_FMT_ATTR(2, 3)
43 Visitor *validate_test_init(TestInputVisitorData *data,
44 const char *json_string, ...)
46 Visitor *v;
47 va_list ap;
49 va_start(ap, json_string);
50 data->obj = qobject_from_jsonv(json_string, &ap);
51 va_end(ap);
53 g_assert(data->obj != NULL);
55 data->qiv = qmp_input_visitor_new_strict(data->obj);
56 g_assert(data->qiv != NULL);
58 v = qmp_input_get_visitor(data->qiv);
59 g_assert(v != NULL);
61 return v;
64 typedef struct TestStruct
66 int64_t integer;
67 bool boolean;
68 char *string;
69 } TestStruct;
71 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
72 const char *name, Error **errp)
74 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
75 errp);
77 visit_type_int(v, &(*obj)->integer, "integer", errp);
78 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
79 visit_type_str(v, &(*obj)->string, "string", errp);
81 visit_end_struct(v, errp);
84 static void test_validate_struct(TestInputVisitorData *data,
85 const void *unused)
87 TestStruct *p = NULL;
88 Error *errp = NULL;
89 Visitor *v;
91 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
93 visit_type_TestStruct(v, &p, NULL, &errp);
94 g_assert(!error_is_set(&errp));
95 g_free(p->string);
96 g_free(p);
99 static void test_validate_struct_nested(TestInputVisitorData *data,
100 const void *unused)
102 UserDefNested *udp = NULL;
103 Error *errp = NULL;
104 Visitor *v;
106 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
108 visit_type_UserDefNested(v, &udp, NULL, &errp);
109 g_assert(!error_is_set(&errp));
110 qapi_free_UserDefNested(udp);
113 static void test_validate_list(TestInputVisitorData *data,
114 const void *unused)
116 UserDefOneList *head = NULL;
117 Error *errp = NULL;
118 Visitor *v;
120 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
122 visit_type_UserDefOneList(v, &head, NULL, &errp);
123 g_assert(!error_is_set(&errp));
124 qapi_free_UserDefOneList(head);
127 static void test_validate_union(TestInputVisitorData *data,
128 const void *unused)
130 UserDefUnion *tmp = NULL;
131 Visitor *v;
132 Error *errp = NULL;
134 v = validate_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
136 visit_type_UserDefUnion(v, &tmp, NULL, &errp);
137 g_assert(!error_is_set(&errp));
138 qapi_free_UserDefUnion(tmp);
141 static void test_validate_fail_struct(TestInputVisitorData *data,
142 const void *unused)
144 TestStruct *p = NULL;
145 Error *errp = NULL;
146 Visitor *v;
148 v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
150 visit_type_TestStruct(v, &p, NULL, &errp);
151 g_assert(error_is_set(&errp));
152 if (p) {
153 g_free(p->string);
155 g_free(p);
158 static void test_validate_fail_struct_nested(TestInputVisitorData *data,
159 const void *unused)
161 UserDefNested *udp = NULL;
162 Error *errp = NULL;
163 Visitor *v;
165 v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
167 visit_type_UserDefNested(v, &udp, NULL, &errp);
168 g_assert(error_is_set(&errp));
169 qapi_free_UserDefNested(udp);
172 static void test_validate_fail_list(TestInputVisitorData *data,
173 const void *unused)
175 UserDefOneList *head = NULL;
176 Error *errp = NULL;
177 Visitor *v;
179 v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
181 visit_type_UserDefOneList(v, &head, NULL, &errp);
182 g_assert(error_is_set(&errp));
183 qapi_free_UserDefOneList(head);
186 static void test_validate_fail_union(TestInputVisitorData *data,
187 const void *unused)
189 UserDefUnion *tmp = NULL;
190 Error *errp = NULL;
191 Visitor *v;
193 v = validate_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 }, 'extra': 'yyy' }");
195 visit_type_UserDefUnion(v, &tmp, NULL, &errp);
196 g_assert(error_is_set(&errp));
197 qapi_free_UserDefUnion(tmp);
200 static void validate_test_add(const char *testpath,
201 TestInputVisitorData *data,
202 void (*test_func)(TestInputVisitorData *data, const void *user_data))
204 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
205 validate_teardown);
208 int main(int argc, char **argv)
210 TestInputVisitorData testdata;
212 g_test_init(&argc, &argv, NULL);
214 validate_test_add("/visitor/input-strict/pass/struct",
215 &testdata, test_validate_struct);
216 validate_test_add("/visitor/input-strict/pass/struct-nested",
217 &testdata, test_validate_struct_nested);
218 validate_test_add("/visitor/input-strict/pass/list",
219 &testdata, test_validate_list);
220 validate_test_add("/visitor/input-strict/pass/union",
221 &testdata, test_validate_union);
222 validate_test_add("/visitor/input-strict/fail/struct",
223 &testdata, test_validate_fail_struct);
224 validate_test_add("/visitor/input-strict/fail/struct-nested",
225 &testdata, test_validate_fail_struct_nested);
226 validate_test_add("/visitor/input-strict/fail/list",
227 &testdata, test_validate_fail_list);
228 validate_test_add("/visitor/input-strict/fail/union",
229 &testdata, test_validate_fail_union);
231 g_test_run();
233 return 0;