2 * qapi event unit-tests.
4 * Copyright (c) 2014 Wenchao Xia
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.
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 {
30 typedef struct QDictCmpData {
35 TestEventData *test_event_data;
36 static CompatGMutex test_event_lock;
38 /* Only compares bool, int, string */
40 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque)
44 QDictCmpData d_new, *d = opaque;
50 obj2 = qdict_get(d->expect, key);
56 if (qobject_type(obj1) != qobject_type(obj2)) {
61 switch (qobject_type(obj1)) {
63 d->result = (qbool_get_bool(qobject_to_qbool(obj1)) ==
64 qbool_get_bool(qobject_to_qbool(obj2)));
67 d->result = (qint_get_int(qobject_to_qint(obj1)) ==
68 qint_get_int(qobject_to_qint(obj2)));
71 d->result = g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
72 qstring_get_str(qobject_to_qstring(obj2))) == 0;
75 d_new.expect = qobject_to_qdict(obj2);
77 qdict_iter(qobject_to_qdict(obj1), qdict_cmp_do_simple, &d_new);
78 d->result = d_new.result;
85 static bool qdict_cmp_simple(QDict *a, QDict *b)
91 qdict_iter(a, qdict_cmp_do_simple, &d);
95 /* This function is hooked as final emit function, which can verify the
97 static void event_test_emit(TEST_QAPIEvent event, QDict *d, Error **errp)
103 /* Verify that we have timestamp, then remove it to compare other fields */
104 obj = qdict_get(d, "timestamp");
106 t = qobject_to_qdict(obj);
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));
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,
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,
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,
158 static void test_event_a(TestEventData *data,
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,
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,
179 QDict *d, *d_data, *d_b;
185 b.string = g_strdup("test1");
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"));
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);
207 static void test_event_d(TestEventData *data,
213 QDict *d, *d_data, *d_a, *d_struct1;
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");
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"));
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"));
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,
248 g_free(struct1.string);
252 int main(int argc, char **argv)
254 #if !GLIB_CHECK_VERSION(2, 31, 0)
255 if (!g_thread_supported()) {
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);