hw/core/split-irq: Device that splits IRQ lines
[qemu.git] / tests / qmp-test.c
blob580848307ad1d60176b9b344df92c8e20ab11a5a
1 /*
2 * QMP protocol test cases
4 * Copyright (c) 2017 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-visit.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qlist.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "qapi/util.h"
21 #include "qapi/visitor.h"
23 const char common_args[] = "-nodefaults -machine none";
25 static const char *get_error_class(QDict *resp)
27 QDict *error = qdict_get_qdict(resp, "error");
28 const char *desc = qdict_get_try_str(error, "desc");
30 g_assert(desc);
31 return error ? qdict_get_try_str(error, "class") : NULL;
34 static void test_version(QObject *version)
36 Visitor *v;
37 VersionInfo *vinfo;
39 g_assert(version);
40 v = qobject_input_visitor_new(version);
41 visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
42 qapi_free_VersionInfo(vinfo);
43 visit_free(v);
46 static void test_malformed(QTestState *qts)
48 QDict *resp;
50 /* Not even a dictionary */
51 resp = qtest_qmp(qts, "null");
52 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
53 QDECREF(resp);
55 /* No "execute" key */
56 resp = qtest_qmp(qts, "{}");
57 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
58 QDECREF(resp);
60 /* "execute" isn't a string */
61 resp = qtest_qmp(qts, "{ 'execute': true }");
62 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
63 QDECREF(resp);
65 /* "arguments" isn't a dictionary */
66 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
67 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
68 QDECREF(resp);
70 /* extra key */
71 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
72 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
73 QDECREF(resp);
76 static void test_qmp_protocol(void)
78 QDict *resp, *q, *ret;
79 QList *capabilities;
80 QTestState *qts;
82 qts = qtest_init_without_qmp_handshake(common_args);
84 /* Test greeting */
85 resp = qtest_qmp_receive(qts);
86 q = qdict_get_qdict(resp, "QMP");
87 g_assert(q);
88 test_version(qdict_get(q, "version"));
89 capabilities = qdict_get_qlist(q, "capabilities");
90 g_assert(capabilities && qlist_empty(capabilities));
91 QDECREF(resp);
93 /* Test valid command before handshake */
94 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
95 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
96 QDECREF(resp);
98 /* Test malformed commands before handshake */
99 test_malformed(qts);
101 /* Test handshake */
102 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
103 ret = qdict_get_qdict(resp, "return");
104 g_assert(ret && !qdict_size(ret));
105 QDECREF(resp);
107 /* Test repeated handshake */
108 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
109 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
110 QDECREF(resp);
112 /* Test valid command */
113 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
114 test_version(qdict_get(resp, "return"));
115 QDECREF(resp);
117 /* Test malformed commands */
118 test_malformed(qts);
120 /* Test 'id' */
121 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
122 ret = qdict_get_qdict(resp, "return");
123 g_assert(ret);
124 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
125 QDECREF(resp);
127 /* Test command failure with 'id' */
128 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
129 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
130 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
131 QDECREF(resp);
133 qtest_quit(qts);
136 static int query_error_class(const char *cmd)
138 static struct {
139 const char *cmd;
140 int err_class;
141 } fails[] = {
142 /* Success depends on build configuration: */
143 #ifndef CONFIG_SPICE
144 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
145 #endif
146 #ifndef CONFIG_VNC
147 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
148 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
149 #endif
150 #ifndef CONFIG_REPLICATION
151 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
152 #endif
153 /* Likewise, and require special QEMU command-line arguments: */
154 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
155 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
156 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
157 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
158 { NULL, -1 }
160 int i;
162 for (i = 0; fails[i].cmd; i++) {
163 if (!strcmp(cmd, fails[i].cmd)) {
164 return fails[i].err_class;
167 return -1;
170 static void test_query(const void *data)
172 const char *cmd = data;
173 int expected_error_class = query_error_class(cmd);
174 QDict *resp, *error;
175 const char *error_class;
177 qtest_start(common_args);
179 resp = qmp("{ 'execute': %s }", cmd);
180 error = qdict_get_qdict(resp, "error");
181 error_class = error ? qdict_get_str(error, "class") : NULL;
183 if (expected_error_class < 0) {
184 g_assert(qdict_haskey(resp, "return"));
185 } else {
186 g_assert(error);
187 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
188 -1, &error_abort),
189 ==, expected_error_class);
191 QDECREF(resp);
193 qtest_end();
196 static bool query_is_blacklisted(const char *cmd)
198 const char *blacklist[] = {
199 /* Not actually queries: */
200 "add-fd",
201 /* Success depends on target arch: */
202 "query-cpu-definitions", /* arm, i386, ppc, s390x */
203 "query-gic-capabilities", /* arm */
204 /* Success depends on target-specific build configuration: */
205 "query-pci", /* CONFIG_PCI */
206 NULL
208 int i;
210 for (i = 0; blacklist[i]; i++) {
211 if (!strcmp(cmd, blacklist[i])) {
212 return true;
215 return false;
218 typedef struct {
219 SchemaInfoList *list;
220 GHashTable *hash;
221 } QmpSchema;
223 static void qmp_schema_init(QmpSchema *schema)
225 QDict *resp;
226 Visitor *qiv;
227 SchemaInfoList *tail;
229 qtest_start(common_args);
230 resp = qmp("{ 'execute': 'query-qmp-schema' }");
232 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
233 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
234 visit_free(qiv);
236 QDECREF(resp);
237 qtest_end();
239 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
241 /* Build @schema: hash table mapping entity name to SchemaInfo */
242 for (tail = schema->list; tail; tail = tail->next) {
243 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
247 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
249 return g_hash_table_lookup(schema->hash, name);
252 static void qmp_schema_cleanup(QmpSchema *schema)
254 qapi_free_SchemaInfoList(schema->list);
255 g_hash_table_destroy(schema->hash);
258 static bool object_type_has_mandatory_members(SchemaInfo *type)
260 SchemaInfoObjectMemberList *tail;
262 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
264 for (tail = type->u.object.members; tail; tail = tail->next) {
265 if (!tail->value->has_q_default) {
266 return true;
270 return false;
273 static void add_query_tests(QmpSchema *schema)
275 SchemaInfoList *tail;
276 SchemaInfo *si, *arg_type, *ret_type;
277 char *test_name;
279 /* Test the query-like commands */
280 for (tail = schema->list; tail; tail = tail->next) {
281 si = tail->value;
282 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
283 continue;
286 if (query_is_blacklisted(si->name)) {
287 continue;
290 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
291 if (object_type_has_mandatory_members(arg_type)) {
292 continue;
295 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
296 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
297 && !ret_type->u.object.members) {
298 continue;
301 test_name = g_strdup_printf("qmp/%s", si->name);
302 qtest_add_data_func(test_name, si->name, test_query);
303 g_free(test_name);
307 int main(int argc, char *argv[])
309 QmpSchema schema;
310 int ret;
312 g_test_init(&argc, &argv, NULL);
314 qtest_add_func("qmp/protocol", test_qmp_protocol);
315 qmp_schema_init(&schema);
316 add_query_tests(&schema);
318 ret = g_test_run();
320 qmp_schema_cleanup(&schema);
321 return ret;