qmp-test.c 13.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * QMP protocol test cases
 *
 * Copyright (c) 2017 Red Hat Inc.
 *
 * Authors:
 *  Markus Armbruster <armbru@redhat.com>,
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "libqtest.h"
#include "qapi/error.h"
16 17
#include "qapi/qapi-visit-introspect.h"
#include "qapi/qapi-visit-misc.h"
18
#include "qapi/qmp/qdict.h"
19
#include "qapi/qmp/qlist.h"
20
#include "qapi/qobject-input-visitor.h"
21
#include "qapi/util.h"
22
#include "qapi/visitor.h"
P
Peter Xu 已提交
23
#include "qapi/qmp/qstring.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

const char common_args[] = "-nodefaults -machine none";

static const char *get_error_class(QDict *resp)
{
    QDict *error = qdict_get_qdict(resp, "error");
    const char *desc = qdict_get_try_str(error, "desc");

    g_assert(desc);
    return error ? qdict_get_try_str(error, "class") : NULL;
}

static void test_version(QObject *version)
{
    Visitor *v;
    VersionInfo *vinfo;

    g_assert(version);
42
    v = qobject_input_visitor_new(version);
43 44 45 46 47
    visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
    qapi_free_VersionInfo(vinfo);
    visit_free(v);
}

48
static void test_malformed(QTestState *qts)
49 50 51 52
{
    QDict *resp;

    /* Not even a dictionary */
53
    resp = qtest_qmp(qts, "null");
54
    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
55
    qobject_unref(resp);
56 57

    /* No "execute" key */
58
    resp = qtest_qmp(qts, "{}");
59
    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
60
    qobject_unref(resp);
61 62

    /* "execute" isn't a string */
63
    resp = qtest_qmp(qts, "{ 'execute': true }");
64
    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
65
    qobject_unref(resp);
66 67

    /* "arguments" isn't a dictionary */
68
    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
69
    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
70
    qobject_unref(resp);
71 72

    /* extra key */
73
    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
74
    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
75
    qobject_unref(resp);
76 77 78 79 80 81
}

