xref: /openbmc/qemu/tests/unit/test-qmp-event.c (revision 191710c221f65b1542f6ea7fa4d30dde6e134fd7)
1da668aa1SThomas Huth /*
2da668aa1SThomas Huth  * qapi event unit-tests.
3da668aa1SThomas Huth  *
4da668aa1SThomas Huth  * Copyright (c) 2014 Wenchao Xia
5da668aa1SThomas Huth  *
6da668aa1SThomas Huth  * Authors:
7da668aa1SThomas Huth  *  Wenchao Xia   <wenchaoqemu@gmail.com>
8da668aa1SThomas Huth  *
9da668aa1SThomas Huth  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10da668aa1SThomas Huth  * See the COPYING.LIB file in the top-level directory.
11da668aa1SThomas Huth  *
12da668aa1SThomas Huth  */
13da668aa1SThomas Huth 
14da668aa1SThomas Huth #include "qemu/osdep.h"
15da668aa1SThomas Huth 
16278fc2f7SMarkus Armbruster #include "qapi/compat-policy.h"
17da668aa1SThomas Huth #include "qapi/error.h"
18da668aa1SThomas Huth #include "qapi/qmp/qbool.h"
19da668aa1SThomas Huth #include "qapi/qmp/qdict.h"
20da668aa1SThomas Huth #include "qapi/qmp/qjson.h"
21da668aa1SThomas Huth #include "qapi/qmp/qnum.h"
22da668aa1SThomas Huth #include "qapi/qmp/qstring.h"
23da668aa1SThomas Huth #include "qapi/qmp-event.h"
24da668aa1SThomas Huth #include "test-qapi-events.h"
25da668aa1SThomas Huth #include "test-qapi-emit-events.h"
26da668aa1SThomas Huth 
27*17b2ecc3SMarkus Armbruster static QDict *expected_event;
28da668aa1SThomas Huth 
test_qapi_event_emit(test_QAPIEvent event,QDict * d)29da668aa1SThomas Huth void test_qapi_event_emit(test_QAPIEvent event, QDict *d)
30da668aa1SThomas Huth {
31da668aa1SThomas Huth     QDict *t;
32da668aa1SThomas Huth     int64_t s, ms;
33da668aa1SThomas Huth 
34*17b2ecc3SMarkus Armbruster     g_assert(expected_event);
355712b7e4SMarkus Armbruster 
36da668aa1SThomas Huth     /* Verify that we have timestamp, then remove it to compare other fields */
37da668aa1SThomas Huth     t = qdict_get_qdict(d, "timestamp");
38da668aa1SThomas Huth     g_assert(t);
39da668aa1SThomas Huth     s = qdict_get_try_int(t, "seconds", -2);
40da668aa1SThomas Huth     ms = qdict_get_try_int(t, "microseconds", -2);
41da668aa1SThomas Huth     if (s == -1) {
42da668aa1SThomas Huth         g_assert(ms == -1);
43da668aa1SThomas Huth     } else {
44da668aa1SThomas Huth         g_assert(s >= 0);
45da668aa1SThomas Huth         g_assert(ms >= 0 && ms <= 999999);
46da668aa1SThomas Huth     }
47da668aa1SThomas Huth     g_assert(qdict_size(t) == 2);
48da668aa1SThomas Huth 
49da668aa1SThomas Huth     qdict_del(d, "timestamp");
50da668aa1SThomas Huth 
51*17b2ecc3SMarkus Armbruster     g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(expected_event)));
52*17b2ecc3SMarkus Armbruster     qobject_unref(expected_event);
53*17b2ecc3SMarkus Armbruster     expected_event = NULL;
54da668aa1SThomas Huth }
55da668aa1SThomas Huth 
test_event_a(void)56*17b2ecc3SMarkus Armbruster static void test_event_a(void)
57da668aa1SThomas Huth {
58*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
59da668aa1SThomas Huth     qapi_event_send_event_a();
60*17b2ecc3SMarkus Armbruster     g_assert(!expected_event);
61da668aa1SThomas Huth }
62da668aa1SThomas Huth 
test_event_b(void)63*17b2ecc3SMarkus Armbruster static void test_event_b(void)
64da668aa1SThomas Huth {
65*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
66da668aa1SThomas Huth     qapi_event_send_event_b();
67*17b2ecc3SMarkus Armbruster     g_assert(!expected_event);
68da668aa1SThomas Huth }
69da668aa1SThomas Huth 
test_event_c(void)70*17b2ecc3SMarkus Armbruster static void test_event_c(void)
71da668aa1SThomas Huth {
72da668aa1SThomas Huth     UserDefOne b = { .integer = 2, .string = (char *)"test1" };
73da668aa1SThomas Huth 
74*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail(
75da668aa1SThomas Huth         "{ 'event': 'EVENT_C', 'data': {"
76da668aa1SThomas Huth         " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }");
774b2fc7dbSMarkus Armbruster     qapi_event_send_event_c(true, 1, &b, "test2");
78*17b2ecc3SMarkus Armbruster     g_assert(!expected_event);
79da668aa1SThomas Huth }
80da668aa1SThomas Huth 
81da668aa1SThomas Huth /* Complex type */
test_event_d(void)82*17b2ecc3SMarkus Armbruster static void test_event_d(void)
83da668aa1SThomas Huth {
84da668aa1SThomas Huth     UserDefOne struct1 = {
85da668aa1SThomas Huth         .integer = 2, .string = (char *)"test1",
86da668aa1SThomas Huth         .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1,
87da668aa1SThomas Huth     };
88da668aa1SThomas Huth     EventStructOne a = {
89da668aa1SThomas Huth         .struct1 = &struct1,
90da668aa1SThomas Huth         .string = (char *)"test2",
91da668aa1SThomas Huth         .has_enum2 = true,
92da668aa1SThomas Huth         .enum2 = ENUM_ONE_VALUE2,
93da668aa1SThomas Huth     };
94da668aa1SThomas Huth 
95*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail(
96da668aa1SThomas Huth         "{ 'event': 'EVENT_D', 'data': {"
97da668aa1SThomas Huth         " 'a': {"
98da668aa1SThomas Huth         "  'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' },"
99da668aa1SThomas Huth         "  'string': 'test2', 'enum2': 'value2' },"
100da668aa1SThomas Huth         " 'b': 'test3', 'enum3': 'value3' } }");
1014b2fc7dbSMarkus Armbruster     qapi_event_send_event_d(&a, "test3", NULL, true, ENUM_ONE_VALUE3);
102*17b2ecc3SMarkus Armbruster     g_assert(!expected_event);
103da668aa1SThomas Huth }
104da668aa1SThomas Huth 
test_event_deprecated(void)105*17b2ecc3SMarkus Armbruster static void test_event_deprecated(void)
106278fc2f7SMarkus Armbruster {
107*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES1' }");
108278fc2f7SMarkus Armbruster 
109278fc2f7SMarkus Armbruster     memset(&compat_policy, 0, sizeof(compat_policy));
110278fc2f7SMarkus Armbruster 
111278fc2f7SMarkus Armbruster     qapi_event_send_test_event_features1();
112*17b2ecc3SMarkus Armbruster     g_assert(!expected_event);
113278fc2f7SMarkus Armbruster 
114278fc2f7SMarkus Armbruster     compat_policy.has_deprecated_output = true;
115278fc2f7SMarkus Armbruster     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
116278fc2f7SMarkus Armbruster     qapi_event_send_test_event_features1();
117278fc2f7SMarkus Armbruster }
118278fc2f7SMarkus Armbruster 
test_event_deprecated_data(void)119*17b2ecc3SMarkus Armbruster static void test_event_deprecated_data(void)
120a291a38fSMarkus Armbruster {
121a291a38fSMarkus Armbruster     memset(&compat_policy, 0, sizeof(compat_policy));
122a291a38fSMarkus Armbruster 
123*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0',"
124a291a38fSMarkus Armbruster                                            " 'data': { 'foo': 42 } }");
125a291a38fSMarkus Armbruster     qapi_event_send_test_event_features0(42);
126*17b2ecc3SMarkus Armbruster     g_assert(!expected_event);
127a291a38fSMarkus Armbruster 
128a291a38fSMarkus Armbruster 
129a291a38fSMarkus Armbruster     compat_policy.has_deprecated_output = true;
130a291a38fSMarkus Armbruster     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
131*17b2ecc3SMarkus Armbruster     expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0' }");
132a291a38fSMarkus Armbruster     qapi_event_send_test_event_features0(42);
133a291a38fSMarkus Armbruster }
134a291a38fSMarkus Armbruster 
main(int argc,char ** argv)135da668aa1SThomas Huth int main(int argc, char **argv)
136da668aa1SThomas Huth {
137da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
138da668aa1SThomas Huth 
139*17b2ecc3SMarkus Armbruster     g_test_add_func("/event/event_a", test_event_a);
140*17b2ecc3SMarkus Armbruster     g_test_add_func("/event/event_b", test_event_b);
141*17b2ecc3SMarkus Armbruster     g_test_add_func("/event/event_c", test_event_c);
142*17b2ecc3SMarkus Armbruster     g_test_add_func("/event/event_d", test_event_d);
143*17b2ecc3SMarkus Armbruster     g_test_add_func("/event/deprecated", test_event_deprecated);
144*17b2ecc3SMarkus Armbruster     g_test_add_func("/event/deprecated_data", test_event_deprecated_data);
145da668aa1SThomas Huth     g_test_run();
146da668aa1SThomas Huth 
147da668aa1SThomas Huth     return 0;
148da668aa1SThomas Huth }
149