]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qmp-test.c
Merge remote-tracking branch 'remotes/alistair/tags/pull-riscv-pullreq-20180905'...
[mirror_qemu.git] / tests / qmp-test.c
1 /*
2 * QMP protocol test cases
3 *
4 * Copyright (c) 2017-2018 Red Hat Inc.
5 *
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>
8 *
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include "libqtest.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"
21
22 const char common_args[] = "-nodefaults -machine none";
23
24 static 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
36 static void assert_recovered(QTestState *qts)
37 {
38 QDict *resp;
39
40 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
41 qmp_assert_error_class(resp, "CommandNotFound");
42 }
43
44 static void test_malformed(QTestState *qts)
45 {
46 QDict *resp;
47
48 /* syntax error */
49 qtest_qmp_send_raw(qts, "{]\n");
50 resp = qtest_qmp_receive(qts);
51 qmp_assert_error_class(resp, "GenericError");
52 assert_recovered(qts);
53
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);
59
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);
65
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);
71
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);
77
78 /* lexical error: interpolation */
79 qtest_qmp_send_raw(qts, "%%p\n");
80 /* two errors, one for "%", one for "p" */
81 resp = qtest_qmp_receive(qts);
82 qmp_assert_error_class(resp, "GenericError");
83 resp = qtest_qmp_receive(qts);
84 qmp_assert_error_class(resp, "GenericError");
85 assert_recovered(qts);
86
87 /* Not even a dictionary */
88 resp = qtest_qmp(qts, "null");
89 qmp_assert_error_class(resp, "GenericError");
90
91 /* No "execute" key */
92 resp = qtest_qmp(qts, "{}");
93 qmp_assert_error_class(resp, "GenericError");
94
95 /* "execute" isn't a string */
96 resp = qtest_qmp(qts, "{ 'execute': true }");
97 qmp_assert_error_class(resp, "GenericError");
98
99 /* "arguments" isn't a dictionary */
100 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
101 qmp_assert_error_class(resp, "GenericError");
102
103 /* extra key */
104 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
105 qmp_assert_error_class(resp, "GenericError");
106 }
107
108 static void test_qmp_protocol(void)
109 {
110 QDict *resp, *q, *ret;
111 QList *capabilities;
112 QTestState *qts;
113
114 qts = qtest_init_without_qmp_handshake(false, common_args);
115
116 /* Test greeting */
117 resp = qtest_qmp_receive(qts);
118 q = qdict_get_qdict(resp, "QMP");
119 g_assert(q);
120 test_version(qdict_get(q, "version"));
121 capabilities = qdict_get_qlist(q, "capabilities");
122 g_assert(capabilities && qlist_empty(capabilities));
123 qobject_unref(resp);
124
125 /* Test valid command before handshake */
126 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
127 qmp_assert_error_class(resp, "CommandNotFound");
128
129 /* Test malformed commands before handshake */
130 test_malformed(qts);
131
132 /* Test handshake */
133 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
134 ret = qdict_get_qdict(resp, "return");
135 g_assert(ret && !qdict_size(ret));
136 qobject_unref(resp);
137
138 /* Test repeated handshake */
139 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
140 qmp_assert_error_class(resp, "CommandNotFound");
141
142 /* Test valid command */
143 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
144 test_version(qdict_get(resp, "return"));
145 qobject_unref(resp);
146
147 /* Test malformed commands */
148 test_malformed(qts);
149
150 /* Test 'id' */
151 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
152 ret = qdict_get_qdict(resp, "return");
153 g_assert(ret);
154 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
155 qobject_unref(resp);
156
157 /* Test command failure with 'id' */
158 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
159 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
160 qmp_assert_error_class(resp, "GenericError");
161
162 qtest_quit(qts);
163 }
164
165 /* Out-of-band tests */
166
167 char tmpdir[] = "/tmp/qmp-test-XXXXXX";
168 char *fifo_name;
169
170 static void setup_blocking_cmd(void)
171 {
172 if (!mkdtemp(tmpdir)) {
173 g_error("mkdtemp: %s", strerror(errno));
174 }
175 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
176 if (mkfifo(fifo_name, 0666)) {
177 g_error("mkfifo: %s", strerror(errno));
178 }
179 }
180
181 static void cleanup_blocking_cmd(void)
182 {
183 unlink(fifo_name);
184 rmdir(tmpdir);
185 }
186
187 static void send_cmd_that_blocks(QTestState *s, const char *id)
188 {
189 qtest_qmp_send(s, "{ 'execute': 'blockdev-add', 'id': %s,"
190 " 'arguments': {"
191 " 'driver': 'blkdebug', 'node-name': %s,"
192 " 'config': %s,"
193 " 'image': { 'driver': 'null-co' } } }",
194 id, id, fifo_name);
195 }
196
197 static void unblock_blocked_cmd(void)
198 {
199 int fd = open(fifo_name, O_WRONLY);
200 g_assert(fd >= 0);
201 close(fd);
202 }
203
204 static void send_oob_cmd_that_fails(QTestState *s, const char *id)
205 {
206 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
207 }
208
209 static void recv_cmd_id(QTestState *s, const char *id)
210 {
211 QDict *resp = qtest_qmp_receive(s);
212
213 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
214 qobject_unref(resp);
215 }
216
217 static void test_qmp_oob(void)
218 {
219 QTestState *qts;
220 QDict *resp, *q;
221 const QListEntry *entry;
222 QList *capabilities;
223 QString *qstr;
224
225 qts = qtest_init_without_qmp_handshake(true, common_args);
226
227 /* Check the greeting message. */
228 resp = qtest_qmp_receive(qts);
229 q = qdict_get_qdict(resp, "QMP");
230 g_assert(q);
231 capabilities = qdict_get_qlist(q, "capabilities");
232 g_assert(capabilities && !qlist_empty(capabilities));
233 entry = qlist_first(capabilities);
234 g_assert(entry);
235 qstr = qobject_to(QString, entry->value);
236 g_assert(qstr);
237 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
238 qobject_unref(resp);
239
240 /* Try a fake capability, it should fail. */
241 resp = qtest_qmp(qts,
242 "{ 'execute': 'qmp_capabilities', "
243 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
244 g_assert(qdict_haskey(resp, "error"));
245 qobject_unref(resp);
246
247 /* Now, enable OOB in current QMP session, it should succeed. */
248 resp = qtest_qmp(qts,
249 "{ 'execute': 'qmp_capabilities', "
250 " 'arguments': { 'enable': [ 'oob' ] } }");
251 g_assert(qdict_haskey(resp, "return"));
252 qobject_unref(resp);
253
254 /*
255 * Try any command that does not support OOB but with OOB flag. We
256 * should get failure.
257 */
258 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
259 g_assert(qdict_haskey(resp, "error"));
260 qobject_unref(resp);
261
262 /* OOB command overtakes slow in-band command */
263 setup_blocking_cmd();
264 send_cmd_that_blocks(qts, "ib-blocks-1");
265 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
266 send_oob_cmd_that_fails(qts, "oob-1");
267 recv_cmd_id(qts, "oob-1");
268 unblock_blocked_cmd();
269 recv_cmd_id(qts, "ib-blocks-1");
270 recv_cmd_id(qts, "ib-quick-1");
271
272 /* Even malformed in-band command fails in-band */
273 send_cmd_that_blocks(qts, "blocks-2");
274 qtest_qmp_send(qts, "{ 'id': 'err-2' }");
275 unblock_blocked_cmd();
276 recv_cmd_id(qts, "blocks-2");
277 recv_cmd_id(qts, "err-2");
278 cleanup_blocking_cmd();
279
280 qtest_quit(qts);
281 }
282
283 /* Preconfig tests */
284
285 static void test_qmp_preconfig(void)
286 {
287 QDict *rsp, *ret;
288 QTestState *qs = qtest_initf("%s --preconfig", common_args);
289
290 /* preconfig state */
291 /* enabled commands, no error expected */
292 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
293
294 /* forbidden commands, expected error */
295 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
296
297 /* check that query-status returns preconfig state */
298 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
299 ret = qdict_get_qdict(rsp, "return");
300 g_assert(ret);
301 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "preconfig");
302 qobject_unref(rsp);
303
304 /* exit preconfig state */
305 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
306 qtest_qmp_eventwait(qs, "RESUME");
307
308 /* check that query-status returns running state */
309 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
310 ret = qdict_get_qdict(rsp, "return");
311 g_assert(ret);
312 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
313 qobject_unref(rsp);
314
315 /* check that x-exit-preconfig returns error after exiting preconfig */
316 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
317
318 /* enabled commands, no error expected */
319 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
320
321 qtest_quit(qs);
322 }
323
324 int main(int argc, char *argv[])
325 {
326 g_test_init(&argc, &argv, NULL);
327
328 qtest_add_func("qmp/protocol", test_qmp_protocol);
329 qtest_add_func("qmp/oob", test_qmp_oob);
330 qtest_add_func("qmp/preconfig", test_qmp_preconfig);
331
332 return g_test_run();
333 }