xref: /openbmc/qemu/tests/unit/test-qmp-event.c (revision effd60c8)
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 static QDict *expected_event;
28 
29 void test_qapi_event_emit(test_QAPIEvent event, QDict *d)
30 {
31     QDict *t;
32     int64_t s, ms;
33 
34     g_assert(expected_event);
35 
36     /* Verify that we have timestamp, then remove it to compare other fields */
37     t = qdict_get_qdict(d, "timestamp");
38     g_assert(t);
39     s = qdict_get_try_int(t, "seconds", -2);
40     ms = qdict_get_try_int(t, "microseconds", -2);
41     if (s == -1) {
42         g_assert(ms == -1);
43     } else {
44         g_assert(s >= 0);
45         g_assert(ms >= 0 && ms <= 999999);
46     }
47     g_assert(qdict_size(t) == 2);
48 
49     qdict_del(d, "timestamp");
50 
51     g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(expected_event)));
52     qobject_unref(expected_event);
53     expected_event = NULL;
54 }
55 
56 static void test_event_a(void)
57 {
58     expected_event = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
59     qapi_event_send_event_a();
60     g_assert(!expected_event);
61 }
62 
63 static void test_event_b(void)
64 {
65     expected_event = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
66     qapi_event_send_event_b();
67     g_assert(!expected_event);
68 }
69 
70 static void test_event_c(void)
71 {
72     UserDefOne b = { .integer = 2, .string = (char *)"test1" };
73 
74     expected_event = qdict_from_jsonf_nofail(
75         "{ 'event': 'EVENT_C', 'data': {"
76         " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }");
77     qapi_event_send_event_c(true, 1, &b, "test2");
78     g_assert(!expected_event);
79 }
80 
81 /* Complex type */
82 static void test_event_d(void)
83 {
84     UserDefOne struct1 = {
85         .integer = 2, .string = (char *)"test1",
86         .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1,
87     };
88     EventStructOne a = {
89         .struct1 = &struct1,
90         .string = (char *)"test2",
91         .has_enum2 = true,
92         .enum2 = ENUM_ONE_VALUE2,
93     };
94 
95     expected_event = qdict_from_jsonf_nofail(
96         "{ 'event': 'EVENT_D', 'data': {"
97         " 'a': {"
98         "  'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' },"
99         "  'string': 'test2', 'enum2': 'value2' },"
100         " 'b': 'test3', 'enum3': 'value3' } }");
101     qapi_event_send_event_d(&a, "test3", NULL, true, ENUM_ONE_VALUE3);
102     g_assert(!expected_event);
103 }
104 
105 static void test_event_deprecated(void)
106 {
107     expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES1' }");
108 
109     memset(&compat_policy, 0, sizeof(compat_policy));
110 
111     qapi_event_send_test_event_features1();
112     g_assert(!expected_event);
113 
114     compat_policy.has_deprecated_output = true;
115     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
116     qapi_event_send_test_event_features1();
117 }
118 
119 static void test_event_deprecated_data(void)
120 {
121     memset(&compat_policy, 0, sizeof(compat_policy));
122 
123     expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0',"
124                                            " 'data': { 'foo': 42 } }");
125     qapi_event_send_test_event_features0(42);
126     g_assert(!expected_event);
127 
128 
129     compat_policy.has_deprecated_output = true;
130     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
131     expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES0' }");
132     qapi_event_send_test_event_features0(42);
133 }
134 
135 int main(int argc, char **argv)
136 {
137     g_test_init(&argc, &argv, NULL);
138 
139     g_test_add_func("/event/event_a", test_event_a);
140     g_test_add_func("/event/event_b", test_event_b);
141     g_test_add_func("/event/event_c", test_event_c);
142     g_test_add_func("/event/event_d", test_event_d);
143     g_test_add_func("/event/deprecated", test_event_deprecated);
144     g_test_add_func("/event/deprecated_data", test_event_deprecated_data);
145     g_test_run();
146 
147     return 0;
148 }
149