s390x/kvm: Fixup interrupt type for non-adapter I/O interrupts
[qemu/ar7.git] / tests / test-qmp-event.c
blob633dc874024a87e4118a7f8b022caf8c61947f6d
1 /*
2 * qapi event unit-tests.
4 * Copyright (c) 2014 Wenchao Xia
6 * Authors:
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 "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "test-qapi-event.h"
20 #include "qapi/qmp/types.h"
21 #include "qapi/qmp/qint.h"
22 #include "qapi/qmp/qobject.h"
23 #include "qapi/qmp-event.h"
25 typedef struct TestEventData {
26 QDict *expect;
27 } TestEventData;
29 typedef struct QDictCmpData {
30 QDict *expect;
31 bool result;
32 } QDictCmpData;
34 TestEventData *test_event_data;
35 static CompatGMutex test_event_lock;
37 /* Only compares bool, int, string */
38 static
39 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque)
42 QObject *obj2;
43 QDictCmpData d_new, *d = opaque;
45 if (!d->result) {
46 return;
49 obj2 = qdict_get(d->expect, key);
50 if (!obj2) {
51 d->result = false;
52 return;
55 if (qobject_type(obj1) != qobject_type(obj2)) {
56 d->result = false;
57 return;
60 switch (qobject_type(obj1)) {
61 case QTYPE_QBOOL:
62 d->result = (qbool_get_bool(qobject_to_qbool(obj1)) ==
63 qbool_get_bool(qobject_to_qbool(obj2)));
64 return;
65 case QTYPE_QINT:
66 d->result = (qint_get_int(qobject_to_qint(obj1)) ==
67 qint_get_int(qobject_to_qint(obj2)));
68 return;
69 case QTYPE_QSTRING:
70 d->result = g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
71 qstring_get_str(qobject_to_qstring(obj2))) == 0;
72 return;
73 case QTYPE_QDICT:
74 d_new.expect = qobject_to_qdict(obj2);
75 d_new.result = true;
76 qdict_iter(qobject_to_qdict(obj1), qdict_cmp_do_simple, &d_new);
77 d->result = d_new.result;
78 return;
79 default:
80 abort();
84 static bool qdict_cmp_simple(QDict *a, QDict *b)
86 QDictCmpData d;
88 d.expect = b;
89 d.result = true;
90 qdict_iter(a, qdict_cmp_do_simple, &d);
91 return d.result;
94 /* This function is hooked as final emit function, which can verify the
95 correctness. */
96 static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp)
98 QObject *obj;
99 QDict *t;
100 int64_t s, ms;
102 /* Verify that we have timestamp, then remove it to compare other fields */
103 obj = qdict_get(d, "timestamp");
104 g_assert(obj);
105 t = qobject_to_qdict(obj);
106 g_assert(t);
107 obj = qdict_get(t, "seconds");
108 g_assert(obj && qobject_type(obj) == QTYPE_QINT);
109 s = qint_get_int(qobject_to_qint(obj));
110 obj = qdict_get(t, "microseconds");
111 g_assert(obj && qobject_type(obj) == QTYPE_QINT);
112 ms = qint_get_int(qobject_to_qint(obj));
113 if (s == -1) {
114 g_assert(ms == -1);
115 } else {
116 g_assert(ms >= 0 && ms <= 999999);
118 g_assert(qdict_size(t) == 2);
120 qdict_del(d, "timestamp");
122 g_assert(qdict_cmp_simple(d, test_event_data->expect));
126 static void event_prepare(TestEventData *data,
127 const void *unused)
129 /* Global variable test_event_data was used to pass the expectation, so
130 test cases can't be executed at same time. */
131 g_mutex_lock(&test_event_lock);
133 data->expect = qdict_new();
134 test_event_data = data;
137 static void event_teardown(TestEventData *data,
138 const void *unused)
140 QDECREF(data->expect);
141 test_event_data = NULL;
143 g_mutex_unlock(&test_event_lock);
146 static void event_test_add(const char *testpath,
147 void (*test_func)(TestEventData *data,
148 const void *user_data))
150 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
151 event_teardown);
155 /* Test cases */
157 static void test_event_a(TestEventData *data,
158 const void *unused)
160 QDict *d;
161 d = data->expect;
162 qdict_put(d, "event", qstring_from_str("EVENT_A"));
163 qapi_event_send_event_a(&error_abort);
166 static void test_event_b(TestEventData *data,
167 const void *unused)
169 QDict *d;
170 d = data->expect;
171 qdict_put(d, "event", qstring_from_str("EVENT_B"));
172 qapi_event_send_event_b(&error_abort);
175 static void test_event_c(TestEventData *data,
176 const void *unused)
178 QDict *d, *d_data, *d_b;
180 UserDefOne b;
181 b.integer = 2;
182 b.string = g_strdup("test1");
183 b.has_enum1 = false;
185 d_b = qdict_new();
186 qdict_put(d_b, "integer", qint_from_int(2));
187 qdict_put(d_b, "string", qstring_from_str("test1"));
189 d_data = qdict_new();
190 qdict_put(d_data, "a", qint_from_int(1));
191 qdict_put(d_data, "b", d_b);
192 qdict_put(d_data, "c", qstring_from_str("test2"));
194 d = data->expect;
195 qdict_put(d, "event", qstring_from_str("EVENT_C"));
196 qdict_put(d, "data", d_data);
198 qapi_event_send_event_c(true, 1, true, &b, "test2", &error_abort);
200 g_free(b.string);
203 /* Complex type */
204 static void test_event_d(TestEventData *data,
205 const void *unused)
207 UserDefOne struct1;
208 EventStructOne a;
209 QDict *d, *d_data, *d_a, *d_struct1;
211 struct1.integer = 2;
212 struct1.string = g_strdup("test1");
213 struct1.has_enum1 = true;
214 struct1.enum1 = ENUM_ONE_VALUE1;
216 a.struct1 = &struct1;
217 a.string = g_strdup("test2");
218 a.has_enum2 = true;
219 a.enum2 = ENUM_ONE_VALUE2;
221 d_struct1 = qdict_new();
222 qdict_put(d_struct1, "integer", qint_from_int(2));
223 qdict_put(d_struct1, "string", qstring_from_str("test1"));
224 qdict_put(d_struct1, "enum1", qstring_from_str("value1"));
226 d_a = qdict_new();
227 qdict_put(d_a, "struct1", d_struct1);
228 qdict_put(d_a, "string", qstring_from_str("test2"));
229 qdict_put(d_a, "enum2", qstring_from_str("value2"));
231 d_data = qdict_new();
232 qdict_put(d_data, "a", d_a);
233 qdict_put(d_data, "b", qstring_from_str("test3"));
234 qdict_put(d_data, "enum3", qstring_from_str("value3"));
236 d = data->expect;
237 qdict_put(d, "event", qstring_from_str("EVENT_D"));
238 qdict_put(d, "data", d_data);
240 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3,
241 &error_abort);
243 g_free(struct1.string);
244 g_free(a.string);
247 int main(int argc, char **argv)
249 #if !GLIB_CHECK_VERSION(2, 31, 0)
250 if (!g_thread_supported()) {
251 g_thread_init(NULL);
253 #endif
255 qmp_event_set_func_emit(event_test_emit);
257 g_test_init(&argc, &argv, NULL);
259 event_test_add("/event/event_a", test_event_a);
260 event_test_add("/event/event_b", test_event_b);
261 event_test_add("/event/event_c", test_event_c);
262 event_test_add("/event/event_d", test_event_d);
263 g_test_run();
265 return 0;