1 /* 2 * QMP command test cases 3 * 4 * Copyright (c) 2017 Red Hat Inc. 5 * 6 * Authors: 7 * Markus Armbruster <armbru@redhat.com> 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 "libqos/libqtest.h" 15 #include "qapi/error.h" 16 #include "qapi/qapi-visit-introspect.h" 17 #include "qapi/qmp/qdict.h" 18 #include "qapi/qobject-input-visitor.h" 19 20 const char common_args[] = "-nodefaults -machine none"; 21 22 /* Query smoke tests */ 23 24 static int query_error_class(const char *cmd) 25 { 26 static struct { 27 const char *cmd; 28 int err_class; 29 } fails[] = { 30 /* Success depends on build configuration: */ 31 #ifndef CONFIG_SPICE 32 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND }, 33 #endif 34 #ifndef CONFIG_TCG 35 { "query-replay", ERROR_CLASS_COMMAND_NOT_FOUND }, 36 #endif 37 #ifndef CONFIG_VNC 38 { "query-vnc", ERROR_CLASS_GENERIC_ERROR }, 39 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR }, 40 #endif 41 #ifndef CONFIG_REPLICATION 42 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND }, 43 #endif 44 /* Likewise, and require special QEMU command-line arguments: */ 45 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR }, 46 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE }, 47 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR }, 48 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR }, 49 #ifndef CONFIG_PROFILER 50 { "x-query-profile", ERROR_CLASS_GENERIC_ERROR }, 51 #endif 52 { NULL, -1 } 53 }; 54 int i; 55 56 for (i = 0; fails[i].cmd; i++) { 57 if (!strcmp(cmd, fails[i].cmd)) { 58 return fails[i].err_class; 59 } 60 } 61 return -1; 62 } 63 64 static void test_query(const void *data) 65 { 66 const char *cmd = data; 67 int expected_error_class = query_error_class(cmd); 68 QDict *resp, *error; 69 const char *error_class; 70 QTestState *qts; 71 72 qts = qtest_init(common_args); 73 74 resp = qtest_qmp(qts, "{ 'execute': %s }", cmd); 75 error = qdict_get_qdict(resp, "error"); 76 error_class = error ? qdict_get_str(error, "class") : NULL; 77 78 if (expected_error_class < 0) { 79 g_assert(qdict_haskey(resp, "return")); 80 } else { 81 g_assert(error); 82 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class, 83 -1, &error_abort), 84 ==, expected_error_class); 85 } 86 qobject_unref(resp); 87 88 qtest_quit(qts); 89 } 90 91 static bool query_is_ignored(const char *cmd) 92 { 93 const char *ignored[] = { 94 /* Not actually queries: */ 95 "add-fd", 96 /* Success depends on target arch: */ 97 "query-cpu-definitions", /* arm, i386, ppc, s390x */ 98 "query-gic-capabilities", /* arm */ 99 /* Success depends on target-specific build configuration: */ 100 "query-pci", /* CONFIG_PCI */ 101 /* Success depends on launching SEV guest */ 102 "query-sev-launch-measure", 103 /* Success depends on Host or Hypervisor SEV support */ 104 "query-sev", 105 "query-sev-capabilities", 106 "query-sgx", 107 "query-sgx-capabilities", 108 NULL 109 }; 110 int i; 111 112 for (i = 0; ignored[i]; i++) { 113 if (!strcmp(cmd, ignored[i])) { 114 return true; 115 } 116 } 117 return false; 118 } 119 120 typedef struct { 121 SchemaInfoList *list; 122 GHashTable *hash; 123 } QmpSchema; 124 125 static void qmp_schema_init(QmpSchema *schema) 126 { 127 QDict *resp; 128 Visitor *qiv; 129 SchemaInfoList *tail; 130 QTestState *qts; 131 132 qts = qtest_init(common_args); 133 134 resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }"); 135 136 qiv = qobject_input_visitor_new(qdict_get(resp, "return")); 137 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort); 138 visit_free(qiv); 139 140 qobject_unref(resp); 141 qtest_quit(qts); 142 143 schema->hash = g_hash_table_new(g_str_hash, g_str_equal); 144 145 /* Build @schema: hash table mapping entity name to SchemaInfo */ 146 for (tail = schema->list; tail; tail = tail->next) { 147 g_hash_table_insert(schema->hash, tail->value->name, tail->value); 148 } 149 } 150 151 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name) 152 { 153 return g_hash_table_lookup(schema->hash, name); 154 } 155 156 static void qmp_schema_cleanup(QmpSchema *schema) 157 { 158 qapi_free_SchemaInfoList(schema->list); 159 g_hash_table_destroy(schema->hash); 160 } 161 162 static bool object_type_has_mandatory_members(SchemaInfo *type) 163 { 164 SchemaInfoObjectMemberList *tail; 165 166 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT); 167 168 for (tail = type->u.object.members; tail; tail = tail->next) { 169 if (!tail->value->has_q_default) { 170 return true; 171 } 172 } 173 174 return false; 175 } 176 177 static void add_query_tests(QmpSchema *schema) 178 { 179 SchemaInfoList *tail; 180 SchemaInfo *si, *arg_type, *ret_type; 181 char *test_name; 182 183 /* Test the query-like commands */ 184 for (tail = schema->list; tail; tail = tail->next) { 185 si = tail->value; 186 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) { 187 continue; 188 } 189 190 if (query_is_ignored(si->name)) { 191 continue; 192 } 193 194 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type); 195 if (object_type_has_mandatory_members(arg_type)) { 196 continue; 197 } 198 199 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type); 200 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT 201 && !ret_type->u.object.members) { 202 continue; 203 } 204 205 test_name = g_strdup_printf("qmp/%s", si->name); 206 qtest_add_data_func(test_name, si->name, test_query); 207 g_free(test_name); 208 } 209 } 210 211 static void test_object_add_failure_modes(void) 212 { 213 QTestState *qts; 214 QDict *resp; 215 216 /* attempt to create an object without props */ 217 qts = qtest_init(common_args); 218 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 219 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }"); 220 g_assert_nonnull(resp); 221 qmp_expect_error_and_unref(resp, "GenericError"); 222 223 /* attempt to create an object without qom-type */ 224 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 225 " {'id': 'ram1' } }"); 226 g_assert_nonnull(resp); 227 qmp_expect_error_and_unref(resp, "GenericError"); 228 229 /* attempt to delete an object that does not exist */ 230 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 231 " {'id': 'ram1' } }"); 232 g_assert_nonnull(resp); 233 qmp_expect_error_and_unref(resp, "GenericError"); 234 235 /* attempt to create 2 objects with duplicate id */ 236 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 237 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 238 " 'size': 1048576 } }"); 239 g_assert_nonnull(resp); 240 g_assert(qdict_haskey(resp, "return")); 241 qobject_unref(resp); 242 243 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 244 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 245 " 'size': 1048576 } }"); 246 g_assert_nonnull(resp); 247 qmp_expect_error_and_unref(resp, "GenericError"); 248 249 /* delete ram1 object */ 250 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 251 " {'id': 'ram1' } }"); 252 g_assert_nonnull(resp); 253 g_assert(qdict_haskey(resp, "return")); 254 qobject_unref(resp); 255 256 /* attempt to create an object with a property of a wrong type */ 257 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 258 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 259 " 'size': '1048576' } }"); 260 g_assert_nonnull(resp); 261 /* now do it right */ 262 qmp_expect_error_and_unref(resp, "GenericError"); 263 264 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 265 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 266 " 'size': 1048576 } }"); 267 g_assert_nonnull(resp); 268 g_assert(qdict_haskey(resp, "return")); 269 qobject_unref(resp); 270 271 /* delete ram1 object */ 272 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 273 " {'id': 'ram1' } }"); 274 g_assert_nonnull(resp); 275 g_assert(qdict_haskey(resp, "return")); 276 qobject_unref(resp); 277 278 /* attempt to create an object without the id */ 279 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 280 " {'qom-type': 'memory-backend-ram'," 281 " 'size': 1048576 } }"); 282 g_assert_nonnull(resp); 283 qmp_expect_error_and_unref(resp, "GenericError"); 284 285 /* now do it right */ 286 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 287 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 288 " 'size': 1048576 } }"); 289 g_assert_nonnull(resp); 290 g_assert(qdict_haskey(resp, "return")); 291 qobject_unref(resp); 292 293 /* delete ram1 object */ 294 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 295 " {'id': 'ram1' } }"); 296 g_assert_nonnull(resp); 297 g_assert(qdict_haskey(resp, "return")); 298 qobject_unref(resp); 299 300 /* attempt to set a non existing property */ 301 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 302 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 303 " 'sized': 1048576 } }"); 304 g_assert_nonnull(resp); 305 qmp_expect_error_and_unref(resp, "GenericError"); 306 307 /* now do it right */ 308 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':" 309 " {'qom-type': 'memory-backend-ram', 'id': 'ram1'," 310 " 'size': 1048576 } }"); 311 g_assert_nonnull(resp); 312 g_assert(qdict_haskey(resp, "return")); 313 qobject_unref(resp); 314 315 /* delete ram1 object without id */ 316 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 317 " {'ida': 'ram1' } }"); 318 g_assert_nonnull(resp); 319 qobject_unref(resp); 320 321 /* delete ram1 object */ 322 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 323 " {'id': 'ram1' } }"); 324 g_assert_nonnull(resp); 325 g_assert(qdict_haskey(resp, "return")); 326 qobject_unref(resp); 327 328 /* delete ram1 object that does not exist anymore*/ 329 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':" 330 " {'id': 'ram1' } }"); 331 g_assert_nonnull(resp); 332 qmp_expect_error_and_unref(resp, "GenericError"); 333 334 qtest_quit(qts); 335 } 336 337 int main(int argc, char *argv[]) 338 { 339 QmpSchema schema; 340 int ret; 341 342 g_test_init(&argc, &argv, NULL); 343 344 qmp_schema_init(&schema); 345 add_query_tests(&schema); 346 347 qtest_add_func("qmp/object-add-failure-modes", 348 test_object_add_failure_modes); 349 350 ret = g_test_run(); 351 352 qmp_schema_cleanup(&schema); 353 return ret; 354 } 355