qmp-test.c 9.7 KB
Newer Older
1 2 3
/*
 * QMP protocol test cases
 *
4
 * Copyright (c) 2017-2018 Red Hat Inc.
5 6
 *
 * Authors:
7
 *  Markus Armbruster <armbru@redhat.com>
8 9 10 11 12 13 14 15
 *
 * 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
#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"
P
Peter Xu 已提交
20
#include "qapi/qmp/qstring.h"
21 22 23 24 25 26 27 28 29

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

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

    g_assert(version);
30
    v = qobject_input_visitor_new(version);
31 32 33 34 35
    visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
    qapi_free_VersionInfo(vinfo);
    visit_free(v);
}

36
static void assert_recovered(QTestState *qts)
37 38 39 40
{
    QDict *resp;

    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
41
    qmp_assert_error_class(resp, "CommandNotFound");
42 43
}

44
static void test_malformed(QTestState *qts)
45 46 47
{
    QDict *resp;

48 49 50
    /* syntax error */
    qtest_qmp_send_raw(qts, "{]\n");
    resp = qtest_qmp_receive(qts);
51 52
    qmp_assert_error_class(resp, "GenericError");
    assert_recovered(qts);
53 54 55 56

    /* lexical error: impossible byte outside string */
    qtest_qmp_send_raw(qts, "{\xFF");
    resp = qtest_qmp_receive(qts);
57 58
    qmp_assert_error_class(resp, "GenericError");
    assert_recovered(qts);
59

60 61 62
    /* lexical error: funny control character outside string */
    qtest_qmp_send_raw(qts, "{\x01");
    resp = qtest_qmp_receive(qts);
63 64
    qmp_assert_error_class(resp, "GenericError");
    assert_recovered(qts);
65

66 67 68
    /* lexical error: impossible byte in string */
    qtest_qmp_send_raw(qts, "{'bad \xFF");
    resp = qtest_qmp_receive(qts);
69 70
    qmp_assert_error_class(resp, "GenericError");
    assert_recovered(qts);
71

72
    /* lexical error: control character in string */
73
    qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n");
74
    resp = qtest_qmp_receive(qts);
75 76
    qmp_assert_error_class(resp, "GenericError");
    assert_recovered(qts);
77

78 79
    /* lexical error: interpolation */
    qtest_qmp_send_raw(qts, "%%p\n");
80 81
    /* two errors, one for "%", one for "p" */
    resp = qtest_qmp_receive(qts);
82
    qmp_assert_error_class(resp, "GenericError");
83
    resp = qtest_qmp_receive(qts);
84 85
    qmp_assert_error_class(resp, "GenericError");
    assert_recovered(qts);
86

87
    /* Not even a dictionary */
88
    resp = qtest_qmp(qts, "null");
89
    qmp_assert_error_class(resp, "GenericError");
90 91

    /* No "execute" key */
92
    resp = qtest_qmp(qts, "{}");
93
    qmp_assert_error_class(resp, "GenericError");
94 95

    /* "execute" isn't a string */
96
    resp = qtest_qmp(qts, "{ 'execute': true }");
97
    qmp_assert_error_class(resp, "GenericError");
98 99

    /* "arguments" isn't a dictionary */
100
    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
101
    qmp_assert_error_class(resp, "GenericError");
102 103

    /* extra key */
104
    resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
105
    qmp_assert_error_class(resp, "GenericError");
106 107 108 109 110 111
}

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

114
    qts = qtest_init_without_qmp_handshake(false, common_args);
115 116

    /* Test greeting */
117
    resp = qtest_qmp_receive(qts);
118 119 120 121
    q = qdict_get_qdict(resp, "QMP");
    g_assert(q);
    test_version(qdict_get(q, "version"));
    capabilities = qdict_get_qlist(q, "capabilities");
122
    g_assert(capabilities && qlist_empty(capabilities));
123
    qobject_unref(resp);
124 125

    /* Test valid command before handshake */
126
    resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
127
    qmp_assert_error_class(resp, "CommandNotFound");
128 129

    /* Test malformed commands before handshake */
130
    test_malformed(qts);
131 132

    /* Test handshake */
133
    resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
134 135
    ret = qdict_get_qdict(resp, "return");
    g_assert(ret && !qdict_size(ret));
136
    qobject_unref(resp);
137 138

    /* Test repeated handshake */
139
    resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
140
    qmp_assert_error_class(resp, "CommandNotFound");
141 142

    /* Test valid command */
143
    resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
144
    test_version(qdict_get(resp, "return"));
145
    qobject_unref(resp);
146 147

    /* Test malformed commands */
148
    test_malformed(qts);
149 150

    /* Test 'id' */
151
    resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
152 153 154
    ret = qdict_get_qdict(resp, "return");
    g_assert(ret);
    g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
155
    qobject_unref(resp);
156 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");
161

162
    qtest_quit(qts);
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
/* 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)
{
189 190 191 192 193 194
    qtest_qmp_send(s, "{ 'execute': 'blockdev-add',  'id': %s,"
                   " 'arguments': {"
                   " 'driver': 'blkdebug', 'node-name': %s,"
                   " 'config': %s,"
                   " 'image': { 'driver': 'null-co' } } }",
                   id, id, fifo_name);
195 196 197 198 199 200 201 202 203 204 205
}

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)
{
206
    qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
207 208 209 210 211 212 213 214 215 216
}

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);
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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");
238
    qobject_unref(resp);
239 240 241 242 243 244

    /* 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"));
245
    qobject_unref(resp);
246 247 248 249 250 251

    /* 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"));
252
    qobject_unref(resp);
253 254 255 256 257

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

262 263 264
    /* OOB command overtakes slow in-band command */
    setup_blocking_cmd();
    send_cmd_that_blocks(qts, "ib-blocks-1");
265
    qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
266 267 268 269
    send_oob_cmd_that_fails(qts, "oob-1");
    recv_cmd_id(qts, "oob-1");
    unblock_blocked_cmd();
    recv_cmd_id(qts, "ib-blocks-1");
270
    recv_cmd_id(qts, "ib-quick-1");
271

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 276
    unblock_blocked_cmd();
    recv_cmd_id(qts, "blocks-2");
277
    recv_cmd_id(qts, "err-2");
278
    cleanup_blocking_cmd();
279 280 281 282

    qtest_quit(qts);
}

283 284
/* Preconfig tests */

285 286 287
static void test_qmp_preconfig(void)
{
    QDict *rsp, *ret;
288
    QTestState *qs = qtest_initf("%s --preconfig", common_args);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    /* 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 */
305
    g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
306 307 308 309 310 311 312 313 314
    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);

315 316
    /* check that x-exit-preconfig returns error after exiting preconfig */
    g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }")));
317 318 319 320 321 322 323

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

    qtest_quit(qs);
}

324 325 326 327 328
int main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);

    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);
331

332
    return g_test_run();
333
}