1 /* 2 * QMP protocol test cases 3 * 4 * Copyright (c) 2017-2018 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 "libqtest.h" 15 #include "qapi/error.h" 16 #include "qapi/qapi-visit-control.h" 17 #include "qapi/qmp/qdict.h" 18 #include "qapi/qmp/qlist.h" 19 #include "qapi/qobject-input-visitor.h" 20 #include "qapi/qmp/qstring.h" 21 22 const char common_args[] = "-nodefaults -machine none"; 23 24 static void test_version(QObject *version) 25 { 26 Visitor *v; 27 VersionInfo *vinfo; 28 29 g_assert(version); 30 v = qobject_input_visitor_new(version); 31 visit_type_VersionInfo(v, "version", &vinfo, &error_abort); 32 qapi_free_VersionInfo(vinfo); 33 visit_free(v); 34 } 35 36 static void assert_recovered(QTestState *qts) 37 { 38 QDict *resp; 39 40 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }"); 41 qmp_expect_error_and_unref(resp, "CommandNotFound"); 42 } 43 44 static void test_malformed(QTestState *qts) 45 { 46 QDict *resp; 47 48 /* syntax error */ 49 qtest_qmp_send_raw(qts, "{]\n"); 50 resp = qtest_qmp_receive_dict(qts); 51 qmp_expect_error_and_unref(resp, "GenericError"); 52 assert_recovered(qts); 53 54 /* lexical error: impossible byte outside string */ 55 qtest_qmp_send_raw(qts, "{\xFF"); 56 resp = qtest_qmp_receive_dict(qts); 57 qmp_expect_error_and_unref(resp, "GenericError"); 58 assert_recovered(qts); 59 60 /* lexical error: funny control character outside string */ 61 qtest_qmp_send_raw(qts, "{\x01"); 62 resp = qtest_qmp_receive_dict(qts); 63 qmp_expect_error_and_unref(resp, "GenericError"); 64 assert_recovered(qts); 65 66 /* lexical error: impossible byte in string */ 67 qtest_qmp_send_raw(qts, "{'bad \xFF"); 68 resp = qtest_qmp_receive_dict(qts); 69 qmp_expect_error_and_unref(resp, "GenericError"); 70 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_dict(qts); 75 qmp_expect_error_and_unref(resp, "GenericError"); 76 assert_recovered(qts); 77 78 /* lexical error: interpolation */ 79 qtest_qmp_send_raw(qts, "%%p"); 80 resp = qtest_qmp_receive_dict(qts); 81 qmp_expect_error_and_unref(resp, "GenericError"); 82 assert_recovered(qts); 83 84 /* Not even a dictionary */ 85 resp = qtest_qmp(qts, "null"); 86 qmp_expect_error_and_unref(resp, "GenericError"); 87 88 /* No "execute" key */ 89 resp = qtest_qmp(qts, "{}"); 90 qmp_expect_error_and_unref(resp, "GenericError"); 91 92 /* "execute" isn't a string */ 93 resp = qtest_qmp(qts, "{ 'execute': true }"); 94 qmp_expect_error_and_unref(resp, "GenericError"); 95 96 /* "arguments" isn't a dictionary */ 97 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }"); 98 qmp_expect_error_and_unref(resp, "GenericError"); 99 100 /* extra key */ 101 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }"); 102 qmp_expect_error_and_unref(resp, "GenericError"); 103 } 104 105 static void test_qmp_protocol(void) 106 { 107 QDict *resp, *q, *ret; 108 QList *capabilities; 109 QTestState *qts; 110 111 qts = qtest_init_without_qmp_handshake(common_args); 112 113 /* Test greeting */ 114 resp = qtest_qmp_receive_dict(qts); 115 q = qdict_get_qdict(resp, "QMP"); 116 g_assert(q); 117 test_version(qdict_get(q, "version")); 118 capabilities = qdict_get_qlist(q, "capabilities"); 119 g_assert(capabilities); 120 qobject_unref(resp); 121 122 /* Test valid command before handshake */ 123 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }"); 124 qmp_expect_error_and_unref(resp, "CommandNotFound"); 125 126 /* Test malformed commands before handshake */ 127 test_malformed(qts); 128 129 /* Test handshake */ 130 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }"); 131 ret = qdict_get_qdict(resp, "return"); 132 g_assert(ret && !qdict_size(ret)); 133 qobject_unref(resp); 134 135 /* Test repeated handshake */ 136 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }"); 137 qmp_expect_error_and_unref(resp, "CommandNotFound"); 138 139 /* Test valid command */ 140 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }"); 141 test_version(qdict_get(resp, "return")); 142 qobject_unref(resp); 143 144 /* Test malformed commands */ 145 test_malformed(qts); 146 147 /* Test 'id' */ 148 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }"); 149 ret = qdict_get_qdict(resp, "return"); 150 g_assert(ret); 151 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1"); 152 qobject_unref(resp); 153 154 /* Test command failure with 'id' */ 155 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }"); 156 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2); 157 qmp_expect_error_and_unref(resp, "GenericError"); 158 159 qtest_quit(qts); 160 } 161 162 #ifndef _WIN32 163 164 /* Out-of-band tests */ 165 166 char *tmpdir; 167 char *fifo_name; 168 169 static void setup_blocking_cmd(void) 170 { 171 GError *err = NULL; 172 tmpdir = g_dir_make_tmp("qmp-test-XXXXXX", &err); 173 g_assert_no_error(err); 174 175 fifo_name = g_strdup_printf("%s/fifo", tmpdir); 176 if (mkfifo(fifo_name, 0666)) { 177 g_error("mkfifo: %s", strerror(errno)); 178 } 179 } 180 181 static void cleanup_blocking_cmd(void) 182 { 183 unlink(fifo_name); 184 rmdir(tmpdir); 185 g_free(tmpdir); 186 } 187 188 static void send_cmd_that_blocks(QTestState *s, const char *id) 189 { 190 qtest_qmp_send(s, "{ 'execute': 'blockdev-add', 'id': %s," 191 " 'arguments': {" 192 " 'driver': 'blkdebug', 'node-name': %s," 193 " 'config': %s," 194 " 'image': { 'driver': 'null-co', 'read-zeroes': true } } }", 195 id, id, fifo_name); 196 } 197 198 static void unblock_blocked_cmd(void) 199 { 200 int fd = open(fifo_name, O_WRONLY); 201 g_assert(fd >= 0); 202 close(fd); 203 } 204 205 static void send_oob_cmd_that_fails(QTestState *s, const char *id) 206 { 207 qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id); 208 } 209 210 static void recv_cmd_id(QTestState *s, const char *id) 211 { 212 QDict *resp = qtest_qmp_receive_dict(s); 213 214 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, id); 215 qobject_unref(resp); 216 } 217 218 static void test_qmp_oob(void) 219 { 220 QTestState *qts; 221 QDict *resp, *q; 222 const QListEntry *entry; 223 QList *capabilities; 224 QString *qstr; 225 226 qts = qtest_init_without_qmp_handshake(common_args); 227 228 /* Check the greeting message. */ 229 resp = qtest_qmp_receive_dict(qts); 230 q = qdict_get_qdict(resp, "QMP"); 231 g_assert(q); 232 capabilities = qdict_get_qlist(q, "capabilities"); 233 g_assert(capabilities && !qlist_empty(capabilities)); 234 entry = qlist_first(capabilities); 235 g_assert(entry); 236 qstr = qobject_to(QString, entry->value); 237 g_assert(qstr); 238 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob"); 239 qobject_unref(resp); 240 241 /* Try a fake capability, it should fail. */ 242 resp = qtest_qmp(qts, 243 "{ 'execute': 'qmp_capabilities', " 244 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }"); 245 g_assert(qdict_haskey(resp, "error")); 246 qobject_unref(resp); 247 248 /* Now, enable OOB in current QMP session, it should succeed. */ 249 resp = qtest_qmp(qts, 250 "{ 'execute': 'qmp_capabilities', " 251 " 'arguments': { 'enable': [ 'oob' ] } }"); 252 g_assert(qdict_haskey(resp, "return")); 253 qobject_unref(resp); 254 255 /* 256 * Try any command that does not support OOB but with OOB flag. We 257 * should get failure. 258 */ 259 resp = qtest_qmp(qts, "{ 'exec-oob': 'query-cpus-fast' }"); 260 g_assert(qdict_haskey(resp, "error")); 261 qobject_unref(resp); 262 263 /* OOB command overtakes slow in-band command */ 264 setup_blocking_cmd(); 265 send_cmd_that_blocks(qts, "ib-blocks-1"); 266 qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }"); 267 send_oob_cmd_that_fails(qts, "oob-1"); 268 recv_cmd_id(qts, "oob-1"); 269 unblock_blocked_cmd(); 270 recv_cmd_id(qts, "ib-blocks-1"); 271 recv_cmd_id(qts, "ib-quick-1"); 272 273 /* Even malformed in-band command fails in-band */ 274 send_cmd_that_blocks(qts, "blocks-2"); 275 qtest_qmp_send(qts, "{ 'id': 'err-2' }"); 276 unblock_blocked_cmd(); 277 recv_cmd_id(qts, "blocks-2"); 278 recv_cmd_id(qts, "err-2"); 279 cleanup_blocking_cmd(); 280 281 qtest_quit(qts); 282 } 283 284 #endif /* _WIN32 */ 285 286 /* Preconfig tests */ 287 288 static void test_qmp_preconfig(void) 289 { 290 QDict *rsp, *ret; 291 QTestState *qs = qtest_initf("%s --preconfig", common_args); 292 293 /* preconfig state */ 294 /* enabled commands, no error expected */ 295 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }"))); 296 297 /* forbidden commands, expected error */ 298 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus-fast' }"))); 299 300 /* check that query-status returns preconfig state */ 301 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }"); 302 ret = qdict_get_qdict(rsp, "return"); 303 g_assert(ret); 304 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "prelaunch"); 305 qobject_unref(rsp); 306 307 /* exit preconfig state */ 308 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }"))); 309 qtest_qmp_eventwait(qs, "RESUME"); 310 311 /* check that query-status returns running state */ 312 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }"); 313 ret = qdict_get_qdict(rsp, "return"); 314 g_assert(ret); 315 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running"); 316 qobject_unref(rsp); 317 318 /* check that x-exit-preconfig returns error after exiting preconfig */ 319 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'x-exit-preconfig' }"))); 320 321 /* enabled commands, no error expected */ 322 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus-fast' }"))); 323 324 qtest_quit(qs); 325 } 326 327 static void test_qmp_missing_any_arg(void) 328 { 329 QTestState *qts; 330 QDict *resp; 331 332 qts = qtest_init(common_args); 333 resp = qtest_qmp(qts, "{'execute': 'qom-set', 'arguments':" 334 " { 'path': '/machine', 'property': 'rtc-time' } }"); 335 g_assert_nonnull(resp); 336 qmp_expect_error_and_unref(resp, "GenericError"); 337 qtest_quit(qts); 338 } 339 340 int main(int argc, char *argv[]) 341 { 342 g_test_init(&argc, &argv, NULL); 343 344 qtest_add_func("qmp/protocol", test_qmp_protocol); 345 #ifndef _WIN32 346 /* This case calls mkfifo() which does not exist on win32 */ 347 qtest_add_func("qmp/oob", test_qmp_oob); 348 #endif 349 qtest_add_func("qmp/preconfig", test_qmp_preconfig); 350 qtest_add_func("qmp/missing-any-arg", test_qmp_missing_any_arg); 351 352 return g_test_run(); 353 } 354