vfio: remove bootindex property from qdev to qom
[qemu.git] / tests / test-qmp-event.c
blobcb354e6e81944c4b52320408d67d5b6ef786a06e
1 /*
2 * qapi event unit-tests.
4 * Copyright (c) 2014 Wenchao Xia
6 * Authors:
7 * Wenchao Xia <wenchaoqemu@gmail.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include <glib.h>
15 #include <stdarg.h>
17 #include "qemu-common.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "test-qapi-event.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qint.h"
23 #include "qapi/qmp/qobject.h"
24 #include "qapi/qmp-event.h"
26 typedef struct TestEventData {
27 QDict *expect;
28 } TestEventData;
30 typedef struct QDictCmpData {
31 QDict *expect;
32 bool result;
33 } QDictCmpData;
35 TestEventData *test_event_data;
36 static CompatGMutex test_event_lock;
38 /* Only compares bool, int, string */
39 static
40 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque)
43 QObject *obj2;
44 QDictCmpData d_new, *d = opaque;
46 if (!d->result) {
47 return;
50 obj2 = qdict_get(d->expect, key);
51 if (!obj2) {
52 d->result = false;
53 return;
56 if (qobject_type(obj1) != qobject_type(obj2)) {
57 d->result = false;
58 return;
61 switch (qobject_type(obj1)) {
62 case QTYPE_QBOOL:
63 d->result = (qbool_get_int(qobject_to_qbool(obj1)) ==
64 qbool_get_int(qobject_to_qbool(obj2)));
65 return;
66 case QTYPE_QINT:
67 d->result = (qint_get_int(qobject_to_qint(obj1)) ==
68 qint_get_int(qobject_to_qint(obj2)));
69 return;
70 case QTYPE_QSTRING:
71 d->result = g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
72 qstring_get_str(qobject_to_qstring(obj2))) == 0;
73 return;
74 case QTYPE_QDICT:
75 d_new.expect = qobject_to_qdict(obj2);
76 d_new.result = true;
77 qdict_iter(qobject_to_qdict(obj1), qdict_cmp_do_simple, &d_new);
78 d->result = d_new.result;
79 return;
80 default:
81 abort();
85 static bool qdict_cmp_simple(QDict *a, QDict *b)
87 QDictCmpData d;
89 d.expect = b;
90 d.result = true;
91 qdict_iter(a, qdict_cmp_do_simple, &d);
92 return d.result;
95 /* This function is hooked as final emit function, which can verify the
96 correctness. */
97 static void event_test_emit(TEST_QAPIEvent event, QDict *d, Error **errp)
99 QObject *obj;
100 QDict *t;
101 int64_t s, ms;
103 /* Verify that we have timestamp, then remove it to compare other fields */
104 obj = qdict_get(d, "timestamp");
105 g_assert(obj);
106 t = qobject_to_qdict(obj);
107 g_assert(t);
108 obj = qdict_get(t, "seconds");
109 g_assert(obj && qobject_type(obj) == QTYPE_QINT);
110 s = qint_get_int(qobject_to_qint(obj));
111 obj = qdict_get(t, "microseconds");
112 g_assert(obj && qobject_type(obj) == QTYPE_QINT);
113 ms = qint_get_int(qobject_to_qint(obj));
114 if (s == -1) {
115 g_assert(ms == -1);
116 } else {
117 g_assert(ms >= 0 && ms <= 999999);
119 g_assert(qdict_size(t) == 2);
121 qdict_del(d, "timestamp");
123 g_assert(qdict_cmp_simple(d, test_event_data->expect));
127 static void event_prepare(TestEventData *data,
128 const void *unused)
130 /* Global variable test_event_data was used to pass the expectation, so
131 test cases can't be executed at same time. */
132 g_mutex_lock(&test_event_lock);
134 data->expect = qdict_new();
135 test_event_data = data;
138 static void event_teardown(TestEventData *data,
139 const void *unused)
141 QDECREF(data->expect);
142 test_event_data = NULL;
144 g_mutex_unlock(&test_event_lock);
147 static void event_test_add(const char *testpath,
148 void (*test_func)(TestEventData *data,
149 const void *user_data))
151 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
152 event_teardown);
156 /* Test cases */
158 static void test_event_a(TestEventData *data,
159 const void *unused)
161 QDict *d;
162 d = data->expect;
163 qdict_put(d, "event", qstring_from_str("EVENT_A"));
164 qapi_event_send_event_a(&error_abort);
167 static void test_event_b(TestEventData *data,
168 const void *unused)
170 QDict *d;
171 d = data->expect;
172 qdict_put(d, "event", qstring_from_str("EVENT_B"));
173 qapi_event_send_event_b(&error_abort);
176 static void test_event_c(TestEventData *data,
177 const void *unused)
179 QDict *d, *d_data, *d_b;
181 UserDefOne b;
182 UserDefZero z;
183 z.integer = 2;
184 b.base = &z;
185 b.string = g_strdup("test1");
186 b.has_enum1 = false;
188 d_b = qdict_new();
189 qdict_put(d_b, "integer", qint_from_int(2));
190 qdict_put(d_b, "string", qstring_from_str("test1"));
192 d_data = qdict_new();
193 qdict_put(d_data, "a", qint_from_int(1));
194 qdict_put(d_data, "b", d_b);
195 qdict_put(d_data, "c", qstring_from_str("test2"));
197 d = data->expect;
198 qdict_put(d, "event", qstring_from_str("EVENT_C"));
199 qdict_put(d, "data", d_data);
201 qapi_event_send_event_c(true, 1, true, &b, "test2", &error_abort);
203 g_free(b.string);
206 /* Complex type */
207 static void test_event_d(TestEventData *data,
208 const void *unused)
210 UserDefOne struct1;
211 EventStructOne a;
212 UserDefZero z;
213 QDict *d, *d_data, *d_a, *d_struct1;
215 z.integer = 2;
216 struct1.base = &z;
217 struct1.string = g_strdup("test1");
218 struct1.has_enum1 = true;
219 struct1.enum1 = ENUM_ONE_VALUE1;
221 a.struct1 = &struct1;
222 a.string = g_strdup("test2");
223 a.has_enum2 = true;
224 a.enum2 = ENUM_ONE_VALUE2;
226 d_struct1 = qdict_new();
227 qdict_put(d_struct1, "integer", qint_from_int(2));
228 qdict_put(d_struct1, "string", qstring_from_str("test1"));
229 qdict_put(d_struct1, "enum1", qstring_from_str("value1"));
231 d_a = qdict_new();
232 qdict_put(d_a, "struct1", d_struct1);
233 qdict_put(d_a, "string", qstring_from_str("test2"));
234 qdict_put(d_a, "enum2", qstring_from_str("value2"));
236 d_data = qdict_new();
237 qdict_put(d_data, "a", d_a);
238 qdict_put(d_data, "b", qstring_from_str("test3"));
239 qdict_put(d_data, "enum3", qstring_from_str("value3"));
241 d = data->expect;
242 qdict_put(d, "event", qstring_from_str("EVENT_D"));
243 qdict_put(d, "data", d_data);
245 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3,
246 &error_abort);
248 g_free(struct1.string);
249 g_free(a.string);
252 int main(int argc, char **argv)
254 #if !GLIB_CHECK_VERSION(2, 31, 0)
255 if (!g_thread_supported()) {
256 g_thread_init(NULL);
258 #endif
260 qmp_event_set_func_emit(event_test_emit);
262 g_test_init(&argc, &argv, NULL);
264 event_test_add("/event/event_a", test_event_a);
265 event_test_add("/event/event_b", test_event_b);
266 event_test_add("/event/event_c", test_event_c);
267 event_test_add("/event/event_d", test_event_d);
268 g_test_run();
270 return 0;