1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "libqos/libqtest.h"
15#include "qapi/error.h"
16#include "qapi/qapi-visit-control.h"
17#include "qapi/qmp/qdict.h"
18#include "qapi/qmp/qlist.h"
19#include "qapi/qobject-input-visitor.h"
20#include "qapi/qmp/qstring.h"
21
22const char common_args[] = "-nodefaults -machine none";
23
24static void test_version(QObject *version)
25{
26 Visitor *v;
27 VersionInfo *vinfo;
28
29 g_assert(version);
30 v = qobject_input_visitor_new(version);
31 visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
32 qapi_free_VersionInfo(vinfo);
33 visit_free(v);
34}
35
36static void assert_recovered(QTestState *qts)
37{
38 QDict *resp;
39
40 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
41 qmp_expect_error_and_unref(resp, "CommandNotFound");
42}
43
44static void test_malformed(QTestState *qts)
45{
46 QDict *resp;
47
48
49 qtest_qmp_send_raw(qts, "{]\n");
50 resp = qtest_qmp_receive_dict(qts);
51 qmp_expect_error_and_unref(resp, "GenericError");
52 assert_recovered(qts);
53
54
55 qtest_qmp_send_raw(qts, "{\xFF");
56 resp = qtest_qmp_receive_dict(qts);
57 qmp_expect_error_and_unref(resp, "GenericError");
58 assert_recovered(qts);
59
60
61 qtest_qmp_send_raw(qts, "{\x01");
62 resp = qtest_qmp_receive_dict(qts);
63 qmp_expect_error_and_unref(resp, "GenericError");
64 assert_recovered(qts);
65
66
67 qtest_qmp_send_raw(qts, "{'bad \xFF");
68 resp = qtest_qmp_receive_dict(qts);
69 qmp_expect_error_and_unref(resp, "GenericError");
70 assert_recovered(qts);
71
72
73 qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n");
74 resp = qtest_qmp_receive_dict(qts);
75 qmp_expect_error_and_unref(resp, "GenericError");
76 assert_recovered(qts);
77
78
79 qtest_qmp_send_raw(qts, "%%p");
80 resp = qtest_qmp_receive_dict(qts);
81 qmp_expect_error_and_unref(resp, "GenericError");
82 assert_recovered(qts);
83
84
85 resp = qtest_qmp(qts, "null");
86 qmp_expect_error_and_unref(resp, "GenericError");
87
88
89 resp = qtest_qmp(qts, "{}");
90 qmp_expect_error_and_unref(resp, "GenericError");
91
92
93 resp = qtest_qmp(qts, "{ 'execute': true }");
94 qmp_expect_error_and_unref(resp, "GenericError");
95
96
97 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
98 qmp_expect_error_and_unref(resp, "GenericError");
99
100
101 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
102 qmp_expect_error_and_unref(resp, "GenericError");
103}
104
105static void test_qmp_protocol(void)
106{
107 QDict *resp, *q, *ret;
108 QList *capabilities;
109 QTestState *qts;
110
111 qts = qtest_init_without_qmp_handshake(common_args);
112
113
114 resp = qtest_qmp_receive_dict(qts);
115 q = qdict_get_qdict(resp, "QMP");
116 g_assert(q);
117 test_version(qdict_get(q, "version"));
118 capabilities = qdict_get_qlist(q, "capabilities");
119 g_assert(capabilities);
120 qobject_unref(resp);
121
122
123 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
124 qmp_expect_error_and_unref(resp, "CommandNotFound");
125
126
127 test_malformed(qts);
128
129
130 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
131 ret = qdict_get_qdict(resp, "return");
132 g_assert(ret && !qdict_size(ret));
133 qobject_unref(resp);
134
135
136 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
137 qmp_expect_error_and_unref(resp, "CommandNotFound");
138
139
140 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
141 test_version(qdict_get(resp, "return"));
142 qobject_unref(resp);
143
144
145 test_malformed(qts);
146
147
148 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
149 ret = qdict_get_qdict(resp, "return");
150 g_assert(ret);
151 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
152 qobject_unref(resp);
153
154
155 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
156 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
157 qmp_expect_error_and_unref(resp, "GenericError");
158
159 qtest_quit(qts);
160}
161
162
163
164char tmpdir[] = "/tmp/qmp-test-XXXXXX";
165char *fifo_name;
166
167static void setup_blocking_cmd(void)
168{
169 if (!mkdtemp(tmpdir)) {
170 g_error("mkdtemp: %s", strerror(errno));
171 }
172 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
173 if (mkfifo(fifo_name, 0666)) {
174 g_error("mkfifo: %s", strerror(errno));
175 }
176}
177
178static void cleanup_blocking_cmd(void)
179{
180 unlink(fifo_name);
181 rmdir(tmpdir);
182}
183
184static void send_cmd_that_blocks(QTestState *s, const char *id)
185{
186 qtest_qmp_send(s, "{ 'execute': 'blockdev-add', 'id': %s,"
187 " 'arguments': {"
188 " 'driver': 'blkdebug', 'node-name': %s,"
189 " 'config': %s,"
190 " 'image': { 'driver': 'null-co', 'read-zeroes': true } } }",
191 id, id, fifo_name);
192}
193
194static void unblock_blocked_cmd(void)
195{
196 int fd = open(fifo_name, O_WRONLY);
197 g_assert(fd >= 0);
198 close(fd);
199}
200
201static void send_oob_cmd_that_fails(QTestState *s, const char *id)
202{
203 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
204}
205
206static void recv_cmd_id(QTestState *s, const char *id)
207{
208 QDict *resp = qtest_qmp_receive_dict(s);
209
210 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
211 qobject_unref(resp);
212}
213
214static void test_qmp_oob(void)
215{
216 QTestState *qts;
217 QDict *resp, *q;
218 const QListEntry *entry;
219 QList *capabilities;
220 QString *qstr;
221
222 qts = qtest_init_without_qmp_handshake(common_args);
223
224
225 resp = qtest_qmp_receive_dict(qts);
226 q = qdict_get_qdict(resp, "QMP");
227 g_assert(q);
228 capabilities = qdict_get_qlist(q, "capabilities");
229 g_assert(capabilities && !qlist_empty(capabilities));
230 entry = qlist_first(capabilities);
231 g_assert(entry);
232 qstr = qobject_to(QString, entry->value);
233 g_assert(qstr);
234 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
235 qobject_unref(resp);
236
237
238 resp = qtest_qmp(qts,
239 "{ 'execute': 'qmp_capabilities', "
240 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
241 g_assert(qdict_haskey(resp, "error"));
242 qobject_unref(resp);
243
244
245 resp = qtest_qmp(qts,
246 "{ 'execute': 'qmp_capabilities', "
247 " 'arguments': { 'enable': [ 'oob' ] } }");
248 g_assert(qdict_haskey(resp, "return"));
249 qobject_unref(resp);
250
251
252
253
254
255 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus-fast' }");
256 g_assert(qdict_haskey(resp, "error"));
257 qobject_unref(resp);
258
259
260 setup_blocking_cmd();
261 send_cmd_that_blocks(qts, "ib-blocks-1");
262 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
263 send_oob_cmd_that_fails(qts, "oob-1");
264 recv_cmd_id(qts, "oob-1");
265 unblock_blocked_cmd();
266 recv_cmd_id(qts, "ib-blocks-1");
267 recv_cmd_id(qts, "ib-quick-1");
268
269
270 send_cmd_that_blocks(qts, "blocks-2");
271 qtest_qmp_send(qts, "{ 'id': 'err-2' }");
272 unblock_blocked_cmd();
273 recv_cmd_id(qts, "blocks-2");
274 recv_cmd_id(qts, "err-2");
275 cleanup_blocking_cmd();
276
277 qtest_quit(qts);
278}
279
280
281
282static void test_qmp_preconfig(void)
283{
284 QDict *rsp, *ret;
285 QTestState *qs = qtest_initf("%s --preconfig", common_args);
286
287
288
289 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
290
291
292 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus-fast' }")));
293
294
295 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
296 ret = qdict_get_qdict(rsp, "return");
297 g_assert(ret);
298 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "prelaunch");
299 qobject_unref(rsp);
300
301
302 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
303 qtest_qmp_eventwait(qs, "RESUME");
304
305
306 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
307 ret = qdict_get_qdict(rsp, "return");
308 g_assert(ret);
309 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
310 qobject_unref(rsp);
311
312
313 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
314
315
316 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus-fast' }")));
317
318 qtest_quit(qs);
319}
320
321static void test_qmp_missing_any_arg(void)
322{
323 QTestState *qts;
324 QDict *resp;
325
326 qts = qtest_init(common_args);
327 resp = qtest_qmp(qts, "{'execute': 'qom-set', 'arguments':"
328 " { 'path': '/machine', 'property': 'rtc-time' } }");
329 g_assert_nonnull(resp);
330 qmp_expect_error_and_unref(resp, "GenericError");
331 qtest_quit(qts);
332}
333
334int main(int argc, char *argv[])
335{
336 g_test_init(&argc, &argv, NULL);
337
338 qtest_add_func("qmp/protocol", test_qmp_protocol);
339 qtest_add_func("qmp/oob", test_qmp_oob);
340 qtest_add_func("qmp/preconfig", test_qmp_preconfig);
341 qtest_add_func("qmp/missing-any-arg", test_qmp_missing_any_arg);
342
343 return g_test_run();
344}
345