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), ==, "GenericError");
93 g_assert(recovered(qts));
95 /* lexical error: interpolation */
96 qtest_qmp_send_raw(qts, "%%p\n");
97 /* two errors, one for "%", one for "p" */
98 resp = qtest_qmp_receive(qts);
99 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
101 resp = qtest_qmp_receive(qts);
102 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
104 g_assert(recovered(qts));
106 /* Not even a dictionary */
107 resp = qtest_qmp(qts, "null");
108 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
111 /* No "execute" key */
112 resp = qtest_qmp(qts, "{}");
113 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
116 /* "execute" isn't a string */
117 resp = qtest_qmp(qts, "{ 'execute': true }");
118 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
121 /* "arguments" isn't a dictionary */
122 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
123 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
127 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
128 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
132 static void test_qmp_protocol(void)
134 QDict *resp, *q, *ret;
138 qts = qtest_init_without_qmp_handshake(false, common_args);
141 resp = qtest_qmp_receive(qts);
142 q = qdict_get_qdict(resp, "QMP");
144 test_version(qdict_get(q, "version"));
145 capabilities = qdict_get_qlist(q, "capabilities");
146 g_assert(capabilities && qlist_empty(capabilities));
149 /* Test valid command before handshake */
150 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
151 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
154 /* Test malformed commands before handshake */
158 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
159 ret = qdict_get_qdict(resp, "return");
160 g_assert(ret && !qdict_size(ret));
163 /* Test repeated handshake */
164 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
165 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
168 /* Test valid command */
169 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
170 test_version(qdict_get(resp, "return"));
173 /* Test malformed commands */
177 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
178 ret = qdict_get_qdict(resp, "return");
180 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
183 /* Test command failure with 'id' */
184 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
185 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
186 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
192 /* Out-of-band tests */
194 char tmpdir[] = "/tmp/qmp-test-XXXXXX";
197 static void setup_blocking_cmd(void)
199 if (!mkdtemp(tmpdir)) {
200 g_error("mkdtemp: %s", strerror(errno));
202 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
203 if (mkfifo(fifo_name, 0666)) {
204 g_error("mkfifo: %s", strerror(errno));
208 static void cleanup_blocking_cmd(void)
214 static void send_cmd_that_blocks(QTestState *s, const char *id)
216 qtest_qmp_send(s, "{ 'execute': 'blockdev-add', 'id': %s,"
218 " 'driver': 'blkdebug', 'node-name': %s,"
220 " 'image': { 'driver': 'null-co' } } }",
224 static void unblock_blocked_cmd(void)
226 int fd = open(fifo_name, O_WRONLY);
231 static void send_oob_cmd_that_fails(QTestState *s, const char *id)
233 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
236 static void recv_cmd_id(QTestState *s, const char *id)
238 QDict *resp = qtest_qmp_receive(s);
240 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
244 static void test_qmp_oob(void)
248 const QListEntry *entry;
252 qts = qtest_init_without_qmp_handshake(true, common_args);
254 /* Check the greeting message. */
255 resp = qtest_qmp_receive(qts);
256 q = qdict_get_qdict(resp, "QMP");
258 capabilities = qdict_get_qlist(q, "capabilities");
259 g_assert(capabilities && !qlist_empty(capabilities));
260 entry = qlist_first(capabilities);
262 qstr = qobject_to(QString, entry->value);
264 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
267 /* Try a fake capability, it should fail. */
268 resp = qtest_qmp(qts,
269 "{ 'execute': 'qmp_capabilities', "
270 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
271 g_assert(qdict_haskey(resp, "error"));
274 /* Now, enable OOB in current QMP session, it should succeed. */
275 resp = qtest_qmp(qts,
276 "{ 'execute': 'qmp_capabilities', "
277 " 'arguments': { 'enable': [ 'oob' ] } }");
278 g_assert(qdict_haskey(resp, "return"));
282 * Try any command that does not support OOB but with OOB flag. We
283 * should get failure.
285 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
286 g_assert(qdict_haskey(resp, "error"));
289 /* OOB command overtakes slow in-band command */
290 setup_blocking_cmd();
291 send_cmd_that_blocks(qts, "ib-blocks-1");
292 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
293 send_oob_cmd_that_fails(qts, "oob-1");
294 recv_cmd_id(qts, "oob-1");
295 unblock_blocked_cmd();
296 recv_cmd_id(qts, "ib-blocks-1");
297 recv_cmd_id(qts, "ib-quick-1");
299 /* Even malformed in-band command fails in-band */
300 send_cmd_that_blocks(qts, "blocks-2");
301 qtest_qmp_send(qts, "{ 'id': 'err-2' }");
302 unblock_blocked_cmd();
303 recv_cmd_id(qts, "blocks-2");
304 recv_cmd_id(qts, "err-2");
305 cleanup_blocking_cmd();
310 /* Preconfig tests */
312 static void test_qmp_preconfig(void)
315 QTestState *qs = qtest_initf("%s --preconfig", common_args);
317 /* preconfig state */
318 /* enabled commands, no error expected */
319 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
321 /* forbidden commands, expected error */
322 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
324 /* check that query-status returns preconfig state */
325 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
326 ret = qdict_get_qdict(rsp, "return");
328 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "preconfig");
331 /* exit preconfig state */
332 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
333 qtest_qmp_eventwait(qs, "RESUME");
335 /* check that query-status returns running state */
336 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
337 ret = qdict_get_qdict(rsp, "return");
339 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
342 /* check that x-exit-preconfig returns error after exiting preconfig */
343 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
345 /* enabled commands, no error expected */
346 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
351 int main(int argc, char *argv[])
353 g_test_init(&argc, &argv, NULL);
355 qtest_add_func("qmp/protocol", test_qmp_protocol);
356 qtest_add_func("qmp/oob", test_qmp_oob);
357 qtest_add_func("qmp/preconfig", test_qmp_preconfig);