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