virtio-blk: use aio handler for data plane
[qemu/ar7.git] / tests / test-qmp-event.c
bloba296fdbac218e552b9d81a247dc9f481d67f36b0
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"
15 #include <glib.h>
17 #include "qemu-common.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "test-qapi-event.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qint.h"
23 #include "qapi/qmp/qobject.h"
24 #include "qapi/qmp-event.h"
26 typedef struct TestEventData {
27 QDict *expect;
28 } TestEventData;
30 typedef struct QDictCmpData {
31 QDict *expect;
32 bool result;
33 } QDictCmpData;
35 TestEventData *test_event_data;
36 static CompatGMutex test_event_lock;
38 /* Only compares bool, int, string */
39 static
40 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque)
43 QObject *obj2;
44 QDictCmpData d_new, *d = opaque;
46 if (!d->result) {
47 return;
50 obj2 = qdict_get(d->expect, key);
51 if (!obj2) {
52 d->result = false;
53 return;
56 if (qobject_type(obj1) != qobject_type(obj2)) {
57 d->result = false;
58 return;
61 switch (qobject_type(obj1)) {
62 case QTYPE_QBOOL:
63 d->result = (qbool_get_bool(qobject_to_qbool(obj1)) ==
64 qbool_get_bool(qobject_to_qbool(obj2)));
65 return;
66 case QTYPE_QINT:
67 d->result = (qint_get_int(qobject_to_qint(obj1)) ==
68 qint_get_int(qobject_to_qint(obj2)));
69 return;
70 case QTYPE_QSTRING:
71 d->result = g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
72 qstring_get_str(qobject_to_qstring(obj2))) == 0;
73 return;
74 case QTYPE_QDICT:
75 d_new.expect = qobject_to_qdict(obj2);
76 d_new.result = true;
77 qdict_iter(qobject_to_qdict(obj1), qdict_cmp_do_simple, &d_new);
78 d->result = d_new.result;
79 return;
80 default:
81 abort();
85 static bool qdict_cmp_simple(QDict *a, QDict *b)
87 QDictCmpData d;
89 d.expect = b;
90 d.result = true;
91 qdict_iter(a, qdict_cmp_do_simple, &d);
92 return d.result;
95 /* This function is hooked as final emit function, which can verify the
96 correctness. */
97 static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp)
99 QObject *obj;
100 QDict *t;
101 int64_t s, ms;
103 /* Verify that we have timestamp, then remove it to compare other fields */
104 obj = qdict_get(d, "timestamp");
105 g_assert(obj);
106 t = qobject_to_qdict(obj);
107 g_assert(t);
108 obj = qdict_get(t, "seconds");
109 g_assert(obj && qobject_type(obj) == QTYPE_QINT);
110 s = qint_get_int(qobject_to_qint(obj));
111 obj = qdict_get(t, "microseconds");
112 g_assert(obj && qobject_type(obj) == QTYPE_QINT);
113 ms = qint_get_int(qobject_to_qint(obj));
114 if (s == -1) {
115 g_assert(ms == -1);
116 } else {
117 g_assert(ms >= 0 && ms <= 999999);
119 g_assert(qdict_size(t) == 2);
121 qdict_del(d, "timestamp");
123 g_assert(qdict_cmp_simple(d, test_event_data->expect));
127 static void event_prepare(TestEventData *data,
128 const void *unused)
130 /* Global variable test_event_data was used to pass the expectation, so
131 test cases can't be executed at same time. */
132 g_mutex_lock(&test_event_lock);
134 data->expect = qdict_new();
135 test_event_data = data;
138 static void event_teardown(TestEventData *data,
139 const void *unused)
141 QDECREF(data->expect);
142 test_event_data = NULL;
144 g_mutex_unlock(&test_event_lock);
147 static void event_test_add(const char *testpath,
148 void (*test_func)(TestEventData *data,
149 const void *user_data))
151 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
152 event_teardown);
156 /* Test cases */
158 static void test_event_a(TestEventData *data,
159 const void *unused)
161 QDict *d;
162 d = data->expect;
163 qdict_put(d, "event", qstring_from_str("EVENT_A"));
164 qapi_event_send_event_a(&error_abort);
167 static void test_event_b(TestEventData *data,
168 const void *unused)
170 QDict *d;
171 d = data->expect;
172 qdict_put(d, "event", qstring_from_str("EVENT_B"));
173 qapi_event_send_event_b(&error_abort);
176 static void test_event_c(TestEventData *data,
177 const void *unused)
179 QDict *d, *d_data, *d_b;
181 UserDefOne b;
182 b.integer = 2;
183 b.string = g_strdup("test1");
184 b.has_enum1 = false;
186 d_b = qdict_new();
187 qdict_put(d_b, "integer", qint_from_int(2));
188 qdict_put(d_b, "string", qstring_from_str("test1"));
190 d_data = qdict_new();
191 qdict_put(d_data, "a", qint_from_int(1));
192 qdict_put(d_data, "b", d_b);
193 qdict_put(d_data, "c", qstring_from_str("test2"));
195 d = data->expect;
196 qdict_put(d, "event", qstring_from_str("EVENT_C"));
197 qdict_put(d, "data", d_data);
199 qapi_event_send_event_c(true, 1, true, &b, "test2", &error_abort);
201 g_free(b.string);
204 /* Complex type */
205 static void test_event_d(TestEventData *data,
206 const void *unused)
208 UserDefOne struct1;
209 EventStructOne a;
210 QDict *d, *d_data, *d_a, *d_struct1;
212 struct1.integer = 2;
213 struct1.string = g_strdup("test1");
214 struct1.has_enum1 = true;
215 struct1.enum1 = ENUM_ONE_VALUE1;
217 a.struct1 = &struct1;
218 a.string = g_strdup("test2");
219 a.has_enum2 = true;
220 a.enum2 = ENUM_ONE_VALUE2;
222 d_struct1 = qdict_new();
223 qdict_put(d_struct1, "integer", qint_from_int(2));
224 qdict_put(d_struct1, "string", qstring_from_str("test1"));
225 qdict_put(d_struct1, "enum1", qstring_from_str("value1"));
227 d_a = qdict_new();
228 qdict_put(d_a, "struct1", d_struct1);
229 qdict_put(d_a, "string", qstring_from_str("test2"));
230 qdict_put(d_a, "enum2", qstring_from_str("value2"));
232 d_data = qdict_new();
233 qdict_put(d_data, "a", d_a);
234 qdict_put(d_data, "b", qstring_from_str("test3"));
235 qdict_put(d_data, "enum3", qstring_from_str("value3"));
237 d = data->expect;
238 qdict_put(d, "event", qstring_from_str("EVENT_D"));
239 qdict_put(d, "data", d_data);
241 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3,
242 &error_abort);
244 g_free(struct1.string);
245 g_free(a.string);
248 int main(int argc, char **argv)
250 #if !GLIB_CHECK_VERSION(2, 31, 0)
251 if (!g_thread_supported()) {
252 g_thread_init(NULL);
254 #endif
256 qmp_event_set_func_emit(event_test_emit);
258 g_test_init(&argc, &argv, NULL);
260 event_test_add("/event/event_a", test_event_a);
261 event_test_add("/event/event_b", test_event_b);
262 event_test_add("/event/event_c", test_event_c);
263 event_test_add("/event/event_d", test_event_d);
264 g_test_run();
266 return 0;