]> git.proxmox.com Git - mirror_qemu.git/blame - tests/unit/test-qmp-event.c
Remove qemu-common.h include from most units
[mirror_qemu.git] / tests / unit / test-qmp-event.c
CommitLineData
f6dadb02
WX
1/*
2 * qapi event unit-tests.
3 *
4 * Copyright (c) 2014 Wenchao Xia
5 *
6 * Authors:
7 * Wenchao Xia <wenchaoqemu@gmail.com>
8 *
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.
11 *
12 */
13
681c28a3 14#include "qemu/osdep.h"
f6dadb02 15
278fc2f7 16#include "qapi/compat-policy.h"
e688df6b 17#include "qapi/error.h"
6b673957 18#include "qapi/qmp/qbool.h"
452fcdbc 19#include "qapi/qmp/qdict.h"
3ecc3932 20#include "qapi/qmp/qjson.h"
15280c36 21#include "qapi/qmp/qnum.h"
6b673957 22#include "qapi/qmp/qstring.h"
f6dadb02 23#include "qapi/qmp-event.h"
eb815e24 24#include "test-qapi-events.h"
5d75648b 25#include "test-qapi-emit-events.h"
f6dadb02
WX
26
27typedef struct TestEventData {
28 QDict *expect;
11deae8c 29 bool emitted;
f6dadb02
WX
30} TestEventData;
31
f6dadb02 32TestEventData *test_event_data;
e7b3af81 33static GMutex test_event_lock;
f6dadb02 34
a9529100 35void test_qapi_event_emit(test_QAPIEvent event, QDict *d)
f6dadb02 36{
f6dadb02
WX
37 QDict *t;
38 int64_t s, ms;
39
40 /* Verify that we have timestamp, then remove it to compare other fields */
4b32e11a 41 t = qdict_get_qdict(d, "timestamp");
f6dadb02 42 g_assert(t);
4b32e11a
MA
43 s = qdict_get_try_int(t, "seconds", -2);
44 ms = qdict_get_try_int(t, "microseconds", -2);
f6dadb02
WX
45 if (s == -1) {
46 g_assert(ms == -1);
47 } else {
4b32e11a 48 g_assert(s >= 0);
f6dadb02
WX
49 g_assert(ms >= 0 && ms <= 999999);
50 }
51 g_assert(qdict_size(t) == 2);
52
53 qdict_del(d, "timestamp");
54
052be50c 55 g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(test_event_data->expect)));
11deae8c 56 test_event_data->emitted = true;
f6dadb02
WX
57}
58
59static void event_prepare(TestEventData *data,
60 const void *unused)
61{
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);
f6dadb02
WX
65 test_event_data = data;
66}
67
68static void event_teardown(TestEventData *data,
69 const void *unused)
70{
f6dadb02 71 test_event_data = NULL;
f6dadb02
WX
72 g_mutex_unlock(&test_event_lock);
73}
74
75static void event_test_add(const char *testpath,
76 void (*test_func)(TestEventData *data,
77 const void *user_data))
78{
79 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
80 event_teardown);
81}
82
83
84/* Test cases */
85
86static void test_event_a(TestEventData *data,
87 const void *unused)
88{
3ecc3932 89 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
3ab72385 90 qapi_event_send_event_a();
11deae8c 91 g_assert(data->emitted);
3ecc3932 92 qobject_unref(data->expect);
f6dadb02
WX
93}
94
95static void test_event_b(TestEventData *data,
96 const void *unused)
97{
3ecc3932 98 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
3ab72385 99 qapi_event_send_event_b();
11deae8c 100 g_assert(data->emitted);
3ecc3932 101 qobject_unref(data->expect);
f6dadb02
WX
102}
103
104static void test_event_c(TestEventData *data,
105 const void *unused)
106{
3ecc3932 107 UserDefOne b = { .integer = 2, .string = (char *)"test1" };
f6dadb02 108
3ecc3932
MA
109 data->expect = qdict_from_jsonf_nofail(
110 "{ 'event': 'EVENT_C', 'data': {"
111 " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }");
3ab72385 112 qapi_event_send_event_c(true, 1, true, &b, "test2");
11deae8c 113 g_assert(data->emitted);
3ecc3932 114 qobject_unref(data->expect);
f6dadb02
WX
115}
116
117/* Complex type */
118static void test_event_d(TestEventData *data,
119 const void *unused)
120{
3ecc3932
MA
121 UserDefOne struct1 = {
122 .integer = 2, .string = (char *)"test1",
123 .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1,
124 };
125 EventStructOne a = {
126 .struct1 = &struct1,
127 .string = (char *)"test2",
128 .has_enum2 = true,
129 .enum2 = ENUM_ONE_VALUE2,
130 };
131
132 data->expect = qdict_from_jsonf_nofail(
133 "{ 'event': 'EVENT_D', 'data': {"
134 " 'a': {"
135 " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' },"
136 " 'string': 'test2', 'enum2': 'value2' },"
137 " 'b': 'test3', 'enum3': 'value3' } }");
3ab72385 138 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3);
11deae8c 139 g_assert(data->emitted);
3ecc3932 140 qobject_unref(data->expect);
f6dadb02
WX
141}
142
278fc2f7
MA
143static void test_event_deprecated(TestEventData *data, const void *unused)
144{
d4f4cae8 145 data->expect = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES1' }");
278fc2f7
MA
146
147 memset(&compat_policy, 0, sizeof(compat_policy));
148
149 qapi_event_send_test_event_features1();
150 g_assert(data->emitted);
151
152 compat_policy.has_deprecated_output = true;
153 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
154 data->emitted = false;
155 qapi_event_send_test_event_features1();
156 g_assert(!data->emitted);
157
158 qobject_unref(data->expect);
159}
160
a291a38f
MA
161static void test_event_deprecated_data(TestEventData *data, const void *unused)
162{
163 memset(&compat_policy, 0, sizeof(compat_policy));
164
d4f4cae8 165 data->expect = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0',"
a291a38f
MA
166 " 'data': { 'foo': 42 } }");
167 qapi_event_send_test_event_features0(42);
168 g_assert(data->emitted);
169
170 qobject_unref(data->expect);
171
172 compat_policy.has_deprecated_output = true;
173 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
d4f4cae8 174 data->expect = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0' }");
a291a38f
MA
175 qapi_event_send_test_event_features0(42);
176 g_assert(data->emitted);
177
178 qobject_unref(data->expect);
179}
180
f6dadb02
WX
181int main(int argc, char **argv)
182{
f6dadb02
WX
183 g_test_init(&argc, &argv, NULL);
184
185 event_test_add("/event/event_a", test_event_a);
186 event_test_add("/event/event_b", test_event_b);
187 event_test_add("/event/event_c", test_event_c);
188 event_test_add("/event/event_d", test_event_d);
278fc2f7 189 event_test_add("/event/deprecated", test_event_deprecated);
a291a38f 190 event_test_add("/event/deprecated_data", test_event_deprecated_data);
f6dadb02
WX
191 g_test_run();
192
193 return 0;
194}