]> Git Repo - qemu.git/blame - tests/qmp-test.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / tests / qmp-test.c
CommitLineData
f66e7ac8
MA
1/*
2 * QMP protocol test cases
3 *
aed877c5 4 * Copyright (c) 2017-2018 Red Hat Inc.
f66e7ac8
MA
5 *
6 * Authors:
d93bb9d5 7 * Markus Armbruster <[email protected]>
f66e7ac8
MA
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"
f66e7ac8 15#include "qapi/error.h"
112ed241 16#include "qapi/qapi-visit-misc.h"
452fcdbc 17#include "qapi/qmp/qdict.h"
47e6b297 18#include "qapi/qmp/qlist.h"
f66e7ac8 19#include "qapi/qobject-input-visitor.h"
02130314 20#include "qapi/qmp/qstring.h"
f66e7ac8
MA
21
22const char common_args[] = "-nodefaults -machine none";
23
f66e7ac8
MA
24static void test_version(QObject *version)
25{
26 Visitor *v;
27 VersionInfo *vinfo;
28
29 g_assert(version);
048abb7b 30 v = qobject_input_visitor_new(version);
f66e7ac8
MA
31 visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
32 qapi_free_VersionInfo(vinfo);
33 visit_free(v);
34}
35
ebb4d82d 36static void assert_recovered(QTestState *qts)
aed877c5
MA
37{
38 QDict *resp;
aed877c5
MA
39
40 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
ebb4d82d 41 qmp_assert_error_class(resp, "CommandNotFound");
aed877c5
MA
42}
43
6a5c88f5 44static void test_malformed(QTestState *qts)
f66e7ac8
MA
45{
46 QDict *resp;
47
aed877c5
MA
48 /* syntax error */
49 qtest_qmp_send_raw(qts, "{]\n");
50 resp = qtest_qmp_receive(qts);
ebb4d82d
MAL
51 qmp_assert_error_class(resp, "GenericError");
52 assert_recovered(qts);
aed877c5
MA
53
54 /* lexical error: impossible byte outside string */
55 qtest_qmp_send_raw(qts, "{\xFF");
56 resp = qtest_qmp_receive(qts);
ebb4d82d
MAL
57 qmp_assert_error_class(resp, "GenericError");
58 assert_recovered(qts);
aed877c5 59
6bc93a34
MA
60 /* lexical error: funny control character outside string */
61 qtest_qmp_send_raw(qts, "{\x01");
62 resp = qtest_qmp_receive(qts);
ebb4d82d
MAL
63 qmp_assert_error_class(resp, "GenericError");
64 assert_recovered(qts);
6bc93a34 65
aed877c5
MA
66 /* lexical error: impossible byte in string */
67 qtest_qmp_send_raw(qts, "{'bad \xFF");
68 resp = qtest_qmp_receive(qts);
ebb4d82d
MAL
69 qmp_assert_error_class(resp, "GenericError");
70 assert_recovered(qts);
aed877c5 71
6bc93a34 72 /* lexical error: control character in string */
340db1ed 73 qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n");
6bc93a34 74 resp = qtest_qmp_receive(qts);
ebb4d82d
MAL
75 qmp_assert_error_class(resp, "GenericError");
76 assert_recovered(qts);
6bc93a34 77
aed877c5
MA
78 /* lexical error: interpolation */
79 qtest_qmp_send_raw(qts, "%%p\n");
2cbd15aa
MA
80 /* two errors, one for "%", one for "p" */
81 resp = qtest_qmp_receive(qts);
ebb4d82d 82 qmp_assert_error_class(resp, "GenericError");
aed877c5 83 resp = qtest_qmp_receive(qts);
ebb4d82d
MAL
84 qmp_assert_error_class(resp, "GenericError");
85 assert_recovered(qts);
aed877c5 86
f66e7ac8 87 /* Not even a dictionary */
6a5c88f5 88 resp = qtest_qmp(qts, "null");
ebb4d82d 89 qmp_assert_error_class(resp, "GenericError");
f66e7ac8
MA
90
91 /* No "execute" key */
6a5c88f5 92 resp = qtest_qmp(qts, "{}");
ebb4d82d 93 qmp_assert_error_class(resp, "GenericError");
f66e7ac8
MA
94
95 /* "execute" isn't a string */
6a5c88f5 96 resp = qtest_qmp(qts, "{ 'execute': true }");
ebb4d82d 97 qmp_assert_error_class(resp, "GenericError");
f66e7ac8
MA
98
99 /* "arguments" isn't a dictionary */
6a5c88f5 100 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
ebb4d82d 101 qmp_assert_error_class(resp, "GenericError");
f66e7ac8
MA
102
103 /* extra key */
6a5c88f5 104 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
ebb4d82d 105 qmp_assert_error_class(resp, "GenericError");
f66e7ac8
MA
106}
107
108static void test_qmp_protocol(void)
109{
110 QDict *resp, *q, *ret;
111 QList *capabilities;
6a5c88f5 112 QTestState *qts;
f66e7ac8 113
ddee57e0 114 qts = qtest_init_without_qmp_handshake(false, common_args);
f66e7ac8
MA
115
116 /* Test greeting */
6a5c88f5 117 resp = qtest_qmp_receive(qts);
f66e7ac8
MA
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");
a4f90923 122 g_assert(capabilities && qlist_empty(capabilities));
cb3e7f08 123 qobject_unref(resp);
f66e7ac8
MA
124
125 /* Test valid command before handshake */
6a5c88f5 126 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
ebb4d82d 127 qmp_assert_error_class(resp, "CommandNotFound");
f66e7ac8
MA
128
129 /* Test malformed commands before handshake */
6a5c88f5 130 test_malformed(qts);
f66e7ac8
MA
131
132 /* Test handshake */
6a5c88f5 133 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
f66e7ac8
MA
134 ret = qdict_get_qdict(resp, "return");
135 g_assert(ret && !qdict_size(ret));
cb3e7f08 136 qobject_unref(resp);
f66e7ac8
MA
137
138 /* Test repeated handshake */
6a5c88f5 139 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
ebb4d82d 140 qmp_assert_error_class(resp, "CommandNotFound");
f66e7ac8
MA
141
142 /* Test valid command */
6a5c88f5 143 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
f66e7ac8 144 test_version(qdict_get(resp, "return"));
cb3e7f08 145 qobject_unref(resp);
f66e7ac8
MA
146
147 /* Test malformed commands */
6a5c88f5 148 test_malformed(qts);
f66e7ac8
MA
149
150 /* Test 'id' */
6a5c88f5 151 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
f66e7ac8
MA
152 ret = qdict_get_qdict(resp, "return");
153 g_assert(ret);
154 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
cb3e7f08 155 qobject_unref(resp);
f66e7ac8
MA
156
157 /* Test command failure with 'id' */
6a5c88f5 158 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
f66e7ac8 159 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
ebb4d82d 160 qmp_assert_error_class(resp, "GenericError");
f66e7ac8 161
6a5c88f5 162 qtest_quit(qts);
f66e7ac8
MA
163}
164
97ca0712
MA
165/* Out-of-band tests */
166
167char tmpdir[] = "/tmp/qmp-test-XXXXXX";
168char *fifo_name;
169
170static 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
181static void cleanup_blocking_cmd(void)
182{
183 unlink(fifo_name);
184 rmdir(tmpdir);
185}
186
187static void send_cmd_that_blocks(QTestState *s, const char *id)
188{
4277f1eb
MA
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);
97ca0712
MA
195}
196
197static void unblock_blocked_cmd(void)
198{
199 int fd = open(fifo_name, O_WRONLY);
200 g_assert(fd >= 0);
201 close(fd);
202}
203
204static void send_oob_cmd_that_fails(QTestState *s, const char *id)
205{
4277f1eb 206 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
97ca0712
MA
207}
208
209static 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
fa198ad9
PX
217static void test_qmp_oob(void)
218{
219 QTestState *qts;
220 QDict *resp, *q;
fa198ad9
PX
221 const QListEntry *entry;
222 QList *capabilities;
223 QString *qstr;
fa198ad9
PX
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");
cb3e7f08 238 qobject_unref(resp);
fa198ad9
PX
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"));
cb3e7f08 245 qobject_unref(resp);
fa198ad9
PX
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"));
cb3e7f08 252 qobject_unref(resp);
fa198ad9
PX
253
254 /*
255 * Try any command that does not support OOB but with OOB flag. We
256 * should get failure.
257 */
00ecec15 258 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
fa198ad9 259 g_assert(qdict_haskey(resp, "error"));
cb3e7f08 260 qobject_unref(resp);
fa198ad9 261
97ca0712
MA
262 /* OOB command overtakes slow in-band command */
263 setup_blocking_cmd();
264 send_cmd_that_blocks(qts, "ib-blocks-1");
4277f1eb 265 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
97ca0712
MA
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");
2970b446 270 recv_cmd_id(qts, "ib-quick-1");
e8f4a221 271
69240fe6 272 /* Even malformed in-band command fails in-band */
e8f4a221 273 send_cmd_that_blocks(qts, "blocks-2");
4277f1eb 274 qtest_qmp_send(qts, "{ 'id': 'err-2' }");
e8f4a221
MA
275 unblock_blocked_cmd();
276 recv_cmd_id(qts, "blocks-2");
69240fe6 277 recv_cmd_id(qts, "err-2");
97ca0712 278 cleanup_blocking_cmd();
fa198ad9
PX
279
280 qtest_quit(qts);
281}
282
97ca0712
MA
283/* Preconfig tests */
284
fb1e58f7
IM
285static void test_qmp_preconfig(void)
286{
287 QDict *rsp, *ret;
88b988c8 288 QTestState *qs = qtest_initf("%s --preconfig", common_args);
fb1e58f7
IM
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 */
361ac948 305 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
fb1e58f7
IM
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
361ac948
MA
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' }")));
fb1e58f7
IM
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
f66e7ac8
MA
324int main(int argc, char *argv[])
325{
326 g_test_init(&argc, &argv, NULL);
327
328 qtest_add_func("qmp/protocol", test_qmp_protocol);
fa198ad9 329 qtest_add_func("qmp/oob", test_qmp_oob);
fb1e58f7 330 qtest_add_func("qmp/preconfig", test_qmp_preconfig);
e4a426e7 331
d93bb9d5 332 return g_test_run();
f66e7ac8 333}
This page took 0.179746 seconds and 4 git commands to generate.