OSDN Git Service

docker: add special rule for deboostrapped images
[qmiga/qemu.git] / tests / test-qmp-event.c
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
14 #include "qemu/osdep.h"
15
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qnum.h"
21 #include "qapi/qmp/qstring.h"
22 #include "qapi/qmp-event.h"
23 #include "test-qapi-events.h"
24
25 typedef struct TestEventData {
26     QDict *expect;
27 } TestEventData;
28
29 typedef struct QDictCmpData {
30     QDict *expect;
31     bool result;
32 } QDictCmpData;
33
34 TestEventData *test_event_data;
35 static GMutex test_event_lock;
36
37 /* Only compares bool, int, string */
38 static
39 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque)
40
41 {
42     QObject *obj2;
43     QDictCmpData d_new, *d = opaque;
44     int64_t val1, val2;
45
46     if (!d->result) {
47         return;
48     }
49
50     obj2 = qdict_get(d->expect, key);
51     if (!obj2) {
52         d->result = false;
53         return;
54     }
55
56     if (qobject_type(obj1) != qobject_type(obj2)) {
57         d->result = false;
58         return;
59     }
60
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_QNUM:
67         g_assert(qnum_get_try_int(qobject_to(QNum, obj1), &val1));
68         g_assert(qnum_get_try_int(qobject_to(QNum, obj2), &val2));
69         d->result = val1 == val2;
70         return;
71     case QTYPE_QSTRING:
72         d->result = g_strcmp0(qstring_get_str(qobject_to(QString, obj1)),
73                               qstring_get_str(qobject_to(QString, obj2))) == 0;
74         return;
75     case QTYPE_QDICT:
76         d_new.expect = qobject_to(QDict, obj2);
77         d_new.result = true;
78         qdict_iter(qobject_to(QDict, obj1), qdict_cmp_do_simple, &d_new);
79         d->result = d_new.result;
80         return;
81     default:
82         abort();
83     }
84 }
85
86 static bool qdict_cmp_simple(QDict *a, QDict *b)
87 {
88     QDictCmpData d;
89
90     d.expect = b;
91     d.result = true;
92     qdict_iter(a, qdict_cmp_do_simple, &d);
93     return d.result;
94 }
95
96 /* This function is hooked as final emit function, which can verify the
97    correctness. */
98 static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp)
99 {
100     QDict *t;
101     int64_t s, ms;
102
103     /* Verify that we have timestamp, then remove it to compare other fields */
104     t = qdict_get_qdict(d, "timestamp");
105     g_assert(t);
106     s = qdict_get_try_int(t, "seconds", -2);
107     ms = qdict_get_try_int(t, "microseconds", -2);
108     if (s == -1) {
109         g_assert(ms == -1);
110     } else {
111         g_assert(s >= 0);
112         g_assert(ms >= 0 && ms <= 999999);
113     }
114     g_assert(qdict_size(t) == 2);
115
116     qdict_del(d, "timestamp");
117
118     g_assert(qdict_cmp_simple(d, test_event_data->expect));
119
120 }
121
122 static void event_prepare(TestEventData *data,
123                           const void *unused)
124 {
125     /* Global variable test_event_data was used to pass the expectation, so
126        test cases can't be executed at same time. */
127     g_mutex_lock(&test_event_lock);
128
129     data->expect = qdict_new();
130     test_event_data = data;
131 }
132
133 static void event_teardown(TestEventData *data,
134                            const void *unused)
135 {
136     qobject_unref(data->expect);
137     test_event_data = NULL;
138
139     g_mutex_unlock(&test_event_lock);
140 }
141
142 static void event_test_add(const char *testpath,
143                            void (*test_func)(TestEventData *data,
144                                              const void *user_data))
145 {
146     g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
147                event_teardown);
148 }
149
150
151 /* Test cases */
152
153 static void test_event_a(TestEventData *data,
154                          const void *unused)
155 {
156     QDict *d;
157     d = data->expect;
158     qdict_put_str(d, "event", "EVENT_A");
159     qapi_event_send_event_a(&error_abort);
160 }
161
162 static void test_event_b(TestEventData *data,
163                          const void *unused)
164 {
165     QDict *d;
166     d = data->expect;
167     qdict_put_str(d, "event", "EVENT_B");
168     qapi_event_send_event_b(&error_abort);
169 }
170
171 static void test_event_c(TestEventData *data,
172                          const void *unused)
173 {
174     QDict *d, *d_data, *d_b;
175
176     UserDefOne b;
177     b.integer = 2;
178     b.string = g_strdup("test1");
179     b.has_enum1 = false;
180
181     d_b = qdict_new();
182     qdict_put_int(d_b, "integer", 2);
183     qdict_put_str(d_b, "string", "test1");
184
185     d_data = qdict_new();
186     qdict_put_int(d_data, "a", 1);
187     qdict_put(d_data, "b", d_b);
188     qdict_put_str(d_data, "c", "test2");
189
190     d = data->expect;
191     qdict_put_str(d, "event", "EVENT_C");
192     qdict_put(d, "data", d_data);
193
194     qapi_event_send_event_c(true, 1, true, &b, "test2", &error_abort);
195
196     g_free(b.string);
197 }
198
199 /* Complex type */
200 static void test_event_d(TestEventData *data,
201                          const void *unused)
202 {
203     UserDefOne struct1;
204     EventStructOne a;
205     QDict *d, *d_data, *d_a, *d_struct1;
206
207     struct1.integer = 2;
208     struct1.string = g_strdup("test1");
209     struct1.has_enum1 = true;
210     struct1.enum1 = ENUM_ONE_VALUE1;
211
212     a.struct1 = &struct1;
213     a.string = g_strdup("test2");
214     a.has_enum2 = true;
215     a.enum2 = ENUM_ONE_VALUE2;
216
217     d_struct1 = qdict_new();
218     qdict_put_int(d_struct1, "integer", 2);
219     qdict_put_str(d_struct1, "string", "test1");
220     qdict_put_str(d_struct1, "enum1", "value1");
221
222     d_a = qdict_new();
223     qdict_put(d_a, "struct1", d_struct1);
224     qdict_put_str(d_a, "string", "test2");
225     qdict_put_str(d_a, "enum2", "value2");
226
227     d_data = qdict_new();
228     qdict_put(d_data, "a", d_a);
229     qdict_put_str(d_data, "b", "test3");
230     qdict_put_str(d_data, "enum3", "value3");
231
232     d = data->expect;
233     qdict_put_str(d, "event", "EVENT_D");
234     qdict_put(d, "data", d_data);
235
236     qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3,
237                            &error_abort);
238
239     g_free(struct1.string);
240     g_free(a.string);
241 }
242
243 int main(int argc, char **argv)
244 {
245     qmp_event_set_func_emit(event_test_emit);
246
247     g_test_init(&argc, &argv, NULL);
248
249     event_test_add("/event/event_a", test_event_a);
250     event_test_add("/event/event_b", test_event_b);
251     event_test_add("/event/event_c", test_event_c);
252     event_test_add("/event/event_d", test_event_d);
253     g_test_run();
254
255     return 0;
256 }