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
;
183 b
.string
= g_strdup("test1");
187 qdict_put(d_b
, "integer", qint_from_int(2));
188 qdict_put(d_b
, "string", qstring_from_str("test1"));
190 d_data
= qdict_new();
191 qdict_put(d_data
, "a", qint_from_int(1));
192 qdict_put(d_data
, "b", d_b
);
193 qdict_put(d_data
, "c", qstring_from_str("test2"));
196 qdict_put(d
, "event", qstring_from_str("EVENT_C"));
197 qdict_put(d
, "data", d_data
);
199 qapi_event_send_event_c(true, 1, true, &b
, "test2", &error_abort
);
205 static void test_event_d(TestEventData
*data
,
210 QDict
*d
, *d_data
, *d_a
, *d_struct1
;
213 struct1
.string
= g_strdup("test1");
214 struct1
.has_enum1
= true;
215 struct1
.enum1
= ENUM_ONE_VALUE1
;
217 a
.struct1
= &struct1
;
218 a
.string
= g_strdup("test2");
220 a
.enum2
= ENUM_ONE_VALUE2
;
222 d_struct1
= qdict_new();
223 qdict_put(d_struct1
, "integer", qint_from_int(2));
224 qdict_put(d_struct1
, "string", qstring_from_str("test1"));
225 qdict_put(d_struct1
, "enum1", qstring_from_str("value1"));
228 qdict_put(d_a
, "struct1", d_struct1
);
229 qdict_put(d_a
, "string", qstring_from_str("test2"));
230 qdict_put(d_a
, "enum2", qstring_from_str("value2"));
232 d_data
= qdict_new();
233 qdict_put(d_data
, "a", d_a
);
234 qdict_put(d_data
, "b", qstring_from_str("test3"));
235 qdict_put(d_data
, "enum3", qstring_from_str("value3"));
238 qdict_put(d
, "event", qstring_from_str("EVENT_D"));
239 qdict_put(d
, "data", d_data
);
241 qapi_event_send_event_d(&a
, "test3", false, NULL
, true, ENUM_ONE_VALUE3
,
244 g_free(struct1
.string
);
248 int main(int argc
, char **argv
)
250 #if !GLIB_CHECK_VERSION(2, 31, 0)
251 if (!g_thread_supported()) {
256 qmp_event_set_func_emit(event_test_emit
);
258 g_test_init(&argc
, &argv
, NULL
);
260 event_test_add("/event/event_a", test_event_a
);
261 event_test_add("/event/event_b", test_event_b
);
262 event_test_add("/event/event_c", test_event_c
);
263 event_test_add("/event/event_d", test_event_d
);