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/qjson.h"
21 #include "qapi/qmp/qnum.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp-event.h"
24 #include "test-qapi-events.h"
25 #include "test-qapi-emit-events.h"
27 typedef struct TestEventData
{
32 TestEventData
*test_event_data
;
33 static GMutex test_event_lock
;
35 void test_qapi_event_emit(test_QAPIEvent event
, QDict
*d
)
40 /* Verify that we have timestamp, then remove it to compare other fields */
41 t
= qdict_get_qdict(d
, "timestamp");
43 s
= qdict_get_try_int(t
, "seconds", -2);
44 ms
= qdict_get_try_int(t
, "microseconds", -2);
49 g_assert(ms
>= 0 && ms
<= 999999);
51 g_assert(qdict_size(t
) == 2);
53 qdict_del(d
, "timestamp");
55 g_assert(qobject_is_equal(QOBJECT(d
), QOBJECT(test_event_data
->expect
)));
56 test_event_data
->emitted
= true;
59 static void event_prepare(TestEventData
*data
,
62 /* Global variable test_event_data was used to pass the expectation, so
63 test cases can't be executed at same time. */
64 g_mutex_lock(&test_event_lock
);
65 test_event_data
= data
;
68 static void event_teardown(TestEventData
*data
,
71 test_event_data
= NULL
;
72 g_mutex_unlock(&test_event_lock
);
75 static void event_test_add(const char *testpath
,
76 void (*test_func
)(TestEventData
*data
,
77 const void *user_data
))
79 g_test_add(testpath
, TestEventData
, NULL
, event_prepare
, test_func
,
86 static void test_event_a(TestEventData
*data
,
89 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
90 qapi_event_send_event_a();
91 g_assert(data
->emitted
);
92 qobject_unref(data
->expect
);
95 static void test_event_b(TestEventData
*data
,
98 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
99 qapi_event_send_event_b();
100 g_assert(data
->emitted
);
101 qobject_unref(data
->expect
);
104 static void test_event_c(TestEventData
*data
,
107 UserDefOne b
= { .integer
= 2, .string
= (char *)"test1" };
109 data
->expect
= qdict_from_jsonf_nofail(
110 "{ 'event': 'EVENT_C', 'data': {"
111 " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }");
112 qapi_event_send_event_c(true, 1, true, &b
, "test2");
113 g_assert(data
->emitted
);
114 qobject_unref(data
->expect
);
118 static void test_event_d(TestEventData
*data
,
121 UserDefOne struct1
= {
122 .integer
= 2, .string
= (char *)"test1",
123 .has_enum1
= true, .enum1
= ENUM_ONE_VALUE1
,
127 .string
= (char *)"test2",
129 .enum2
= ENUM_ONE_VALUE2
,
132 data
->expect
= qdict_from_jsonf_nofail(
133 "{ 'event': 'EVENT_D', 'data': {"
135 " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' },"
136 " 'string': 'test2', 'enum2': 'value2' },"
137 " 'b': 'test3', 'enum3': 'value3' } }");
138 qapi_event_send_event_d(&a
, "test3", false, NULL
, true, ENUM_ONE_VALUE3
);
139 g_assert(data
->emitted
);
140 qobject_unref(data
->expect
);
143 int main(int argc
, char **argv
)
145 g_test_init(&argc
, &argv
, NULL
);
147 event_test_add("/event/event_a", test_event_a
);
148 event_test_add("/event/event_b", test_event_b
);
149 event_test_add("/event/event_c", test_event_c
);
150 event_test_add("/event/event_d", test_event_d
);