net: store guest timestamp in dump file instead of time since guest startup
[qemu/rayw.git] / test-qmp-input-visitor.c
blob1f3ab031f7ae313b154dcd882c51f1d38e07a10d
1 /*
2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include <glib.h>
14 #include <stdarg.h>
16 #include "qapi/qmp-input-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
21 typedef struct TestInputVisitorData {
22 QObject *obj;
23 QmpInputVisitor *qiv;
24 } TestInputVisitorData;
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
29 qobject_decref(data->obj);
30 data->obj = NULL;
32 if (data->qiv) {
33 qmp_input_visitor_cleanup(data->qiv);
34 data->qiv = NULL;
38 /* This is provided instead of a test setup function so that the JSON
39 string used by the tests are kept in the test functions (and not
40 int main()) */
41 static Visitor *visitor_input_test_init(TestInputVisitorData *data,
42 const char *json_string, ...)
44 Visitor *v;
45 va_list ap;
47 va_start(ap, json_string);
48 data->obj = qobject_from_jsonv(json_string, &ap);
49 va_end(ap);
51 g_assert(data->obj != NULL);
53 data->qiv = qmp_input_visitor_new(data->obj);
54 g_assert(data->qiv != NULL);
56 v = qmp_input_get_visitor(data->qiv);
57 g_assert(v != NULL);
59 return v;
62 static void test_visitor_in_int(TestInputVisitorData *data,
63 const void *unused)
65 int64_t res = 0, value = -42;
66 Error *errp = NULL;
67 Visitor *v;
69 v = visitor_input_test_init(data, "%d", value);
71 visit_type_int(v, &res, NULL, &errp);
72 g_assert(!error_is_set(&errp));
73 g_assert_cmpint(res, ==, value);
76 static void test_visitor_in_bool(TestInputVisitorData *data,
77 const void *unused)
79 Error *errp = NULL;
80 bool res = false;
81 Visitor *v;
83 v = visitor_input_test_init(data, "true");
85 visit_type_bool(v, &res, NULL, &errp);
86 g_assert(!error_is_set(&errp));
87 g_assert_cmpint(res, ==, true);
90 static void test_visitor_in_number(TestInputVisitorData *data,
91 const void *unused)
93 double res = 0, value = 3.14;
94 Error *errp = NULL;
95 Visitor *v;
97 v = visitor_input_test_init(data, "%f", value);
99 visit_type_number(v, &res, NULL, &errp);
100 g_assert(!error_is_set(&errp));
101 g_assert_cmpfloat(res, ==, value);
104 static void test_visitor_in_string(TestInputVisitorData *data,
105 const void *unused)
107 char *res = NULL, *value = (char *) "Q E M U";
108 Error *errp = NULL;
109 Visitor *v;
111 v = visitor_input_test_init(data, "%s", value);
113 visit_type_str(v, &res, NULL, &errp);
114 g_assert(!error_is_set(&errp));
115 g_assert_cmpstr(res, ==, value);
117 g_free(res);
120 static void test_visitor_in_enum(TestInputVisitorData *data,
121 const void *unused)
123 Error *errp = NULL;
124 Visitor *v;
125 EnumOne i;
127 for (i = 0; EnumOne_lookup[i]; i++) {
128 EnumOne res = -1;
130 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
132 visit_type_EnumOne(v, &res, NULL, &errp);
133 g_assert(!error_is_set(&errp));
134 g_assert_cmpint(i, ==, res);
136 visitor_input_teardown(data, NULL);
139 data->obj = NULL;
140 data->qiv = NULL;
143 typedef struct TestStruct
145 int64_t integer;
146 bool boolean;
147 char *string;
148 } TestStruct;
150 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
151 const char *name, Error **errp)
153 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
154 errp);
156 visit_type_int(v, &(*obj)->integer, "integer", errp);
157 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
158 visit_type_str(v, &(*obj)->string, "string", errp);
160 visit_end_struct(v, errp);
163 static void test_visitor_in_struct(TestInputVisitorData *data,
164 const void *unused)
166 TestStruct *p = NULL;
167 Error *errp = NULL;
168 Visitor *v;
170 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
172 visit_type_TestStruct(v, &p, NULL, &errp);
173 g_assert(!error_is_set(&errp));
174 g_assert_cmpint(p->integer, ==, -42);
175 g_assert(p->boolean == true);
176 g_assert_cmpstr(p->string, ==, "foo");
178 g_free(p->string);
179 g_free(p);
182 static void check_and_free_str(char *str, const char *cmp)
184 g_assert_cmpstr(str, ==, cmp);
185 g_free(str);
188 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
189 const void *unused)
191 UserDefNested *udp = NULL;
192 Error *errp = NULL;
193 Visitor *v;
195 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
197 visit_type_UserDefNested(v, &udp, NULL, &errp);
198 g_assert(!error_is_set(&errp));
200 check_and_free_str(udp->string0, "string0");
201 check_and_free_str(udp->dict1.string1, "string1");
202 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
203 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
204 check_and_free_str(udp->dict1.dict2.string2, "string2");
205 g_assert(udp->dict1.has_dict3 == false);
207 g_free(udp->dict1.dict2.userdef1);
208 g_free(udp);
211 static void test_visitor_in_list(TestInputVisitorData *data,
212 const void *unused)
214 UserDefOneList *item, *head = NULL;
215 Error *errp = NULL;
216 Visitor *v;
217 int i;
219 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
221 visit_type_UserDefOneList(v, &head, NULL, &errp);
222 g_assert(!error_is_set(&errp));
223 g_assert(head != NULL);
225 for (i = 0, item = head; item; item = item->next, i++) {
226 char string[12];
228 snprintf(string, sizeof(string), "string%d", i);
229 g_assert_cmpstr(item->value->string, ==, string);
230 g_assert_cmpint(item->value->integer, ==, 42 + i);
233 qapi_free_UserDefOneList(head);
236 static void input_visitor_test_add(const char *testpath,
237 TestInputVisitorData *data,
238 void (*test_func)(TestInputVisitorData *data, const void *user_data))
240 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
241 visitor_input_teardown);
244 int main(int argc, char **argv)
246 TestInputVisitorData in_visitor_data;
248 g_test_init(&argc, &argv, NULL);
250 input_visitor_test_add("/visitor/input/int",
251 &in_visitor_data, test_visitor_in_int);
252 input_visitor_test_add("/visitor/input/bool",
253 &in_visitor_data, test_visitor_in_bool);
254 input_visitor_test_add("/visitor/input/number",
255 &in_visitor_data, test_visitor_in_number);
256 input_visitor_test_add("/visitor/input/string",
257 &in_visitor_data, test_visitor_in_string);
258 input_visitor_test_add("/visitor/input/enum",
259 &in_visitor_data, test_visitor_in_enum);
260 input_visitor_test_add("/visitor/input/struct",
261 &in_visitor_data, test_visitor_in_struct);
262 input_visitor_test_add("/visitor/input/struct-nested",
263 &in_visitor_data, test_visitor_in_struct_nested);
264 input_visitor_test_add("/visitor/input/list",
265 &in_visitor_data, test_visitor_in_list);
267 g_test_run();
269 return 0;