2 * QMP protocol test cases
4 * Copyright (c) 2017-2018 Red Hat Inc.
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-visit-misc.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"
22 const char common_args[] = "-nodefaults -machine none";
24 static const char *get_error_class(QDict *resp)
26 QDict *error = qdict_get_qdict(resp, "error");
27 const char *desc = qdict_get_try_str(error, "desc");
30 return error ? qdict_get_try_str(error, "class") : NULL;
33 static void test_version(QObject *version)
39 v = qobject_input_visitor_new(version);
40 visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
41 qapi_free_VersionInfo(vinfo);
45 static bool recovered(QTestState *qts)
50 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
51 ret = !strcmp(get_error_class(resp), "CommandNotFound");
56 static void test_malformed(QTestState *qts)
61 qtest_qmp_send_raw(qts, "{]\n");
62 resp = qtest_qmp_receive(qts);
63 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
65 g_assert(recovered(qts));
67 /* lexical error: impossible byte outside string */
68 qtest_qmp_send_raw(qts, "{\xFF");
69 resp = qtest_qmp_receive(qts);
70 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
72 g_assert(recovered(qts));
74 /* lexical error: funny control character outside string */
75 qtest_qmp_send_raw(qts, "{\x01");
76 resp = qtest_qmp_receive(qts);
77 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
79 g_assert(recovered(qts));
81 /* lexical error: impossible byte in string */
82 qtest_qmp_send_raw(qts, "{'bad \xFF");
83 resp = qtest_qmp_receive(qts);
84 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
86 g_assert(recovered(qts));
88 /* lexical error: control character in string */
89 qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n'}");
90 resp = qtest_qmp_receive(qts);
91 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound"); /* BUG */
93 g_assert(recovered(qts));
95 /* lexical error: interpolation */
96 qtest_qmp_send_raw(qts, "%%p\n");
97 resp = qtest_qmp_receive(qts);
98 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
100 g_assert(recovered(qts));
102 /* Not even a dictionary */
103 resp = qtest_qmp(qts, "null");
104 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
107 /* No "execute" key */
108 resp = qtest_qmp(qts, "{}");
109 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
112 /* "execute" isn't a string */
113 resp = qtest_qmp(qts, "{ 'execute': true }");
114 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
117 /* "arguments" isn't a dictionary */
118 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
119 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
123 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
124 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
128 static void test_qmp_protocol(void)
130 QDict *resp, *q, *ret;
134 qts = qtest_init_without_qmp_handshake(false, common_args);
137 resp = qtest_qmp_receive(qts);
138 q = qdict_get_qdict(resp, "QMP");
140 test_version(qdict_get(q, "version"));
141 capabilities = qdict_get_qlist(q, "capabilities");
142 g_assert(capabilities && qlist_empty(capabilities));
145 /* Test valid command before handshake */
146 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
147 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
150 /* Test malformed commands before handshake */
154 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
155 ret = qdict_get_qdict(resp, "return");
156 g_assert(ret && !qdict_size(ret));
159 /* Test repeated handshake */
160 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
161 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
164 /* Test valid command */
165 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
166 test_version(qdict_get(resp, "return"));
169 /* Test malformed commands */
173 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
174 ret = qdict_get_qdict(resp, "return");
176 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
179 /* Test command failure with 'id' */
180 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
181 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
182 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
188 /* Out-of-band tests */
190 char tmpdir[] = "/tmp/qmp-test-XXXXXX";
193 static void setup_blocking_cmd(void)
195 if (!mkdtemp(tmpdir)) {
196 g_error("mkdtemp: %s", strerror(errno));
198 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
199 if (mkfifo(fifo_name, 0666)) {
200 g_error("mkfifo: %s", strerror(errno));
204 static void cleanup_blocking_cmd(void)
210 static void send_cmd_that_blocks(QTestState *s, const char *id)
212 qtest_qmp_send(s, "{ 'execute': 'blockdev-add', 'id': %s,"
214 " 'driver': 'blkdebug', 'node-name': %s,"
216 " 'image': { 'driver': 'null-co' } } }",
220 static void unblock_blocked_cmd(void)
222 int fd = open(fifo_name, O_WRONLY);
227 static void send_oob_cmd_that_fails(QTestState *s, const char *id)
229 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
232 static void recv_cmd_id(QTestState *s, const char *id)
234 QDict *resp = qtest_qmp_receive(s);
236 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
240 static void test_qmp_oob(void)
244 const QListEntry *entry;
248 qts = qtest_init_without_qmp_handshake(true, common_args);
250 /* Check the greeting message. */
251 resp = qtest_qmp_receive(qts);
252 q = qdict_get_qdict(resp, "QMP");
254 capabilities = qdict_get_qlist(q, "capabilities");
255 g_assert(capabilities && !qlist_empty(capabilities));
256 entry = qlist_first(capabilities);
258 qstr = qobject_to(QString, entry->value);
260 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
263 /* Try a fake capability, it should fail. */
264 resp = qtest_qmp(qts,
265 "{ 'execute': 'qmp_capabilities', "
266 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
267 g_assert(qdict_haskey(resp, "error"));
270 /* Now, enable OOB in current QMP session, it should succeed. */
271 resp = qtest_qmp(qts,
272 "{ 'execute': 'qmp_capabilities', "
273 " 'arguments': { 'enable': [ 'oob' ] } }");
274 g_assert(qdict_haskey(resp, "return"));
278 * Try any command that does not support OOB but with OOB flag. We
279 * should get failure.
281 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
282 g_assert(qdict_haskey(resp, "error"));
285 /* OOB command overtakes slow in-band command */
286 setup_blocking_cmd();
287 send_cmd_that_blocks(qts, "ib-blocks-1");
288 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
289 send_oob_cmd_that_fails(qts, "oob-1");
290 recv_cmd_id(qts, "oob-1");
291 unblock_blocked_cmd();
292 recv_cmd_id(qts, "ib-blocks-1");
293 recv_cmd_id(qts, "ib-quick-1");
295 /* Even malformed in-band command fails in-band */
296 send_cmd_that_blocks(qts, "blocks-2");
297 qtest_qmp_send(qts, "{ 'id': 'err-2' }");
298 unblock_blocked_cmd();
299 recv_cmd_id(qts, "blocks-2");
300 recv_cmd_id(qts, "err-2");
301 cleanup_blocking_cmd();
306 /* Preconfig tests */
308 static void test_qmp_preconfig(void)
311 QTestState *qs = qtest_initf("%s --preconfig", common_args);
313 /* preconfig state */
314 /* enabled commands, no error expected */
315 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
317 /* forbidden commands, expected error */
318 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
320 /* check that query-status returns preconfig state */
321 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
322 ret = qdict_get_qdict(rsp, "return");
324 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "preconfig");
327 /* exit preconfig state */
328 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
329 qtest_qmp_eventwait(qs, "RESUME");
331 /* check that query-status returns running state */
332 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
333 ret = qdict_get_qdict(rsp, "return");
335 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
338 /* check that x-exit-preconfig returns error after exiting preconfig */
339 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
341 /* enabled commands, no error expected */
342 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
347 int main(int argc, char *argv[])
349 g_test_init(&argc, &argv, NULL);
351 qtest_add_func("qmp/protocol", test_qmp_protocol);
352 qtest_add_func("qmp/oob", test_qmp_oob);
353 qtest_add_func("qmp/preconfig", test_qmp_preconfig);