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.
14 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qnum.h"
21 #include "qapi/qmp/qstring.h"
22 #include "qapi/qmp-event.h"
23 #include "test-qapi-events.h"
24 #include "test-qapi-emit-events.h"
26 typedef struct TestEventData
{
30 typedef struct QDictCmpData
{
35 TestEventData
*test_event_data
;
36 static GMutex 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
;
51 obj2
= qdict_get(d
->expect
, key
);
57 if (qobject_type(obj1
) != qobject_type(obj2
)) {
62 switch (qobject_type(obj1
)) {
64 d
->result
= (qbool_get_bool(qobject_to(QBool
, obj1
)) ==
65 qbool_get_bool(qobject_to(QBool
, obj2
)));
68 g_assert(qnum_get_try_int(qobject_to(QNum
, obj1
), &val1
));
69 g_assert(qnum_get_try_int(qobject_to(QNum
, obj2
), &val2
));
70 d
->result
= val1
== val2
;
73 d
->result
= g_strcmp0(qstring_get_str(qobject_to(QString
, obj1
)),
74 qstring_get_str(qobject_to(QString
, obj2
))) == 0;
77 d_new
.expect
= qobject_to(QDict
, obj2
);
79 qdict_iter(qobject_to(QDict
, obj1
), qdict_cmp_do_simple
, &d_new
);
80 d
->result
= d_new
.result
;
87 static bool qdict_cmp_simple(QDict
*a
, QDict
*b
)
93 qdict_iter(a
, qdict_cmp_do_simple
, &d
);
97 void test_qapi_event_emit(test_QAPIEvent event
, QDict
*d
)
102 /* Verify that we have timestamp, then remove it to compare other fields */
103 t
= qdict_get_qdict(d
, "timestamp");
105 s
= qdict_get_try_int(t
, "seconds", -2);
106 ms
= qdict_get_try_int(t
, "microseconds", -2);
111 g_assert(ms
>= 0 && ms
<= 999999);
113 g_assert(qdict_size(t
) == 2);
115 qdict_del(d
, "timestamp");
117 g_assert(qdict_cmp_simple(d
, test_event_data
->expect
));
121 static void event_prepare(TestEventData
*data
,
124 /* Global variable test_event_data was used to pass the expectation, so
125 test cases can't be executed at same time. */
126 g_mutex_lock(&test_event_lock
);
128 data
->expect
= qdict_new();
129 test_event_data
= data
;
132 static void event_teardown(TestEventData
*data
,
135 qobject_unref(data
->expect
);
136 test_event_data
= NULL
;
138 g_mutex_unlock(&test_event_lock
);
141 static void event_test_add(const char *testpath
,
142 void (*test_func
)(TestEventData
*data
,
143 const void *user_data
))
145 g_test_add(testpath
, TestEventData
, NULL
, event_prepare
, test_func
,
152 static void test_event_a(TestEventData
*data
,
157 qdict_put_str(d
, "event", "EVENT_A");
158 qapi_event_send_event_a();
161 static void test_event_b(TestEventData
*data
,
166 qdict_put_str(d
, "event", "EVENT_B");
167 qapi_event_send_event_b();
170 static void test_event_c(TestEventData
*data
,
173 QDict
*d
, *d_data
, *d_b
;
177 b
.string
= g_strdup("test1");
181 qdict_put_int(d_b
, "integer", 2);
182 qdict_put_str(d_b
, "string", "test1");
184 d_data
= qdict_new();
185 qdict_put_int(d_data
, "a", 1);
186 qdict_put(d_data
, "b", d_b
);
187 qdict_put_str(d_data
, "c", "test2");
190 qdict_put_str(d
, "event", "EVENT_C");
191 qdict_put(d
, "data", d_data
);
193 qapi_event_send_event_c(true, 1, true, &b
, "test2");
199 static void test_event_d(TestEventData
*data
,
204 QDict
*d
, *d_data
, *d_a
, *d_struct1
;
207 struct1
.string
= g_strdup("test1");
208 struct1
.has_enum1
= true;
209 struct1
.enum1
= ENUM_ONE_VALUE1
;
211 a
.struct1
= &struct1
;
212 a
.string
= g_strdup("test2");
214 a
.enum2
= ENUM_ONE_VALUE2
;
216 d_struct1
= qdict_new();
217 qdict_put_int(d_struct1
, "integer", 2);
218 qdict_put_str(d_struct1
, "string", "test1");
219 qdict_put_str(d_struct1
, "enum1", "value1");
222 qdict_put(d_a
, "struct1", d_struct1
);
223 qdict_put_str(d_a
, "string", "test2");
224 qdict_put_str(d_a
, "enum2", "value2");
226 d_data
= qdict_new();
227 qdict_put(d_data
, "a", d_a
);
228 qdict_put_str(d_data
, "b", "test3");
229 qdict_put_str(d_data
, "enum3", "value3");
232 qdict_put_str(d
, "event", "EVENT_D");
233 qdict_put(d
, "data", d_data
);
235 qapi_event_send_event_d(&a
, "test3", false, NULL
, true, ENUM_ONE_VALUE3
);
237 g_free(struct1
.string
);
241 int main(int argc
, char **argv
)
243 g_test_init(&argc
, &argv
, NULL
);
245 event_test_add("/event/event_a", test_event_a
);
246 event_test_add("/event/event_b", test_event_b
);
247 event_test_add("/event/event_c", test_event_c
);
248 event_test_add("/event/event_d", test_event_d
);