]>
Commit | Line | Data |
---|---|---|
f6dadb02 WX |
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 | ||
681c28a3 | 14 | #include "qemu/osdep.h" |
f6dadb02 WX |
15 | |
16 | #include "qemu-common.h" | |
e688df6b | 17 | #include "qapi/error.h" |
6b673957 | 18 | #include "qapi/qmp/qbool.h" |
452fcdbc | 19 | #include "qapi/qmp/qdict.h" |
3ecc3932 | 20 | #include "qapi/qmp/qjson.h" |
15280c36 | 21 | #include "qapi/qmp/qnum.h" |
6b673957 | 22 | #include "qapi/qmp/qstring.h" |
f6dadb02 | 23 | #include "qapi/qmp-event.h" |
eb815e24 | 24 | #include "test-qapi-events.h" |
5d75648b | 25 | #include "test-qapi-emit-events.h" |
f6dadb02 WX |
26 | |
27 | typedef struct TestEventData { | |
28 | QDict *expect; | |
29 | } TestEventData; | |
30 | ||
f6dadb02 | 31 | TestEventData *test_event_data; |
e7b3af81 | 32 | static GMutex test_event_lock; |
f6dadb02 | 33 | |
a9529100 | 34 | void test_qapi_event_emit(test_QAPIEvent event, QDict *d) |
f6dadb02 | 35 | { |
f6dadb02 WX |
36 | QDict *t; |
37 | int64_t s, ms; | |
38 | ||
39 | /* Verify that we have timestamp, then remove it to compare other fields */ | |
4b32e11a | 40 | t = qdict_get_qdict(d, "timestamp"); |
f6dadb02 | 41 | g_assert(t); |
4b32e11a MA |
42 | s = qdict_get_try_int(t, "seconds", -2); |
43 | ms = qdict_get_try_int(t, "microseconds", -2); | |
f6dadb02 WX |
44 | if (s == -1) { |
45 | g_assert(ms == -1); | |
46 | } else { | |
4b32e11a | 47 | g_assert(s >= 0); |
f6dadb02 WX |
48 | g_assert(ms >= 0 && ms <= 999999); |
49 | } | |
50 | g_assert(qdict_size(t) == 2); | |
51 | ||
52 | qdict_del(d, "timestamp"); | |
53 | ||
052be50c | 54 | g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(test_event_data->expect))); |
f6dadb02 WX |
55 | |
56 | } | |
57 | ||
58 | static void event_prepare(TestEventData *data, | |
59 | const void *unused) | |
60 | { | |
61 | /* Global variable test_event_data was used to pass the expectation, so | |
62 | test cases can't be executed at same time. */ | |
63 | g_mutex_lock(&test_event_lock); | |
f6dadb02 WX |
64 | test_event_data = data; |
65 | } | |
66 | ||
67 | static void event_teardown(TestEventData *data, | |
68 | const void *unused) | |
69 | { | |
f6dadb02 | 70 | test_event_data = NULL; |
f6dadb02 WX |
71 | g_mutex_unlock(&test_event_lock); |
72 | } | |
73 | ||
74 | static void event_test_add(const char *testpath, | |
75 | void (*test_func)(TestEventData *data, | |
76 | const void *user_data)) | |
77 | { | |
78 | g_test_add(testpath, TestEventData, NULL, event_prepare, test_func, | |
79 | event_teardown); | |
80 | } | |
81 | ||
82 | ||
83 | /* Test cases */ | |
84 | ||
85 | static void test_event_a(TestEventData *data, | |
86 | const void *unused) | |
87 | { | |
3ecc3932 | 88 | data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }"); |
3ab72385 | 89 | qapi_event_send_event_a(); |
3ecc3932 | 90 | qobject_unref(data->expect); |
f6dadb02 WX |
91 | } |
92 | ||
93 | static void test_event_b(TestEventData *data, | |
94 | const void *unused) | |
95 | { | |
3ecc3932 | 96 | data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }"); |
3ab72385 | 97 | qapi_event_send_event_b(); |
3ecc3932 | 98 | qobject_unref(data->expect); |
f6dadb02 WX |
99 | } |
100 | ||
101 | static void test_event_c(TestEventData *data, | |
102 | const void *unused) | |
103 | { | |
3ecc3932 | 104 | UserDefOne b = { .integer = 2, .string = (char *)"test1" }; |
f6dadb02 | 105 | |
3ecc3932 MA |
106 | data->expect = qdict_from_jsonf_nofail( |
107 | "{ 'event': 'EVENT_C', 'data': {" | |
108 | " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }"); | |
3ab72385 | 109 | qapi_event_send_event_c(true, 1, true, &b, "test2"); |
3ecc3932 | 110 | qobject_unref(data->expect); |
f6dadb02 WX |
111 | } |
112 | ||
113 | /* Complex type */ | |
114 | static void test_event_d(TestEventData *data, | |
115 | const void *unused) | |
116 | { | |
3ecc3932 MA |
117 | UserDefOne struct1 = { |
118 | .integer = 2, .string = (char *)"test1", | |
119 | .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1, | |
120 | }; | |
121 | EventStructOne a = { | |
122 | .struct1 = &struct1, | |
123 | .string = (char *)"test2", | |
124 | .has_enum2 = true, | |
125 | .enum2 = ENUM_ONE_VALUE2, | |
126 | }; | |
127 | ||
128 | data->expect = qdict_from_jsonf_nofail( | |
129 | "{ 'event': 'EVENT_D', 'data': {" | |
130 | " 'a': {" | |
131 | " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' }," | |
132 | " 'string': 'test2', 'enum2': 'value2' }," | |
133 | " 'b': 'test3', 'enum3': 'value3' } }"); | |
3ab72385 | 134 | qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3); |
3ecc3932 | 135 | qobject_unref(data->expect); |
f6dadb02 WX |
136 | } |
137 | ||
138 | int main(int argc, char **argv) | |
139 | { | |
f6dadb02 WX |
140 | g_test_init(&argc, &argv, NULL); |
141 | ||
142 | event_test_add("/event/event_a", test_event_a); | |
143 | event_test_add("/event/event_b", test_event_b); | |
144 | event_test_add("/event/event_c", test_event_c); | |
145 | event_test_add("/event/event_d", test_event_d); | |
146 | g_test_run(); | |
147 | ||
148 | return 0; | |
149 | } |