spapr: increase the size of the IRQ number space
[qemu.git] / tests / qmp-test.c
blobb3472281ae2c03bdf2732ec55668ae24b45c3982
1 /*
2 * QMP protocol test cases
4 * Copyright (c) 2017-2018 Red Hat Inc.
6 * Authors:
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"
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"
22 const char common_args[] = "-nodefaults -machine none";
24 static void test_version(QObject *version)
26 Visitor *v;
27 VersionInfo *vinfo;
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);
36 static void assert_recovered(QTestState *qts)
38 QDict *resp;
40 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
41 qmp_assert_error_class(resp, "CommandNotFound");
44 static void test_malformed(QTestState *qts)
46 QDict *resp;
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);
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\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);
87 /* Not even a dictionary */
88 resp = qtest_qmp(qts, "null");
89 qmp_assert_error_class(resp, "GenericError");
91 /* No "execute" key */
92 resp = qtest_qmp(qts, "{}");
93 qmp_assert_error_class(resp, "GenericError");
95 /* "execute" isn't a string */
96 resp = qtest_qmp(qts, "{ 'execute': true }");
97 qmp_assert_error_class(resp, "GenericError");
99 /* "arguments" isn't a dictionary */
100 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
101 qmp_assert_error_class(resp, "GenericError");
103 /* extra key */
104 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
105 qmp_assert_error_class(resp, "GenericError");
108 static void test_qmp_protocol(void)
110 QDict *resp, *q, *ret;
111 QList *capabilities;
112 QTestState *qts;
114 qts = qtest_init_without_qmp_handshake(false, common_args);
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);
125 /* Test valid command before handshake */
126 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
127 qmp_assert_error_class(resp, "CommandNotFound");
129 /* Test malformed commands before handshake */
130 test_malformed(qts);
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);
138 /* Test repeated handshake */
139 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
140 qmp_assert_error_class(resp, "CommandNotFound");
142 /* Test valid command */
143 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
144 test_version(qdict_get(resp, "return"));
145 qobject_unref(resp);
147 /* Test malformed commands */
148 test_malformed(qts);
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);
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");
162 qtest_quit(qts);
165 /* Out-of-band tests */
167 char tmpdir[] = "/tmp/qmp-test-XXXXXX";
168 char *fifo_name;
170 static void setup_blocking_cmd(void)
172 if (!mkdtemp(tmpdir)) {
173 g_error("mkdtemp: %s", strerror(errno));
175 fifo_name = g_strdup_printf("%s/fifo", tmpdir);
176 if (mkfifo(fifo_name, 0666)) {
177 g_error("mkfifo: %s", strerror(errno));
181 static void cleanup_blocking_cmd(void)
183 unlink(fifo_name);
184 rmdir(tmpdir);
187 static void send_cmd_that_blocks(QTestState *s, const char *id)
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);
197 static void unblock_blocked_cmd(void)
199 int fd = open(fifo_name, O_WRONLY);
200 g_assert(fd >= 0);
201 close(fd);
204 static void send_oob_cmd_that_fails(QTestState *s, const char *id)
206 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
209 static void recv_cmd_id(QTestState *s, const char *id)
211 QDict *resp = qtest_qmp_receive(s);
213 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
214 qobject_unref(resp);
217 static void test_qmp_oob(void)
219 QTestState *qts;
220 QDict *resp, *q;
221 const QListEntry *entry;
222 QList *capabilities;
223 QString *qstr;
225 qts = qtest_init_without_qmp_handshake(true, common_args);
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);
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);
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);
255 * Try any command that does not support OOB but with OOB flag. We
256 * should get failure.
258 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
259 g_assert(qdict_haskey(resp, "error"));
260 qobject_unref(resp);
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");
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();
280 qtest_quit(qts);
283 /* Preconfig tests */
285 static void test_qmp_preconfig(void)
287 QDict *rsp, *ret;
288 QTestState *qs = qtest_initf("%s --preconfig", common_args);
290 /* preconfig state */
291 /* enabled commands, no error expected */
292 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
294 /* forbidden commands, expected error */
295 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
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);
304 /* exit preconfig state */
305 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
306 qtest_qmp_eventwait(qs, "RESUME");
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);
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' }")));
318 /* enabled commands, no error expected */
319 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
321 qtest_quit(qs);
324 int main(int argc, char *argv[])
326 g_test_init(&argc, &argv, NULL);
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);
332 return g_test_run();