]> Git Repo - qemu.git/blame - tests/qmp-test.c
json: Fix lexer to include the bad character in JSON_ERROR token
[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
24static const char *get_error_class(QDict *resp)
25{
26 QDict *error = qdict_get_qdict(resp, "error");
27 const char *desc = qdict_get_try_str(error, "desc");
28
29 g_assert(desc);
30 return error ? qdict_get_try_str(error, "class") : NULL;
31}
32
33static void test_version(QObject *version)
34{
35 Visitor *v;
36 VersionInfo *vinfo;
37
38 g_assert(version);
048abb7b 39 v = qobject_input_visitor_new(version);
f66e7ac8
MA
40 visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
41 qapi_free_VersionInfo(vinfo);
42 visit_free(v);
43}
44
aed877c5
MA
45static bool recovered(QTestState *qts)
46{
47 QDict *resp;
48 bool ret;
49
50 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
51 ret = !strcmp(get_error_class(resp), "CommandNotFound");
52 qobject_unref(resp);
53 return ret;
54}
55
6a5c88f5 56static void test_malformed(QTestState *qts)
f66e7ac8
MA
57{
58 QDict *resp;
59
aed877c5
MA
60 /* syntax error */
61 qtest_qmp_send_raw(qts, "{]\n");
62 resp = qtest_qmp_receive(qts);
63 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
64 qobject_unref(resp);
65 g_assert(recovered(qts));
66
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");
71 qobject_unref(resp);
72 g_assert(recovered(qts));
73
6bc93a34
MA
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");
78 qobject_unref(resp);
79 g_assert(recovered(qts));
80
aed877c5
MA
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");
85 qobject_unref(resp);
86 g_assert(recovered(qts));
87
6bc93a34
MA
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 */
92 qobject_unref(resp);
93 g_assert(recovered(qts));
94
aed877c5
MA
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");
99 qobject_unref(resp);
100 g_assert(recovered(qts));
101
f66e7ac8 102 /* Not even a dictionary */
6a5c88f5 103 resp = qtest_qmp(qts, "null");
f66e7ac8 104 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
cb3e7f08 105 qobject_unref(resp);
f66e7ac8
MA
106
107 /* No "execute" key */
6a5c88f5 108 resp = qtest_qmp(qts, "{}");
f66e7ac8 109 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
cb3e7f08 110 qobject_unref(resp);
f66e7ac8
MA
111
112 /* "execute" isn't a string */
6a5c88f5 113 resp = qtest_qmp(qts, "{ 'execute': true }");
f66e7ac8 114 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
cb3e7f08 115 qobject_unref(resp);
f66e7ac8
MA
116
117 /* "arguments" isn't a dictionary */
6a5c88f5 118 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
f66e7ac8 119 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
cb3e7f08 120 qobject_unref(resp);
f66e7ac8
MA
121
122 /* extra key */
6a5c88f5 123 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
f66e7ac8 124 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
cb3e7f08 125 qobject_unref(resp);
f66e7ac8
MA
126}
127
128static void test_qmp_protocol(void)
129{
130 QDict *resp, *q, *ret;
131 QList *capabilities;
6a5c88f5 132 QTestState *qts;
f66e7ac8 133
ddee57e0 134 qts = qtest_init_without_qmp_handshake(false, common_args);
f66e7ac8
MA
135
136 /* Test greeting */
6a5c88f5 137 resp = qtest_qmp_receive(qts);
f66e7ac8
MA
138 q = qdict_get_qdict(resp, "QMP");
139 g_assert(q);
140 test_version(qdict_get(q, "version"));
141 capabilities = qdict_get_qlist(q, "capabilities");
a4f90923 142 g_assert(capabilities && qlist_empty(capabilities));
cb3e7f08 143 qobject_unref(resp);
f66e7ac8
MA
144
145 /* Test valid command before handshake */
6a5c88f5 146 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
f66e7ac8 147 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
cb3e7f08 148 qobject_unref(resp);
f66e7ac8
MA
149
150 /* Test malformed commands before handshake */
6a5c88f5 151 test_malformed(qts);
f66e7ac8
MA
152
153 /* Test handshake */
6a5c88f5 154 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
f66e7ac8
MA
155 ret = qdict_get_qdict(resp, "return");
156 g_assert(ret && !qdict_size(ret));
cb3e7f08 157 qobject_unref(resp);
f66e7ac8
MA
158
159 /* Test repeated handshake */
6a5c88f5 160 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
f66e7ac8 161 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
cb3e7f08 162 qobject_unref(resp);
f66e7ac8
MA
163
164 /* Test valid command */
6a5c88f5 165 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
f66e7ac8 166 test_version(qdict_get(resp, "return"));
cb3e7f08 167 qobject_unref(resp);
f66e7ac8
MA
168
169 /* Test malformed commands */
6a5c88f5 170 test_malformed(qts);
f66e7ac8
MA
171
172 /* Test 'id' */
6a5c88f5 173 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
f66e7ac8
MA
174 ret = qdict_get_qdict(resp, "return");
175 g_assert(ret);
176 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
cb3e7f08 177 qobject_unref(resp);
f66e7ac8
MA
178
179 /* Test command failure with 'id' */
6a5c88f5 180 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
f66e7ac8
MA
181 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
182 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
cb3e7f08 183 qobject_unref(resp);
f66e7ac8 184
6a5c88f5 185 qtest_quit(qts);
f66e7ac8
MA
186}
187
97ca0712
MA
188/* Out-of-band tests */
189
190char tmpdir[] = "/tmp/qmp-test-XXXXXX";
191char *fifo_name;
192
193static void setup_blocking_cmd(void)
194{
195 if (!mkdtemp(tmpdir)) {
196 g_error("mkdtemp: %s", strerror(errno));
197 }
198 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
199 if (mkfifo(fifo_name, 0666)) {
200 g_error("mkfifo: %s", strerror(errno));
201 }
202}
203
204static void cleanup_blocking_cmd(void)
205{
206 unlink(fifo_name);
207 rmdir(tmpdir);
208}
209
210static void send_cmd_that_blocks(QTestState *s, const char *id)
211{
4277f1eb
MA
212 qtest_qmp_send(s, "{ 'execute': 'blockdev-add', 'id': %s,"
213 " 'arguments': {"
214 " 'driver': 'blkdebug', 'node-name': %s,"
215 " 'config': %s,"
216 " 'image': { 'driver': 'null-co' } } }",
217 id, id, fifo_name);
97ca0712
MA
218}
219
220static void unblock_blocked_cmd(void)
221{
222 int fd = open(fifo_name, O_WRONLY);
223 g_assert(fd >= 0);
224 close(fd);
225}
226
227static void send_oob_cmd_that_fails(QTestState *s, const char *id)
228{
4277f1eb 229 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
97ca0712
MA
230}
231
232static void recv_cmd_id(QTestState *s, const char *id)
233{
234 QDict *resp = qtest_qmp_receive(s);
235
236 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
237 qobject_unref(resp);
238}
239
fa198ad9
PX
240static void test_qmp_oob(void)
241{
242 QTestState *qts;
243 QDict *resp, *q;
fa198ad9
PX
244 const QListEntry *entry;
245 QList *capabilities;
246 QString *qstr;
fa198ad9
PX
247
248 qts = qtest_init_without_qmp_handshake(true, common_args);
249
250 /* Check the greeting message. */
251 resp = qtest_qmp_receive(qts);
252 q = qdict_get_qdict(resp, "QMP");
253 g_assert(q);
254 capabilities = qdict_get_qlist(q, "capabilities");
255 g_assert(capabilities && !qlist_empty(capabilities));
256 entry = qlist_first(capabilities);
257 g_assert(entry);
258 qstr = qobject_to(QString, entry->value);
259 g_assert(qstr);
260 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
cb3e7f08 261 qobject_unref(resp);
fa198ad9
PX
262
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"));
cb3e7f08 268 qobject_unref(resp);
fa198ad9
PX
269
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"));
cb3e7f08 275 qobject_unref(resp);
fa198ad9
PX
276
277 /*
278 * Try any command that does not support OOB but with OOB flag. We
279 * should get failure.
280 */
00ecec15 281 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
fa198ad9 282 g_assert(qdict_haskey(resp, "error"));
cb3e7f08 283 qobject_unref(resp);
fa198ad9 284
97ca0712
MA
285 /* OOB command overtakes slow in-band command */
286 setup_blocking_cmd();
287 send_cmd_that_blocks(qts, "ib-blocks-1");
4277f1eb 288 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
97ca0712
MA
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");
2970b446 293 recv_cmd_id(qts, "ib-quick-1");
e8f4a221 294
69240fe6 295 /* Even malformed in-band command fails in-band */
e8f4a221 296 send_cmd_that_blocks(qts, "blocks-2");
4277f1eb 297 qtest_qmp_send(qts, "{ 'id': 'err-2' }");
e8f4a221
MA
298 unblock_blocked_cmd();
299 recv_cmd_id(qts, "blocks-2");
69240fe6 300 recv_cmd_id(qts, "err-2");
97ca0712 301 cleanup_blocking_cmd();
fa198ad9
PX
302
303 qtest_quit(qts);
304}
305
97ca0712
MA
306/* Preconfig tests */
307
fb1e58f7
IM
308static void test_qmp_preconfig(void)
309{
310 QDict *rsp, *ret;
88b988c8 311 QTestState *qs = qtest_initf("%s --preconfig", common_args);
fb1e58f7
IM
312
313 /* preconfig state */
314 /* enabled commands, no error expected */
315 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
316
317 /* forbidden commands, expected error */
318 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
319
320 /* check that query-status returns preconfig state */
321 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
322 ret = qdict_get_qdict(rsp, "return");
323 g_assert(ret);
324 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "preconfig");
325 qobject_unref(rsp);
326
327 /* exit preconfig state */
361ac948 328 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
fb1e58f7
IM
329 qtest_qmp_eventwait(qs, "RESUME");
330
331 /* check that query-status returns running state */
332 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
333 ret = qdict_get_qdict(rsp, "return");
334 g_assert(ret);
335 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
336 qobject_unref(rsp);
337
361ac948
MA
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' }")));
fb1e58f7
IM
340
341 /* enabled commands, no error expected */
342 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
343
344 qtest_quit(qs);
345}
346
f66e7ac8
MA
347int main(int argc, char *argv[])
348{
349 g_test_init(&argc, &argv, NULL);
350
351 qtest_add_func("qmp/protocol", test_qmp_protocol);
fa198ad9 352 qtest_add_func("qmp/oob", test_qmp_oob);
fb1e58f7 353 qtest_add_func("qmp/preconfig", test_qmp_preconfig);
e4a426e7 354
d93bb9d5 355 return g_test_run();
f66e7ac8 356}
This page took 0.142924 seconds and 4 git commands to generate.