Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / tests / unit / test-qmp-event.c
blob08e95a382bd8c1ebce3d8e15472e112514ac9126
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"
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"
27 static QDict *expected_event;
29 void test_qapi_event_emit(test_QAPIEvent event, QDict *d)
31 QDict *t;
32 int64_t s, ms;
34 g_assert(expected_event);
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);
47 g_assert(qdict_size(t) == 2);
49 qdict_del(d, "timestamp");
51 g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(expected_event)));
52 qobject_unref(expected_event);
53 expected_event = NULL;
56 static void test_event_a(void)
58 expected_event = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }");
59 qapi_event_send_event_a();
60 g_assert(!expected_event);
63 static void test_event_b(void)
65 expected_event = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }");
66 qapi_event_send_event_b();
67 g_assert(!expected_event);
70 static void test_event_c(void)
72 UserDefOne b = { .integer = 2, .string = (char *)"test1" };
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);
81 /* Complex type */
82 static void test_event_d(void)
84 UserDefOne struct1 = {
85 .integer = 2, .string = (char *)"test1",
86 .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1,
88 EventStructOne a = {
89 .struct1 = &struct1,
90 .string = (char *)"test2",
91 .has_enum2 = true,
92 .enum2 = ENUM_ONE_VALUE2,
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);
105 static void test_event_deprecated(void)
107 expected_event = qdict_from_jsonf_nofail("{ 'event': 'TEST_EVENT_FEATURES1' }");
109 memset(&compat_policy, 0, sizeof(compat_policy));
111 qapi_event_send_test_event_features1();
112 g_assert(!expected_event);
114 compat_policy.has_deprecated_output = true;
115 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
116 qapi_event_send_test_event_features1();
119 static void test_event_deprecated_data(void)
121 memset(&compat_policy, 0, sizeof(compat_policy));
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);
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);
135 int main(int argc, char **argv)
137 g_test_init(&argc, &argv, NULL);
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();
147 return 0;