2 * QMP protocol test cases
4 * Copyright (c) 2017-2018 Red Hat Inc.
7 * Markus Armbruster <armbru@redhat.com>
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 void test_version(QObject
*version
)
30 v
= qobject_input_visitor_new(version
);
31 visit_type_VersionInfo(v
, "version", &vinfo
, &error_abort
);
32 qapi_free_VersionInfo(vinfo
);
36 static void assert_recovered(QTestState
*qts
)
40 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd' }");
41 qmp_assert_error_class(resp
, "CommandNotFound");
44 static void test_malformed(QTestState
*qts
)
49 qtest_qmp_send_raw(qts
, "{]\n");
50 resp
= qtest_qmp_receive(qts
);
51 qmp_assert_error_class(resp
, "GenericError");
52 assert_recovered(qts
);
54 /* lexical error: impossible byte outside string */
55 qtest_qmp_send_raw(qts
, "{\xFF");
56 resp
= qtest_qmp_receive(qts
);
57 qmp_assert_error_class(resp
, "GenericError");
58 assert_recovered(qts
);
60 /* lexical error: funny control character outside string */
61 qtest_qmp_send_raw(qts
, "{\x01");
62 resp
= qtest_qmp_receive(qts
);
63 qmp_assert_error_class(resp
, "GenericError");
64 assert_recovered(qts
);
66 /* lexical error: impossible byte in string */
67 qtest_qmp_send_raw(qts
, "{'bad \xFF");
68 resp
= qtest_qmp_receive(qts
);
69 qmp_assert_error_class(resp
, "GenericError");
70 assert_recovered(qts
);
72 /* lexical error: control character in string */
73 qtest_qmp_send_raw(qts
, "{'execute': 'nonexistent', 'id':'\n");
74 resp
= qtest_qmp_receive(qts
);
75 qmp_assert_error_class(resp
, "GenericError");
76 assert_recovered(qts
);
78 /* lexical error: interpolation */
79 qtest_qmp_send_raw(qts
, "%%p");
80 resp
= qtest_qmp_receive(qts
);
81 qmp_assert_error_class(resp
, "GenericError");
82 assert_recovered(qts
);
84 /* Not even a dictionary */
85 resp
= qtest_qmp(qts
, "null");
86 qmp_assert_error_class(resp
, "GenericError");
88 /* No "execute" key */
89 resp
= qtest_qmp(qts
, "{}");
90 qmp_assert_error_class(resp
, "GenericError");
92 /* "execute" isn't a string */
93 resp
= qtest_qmp(qts
, "{ 'execute': true }");
94 qmp_assert_error_class(resp
, "GenericError");
96 /* "arguments" isn't a dictionary */
97 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
98 qmp_assert_error_class(resp
, "GenericError");
101 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'extra': true }");
102 qmp_assert_error_class(resp
, "GenericError");
105 static void test_qmp_protocol(void)
107 QDict
*resp
, *q
, *ret
;
111 qts
= qtest_init_without_qmp_handshake(false, common_args
);
114 resp
= qtest_qmp_receive(qts
);
115 q
= qdict_get_qdict(resp
, "QMP");
117 test_version(qdict_get(q
, "version"));
118 capabilities
= qdict_get_qlist(q
, "capabilities");
119 g_assert(capabilities
&& qlist_empty(capabilities
));
122 /* Test valid command before handshake */
123 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
124 qmp_assert_error_class(resp
, "CommandNotFound");
126 /* Test malformed commands before handshake */
130 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
131 ret
= qdict_get_qdict(resp
, "return");
132 g_assert(ret
&& !qdict_size(ret
));
135 /* Test repeated handshake */
136 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
137 qmp_assert_error_class(resp
, "CommandNotFound");
139 /* Test valid command */
140 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
141 test_version(qdict_get(resp
, "return"));
144 /* Test malformed commands */
148 resp
= qtest_qmp(qts
, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
149 ret
= qdict_get_qdict(resp
, "return");
151 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, "cookie#1");
154 /* Test command failure with 'id' */
155 resp
= qtest_qmp(qts
, "{ 'execute': 'human-monitor-command', 'id': 2 }");
156 g_assert_cmpint(qdict_get_int(resp
, "id"), ==, 2);
157 qmp_assert_error_class(resp
, "GenericError");
162 /* Out-of-band tests */
164 char tmpdir
[] = "/tmp/qmp-test-XXXXXX";
167 static void setup_blocking_cmd(void)
169 if (!mkdtemp(tmpdir
)) {
170 g_error("mkdtemp: %s", strerror(errno
));
172 fifo_name
= g_strdup_printf("%s/fifo", tmpdir
);
173 if (mkfifo(fifo_name
, 0666)) {
174 g_error("mkfifo: %s", strerror(errno
));
178 static void cleanup_blocking_cmd(void)
184 static void send_cmd_that_blocks(QTestState
*s
, const char *id
)
186 qtest_qmp_send(s
, "{ 'execute': 'blockdev-add', 'id': %s,"
188 " 'driver': 'blkdebug', 'node-name': %s,"
190 " 'image': { 'driver': 'null-co' } } }",
194 static void unblock_blocked_cmd(void)
196 int fd
= open(fifo_name
, O_WRONLY
);
201 static void send_oob_cmd_that_fails(QTestState
*s
, const char *id
)
203 qtest_qmp_send(s
, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id
);
206 static void recv_cmd_id(QTestState
*s
, const char *id
)
208 QDict
*resp
= qtest_qmp_receive(s
);
210 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, id
);
214 static void test_qmp_oob(void)
218 const QListEntry
*entry
;
222 qts
= qtest_init_without_qmp_handshake(true, common_args
);
224 /* Check the greeting message. */
225 resp
= qtest_qmp_receive(qts
);
226 q
= qdict_get_qdict(resp
, "QMP");
228 capabilities
= qdict_get_qlist(q
, "capabilities");
229 g_assert(capabilities
&& !qlist_empty(capabilities
));
230 entry
= qlist_first(capabilities
);
232 qstr
= qobject_to(QString
, entry
->value
);
234 g_assert_cmpstr(qstring_get_str(qstr
), ==, "oob");
237 /* Try a fake capability, it should fail. */
238 resp
= qtest_qmp(qts
,
239 "{ 'execute': 'qmp_capabilities', "
240 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
241 g_assert(qdict_haskey(resp
, "error"));
244 /* Now, enable OOB in current QMP session, it should succeed. */
245 resp
= qtest_qmp(qts
,
246 "{ 'execute': 'qmp_capabilities', "
247 " 'arguments': { 'enable': [ 'oob' ] } }");
248 g_assert(qdict_haskey(resp
, "return"));
252 * Try any command that does not support OOB but with OOB flag. We
253 * should get failure.
255 resp
= qtest_qmp(qts
, "{ 'exec-oob': 'query-cpus' }");
256 g_assert(qdict_haskey(resp
, "error"));
259 /* OOB command overtakes slow in-band command */
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");
269 /* Even malformed in-band command fails in-band */
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();
280 /* Preconfig tests */
282 static void test_qmp_preconfig(void)
285 QTestState
*qs
= qtest_initf("%s --preconfig", common_args
);
287 /* preconfig state */
288 /* enabled commands, no error expected */
289 g_assert(!qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'query-commands' }")));
291 /* forbidden commands, expected error */
292 g_assert(qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'query-cpus' }")));
294 /* check that query-status returns preconfig state */
295 rsp
= qtest_qmp(qs
, "{ 'execute': 'query-status' }");
296 ret
= qdict_get_qdict(rsp
, "return");
298 g_assert_cmpstr(qdict_get_try_str(ret
, "status"), ==, "preconfig");
301 /* exit preconfig state */
302 g_assert(!qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'x-exit-preconfig' }")));
303 qtest_qmp_eventwait(qs
, "RESUME");
305 /* check that query-status returns running state */
306 rsp
= qtest_qmp(qs
, "{ 'execute': 'query-status' }");
307 ret
= qdict_get_qdict(rsp
, "return");
309 g_assert_cmpstr(qdict_get_try_str(ret
, "status"), ==, "running");
312 /* check that x-exit-preconfig returns error after exiting preconfig */
313 g_assert(qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'x-exit-preconfig' }")));
315 /* enabled commands, no error expected */
316 g_assert(!qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'query-cpus' }")));
321 int main(int argc
, char *argv
[])
323 g_test_init(&argc
, &argv
, NULL
);
325 qtest_add_func("qmp/protocol", test_qmp_protocol
);
326 qtest_add_func("qmp/oob", test_qmp_oob
);
327 qtest_add_func("qmp/preconfig", test_qmp_preconfig
);