1 /* 2 * QTest 3 * 4 * Copyright IBM, Corp. 2012 5 * Copyright Red Hat, Inc. 2012 6 * Copyright SUSE LINUX Products GmbH 2013 7 * 8 * Authors: 9 * Anthony Liguori <aliguori@us.ibm.com> 10 * Paolo Bonzini <pbonzini@redhat.com> 11 * Andreas Färber <afaerber@suse.de> 12 * 13 * This work is licensed under the terms of the GNU GPL, version 2 or later. 14 * See the COPYING file in the top-level directory. 15 */ 16 17 #include "qemu/osdep.h" 18 19 #include <sys/socket.h> 20 #include <sys/wait.h> 21 #include <sys/un.h> 22 23 #include "libqos/libqtest.h" 24 #include "qemu/ctype.h" 25 #include "qemu/cutils.h" 26 #include "qapi/error.h" 27 #include "qapi/qmp/json-parser.h" 28 #include "qapi/qmp/qdict.h" 29 #include "qapi/qmp/qjson.h" 30 #include "qapi/qmp/qlist.h" 31 #include "qapi/qmp/qstring.h" 32 33 #define MAX_IRQ 256 34 #define SOCKET_TIMEOUT 50 35 #define SOCKET_MAX_FDS 16 36 37 38 typedef void (*QTestSendFn)(QTestState *s, const char *buf); 39 typedef void (*ExternalSendFn)(void *s, const char *buf); 40 typedef GString* (*QTestRecvFn)(QTestState *); 41 42 typedef struct QTestClientTransportOps { 43 QTestSendFn send; /* for sending qtest commands */ 44 45 /* 46 * use external_send to send qtest command strings through functions which 47 * do not accept a QTestState as the first parameter. 48 */ 49 ExternalSendFn external_send; 50 51 QTestRecvFn recv_line; /* for receiving qtest command responses */ 52 } QTestTransportOps; 53 54 struct QTestState 55 { 56 int fd; 57 int qmp_fd; 58 pid_t qemu_pid; /* our child QEMU process */ 59 int wstatus; 60 int expected_status; 61 bool big_endian; 62 bool irq_level[MAX_IRQ]; 63 GString *rx; 64 QTestTransportOps ops; 65 GList *pending_events; 66 }; 67 68 static GHookList abrt_hooks; 69 static struct sigaction sigact_old; 70 71 static int qtest_query_target_endianness(QTestState *s); 72 73 static void qtest_client_socket_send(QTestState*, const char *buf); 74 static void socket_send(int fd, const char *buf, size_t size); 75 76 static GString *qtest_client_socket_recv_line(QTestState *); 77 78 static void qtest_client_set_tx_handler(QTestState *s, QTestSendFn send); 79 static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv); 80 81 static int init_socket(const char *socket_path) 82 { 83 int sock = qtest_socket_server(socket_path); 84 qemu_set_cloexec(sock); 85 return sock; 86 } 87 88 static int socket_accept(int sock) 89 { 90 struct sockaddr_un addr; 91 socklen_t addrlen; 92 int ret; 93 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT, 94 .tv_usec = 0 }; 95 96 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, 97 (void *)&timeout, sizeof(timeout))) { 98 fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n", 99 __func__, strerror(errno)); 100 close(sock); 101 return -1; 102 } 103 104 do { 105 addrlen = sizeof(addr); 106 ret = accept(sock, (struct sockaddr *)&addr, &addrlen); 107 } while (ret == -1 && errno == EINTR); 108 if (ret == -1) { 109 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno)); 110 } 111 close(sock); 112 113 return ret; 114 } 115 116 bool qtest_probe_child(QTestState *s) 117 { 118 pid_t pid = s->qemu_pid; 119 120 if (pid != -1) { 121 pid = waitpid(pid, &s->wstatus, WNOHANG); 122 if (pid == 0) { 123 return true; 124 } 125 s->qemu_pid = -1; 126 } 127 return false; 128 } 129 130 void qtest_set_expected_status(QTestState *s, int status) 131 { 132 s->expected_status = status; 133 } 134 135 void qtest_kill_qemu(QTestState *s) 136 { 137 pid_t pid = s->qemu_pid; 138 int wstatus; 139 140 /* Skip wait if qtest_probe_child already reaped. */ 141 if (pid != -1) { 142 kill(pid, SIGTERM); 143 TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0)); 144 assert(pid == s->qemu_pid); 145 s->qemu_pid = -1; 146 } 147 148 /* 149 * Check whether qemu exited with expected exit status; anything else is 150 * fishy and should be logged with as much detail as possible. 151 */ 152 wstatus = s->wstatus; 153 if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != s->expected_status) { 154 fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU " 155 "process but encountered exit status %d (expected %d)\n", 156 __FILE__, __LINE__, WEXITSTATUS(wstatus), s->expected_status); 157 abort(); 158 } else if (WIFSIGNALED(wstatus)) { 159 int sig = WTERMSIG(wstatus); 160 const char *signame = strsignal(sig) ?: "unknown ???"; 161 const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : ""; 162 163 fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death " 164 "from signal %d (%s)%s\n", 165 __FILE__, __LINE__, sig, signame, dump); 166 abort(); 167 } 168 } 169 170 static void kill_qemu_hook_func(void *s) 171 { 172 qtest_kill_qemu(s); 173 } 174 175 static void sigabrt_handler(int signo) 176 { 177 g_hook_list_invoke(&abrt_hooks, FALSE); 178 } 179 180 static void setup_sigabrt_handler(void) 181 { 182 struct sigaction sigact; 183 184 /* Catch SIGABRT to clean up on g_assert() failure */ 185 sigact = (struct sigaction){ 186 .sa_handler = sigabrt_handler, 187 .sa_flags = SA_RESETHAND, 188 }; 189 sigemptyset(&sigact.sa_mask); 190 sigaction(SIGABRT, &sigact, &sigact_old); 191 } 192 193 static void cleanup_sigabrt_handler(void) 194 { 195 sigaction(SIGABRT, &sigact_old, NULL); 196 } 197 198 static bool hook_list_is_empty(GHookList *hook_list) 199 { 200 GHook *hook = g_hook_first_valid(hook_list, TRUE); 201 202 if (!hook) { 203 return false; 204 } 205 206 g_hook_unref(hook_list, hook); 207 return true; 208 } 209 210 void qtest_add_abrt_handler(GHookFunc fn, const void *data) 211 { 212 GHook *hook; 213 214 if (!abrt_hooks.is_setup) { 215 g_hook_list_init(&abrt_hooks, sizeof(GHook)); 216 } 217 218 /* Only install SIGABRT handler once */ 219 if (hook_list_is_empty(&abrt_hooks)) { 220 setup_sigabrt_handler(); 221 } 222 223 hook = g_hook_alloc(&abrt_hooks); 224 hook->func = fn; 225 hook->data = (void *)data; 226 227 g_hook_prepend(&abrt_hooks, hook); 228 } 229 230 void qtest_remove_abrt_handler(void *data) 231 { 232 GHook *hook = g_hook_find_data(&abrt_hooks, TRUE, data); 233 g_hook_destroy_link(&abrt_hooks, hook); 234 235 /* Uninstall SIGABRT handler on last instance */ 236 if (hook_list_is_empty(&abrt_hooks)) { 237 cleanup_sigabrt_handler(); 238 } 239 } 240 241 static const char *qtest_qemu_binary(void) 242 { 243 const char *qemu_bin; 244 245 qemu_bin = getenv("QTEST_QEMU_BINARY"); 246 if (!qemu_bin) { 247 fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n"); 248 exit(1); 249 } 250 251 return qemu_bin; 252 } 253 254 QTestState *qtest_init_without_qmp_handshake(const char *extra_args) 255 { 256 QTestState *s; 257 int sock, qmpsock, i; 258 gchar *socket_path; 259 gchar *qmp_socket_path; 260 gchar *command; 261 const char *qemu_binary = qtest_qemu_binary(); 262 const char *trace = g_getenv("QTEST_TRACE"); 263 g_autofree char *tracearg = trace ? 264 g_strdup_printf("-trace %s ", trace) : g_strdup(""); 265 266 s = g_new(QTestState, 1); 267 268 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid()); 269 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid()); 270 271 /* It's possible that if an earlier test run crashed it might 272 * have left a stale unix socket lying around. Delete any 273 * stale old socket to avoid spurious test failures with 274 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1) 275 */ 276 unlink(socket_path); 277 unlink(qmp_socket_path); 278 279 sock = init_socket(socket_path); 280 qmpsock = init_socket(qmp_socket_path); 281 282 qtest_client_set_rx_handler(s, qtest_client_socket_recv_line); 283 qtest_client_set_tx_handler(s, qtest_client_socket_send); 284 285 qtest_add_abrt_handler(kill_qemu_hook_func, s); 286 287 command = g_strdup_printf("exec %s %s" 288 "-qtest unix:%s " 289 "-qtest-log %s " 290 "-chardev socket,path=%s,id=char0 " 291 "-mon chardev=char0,mode=control " 292 "-display none " 293 "%s" 294 " -accel qtest", 295 qemu_binary, tracearg, socket_path, 296 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null", 297 qmp_socket_path, 298 extra_args ?: ""); 299 300 g_test_message("starting QEMU: %s", command); 301 302 s->pending_events = NULL; 303 s->wstatus = 0; 304 s->expected_status = 0; 305 s->qemu_pid = fork(); 306 if (s->qemu_pid == 0) { 307 if (!g_setenv("QEMU_AUDIO_DRV", "none", true)) { 308 exit(1); 309 } 310 execlp("/bin/sh", "sh", "-c", command, NULL); 311 exit(1); 312 } 313 314 g_free(command); 315 s->fd = socket_accept(sock); 316 if (s->fd >= 0) { 317 s->qmp_fd = socket_accept(qmpsock); 318 } 319 unlink(socket_path); 320 unlink(qmp_socket_path); 321 g_free(socket_path); 322 g_free(qmp_socket_path); 323 324 g_assert(s->fd >= 0 && s->qmp_fd >= 0); 325 326 s->rx = g_string_new(""); 327 for (i = 0; i < MAX_IRQ; i++) { 328 s->irq_level[i] = false; 329 } 330 331 if (getenv("QTEST_STOP")) { 332 kill(s->qemu_pid, SIGSTOP); 333 } 334 335 /* ask endianness of the target */ 336 337 s->big_endian = qtest_query_target_endianness(s); 338 339 return s; 340 } 341 342 QTestState *qtest_init(const char *extra_args) 343 { 344 QTestState *s = qtest_init_without_qmp_handshake(extra_args); 345 QDict *greeting; 346 347 /* Read the QMP greeting and then do the handshake */ 348 greeting = qtest_qmp_receive(s); 349 qobject_unref(greeting); 350 qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }")); 351 352 return s; 353 } 354 355 QTestState *qtest_vinitf(const char *fmt, va_list ap) 356 { 357 char *args = g_strdup_vprintf(fmt, ap); 358 QTestState *s; 359 360 s = qtest_init(args); 361 g_free(args); 362 return s; 363 } 364 365 QTestState *qtest_initf(const char *fmt, ...) 366 { 367 va_list ap; 368 QTestState *s; 369 370 va_start(ap, fmt); 371 s = qtest_vinitf(fmt, ap); 372 va_end(ap); 373 return s; 374 } 375 376 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd) 377 { 378 int sock_fd_init; 379 char *sock_path, sock_dir[] = "/tmp/qtest-serial-XXXXXX"; 380 QTestState *qts; 381 382 g_assert_true(mkdtemp(sock_dir) != NULL); 383 sock_path = g_strdup_printf("%s/sock", sock_dir); 384 385 sock_fd_init = init_socket(sock_path); 386 387 qts = qtest_initf("-chardev socket,id=s0,path=%s -serial chardev:s0 %s", 388 sock_path, extra_args); 389 390 *sock_fd = socket_accept(sock_fd_init); 391 392 unlink(sock_path); 393 g_free(sock_path); 394 rmdir(sock_dir); 395 396 g_assert_true(*sock_fd >= 0); 397 398 return qts; 399 } 400 401 void qtest_quit(QTestState *s) 402 { 403 qtest_remove_abrt_handler(s); 404 405 qtest_kill_qemu(s); 406 close(s->fd); 407 close(s->qmp_fd); 408 g_string_free(s->rx, true); 409 410 for (GList *it = s->pending_events; it != NULL; it = it->next) { 411 qobject_unref((QDict *)it->data); 412 } 413 414 g_list_free(s->pending_events); 415 416 g_free(s); 417 } 418 419 static void socket_send(int fd, const char *buf, size_t size) 420 { 421 size_t res = qemu_write_full(fd, buf, size); 422 423 assert(res == size); 424 } 425 426 static void qtest_client_socket_send(QTestState *s, const char *buf) 427 { 428 socket_send(s->fd, buf, strlen(buf)); 429 } 430 431 static void G_GNUC_PRINTF(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...) 432 { 433 va_list ap; 434 435 va_start(ap, fmt); 436 gchar *str = g_strdup_vprintf(fmt, ap); 437 va_end(ap); 438 439 s->ops.send(s, str); 440 g_free(str); 441 } 442 443 /* Sends a message and file descriptors to the socket. 444 * It's needed for qmp-commands like getfd/add-fd */ 445 static void socket_send_fds(int socket_fd, int *fds, size_t fds_num, 446 const char *buf, size_t buf_size) 447 { 448 ssize_t ret; 449 struct msghdr msg = { 0 }; 450 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)] = { 0 }; 451 size_t fdsize = sizeof(int) * fds_num; 452 struct cmsghdr *cmsg; 453 struct iovec iov = { .iov_base = (char *)buf, .iov_len = buf_size }; 454 455 msg.msg_iov = &iov; 456 msg.msg_iovlen = 1; 457 458 if (fds && fds_num > 0) { 459 g_assert_cmpuint(fds_num, <, SOCKET_MAX_FDS); 460 461 msg.msg_control = control; 462 msg.msg_controllen = CMSG_SPACE(fdsize); 463 464 cmsg = CMSG_FIRSTHDR(&msg); 465 cmsg->cmsg_len = CMSG_LEN(fdsize); 466 cmsg->cmsg_level = SOL_SOCKET; 467 cmsg->cmsg_type = SCM_RIGHTS; 468 memcpy(CMSG_DATA(cmsg), fds, fdsize); 469 } 470 471 do { 472 ret = sendmsg(socket_fd, &msg, 0); 473 } while (ret < 0 && errno == EINTR); 474 g_assert_cmpint(ret, >, 0); 475 } 476 477 static GString *qtest_client_socket_recv_line(QTestState *s) 478 { 479 GString *line; 480 size_t offset; 481 char *eol; 482 483 while ((eol = strchr(s->rx->str, '\n')) == NULL) { 484 ssize_t len; 485 char buffer[1024]; 486 487 len = read(s->fd, buffer, sizeof(buffer)); 488 if (len == -1 && errno == EINTR) { 489 continue; 490 } 491 492 if (len == -1 || len == 0) { 493 fprintf(stderr, "Broken pipe\n"); 494 abort(); 495 } 496 497 g_string_append_len(s->rx, buffer, len); 498 } 499 500 offset = eol - s->rx->str; 501 line = g_string_new_len(s->rx->str, offset); 502 g_string_erase(s->rx, 0, offset + 1); 503 504 return line; 505 } 506 507 static gchar **qtest_rsp_args(QTestState *s, int expected_args) 508 { 509 GString *line; 510 gchar **words; 511 int i; 512 513 redo: 514 line = s->ops.recv_line(s); 515 words = g_strsplit(line->str, " ", 0); 516 g_string_free(line, TRUE); 517 518 if (strcmp(words[0], "IRQ") == 0) { 519 long irq; 520 int ret; 521 522 g_assert(words[1] != NULL); 523 g_assert(words[2] != NULL); 524 525 ret = qemu_strtol(words[2], NULL, 0, &irq); 526 g_assert(!ret); 527 g_assert_cmpint(irq, >=, 0); 528 g_assert_cmpint(irq, <, MAX_IRQ); 529 530 if (strcmp(words[1], "raise") == 0) { 531 s->irq_level[irq] = true; 532 } else { 533 s->irq_level[irq] = false; 534 } 535 536 g_strfreev(words); 537 goto redo; 538 } 539 540 g_assert(words[0] != NULL); 541 g_assert_cmpstr(words[0], ==, "OK"); 542 543 for (i = 0; i < expected_args; i++) { 544 g_assert(words[i] != NULL); 545 } 546 547 return words; 548 } 549 550 static void qtest_rsp(QTestState *s) 551 { 552 gchar **words = qtest_rsp_args(s, 0); 553 554 g_strfreev(words); 555 } 556 557 static int qtest_query_target_endianness(QTestState *s) 558 { 559 gchar **args; 560 int big_endian; 561 562 qtest_sendf(s, "endianness\n"); 563 args = qtest_rsp_args(s, 1); 564 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0); 565 big_endian = strcmp(args[1], "big") == 0; 566 g_strfreev(args); 567 568 return big_endian; 569 } 570 571 typedef struct { 572 JSONMessageParser parser; 573 QDict *response; 574 } QMPResponseParser; 575 576 static void qmp_response(void *opaque, QObject *obj, Error *err) 577 { 578 QMPResponseParser *qmp = opaque; 579 580 assert(!obj != !err); 581 582 if (err) { 583 error_prepend(&err, "QMP JSON response parsing failed: "); 584 error_report_err(err); 585 abort(); 586 } 587 588 g_assert(!qmp->response); 589 qmp->response = qobject_to(QDict, obj); 590 g_assert(qmp->response); 591 } 592 593 QDict *qmp_fd_receive(int fd) 594 { 595 QMPResponseParser qmp; 596 bool log = getenv("QTEST_LOG") != NULL; 597 598 qmp.response = NULL; 599 json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL); 600 while (!qmp.response) { 601 ssize_t len; 602 char c; 603 604 len = read(fd, &c, 1); 605 if (len == -1 && errno == EINTR) { 606 continue; 607 } 608 609 if (len == -1 || len == 0) { 610 fprintf(stderr, "Broken pipe\n"); 611 abort(); 612 } 613 614 if (log) { 615 g_assert(write(2, &c, 1) == 1); 616 } 617 json_message_parser_feed(&qmp.parser, &c, 1); 618 } 619 if (log) { 620 g_assert(write(2, "\n", 1) == 1); 621 } 622 json_message_parser_destroy(&qmp.parser); 623 624 return qmp.response; 625 } 626 627 QDict *qtest_qmp_receive(QTestState *s) 628 { 629 while (true) { 630 QDict *response = qtest_qmp_receive_dict(s); 631 632 if (!qdict_get_try_str(response, "event")) { 633 return response; 634 } 635 /* Stash the event for a later consumption */ 636 s->pending_events = g_list_append(s->pending_events, response); 637 } 638 } 639 640 QDict *qtest_qmp_receive_dict(QTestState *s) 641 { 642 return qmp_fd_receive(s->qmp_fd); 643 } 644 645 int qtest_socket_server(const char *socket_path) 646 { 647 struct sockaddr_un addr; 648 int sock; 649 int ret; 650 651 sock = socket(PF_UNIX, SOCK_STREAM, 0); 652 g_assert_cmpint(sock, !=, -1); 653 654 addr.sun_family = AF_UNIX; 655 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path); 656 657 do { 658 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)); 659 } while (ret == -1 && errno == EINTR); 660 g_assert_cmpint(ret, !=, -1); 661 ret = listen(sock, 1); 662 g_assert_cmpint(ret, !=, -1); 663 664 return sock; 665 } 666 667 /** 668 * Allow users to send a message without waiting for the reply, 669 * in the case that they choose to discard all replies up until 670 * a particular EVENT is received. 671 */ 672 void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num, 673 const char *fmt, va_list ap) 674 { 675 QObject *qobj; 676 677 /* Going through qobject ensures we escape strings properly */ 678 qobj = qobject_from_vjsonf_nofail(fmt, ap); 679 680 /* No need to send anything for an empty QObject. */ 681 if (qobj) { 682 int log = getenv("QTEST_LOG") != NULL; 683 GString *str = qobject_to_json(qobj); 684 685 /* 686 * BUG: QMP doesn't react to input until it sees a newline, an 687 * object, or an array. Work-around: give it a newline. 688 */ 689 g_string_append_c(str, '\n'); 690 691 if (log) { 692 fprintf(stderr, "%s", str->str); 693 } 694 /* Send QMP request */ 695 if (fds && fds_num > 0) { 696 socket_send_fds(fd, fds, fds_num, str->str, str->len); 697 } else { 698 socket_send(fd, str->str, str->len); 699 } 700 701 g_string_free(str, true); 702 qobject_unref(qobj); 703 } 704 } 705 706 void qmp_fd_vsend(int fd, const char *fmt, va_list ap) 707 { 708 qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap); 709 } 710 711 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num, 712 const char *fmt, va_list ap) 713 { 714 qmp_fd_vsend_fds(s->qmp_fd, fds, fds_num, fmt, ap); 715 } 716 717 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap) 718 { 719 qmp_fd_vsend_fds(s->qmp_fd, NULL, 0, fmt, ap); 720 } 721 722 QDict *qmp_fdv(int fd, const char *fmt, va_list ap) 723 { 724 qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap); 725 726 return qmp_fd_receive(fd); 727 } 728 729 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num, 730 const char *fmt, va_list ap) 731 { 732 qtest_qmp_vsend_fds(s, fds, fds_num, fmt, ap); 733 734 /* Receive reply */ 735 return qtest_qmp_receive(s); 736 } 737 738 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap) 739 { 740 qtest_qmp_vsend(s, fmt, ap); 741 742 /* Receive reply */ 743 return qtest_qmp_receive(s); 744 } 745 746 QDict *qmp_fd(int fd, const char *fmt, ...) 747 { 748 va_list ap; 749 QDict *response; 750 751 va_start(ap, fmt); 752 response = qmp_fdv(fd, fmt, ap); 753 va_end(ap); 754 return response; 755 } 756 757 void qmp_fd_send(int fd, const char *fmt, ...) 758 { 759 va_list ap; 760 761 va_start(ap, fmt); 762 qmp_fd_vsend(fd, fmt, ap); 763 va_end(ap); 764 } 765 766 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num, 767 const char *fmt, ...) 768 { 769 va_list ap; 770 QDict *response; 771 772 va_start(ap, fmt); 773 response = qtest_vqmp_fds(s, fds, fds_num, fmt, ap); 774 va_end(ap); 775 return response; 776 } 777 778 QDict *qtest_qmp(QTestState *s, const char *fmt, ...) 779 { 780 va_list ap; 781 QDict *response; 782 783 va_start(ap, fmt); 784 response = qtest_vqmp(s, fmt, ap); 785 va_end(ap); 786 return response; 787 } 788 789 void qtest_qmp_send(QTestState *s, const char *fmt, ...) 790 { 791 va_list ap; 792 793 va_start(ap, fmt); 794 qtest_qmp_vsend(s, fmt, ap); 795 va_end(ap); 796 } 797 798 void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap) 799 { 800 bool log = getenv("QTEST_LOG") != NULL; 801 char *str = g_strdup_vprintf(fmt, ap); 802 803 if (log) { 804 fprintf(stderr, "%s", str); 805 } 806 socket_send(fd, str, strlen(str)); 807 g_free(str); 808 } 809 810 void qmp_fd_send_raw(int fd, const char *fmt, ...) 811 { 812 va_list ap; 813 814 va_start(ap, fmt); 815 qmp_fd_vsend_raw(fd, fmt, ap); 816 va_end(ap); 817 } 818 819 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...) 820 { 821 va_list ap; 822 823 va_start(ap, fmt); 824 qmp_fd_vsend_raw(s->qmp_fd, fmt, ap); 825 va_end(ap); 826 } 827 828 QDict *qtest_qmp_event_ref(QTestState *s, const char *event) 829 { 830 while (s->pending_events) { 831 832 GList *first = s->pending_events; 833 QDict *response = (QDict *)first->data; 834 835 s->pending_events = g_list_delete_link(s->pending_events, first); 836 837 if (!strcmp(qdict_get_str(response, "event"), event)) { 838 return response; 839 } 840 qobject_unref(response); 841 } 842 return NULL; 843 } 844 845 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event) 846 { 847 QDict *response = qtest_qmp_event_ref(s, event); 848 849 if (response) { 850 return response; 851 } 852 853 for (;;) { 854 response = qtest_qmp_receive_dict(s); 855 if ((qdict_haskey(response, "event")) && 856 (strcmp(qdict_get_str(response, "event"), event) == 0)) { 857 return response; 858 } 859 qobject_unref(response); 860 } 861 } 862 863 void qtest_qmp_eventwait(QTestState *s, const char *event) 864 { 865 QDict *response; 866 867 response = qtest_qmp_eventwait_ref(s, event); 868 qobject_unref(response); 869 } 870 871 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap) 872 { 873 char *cmd; 874 QDict *resp; 875 char *ret; 876 877 cmd = g_strdup_vprintf(fmt, ap); 878 resp = qtest_qmp(s, "{'execute': 'human-monitor-command'," 879 " 'arguments': {'command-line': %s}}", 880 cmd); 881 ret = g_strdup(qdict_get_try_str(resp, "return")); 882 g_assert(ret); 883 qobject_unref(resp); 884 g_free(cmd); 885 return ret; 886 } 887 888 char *qtest_hmp(QTestState *s, const char *fmt, ...) 889 { 890 va_list ap; 891 char *ret; 892 893 va_start(ap, fmt); 894 ret = qtest_vhmp(s, fmt, ap); 895 va_end(ap); 896 return ret; 897 } 898 899 const char *qtest_get_arch(void) 900 { 901 const char *qemu = qtest_qemu_binary(); 902 const char *end = strrchr(qemu, '-'); 903 904 if (!end) { 905 fprintf(stderr, "Can't determine architecture from binary name.\n"); 906 exit(1); 907 } 908 909 if (!strstr(qemu, "-system-")) { 910 fprintf(stderr, "QTEST_QEMU_BINARY must end with *-system-<arch> " 911 "where 'arch' is the target\narchitecture (x86_64, aarch64, " 912 "etc).\n"); 913 exit(1); 914 } 915 916 return end + 1; 917 } 918 919 bool qtest_has_accel(const char *accel_name) 920 { 921 if (g_str_equal(accel_name, "tcg")) { 922 #if defined(CONFIG_TCG) 923 return true; 924 #else 925 return false; 926 #endif 927 } else if (g_str_equal(accel_name, "kvm")) { 928 int i; 929 const char *arch = qtest_get_arch(); 930 const char *targets[] = { CONFIG_KVM_TARGETS }; 931 932 for (i = 0; i < ARRAY_SIZE(targets); i++) { 933 if (!strncmp(targets[i], arch, strlen(arch))) { 934 if (!access("/dev/kvm", R_OK | W_OK)) { 935 return true; 936 } 937 } 938 } 939 } else { 940 /* not implemented */ 941 g_assert_not_reached(); 942 } 943 return false; 944 } 945 946 bool qtest_get_irq(QTestState *s, int num) 947 { 948 /* dummy operation in order to make sure irq is up to date */ 949 qtest_inb(s, 0); 950 951 return s->irq_level[num]; 952 } 953 954 void qtest_module_load(QTestState *s, const char *prefix, const char *libname) 955 { 956 qtest_sendf(s, "module_load %s %s\n", prefix, libname); 957 qtest_rsp(s); 958 } 959 960 static int64_t qtest_clock_rsp(QTestState *s) 961 { 962 gchar **words; 963 int64_t clock; 964 words = qtest_rsp_args(s, 2); 965 clock = g_ascii_strtoll(words[1], NULL, 0); 966 g_strfreev(words); 967 return clock; 968 } 969 970 int64_t qtest_clock_step_next(QTestState *s) 971 { 972 qtest_sendf(s, "clock_step\n"); 973 return qtest_clock_rsp(s); 974 } 975 976 int64_t qtest_clock_step(QTestState *s, int64_t step) 977 { 978 qtest_sendf(s, "clock_step %"PRIi64"\n", step); 979 return qtest_clock_rsp(s); 980 } 981 982 int64_t qtest_clock_set(QTestState *s, int64_t val) 983 { 984 qtest_sendf(s, "clock_set %"PRIi64"\n", val); 985 return qtest_clock_rsp(s); 986 } 987 988 void qtest_irq_intercept_out(QTestState *s, const char *qom_path) 989 { 990 qtest_sendf(s, "irq_intercept_out %s\n", qom_path); 991 qtest_rsp(s); 992 } 993 994 void qtest_irq_intercept_in(QTestState *s, const char *qom_path) 995 { 996 qtest_sendf(s, "irq_intercept_in %s\n", qom_path); 997 qtest_rsp(s); 998 } 999 1000 void qtest_set_irq_in(QTestState *s, const char *qom_path, const char *name, 1001 int num, int level) 1002 { 1003 if (!name) { 1004 name = "unnamed-gpio-in"; 1005 } 1006 qtest_sendf(s, "set_irq_in %s %s %d %d\n", qom_path, name, num, level); 1007 qtest_rsp(s); 1008 } 1009 1010 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value) 1011 { 1012 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value); 1013 qtest_rsp(s); 1014 } 1015 1016 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value) 1017 { 1018 qtest_out(s, "outb", addr, value); 1019 } 1020 1021 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value) 1022 { 1023 qtest_out(s, "outw", addr, value); 1024 } 1025 1026 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value) 1027 { 1028 qtest_out(s, "outl", addr, value); 1029 } 1030 1031 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr) 1032 { 1033 gchar **args; 1034 int ret; 1035 unsigned long value; 1036 1037 qtest_sendf(s, "%s 0x%x\n", cmd, addr); 1038 args = qtest_rsp_args(s, 2); 1039 ret = qemu_strtoul(args[1], NULL, 0, &value); 1040 g_assert(!ret && value <= UINT32_MAX); 1041 g_strfreev(args); 1042 1043 return value; 1044 } 1045 1046 uint8_t qtest_inb(QTestState *s, uint16_t addr) 1047 { 1048 return qtest_in(s, "inb", addr); 1049 } 1050 1051 uint16_t qtest_inw(QTestState *s, uint16_t addr) 1052 { 1053 return qtest_in(s, "inw", addr); 1054 } 1055 1056 uint32_t qtest_inl(QTestState *s, uint16_t addr) 1057 { 1058 return qtest_in(s, "inl", addr); 1059 } 1060 1061 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr, 1062 uint64_t value) 1063 { 1064 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value); 1065 qtest_rsp(s); 1066 } 1067 1068 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value) 1069 { 1070 qtest_write(s, "writeb", addr, value); 1071 } 1072 1073 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value) 1074 { 1075 qtest_write(s, "writew", addr, value); 1076 } 1077 1078 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value) 1079 { 1080 qtest_write(s, "writel", addr, value); 1081 } 1082 1083 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value) 1084 { 1085 qtest_write(s, "writeq", addr, value); 1086 } 1087 1088 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr) 1089 { 1090 gchar **args; 1091 int ret; 1092 uint64_t value; 1093 1094 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr); 1095 args = qtest_rsp_args(s, 2); 1096 ret = qemu_strtou64(args[1], NULL, 0, &value); 1097 g_assert(!ret); 1098 g_strfreev(args); 1099 1100 return value; 1101 } 1102 1103 uint8_t qtest_readb(QTestState *s, uint64_t addr) 1104 { 1105 return qtest_read(s, "readb", addr); 1106 } 1107 1108 uint16_t qtest_readw(QTestState *s, uint64_t addr) 1109 { 1110 return qtest_read(s, "readw", addr); 1111 } 1112 1113 uint32_t qtest_readl(QTestState *s, uint64_t addr) 1114 { 1115 return qtest_read(s, "readl", addr); 1116 } 1117 1118 uint64_t qtest_readq(QTestState *s, uint64_t addr) 1119 { 1120 return qtest_read(s, "readq", addr); 1121 } 1122 1123 static int hex2nib(char ch) 1124 { 1125 if (ch >= '0' && ch <= '9') { 1126 return ch - '0'; 1127 } else if (ch >= 'a' && ch <= 'f') { 1128 return 10 + (ch - 'a'); 1129 } else if (ch >= 'A' && ch <= 'F') { 1130 return 10 + (ch - 'a'); 1131 } else { 1132 return -1; 1133 } 1134 } 1135 1136 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size) 1137 { 1138 uint8_t *ptr = data; 1139 gchar **args; 1140 size_t i; 1141 1142 if (!size) { 1143 return; 1144 } 1145 1146 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size); 1147 args = qtest_rsp_args(s, 2); 1148 1149 for (i = 0; i < size; i++) { 1150 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4; 1151 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]); 1152 } 1153 1154 g_strfreev(args); 1155 } 1156 1157 uint64_t qtest_rtas_call(QTestState *s, const char *name, 1158 uint32_t nargs, uint64_t args, 1159 uint32_t nret, uint64_t ret) 1160 { 1161 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n", 1162 name, nargs, args, nret, ret); 1163 qtest_rsp(s); 1164 return 0; 1165 } 1166 1167 void qtest_add_func(const char *str, void (*fn)(void)) 1168 { 1169 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str); 1170 g_test_add_func(path, fn); 1171 g_free(path); 1172 } 1173 1174 void qtest_add_data_func_full(const char *str, void *data, 1175 void (*fn)(const void *), 1176 GDestroyNotify data_free_func) 1177 { 1178 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str); 1179 g_test_add_data_func_full(path, data, fn, data_free_func); 1180 g_free(path); 1181 } 1182 1183 void qtest_add_data_func(const char *str, const void *data, 1184 void (*fn)(const void *)) 1185 { 1186 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str); 1187 g_test_add_data_func(path, data, fn); 1188 g_free(path); 1189 } 1190 1191 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size) 1192 { 1193 gchar *bdata; 1194 1195 bdata = g_base64_encode(data, size); 1196 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size); 1197 s->ops.send(s, bdata); 1198 s->ops.send(s, "\n"); 1199 qtest_rsp(s); 1200 g_free(bdata); 1201 } 1202 1203 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size) 1204 { 1205 gchar **args; 1206 size_t len; 1207 1208 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size); 1209 args = qtest_rsp_args(s, 2); 1210 1211 g_base64_decode_inplace(args[1], &len); 1212 if (size != len) { 1213 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n", 1214 size, len); 1215 len = MIN(len, size); 1216 } 1217 1218 memcpy(data, args[1], len); 1219 g_strfreev(args); 1220 } 1221 1222 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size) 1223 { 1224 const uint8_t *ptr = data; 1225 size_t i; 1226 char *enc; 1227 1228 if (!size) { 1229 return; 1230 } 1231 1232 enc = g_malloc(2 * size + 1); 1233 1234 for (i = 0; i < size; i++) { 1235 sprintf(&enc[i * 2], "%02x", ptr[i]); 1236 } 1237 1238 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc); 1239 qtest_rsp(s); 1240 g_free(enc); 1241 } 1242 1243 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size) 1244 { 1245 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern); 1246 qtest_rsp(s); 1247 } 1248 1249 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...) 1250 { 1251 va_list ap; 1252 QDict *response; 1253 1254 va_start(ap, fmt); 1255 response = qtest_vqmp(qts, fmt, ap); 1256 va_end(ap); 1257 1258 g_assert(response); 1259 if (!qdict_haskey(response, "return")) { 1260 GString *s = qobject_to_json_pretty(QOBJECT(response), true); 1261 g_test_message("%s", s->str); 1262 g_string_free(s, true); 1263 } 1264 g_assert(qdict_haskey(response, "return")); 1265 qobject_unref(response); 1266 } 1267 1268 bool qtest_big_endian(QTestState *s) 1269 { 1270 return s->big_endian; 1271 } 1272 1273 static bool qtest_check_machine_version(const char *mname, const char *basename, 1274 int major, int minor) 1275 { 1276 char *newname; 1277 bool is_equal; 1278 1279 newname = g_strdup_printf("%s-%i.%i", basename, major, minor); 1280 is_equal = g_str_equal(mname, newname); 1281 g_free(newname); 1282 1283 return is_equal; 1284 } 1285 1286 static bool qtest_is_old_versioned_machine(const char *mname) 1287 { 1288 const char *dash = strrchr(mname, '-'); 1289 const char *dot = strrchr(mname, '.'); 1290 const char *chr; 1291 char *bname; 1292 const int major = QEMU_VERSION_MAJOR; 1293 const int minor = QEMU_VERSION_MINOR; 1294 bool res = false; 1295 1296 if (dash && dot && dot > dash) { 1297 for (chr = dash + 1; *chr; chr++) { 1298 if (!qemu_isdigit(*chr) && *chr != '.') { 1299 return false; 1300 } 1301 } 1302 /* 1303 * Now check if it is one of the latest versions. Check major + 1 1304 * and minor + 1 versions as well, since they might already exist 1305 * in the development branch. 1306 */ 1307 bname = g_strdup(mname); 1308 bname[dash - mname] = 0; 1309 res = !qtest_check_machine_version(mname, bname, major + 1, 0) && 1310 !qtest_check_machine_version(mname, bname, major, minor + 1) && 1311 !qtest_check_machine_version(mname, bname, major, minor); 1312 g_free(bname); 1313 } 1314 1315 return res; 1316 } 1317 1318 struct MachInfo { 1319 char *name; 1320 char *alias; 1321 }; 1322 1323 /* 1324 * Returns an array with pointers to the available machine names. 1325 * The terminating entry has the name set to NULL. 1326 */ 1327 static struct MachInfo *qtest_get_machines(void) 1328 { 1329 static struct MachInfo *machines; 1330 QDict *response, *minfo; 1331 QList *list; 1332 const QListEntry *p; 1333 QObject *qobj; 1334 QString *qstr; 1335 QTestState *qts; 1336 int idx; 1337 1338 if (machines) { 1339 return machines; 1340 } 1341 1342 qts = qtest_init("-machine none"); 1343 response = qtest_qmp(qts, "{ 'execute': 'query-machines' }"); 1344 g_assert(response); 1345 list = qdict_get_qlist(response, "return"); 1346 g_assert(list); 1347 1348 machines = g_new(struct MachInfo, qlist_size(list) + 1); 1349 1350 for (p = qlist_first(list), idx = 0; p; p = qlist_next(p), idx++) { 1351 minfo = qobject_to(QDict, qlist_entry_obj(p)); 1352 g_assert(minfo); 1353 1354 qobj = qdict_get(minfo, "name"); 1355 g_assert(qobj); 1356 qstr = qobject_to(QString, qobj); 1357 g_assert(qstr); 1358 machines[idx].name = g_strdup(qstring_get_str(qstr)); 1359 1360 qobj = qdict_get(minfo, "alias"); 1361 if (qobj) { /* The alias is optional */ 1362 qstr = qobject_to(QString, qobj); 1363 g_assert(qstr); 1364 machines[idx].alias = g_strdup(qstring_get_str(qstr)); 1365 } else { 1366 machines[idx].alias = NULL; 1367 } 1368 } 1369 1370 qtest_quit(qts); 1371 qobject_unref(response); 1372 1373 memset(&machines[idx], 0, sizeof(struct MachInfo)); /* Terminating entry */ 1374 return machines; 1375 } 1376 1377 void qtest_cb_for_every_machine(void (*cb)(const char *machine), 1378 bool skip_old_versioned) 1379 { 1380 struct MachInfo *machines; 1381 int i; 1382 1383 machines = qtest_get_machines(); 1384 1385 for (i = 0; machines[i].name != NULL; i++) { 1386 /* Ignore machines that cannot be used for qtests */ 1387 if (!strncmp("xenfv", machines[i].name, 5) || 1388 g_str_equal("xenpv", machines[i].name)) { 1389 continue; 1390 } 1391 if (!skip_old_versioned || 1392 !qtest_is_old_versioned_machine(machines[i].name)) { 1393 cb(machines[i].name); 1394 } 1395 } 1396 } 1397 1398 bool qtest_has_machine(const char *machine) 1399 { 1400 struct MachInfo *machines; 1401 int i; 1402 1403 machines = qtest_get_machines(); 1404 1405 for (i = 0; machines[i].name != NULL; i++) { 1406 if (g_str_equal(machine, machines[i].name) || 1407 (machines[i].alias && g_str_equal(machine, machines[i].alias))) { 1408 return true; 1409 } 1410 } 1411 1412 return false; 1413 } 1414 1415 bool qtest_has_device(const char *device) 1416 { 1417 static QList *list; 1418 const QListEntry *p; 1419 QObject *qobj; 1420 QString *qstr; 1421 QDict *devinfo; 1422 int idx; 1423 1424 if (!list) { 1425 QDict *resp; 1426 QDict *args; 1427 QTestState *qts = qtest_init("-machine none"); 1428 1429 args = qdict_new(); 1430 qdict_put_bool(args, "abstract", false); 1431 qdict_put_str(args, "implements", "device"); 1432 1433 resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }", 1434 args); 1435 g_assert(qdict_haskey(resp, "return")); 1436 list = qdict_get_qlist(resp, "return"); 1437 qobject_ref(list); 1438 qobject_unref(resp); 1439 1440 qtest_quit(qts); 1441 } 1442 1443 for (p = qlist_first(list), idx = 0; p; p = qlist_next(p), idx++) { 1444 devinfo = qobject_to(QDict, qlist_entry_obj(p)); 1445 g_assert(devinfo); 1446 1447 qobj = qdict_get(devinfo, "name"); 1448 g_assert(qobj); 1449 qstr = qobject_to(QString, qobj); 1450 g_assert(qstr); 1451 if (g_str_equal(qstring_get_str(qstr), device)) { 1452 return true; 1453 } 1454 } 1455 1456 return false; 1457 } 1458 1459 /* 1460 * Generic hot-plugging test via the device_add QMP commands. 1461 */ 1462 void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv, 1463 const QDict *arguments) 1464 { 1465 QDict *resp; 1466 QDict *args = arguments ? qdict_clone_shallow(arguments) : qdict_new(); 1467 1468 g_assert(!qdict_haskey(args, "driver")); 1469 qdict_put_str(args, "driver", drv); 1470 resp = qtest_qmp(qts, "{'execute': 'device_add', 'arguments': %p}", args); 1471 g_assert(resp); 1472 g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */ 1473 g_assert(!qdict_haskey(resp, "error")); 1474 qobject_unref(resp); 1475 } 1476 1477 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id, 1478 const char *fmt, ...) 1479 { 1480 QDict *args; 1481 va_list ap; 1482 1483 va_start(ap, fmt); 1484 args = qdict_from_vjsonf_nofail(fmt, ap); 1485 va_end(ap); 1486 1487 g_assert(!qdict_haskey(args, "id")); 1488 qdict_put_str(args, "id", id); 1489 1490 qtest_qmp_device_add_qdict(qts, driver, args); 1491 qobject_unref(args); 1492 } 1493 1494 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd) 1495 { 1496 QDict *resp; 1497 1498 resp = qtest_qmp_fds(qts, &fd, 1, "{'execute': 'getfd'," 1499 "'arguments': {'fdname': 'fdname'}}"); 1500 g_assert(resp); 1501 g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */ 1502 g_assert(!qdict_haskey(resp, "error")); 1503 qobject_unref(resp); 1504 1505 resp = qtest_qmp( 1506 qts, "{'execute': 'add_client'," 1507 "'arguments': {'protocol': %s, 'fdname': 'fdname'}}", protocol); 1508 g_assert(resp); 1509 g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */ 1510 g_assert(!qdict_haskey(resp, "error")); 1511 qobject_unref(resp); 1512 } 1513 1514 /* 1515 * Generic hot-unplugging test via the device_del QMP command. 1516 * Device deletion will get one response and one event. For example: 1517 * 1518 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}} 1519 * 1520 * will get this one: 1521 * 1522 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862}, 1523 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd", 1524 * "path": "/machine/peripheral/scsi-hd"}} 1525 * 1526 * and this one: 1527 * 1528 * {"return": {}} 1529 */ 1530 void qtest_qmp_device_del(QTestState *qts, const char *id) 1531 { 1532 QDict *rsp; 1533 1534 rsp = qtest_qmp(qts, "{'execute': 'device_del', 'arguments': {'id': %s}}", 1535 id); 1536 1537 g_assert(qdict_haskey(rsp, "return")); 1538 qobject_unref(rsp); 1539 qtest_qmp_eventwait(qts, "DEVICE_DELETED"); 1540 } 1541 1542 bool qmp_rsp_is_err(QDict *rsp) 1543 { 1544 QDict *error = qdict_get_qdict(rsp, "error"); 1545 qobject_unref(rsp); 1546 return !!error; 1547 } 1548 1549 void qmp_expect_error_and_unref(QDict *rsp, const char *class) 1550 { 1551 QDict *error = qdict_get_qdict(rsp, "error"); 1552 1553 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class); 1554 g_assert_nonnull(qdict_get_try_str(error, "desc")); 1555 g_assert(!qdict_haskey(rsp, "return")); 1556 1557 qobject_unref(rsp); 1558 } 1559 1560 static void qtest_client_set_tx_handler(QTestState *s, 1561 QTestSendFn send) 1562 { 1563 s->ops.send = send; 1564 } 1565 static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv) 1566 { 1567 s->ops.recv_line = recv; 1568 } 1569 /* A type-safe wrapper for s->send() */ 1570 static void send_wrapper(QTestState *s, const char *buf) 1571 { 1572 s->ops.external_send(s, buf); 1573 } 1574 1575 static GString *qtest_client_inproc_recv_line(QTestState *s) 1576 { 1577 GString *line; 1578 size_t offset; 1579 char *eol; 1580 1581 eol = strchr(s->rx->str, '\n'); 1582 offset = eol - s->rx->str; 1583 line = g_string_new_len(s->rx->str, offset); 1584 g_string_erase(s->rx, 0, offset + 1); 1585 return line; 1586 } 1587 1588 QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch, 1589 void (*send)(void*, const char*)) 1590 { 1591 QTestState *qts; 1592 qts = g_new0(QTestState, 1); 1593 qts->pending_events = NULL; 1594 *s = qts; /* Expose qts early on, since the query endianness relies on it */ 1595 qts->wstatus = 0; 1596 for (int i = 0; i < MAX_IRQ; i++) { 1597 qts->irq_level[i] = false; 1598 } 1599 1600 qtest_client_set_rx_handler(qts, qtest_client_inproc_recv_line); 1601 1602 /* send() may not have a matching protoype, so use a type-safe wrapper */ 1603 qts->ops.external_send = send; 1604 qtest_client_set_tx_handler(qts, send_wrapper); 1605 1606 qts->big_endian = qtest_query_target_endianness(qts); 1607 1608 /* 1609 * Set a dummy path for QTEST_QEMU_BINARY. Doesn't need to exist, but this 1610 * way, qtest_get_arch works for inproc qtest. 1611 */ 1612 gchar *bin_path = g_strconcat("/qemu-system-", arch, NULL); 1613 setenv("QTEST_QEMU_BINARY", bin_path, 0); 1614 g_free(bin_path); 1615 1616 return qts; 1617 } 1618 1619 void qtest_client_inproc_recv(void *opaque, const char *str) 1620 { 1621 QTestState *qts = *(QTestState **)opaque; 1622 1623 if (!qts->rx) { 1624 qts->rx = g_string_new(NULL); 1625 } 1626 g_string_append(qts->rx, str); 1627 return; 1628 } 1629