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/compat-policy.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qjson.h"
22 #include "qapi/qmp/qnum.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp-event.h"
25 #include "test-qapi-events.h"
26 #include "test-qapi-emit-events.h"
28 typedef struct TestEventData
{
33 TestEventData
*test_event_data
;
34 static GMutex test_event_lock
;
36 void test_qapi_event_emit(test_QAPIEvent event
, QDict
*d
)
41 /* Verify that we have timestamp, then remove it to compare other fields */
42 t
= qdict_get_qdict(d
, "timestamp");
44 s
= qdict_get_try_int(t
, "seconds", -2);
45 ms
= qdict_get_try_int(t
, "microseconds", -2);
50 g_assert(ms
>= 0 && ms
<= 999999);
52 g_assert(qdict_size(t
) == 2);
54 qdict_del(d
, "timestamp");
56 g_assert(qobject_is_equal(QOBJECT(d
), QOBJECT(test_event_data
->expect
)));
57 test_event_data
->emitted
= true;
60 static void event_prepare(TestEventData
*data
,
63 /* Global variable test_event_data was used to pass the expectation, so
64 test cases can't be executed at same time. */
65 g_mutex_lock(&test_event_lock
);
66 test_event_data
= data
;
69 static void event_teardown(TestEventData
*data
,
72 test_event_data
= NULL
;
73 g_mutex_unlock(&test_event_lock
);
76 static void event_test_add(const char *testpath
,
77 void (*test_func
)(TestEventData
*data
,
78 const void *user_data
))
80 g_test_add(testpath
, TestEventData
, NULL
, event_prepare
, test_func
,
87 static void test_event_a(TestEventData
*data
,
90 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
91 qapi_event_send_event_a();
92 g_assert(data
->emitted
);
93 qobject_unref(data
->expect
);
96 static void test_event_b(TestEventData
*data
,
99 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
100 qapi_event_send_event_b();
101 g_assert(data
->emitted
);
102 qobject_unref(data
->expect
);
105 static void test_event_c(TestEventData
*data
,
108 UserDefOne b
= { .integer
= 2, .string
= (char *)"test1" };
110 data
->expect
= qdict_from_jsonf_nofail(
111 "{ 'event': 'EVENT_C', 'data': {"
112 " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }");
113 qapi_event_send_event_c(true, 1, true, &b
, "test2");
114 g_assert(data
->emitted
);
115 qobject_unref(data
->expect
);
119 static void test_event_d(TestEventData
*data
,
122 UserDefOne struct1
= {
123 .integer
= 2, .string
= (char *)"test1",
124 .has_enum1
= true, .enum1
= ENUM_ONE_VALUE1
,
128 .string
= (char *)"test2",
130 .enum2
= ENUM_ONE_VALUE2
,
133 data
->expect
= qdict_from_jsonf_nofail(
134 "{ 'event': 'EVENT_D', 'data': {"
136 " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' },"
137 " 'string': 'test2', 'enum2': 'value2' },"
138 " 'b': 'test3', 'enum3': 'value3' } }");
139 qapi_event_send_event_d(&a
, "test3", false, NULL
, true, ENUM_ONE_VALUE3
);
140 g_assert(data
->emitted
);
141 qobject_unref(data
->expect
);
144 static void test_event_deprecated(TestEventData
*data
, const void *unused
)
146 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES1' }");
148 memset(&compat_policy
, 0, sizeof(compat_policy
));
150 qapi_event_send_test_event_features1();
151 g_assert(data
->emitted
);
153 compat_policy
.has_deprecated_output
= true;
154 compat_policy
.deprecated_output
= COMPAT_POLICY_OUTPUT_HIDE
;
155 data
->emitted
= false;
156 qapi_event_send_test_event_features1();
157 g_assert(!data
->emitted
);
159 qobject_unref(data
->expect
);
162 static void test_event_deprecated_data(TestEventData
*data
, const void *unused
)
164 memset(&compat_policy
, 0, sizeof(compat_policy
));
166 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0',"
167 " 'data': { 'foo': 42 } }");
168 qapi_event_send_test_event_features0(42);
169 g_assert(data
->emitted
);
171 qobject_unref(data
->expect
);
173 compat_policy
.has_deprecated_output
= true;
174 compat_policy
.deprecated_output
= COMPAT_POLICY_OUTPUT_HIDE
;
175 data
->expect
= qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0' }");
176 qapi_event_send_test_event_features0(42);
177 g_assert(data
->emitted
);
179 qobject_unref(data
->expect
);
182 int main(int argc
, char **argv
)
184 g_test_init(&argc
, &argv
, NULL
);
186 event_test_add("/event/event_a", test_event_a
);
187 event_test_add("/event/event_b", test_event_b
);
188 event_test_add("/event/event_c", test_event_c
);
189 event_test_add("/event/event_d", test_event_d
);
190 event_test_add("/event/deprecated", test_event_deprecated
);
191 event_test_add("/event/deprecated_data", test_event_deprecated_data
);