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