2 * QMP protocol test cases
4 * Copyright (c) 2017 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-visit.h"
16 #include "qapi/error.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/visitor.h"
20 const char common_args
[] = "-nodefaults -machine none";
22 static const char *get_error_class(QDict
*resp
)
24 QDict
*error
= qdict_get_qdict(resp
, "error");
25 const char *desc
= qdict_get_try_str(error
, "desc");
28 return error
? qdict_get_try_str(error
, "class") : NULL
;
31 static void test_version(QObject
*version
)
37 v
= qobject_input_visitor_new(version
);
38 visit_type_VersionInfo(v
, "version", &vinfo
, &error_abort
);
39 qapi_free_VersionInfo(vinfo
);
43 static void test_malformed(void)
47 /* Not even a dictionary */
49 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
52 /* No "execute" key */
54 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
57 /* "execute" isn't a string */
58 resp
= qmp("{ 'execute': true }");
59 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
62 /* "arguments" isn't a dictionary */
63 resp
= qmp("{ 'execute': 'no-such-cmd', 'arguments': [] }");
64 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
68 resp
= qmp("{ 'execute': 'no-such-cmd', 'extra': true }");
69 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
73 static void test_qmp_protocol(void)
75 QDict
*resp
, *q
, *ret
;
78 global_qtest
= qtest_init_without_qmp_handshake(common_args
);
82 q
= qdict_get_qdict(resp
, "QMP");
84 test_version(qdict_get(q
, "version"));
85 capabilities
= qdict_get_qlist(q
, "capabilities");
86 g_assert(capabilities
&& qlist_empty(capabilities
));
89 /* Test valid command before handshake */
90 resp
= qmp("{ 'execute': 'query-version' }");
91 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
94 /* Test malformed commands before handshake */
98 resp
= qmp("{ 'execute': 'qmp_capabilities' }");
99 ret
= qdict_get_qdict(resp
, "return");
100 g_assert(ret
&& !qdict_size(ret
));
103 /* Test repeated handshake */
104 resp
= qmp("{ 'execute': 'qmp_capabilities' }");
105 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
108 /* Test valid command */
109 resp
= qmp("{ 'execute': 'query-version' }");
110 test_version(qdict_get(resp
, "return"));
113 /* Test malformed commands */
117 resp
= qmp("{ 'execute': 'query-name', 'id': 'cookie#1' }");
118 ret
= qdict_get_qdict(resp
, "return");
120 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, "cookie#1");
123 /* Test command failure with 'id' */
124 resp
= qmp("{ 'execute': 'human-monitor-command', 'id': 2 }");
125 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
126 g_assert_cmpint(qdict_get_int(resp
, "id"), ==, 2);
132 int main(int argc
, char *argv
[])
134 g_test_init(&argc
, &argv
, NULL
);
136 qtest_add_func("qmp/protocol", test_qmp_protocol
);