static void test_qmp_protocol(void)
{
    QDict *resp, *q, *ret;
    QList *capabilities;
82
    QTestState *qts;
83

84
    qts = qtest_init_without_qmp_handshake(false, common_args);
85 86

    /* Test greeting */
87
    resp = qtest_qmp_receive(qts);
88 89 90 91
    q = qdict_get_qdict(resp, "QMP");
    g_assert(q);
    test_version(qdict_get(q, "version"));
    capabilities = qdict_get_qlist(q, "capabilities");
92
    g_assert(capabilities && qlist_empty(capabilities));
93
    qobject_unref(resp);
94 95

    /* Test valid command before handshake */
96
    resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
97
    g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
98
    qobject_unref(resp);
99 100

    /* Test malformed commands before handshake */
101
    test_malformed(qts);
102 103

    /* Test handshake */
104
    resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
105 106
    ret = qdict_get_qdict(resp, "return");
    g_assert(ret && !qdict_size(ret));
107
    qobject_unref(resp);
108 109

    /* Test repeated handshake */
110
    resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
111
    g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
112
    qobject_unref(resp);
113 114

    /* Test valid command */
115
    resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
116
    test_version(qdict_get(resp, "return"));
117
    qobject_unref(resp);
118 119

    /* Test malformed commands */
120
    test_malformed(qts);
121 122

    /* Test 'id' */
123
    resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
124 125 126
    ret = qdict_get_qdict(resp, "return");
    g_assert(ret);
    g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
127
    qobject_unref(resp);
128 129

    /* Test command failure with 'id' */
130
    resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
131 132
    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
    g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
133
    qobject_unref(resp);
134

135
    qtest_quit(qts);
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/* Out-of-band tests */

char tmpdir[] = "/tmp/qmp-test-XXXXXX";
char *fifo_name;

static void setup_blocking_cmd(void)
{
    if (!mkdtemp(tmpdir)) {
        g_error("mkdtemp: %s", strerror(errno));
    }
    fifo_name = g_strdup_printf("%s/fifo", tmpdir);
    if (mkfifo(fifo_name, 0666)) {
        g_error("mkfifo: %s", strerror(errno));
    }
}

static void cleanup_blocking_cmd(void)
{
    unlink(fifo_name);
    rmdir(tmpdir);
}

static void send_cmd_that_blocks(QTestState *s, const char *id)
{
    qtest_async_qmp(s, "{ 'execute': 'blockdev-add',  'id': %s,"
                    " 'arguments': {"
                    " 'driver': 'blkdebug', 'node-name': %s,"
                    " 'config': %s,"
                    " 'image': { 'driver': 'null-co' } } }",
                    id, id, fifo_name);
}

static void unblock_blocked_cmd(void)
{
    int fd = open(fifo_name, O_WRONLY);
    g_assert(fd >= 0);
    close(fd);
}

static void send_oob_cmd_that_fails(QTestState *s, const char *id)
{
179
    qtest_async_qmp(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
180 181 182 183 184 185 186 187 188 189
}

static void recv_cmd_id(QTestState *s, const char *id)
{
    QDict *resp = qtest_qmp_receive(s);

    g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id);
    qobject_unref(resp);
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static void test_qmp_oob(void)
{
    QTestState *qts;
    QDict *resp, *q;
    const QListEntry *entry;
    QList *capabilities;
    QString *qstr;

    qts = qtest_init_without_qmp_handshake(true, common_args);

    /* Check the greeting message. */
    resp = qtest_qmp_receive(qts);
    q = qdict_get_qdict(resp, "QMP");
    g_assert(q);
    capabilities = qdict_get_qlist(q, "capabilities");
    g_assert(capabilities && !qlist_empty(capabilities));
    entry = qlist_first(capabilities);
    g_assert(entry);
    qstr = qobject_to(QString, entry->value);
    g_assert(qstr);
    g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
211
    qobject_unref(resp);
212 213 214 215 216 217

    /* Try a fake capability, it should fail. */
    resp = qtest_qmp(qts,
                     "{ 'execute': 'qmp_capabilities', "
                     "  'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
    g_assert(qdict_haskey(resp, "error"));
218
    qobject_unref(resp);
219 220 221 222 223 224

    /* Now, enable OOB in current QMP session, it should succeed. */
    resp = qtest_qmp(qts,
                     "{ 'execute': 'qmp_capabilities', "
                     "  'arguments': { 'enable': [ 'oob' ] } }");
    g_assert(qdict_haskey(resp, "return"));
225
    qobject_unref(resp);
226 227 228 229 230

    /*
     * Try any command that does not support OOB but with OOB flag. We
     * should get failure.
     */
231
    resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus' }");
232
    g_assert(qdict_haskey(resp, "error"));
233
    qobject_unref(resp);
234

235 236 237
    /* OOB command overtakes slow in-band command */
    setup_blocking_cmd();
    send_cmd_that_blocks(qts, "ib-blocks-1");
238
    qtest_async_qmp(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
239 240 241 242
    send_oob_cmd_that_fails(qts, "oob-1");
    recv_cmd_id(qts, "oob-1");
    unblock_blocked_cmd();
    recv_cmd_id(qts, "ib-blocks-1");
243
    recv_cmd_id(qts, "ib-quick-1");
244

245
    /* Even malformed in-band command fails in-band */
246 247 248 249
    send_cmd_that_blocks(qts, "blocks-2");
    qtest_async_qmp(qts, "{ 'id': 'err-2' }");
    unblock_blocked_cmd();
    recv_cmd_id(qts, "blocks-2");
250
    recv_cmd_id(qts, "err-2");
251
    cleanup_blocking_cmd();
252 253 254 255

    qtest_quit(qts);
}

256 257
/* Query smoke tests */

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static int query_error_class(const char *cmd)
{
    static struct {
        const char *cmd;
        int err_class;
    } fails[] = {
        /* Success depends on build configuration: */
#ifndef CONFIG_SPICE
        { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
#endif
#ifndef CONFIG_VNC
        { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
        { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
#endif
#ifndef CONFIG_REPLICATION
        { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
#endif
        /* Likewise, and require special QEMU command-line arguments: */
        { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
        { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
        { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
        { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
        { NULL, -1 }
    };
    int i;

    for (i = 0; fails[i].cmd; i++) {
        if (!strcmp(cmd, fails[i].cmd)) {
            return fails[i].err_class;
        }
    }
    return -1;
}

static void test_query(const void *data)
{
    const char *cmd = data;
    int expected_error_class = query_error_class(cmd);
    QDict *resp, *error;
    const char *error_class;

    qtest_start(common_args);

    resp = qmp("{ 'execute': %s }", cmd);
    error = qdict_get_qdict(resp, "error");
    error_class = error ? qdict_get_str(error, "class") : NULL;

    if (expected_error_class < 0) {
        g_assert(qdict_haskey(resp, "return"));
    } else {
        g_assert(error);
309
        g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
310
                                        -1, &error_abort),
311 312
                        ==, expected_error_class);
    }
313
    qobject_unref(resp);
314 315 316 317 318 319 320 321 322 323 324 325 326 327

    qtest_end();
}

static bool query_is_blacklisted(const char *cmd)
{
    const char *blacklist[] = {
        /* Not actually queries: */
        "add-fd",
        /* Success depends on target arch: */
        "query-cpu-definitions",  /* arm, i386, ppc, s390x */
        "query-gic-capabilities", /* arm */
        /* Success depends on target-specific build configuration: */
        "query-pci",              /* CONFIG_PCI */
328 329
        /* Success depends on launching SEV guest */
        "query-sev-launch-measure",
330 331
        /* Success depends on Host or Hypervisor SEV support */
        "query-sev",
332
        "query-sev-capabilities",
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        NULL
    };
    int i;

    for (i = 0; blacklist[i]; i++) {
        if (!strcmp(cmd, blacklist[i])) {
            return true;
        }
    }
    return false;
}

typedef struct {
    SchemaInfoList *list;
    GHashTable *hash;
} QmpSchema;

static void qmp_schema_init(QmpSchema *schema)
{
    QDict *resp;
    Visitor *qiv;
    SchemaInfoList *tail;

    qtest_start(common_args);
    resp = qmp("{ 'execute': 'query-qmp-schema' }");

    qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
    visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
    visit_free(qiv);

363
    qobject_unref(resp);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    qtest_end();

    schema->hash = g_hash_table_new(g_str_hash, g_str_equal);

    /* Build @schema: hash table mapping entity name to SchemaInfo */
    for (tail = schema->list; tail; tail = tail->next) {
        g_hash_table_insert(schema->hash, tail->value->name, tail->value);
    }
}

static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
{
    return g_hash_table_lookup(schema->hash, name);
}

static void qmp_schema_cleanup(QmpSchema *schema)
{
    qapi_free_SchemaInfoList(schema->list);
    g_hash_table_destroy(schema->hash);
}

static bool object_type_has_mandatory_members(SchemaInfo *type)
{
    SchemaInfoObjectMemberList *tail;

    g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);

    for (tail = type->u.object.members; tail; tail = tail->next) {
        if (!tail->value->has_q_default) {
            return true;
        }
    }

    return false;
}

static void add_query_tests(QmpSchema *schema)
{
    SchemaInfoList *tail;
    SchemaInfo *si, *arg_type, *ret_type;
M
Marc-André Lureau 已提交
404
    char *test_name;
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

    /* Test the query-like commands */
    for (tail = schema->list; tail; tail = tail->next) {
        si = tail->value;
        if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
            continue;
        }

        if (query_is_blacklisted(si->name)) {
            continue;
        }

        arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
        if (object_type_has_mandatory_members(arg_type)) {
            continue;
        }

        ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
        if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
            && !ret_type->u.object.members) {
            continue;
        }

        test_name = g_strdup_printf("qmp/%s", si->name);
        qtest_add_data_func(test_name, si->name, test_query);
M
Marc-André Lureau 已提交
430
        g_free(test_name);
431 432 433
    }
}

434 435
/* Preconfig tests */

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
static void test_qmp_preconfig(void)
{
    QDict *rsp, *ret;
    QTestState *qs = qtest_startf("%s --preconfig", common_args);

    /* preconfig state */
    /* enabled commands, no error expected  */
    g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));

    /* forbidden commands, expected error */
    g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));

    /* check that query-status returns preconfig state */
    rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
    ret = qdict_get_qdict(rsp, "return");
    g_assert(ret);
    g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "preconfig");
    qobject_unref(rsp);

    /* exit preconfig state */
    g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'exit-preconfig' }")));
    qtest_qmp_eventwait(qs, "RESUME");

    /* check that query-status returns running state */
    rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
    ret = qdict_get_qdict(rsp, "return");
    g_assert(ret);
    g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
    qobject_unref(rsp);

    /* check that exit-preconfig returns error after exiting preconfig */
    g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'exit-preconfig' }")));

    /* enabled commands, no error expected  */
    g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));

    qtest_quit(qs);
}

475 476
int main(int argc, char *argv[])
{
477 478 479
    QmpSchema schema;
    int ret;

480 481 482
    g_test_init(&argc, &argv, NULL);

    qtest_add_func("qmp/protocol", test_qmp_protocol);
483
    qtest_add_func("qmp/oob", test_qmp_oob);
484 485
    qmp_schema_init(&schema);
    add_query_tests(&schema);
486
    qtest_add_func("qmp/preconfig", test_qmp_preconfig);
487 488

    ret = g_test_run();
489

490 491
    qmp_schema_cleanup(&schema);
    return ret;
492
}