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"
25 typedef struct TestEventData
{
29 typedef struct QDictCmpData
{
34 TestEventData
*test_event_data
;
35 static GMutex test_event_lock
;
37 /* Only compares bool, int, string */
39 void qdict_cmp_do_simple(const char *key
, QObject
*obj1
, void *opaque
)
43 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 g_assert(qnum_get_try_int(qobject_to(QNum
, obj1
), &val1
));
68 g_assert(qnum_get_try_int(qobject_to(QNum
, obj2
), &val2
));
69 d
->result
= val1
== val2
;
72 d
->result
= g_strcmp0(qstring_get_str(qobject_to(QString
, obj1
)),
73 qstring_get_str(qobject_to(QString
, obj2
))) == 0;
76 d_new
.expect
= qobject_to(QDict
, obj2
);
78 qdict_iter(qobject_to(QDict
, obj1
), qdict_cmp_do_simple
, &d_new
);
79 d
->result
= d_new
.result
;
86 static bool qdict_cmp_simple(QDict
*a
, QDict
*b
)
92 qdict_iter(a
, qdict_cmp_do_simple
, &d
);
96 void test_qapi_event_emit(test_QAPIEvent event
, QDict
*d
)
101 /* Verify that we have timestamp, then remove it to compare other fields */
102 t
= qdict_get_qdict(d
, "timestamp");
104 s
= qdict_get_try_int(t
, "seconds", -2);
105 ms
= qdict_get_try_int(t
, "microseconds", -2);
110 g_assert(ms
>= 0 && ms
<= 999999);
112 g_assert(qdict_size(t
) == 2);
114 qdict_del(d
, "timestamp");
116 g_assert(qdict_cmp_simple(d
, test_event_data
->expect
));
120 static void event_prepare(TestEventData
*data
,
123 /* Global variable test_event_data was used to pass the expectation, so
124 test cases can't be executed at same time. */
125 g_mutex_lock(&test_event_lock
);
127 data
->expect
= qdict_new();
128 test_event_data
= data
;
131 static void event_teardown(TestEventData
*data
,
134 qobject_unref(data
->expect
);
135 test_event_data
= NULL
;
137 g_mutex_unlock(&test_event_lock
);
140 static void event_test_add(const char *testpath
,
141 void (*test_func
)(TestEventData
*data
,
142 const void *user_data
))
144 g_test_add(testpath
, TestEventData
, NULL
, event_prepare
, test_func
,
151 static void test_event_a(TestEventData
*data
,
156 qdict_put_str(d
, "event", "EVENT_A");
157 qapi_event_send_event_a();
160 static void test_event_b(TestEventData
*data
,
165 qdict_put_str(d
, "event", "EVENT_B");
166 qapi_event_send_event_b();
169 static void test_event_c(TestEventData
*data
,
172 QDict
*d
, *d_data
, *d_b
;
176 b
.string
= g_strdup("test1");
180 qdict_put_int(d_b
, "integer", 2);
181 qdict_put_str(d_b
, "string", "test1");
183 d_data
= qdict_new();
184 qdict_put_int(d_data
, "a", 1);
185 qdict_put(d_data
, "b", d_b
);
186 qdict_put_str(d_data
, "c", "test2");
189 qdict_put_str(d
, "event", "EVENT_C");
190 qdict_put(d
, "data", d_data
);
192 qapi_event_send_event_c(true, 1, true, &b
, "test2");
198 static void test_event_d(TestEventData
*data
,
203 QDict
*d
, *d_data
, *d_a
, *d_struct1
;
206 struct1
.string
= g_strdup("test1");
207 struct1
.has_enum1
= true;
208 struct1
.enum1
= ENUM_ONE_VALUE1
;
210 a
.struct1
= &struct1
;
211 a
.string
= g_strdup("test2");
213 a
.enum2
= ENUM_ONE_VALUE2
;
215 d_struct1
= qdict_new();
216 qdict_put_int(d_struct1
, "integer", 2);
217 qdict_put_str(d_struct1
, "string", "test1");
218 qdict_put_str(d_struct1
, "enum1", "value1");
221 qdict_put(d_a
, "struct1", d_struct1
);
222 qdict_put_str(d_a
, "string", "test2");
223 qdict_put_str(d_a
, "enum2", "value2");
225 d_data
= qdict_new();
226 qdict_put(d_data
, "a", d_a
);
227 qdict_put_str(d_data
, "b", "test3");
228 qdict_put_str(d_data
, "enum3", "value3");
231 qdict_put_str(d
, "event", "EVENT_D");
232 qdict_put(d
, "data", d_data
);
234 qapi_event_send_event_d(&a
, "test3", false, NULL
, true, ENUM_ONE_VALUE3
);
236 g_free(struct1
.string
);
240 int main(int argc
, char **argv
)
242 g_test_init(&argc
, &argv
, NULL
);
244 event_test_add("/event/event_a", test_event_a
);
245 event_test_add("/event/event_b", test_event_b
);
246 event_test_add("/event/event_c", test_event_c
);
247 event_test_add("/event/event_d", test_event_d
);