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 #ifndef LIBQTEST_H 18 #define LIBQTEST_H 19 20 #include "qobject/qobject.h" 21 #include "qobject/qdict.h" 22 #include "qobject/qlist.h" 23 #include "libqmp.h" 24 25 typedef struct QTestState QTestState; 26 27 /** 28 * qtest_initf: 29 * @fmt: Format for creating other arguments to pass to QEMU, formatted 30 * like sprintf(). 31 * 32 * Convenience wrapper around qtest_init(). 33 * 34 * Returns: #QTestState instance. 35 */ 36 QTestState *qtest_initf(const char *fmt, ...) G_GNUC_PRINTF(1, 2); 37 38 /** 39 * qtest_vinitf: 40 * @fmt: Format for creating other arguments to pass to QEMU, formatted 41 * like vsprintf(). 42 * @ap: Format arguments. 43 * 44 * Convenience wrapper around qtest_init(). 45 * 46 * Returns: #QTestState instance. 47 */ 48 QTestState *qtest_vinitf(const char *fmt, va_list ap) G_GNUC_PRINTF(1, 0); 49 50 /** 51 * qtest_init: 52 * @extra_args: other arguments to pass to QEMU. CAUTION: these 53 * arguments are subject to word splitting and shell evaluation. 54 * 55 * Returns: #QTestState instance. 56 */ 57 QTestState *qtest_init(const char *extra_args); 58 59 /** 60 * qtest_init_with_env: 61 * @var: Environment variable from where to take the QEMU binary 62 * @extra_args: Other arguments to pass to QEMU. CAUTION: these 63 * arguments are subject to word splitting and shell evaluation. 64 * @do_connect: connect to qemu monitor and qtest socket. 65 * 66 * Like qtest_init(), but use a different environment variable for the 67 * QEMU binary. 68 * 69 * Returns: #QTestState instance. 70 */ 71 QTestState *qtest_init_with_env(const char *var, const char *extra_args, 72 bool do_connect); 73 74 /** 75 * qtest_init_with_env_and_capabilities: 76 * @var: Environment variable from where to take the QEMU binary 77 * @extra_args: Other arguments to pass to QEMU. CAUTION: these 78 * arguments are subject to word splitting and shell evaluation. 79 * @capabilities: list of QMP capabilities (strings) to enable 80 * @do_connect: connect to qemu monitor and qtest socket. 81 * 82 * Like qtest_init_with_env(), but enable specified capabilities during 83 * hadshake. 84 * 85 * Returns: #QTestState instance. 86 */ 87 QTestState *qtest_init_with_env_and_capabilities(const char *var, 88 const char *extra_args, 89 QList *capabilities, 90 bool do_connect); 91 92 /** 93 * qtest_init_without_qmp_handshake: 94 * @extra_args: other arguments to pass to QEMU. CAUTION: these 95 * arguments are subject to word splitting and shell evaluation. 96 * 97 * Returns: #QTestState instance. 98 */ 99 QTestState *qtest_init_without_qmp_handshake(const char *extra_args); 100 101 /** 102 * qtest_connect 103 * @s: #QTestState instance to connect 104 * Connect to qemu monitor and qtest socket, after skipping them in 105 * qtest_init_with_env. Does not handshake with the monitor. 106 */ 107 void qtest_connect(QTestState *s); 108 109 /** 110 * qtest_qmp_handshake: 111 * @s: #QTestState instance to operate on. 112 * @capabilities: list of QMP capabilities (strings) to enable 113 * Perform handshake after connecting to qemu monitor. 114 */ 115 void qtest_qmp_handshake(QTestState *s, QList *capabilities); 116 117 /** 118 * qtest_init_with_serial: 119 * @extra_args: other arguments to pass to QEMU. CAUTION: these 120 * arguments are subject to word splitting and shell evaluation. 121 * @sock_fd: pointer to store the socket file descriptor for 122 * connection with serial. 123 * 124 * Returns: #QTestState instance. 125 */ 126 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd); 127 128 /** 129 * qtest_system_reset: 130 * @s: #QTestState instance to operate on. 131 * 132 * Send a "system_reset" command to the QEMU under test, and wait for 133 * the reset to complete before returning. 134 */ 135 void qtest_system_reset(QTestState *s); 136 137 /** 138 * qtest_system_reset_nowait: 139 * @s: #QTestState instance to operate on. 140 * 141 * Send a "system_reset" command to the QEMU under test, but do not 142 * wait for the reset to complete before returning. The caller is 143 * responsible for waiting for either the RESET event or some other 144 * event of interest to them before proceeding. 145 * 146 * This function should only be used if you're specifically testing 147 * for some other event; in that case you can't use qtest_system_reset() 148 * because it will read and discard any other QMP events that arrive 149 * before the RESET event. 150 */ 151 void qtest_system_reset_nowait(QTestState *s); 152 153 /** 154 * qtest_wait_qemu: 155 * @s: #QTestState instance to operate on. 156 * 157 * Wait for the QEMU process to terminate. It is safe to call this function 158 * multiple times. 159 */ 160 void qtest_wait_qemu(QTestState *s); 161 162 /** 163 * qtest_kill_qemu: 164 * @s: #QTestState instance to operate on. 165 * 166 * Kill the QEMU process and wait for it to terminate. It is safe to call this 167 * function multiple times. Normally qtest_quit() is used instead because it 168 * also frees QTestState. Use qtest_kill_qemu() when you just want to kill QEMU 169 * and qtest_quit() will be called later. 170 */ 171 void qtest_kill_qemu(QTestState *s); 172 173 /** 174 * qtest_quit: 175 * @s: #QTestState instance to operate on. 176 * 177 * Shut down the QEMU process associated to @s. 178 */ 179 void qtest_quit(QTestState *s); 180 181 #ifndef _WIN32 182 /** 183 * qtest_qmp_fds: 184 * @s: #QTestState instance to operate on. 185 * @fds: array of file descriptors 186 * @fds_num: number of elements in @fds 187 * @fmt: QMP message to send to qemu, formatted like 188 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 189 * supported after '%'. 190 * 191 * Sends a QMP message to QEMU with fds and returns the response. 192 */ 193 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num, 194 const char *fmt, ...) 195 G_GNUC_PRINTF(4, 5); 196 #endif /* _WIN32 */ 197 198 /** 199 * qtest_qmp: 200 * @s: #QTestState instance to operate on. 201 * @fmt: QMP message to send to qemu, formatted like 202 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 203 * supported after '%'. 204 * 205 * Sends a QMP message to QEMU and returns the response. 206 */ 207 QDict *qtest_qmp(QTestState *s, const char *fmt, ...) 208 G_GNUC_PRINTF(2, 3); 209 210 /** 211 * qtest_qmp_send: 212 * @s: #QTestState instance to operate on. 213 * @fmt: QMP message to send to qemu, formatted like 214 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 215 * supported after '%'. 216 * 217 * Sends a QMP message to QEMU and leaves the response in the stream. 218 */ 219 void qtest_qmp_send(QTestState *s, const char *fmt, ...) 220 G_GNUC_PRINTF(2, 3); 221 222 /** 223 * qtest_qmp_send_raw: 224 * @s: #QTestState instance to operate on. 225 * @fmt: text to send, formatted like sprintf() 226 * 227 * Sends text to the QMP monitor verbatim. Need not be valid JSON; 228 * this is useful for negative tests. 229 */ 230 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...) 231 G_GNUC_PRINTF(2, 3); 232 233 /** 234 * qtest_socket_server: 235 * @socket_path: the UNIX domain socket path 236 * 237 * Create and return a listen socket file descriptor, or abort on failure. 238 */ 239 int qtest_socket_server(const char *socket_path); 240 241 #ifndef _WIN32 242 /** 243 * qtest_vqmp_fds: 244 * @s: #QTestState instance to operate on. 245 * @fds: array of file descriptors 246 * @fds_num: number of elements in @fds 247 * @fmt: QMP message to send to QEMU, formatted like 248 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 249 * supported after '%'. 250 * @ap: QMP message arguments 251 * 252 * Sends a QMP message to QEMU with fds and returns the response. 253 */ 254 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num, 255 const char *fmt, va_list ap) 256 G_GNUC_PRINTF(4, 0); 257 #endif /* _WIN32 */ 258 259 /** 260 * qtest_vqmp: 261 * @s: #QTestState instance to operate on. 262 * @fmt: QMP message to send to QEMU, formatted like 263 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 264 * supported after '%'. 265 * @ap: QMP message arguments 266 * 267 * Sends a QMP message to QEMU and returns the response. 268 */ 269 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap) 270 G_GNUC_PRINTF(2, 0); 271 272 #ifndef _WIN32 273 /** 274 * qtest_qmp_vsend_fds: 275 * @s: #QTestState instance to operate on. 276 * @fds: array of file descriptors 277 * @fds_num: number of elements in @fds 278 * @fmt: QMP message to send to QEMU, formatted like 279 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 280 * supported after '%'. 281 * @ap: QMP message arguments 282 * 283 * Sends a QMP message to QEMU and leaves the response in the stream. 284 */ 285 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num, 286 const char *fmt, va_list ap) 287 G_GNUC_PRINTF(4, 0); 288 #endif /* _WIN32 */ 289 290 /** 291 * qtest_qmp_vsend: 292 * @s: #QTestState instance to operate on. 293 * @fmt: QMP message to send to QEMU, formatted like 294 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 295 * supported after '%'. 296 * @ap: QMP message arguments 297 * 298 * Sends a QMP message to QEMU and leaves the response in the stream. 299 */ 300 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap) 301 G_GNUC_PRINTF(2, 0); 302 303 /** 304 * qtest_qmp_receive_dict: 305 * @s: #QTestState instance to operate on. 306 * 307 * Reads a QMP message from QEMU and returns the response. 308 */ 309 QDict *qtest_qmp_receive_dict(QTestState *s); 310 311 /** 312 * qtest_qmp_receive: 313 * @s: #QTestState instance to operate on. 314 * 315 * Reads a QMP message from QEMU and returns the response. 316 * 317 * If a callback is registered with qtest_qmp_set_event_callback, 318 * it will be invoked for every event seen, otherwise events 319 * will be buffered until a call to one of the qtest_qmp_eventwait 320 * family of functions. 321 */ 322 QDict *qtest_qmp_receive(QTestState *s); 323 324 /* 325 * QTestQMPEventCallback: 326 * @s: #QTestState instance event was received on 327 * @name: name of the event type 328 * @event: #QDict for the event details 329 * @opaque: opaque data from time of callback registration 330 * 331 * This callback will be invoked whenever an event is received. 332 * If the callback returns true the event will be consumed, 333 * otherwise it will be put on the list of pending events. 334 * Pending events can be later handled by calling either 335 * qtest_qmp_eventwait or qtest_qmp_eventwait_ref. 336 * 337 * Return: true to consume the event, false to let it be queued 338 */ 339 typedef bool (*QTestQMPEventCallback)(QTestState *s, const char *name, 340 QDict *event, void *opaque); 341 342 /** 343 * qtest_qmp_set_event_callback: 344 * @s: #QTestSTate instance to operate on 345 * @cb: callback to invoke for events 346 * @opaque: data to pass to @cb 347 * 348 * Register a callback to be invoked whenever an event arrives 349 */ 350 void qtest_qmp_set_event_callback(QTestState *s, 351 QTestQMPEventCallback cb, void *opaque); 352 353 /** 354 * qtest_qmp_eventwait: 355 * @s: #QTestState instance to operate on. 356 * @event: event to wait for. 357 * 358 * Continuously polls for QMP responses until it receives the desired event. 359 * 360 * Any callback registered with qtest_qmp_set_event_callback will 361 * be invoked for every event seen. 362 */ 363 void qtest_qmp_eventwait(QTestState *s, const char *event); 364 365 /** 366 * qtest_qmp_eventwait_ref: 367 * @s: #QTestState instance to operate on. 368 * @event: event to wait for. 369 * 370 * Continuously polls for QMP responses until it receives the desired event. 371 * 372 * Any callback registered with qtest_qmp_set_event_callback will 373 * be invoked for every event seen. 374 * 375 * Returns a copy of the event for further investigation. 376 */ 377 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event); 378 379 /** 380 * qtest_qmp_event_ref: 381 * @s: #QTestState instance to operate on. 382 * @event: event to return. 383 * 384 * Removes non-matching events from the buffer that was set by 385 * qtest_qmp_receive, until an event bearing the given name is found, 386 * and returns it. 387 * If no event matches, clears the buffer and returns NULL. 388 * 389 */ 390 QDict *qtest_qmp_event_ref(QTestState *s, const char *event); 391 392 /** 393 * qtest_hmp: 394 * @s: #QTestState instance to operate on. 395 * @fmt: HMP command to send to QEMU, formats arguments like sprintf(). 396 * 397 * Send HMP command to QEMU via QMP's human-monitor-command. 398 * QMP events are discarded. 399 * 400 * Returns: the command's output. The caller should g_free() it. 401 */ 402 char *qtest_hmp(QTestState *s, const char *fmt, ...) G_GNUC_PRINTF(2, 3); 403 404 /** 405 * qtest_vhmp: 406 * @s: #QTestState instance to operate on. 407 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf(). 408 * @ap: HMP command arguments 409 * 410 * Send HMP command to QEMU via QMP's human-monitor-command. 411 * QMP events are discarded. 412 * 413 * Returns: the command's output. The caller should g_free() it. 414 */ 415 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap) 416 G_GNUC_PRINTF(2, 0); 417 418 void qtest_module_load(QTestState *s, const char *prefix, const char *libname); 419 420 /** 421 * qtest_get_irq: 422 * @s: #QTestState instance to operate on. 423 * @num: Interrupt to observe. 424 * 425 * Returns: The level of the @num interrupt. 426 */ 427 bool qtest_get_irq(QTestState *s, int num); 428 429 /** 430 * qtest_irq_intercept_in: 431 * @s: #QTestState instance to operate on. 432 * @string: QOM path of a device. 433 * 434 * Associate qtest irqs with the GPIO-in pins of the device 435 * whose path is specified by @string. 436 */ 437 void qtest_irq_intercept_in(QTestState *s, const char *string); 438 439 /** 440 * qtest_irq_intercept_out: 441 * @s: #QTestState instance to operate on. 442 * @string: QOM path of a device. 443 * 444 * Associate qtest irqs with the GPIO-out pins of the device 445 * whose path is specified by @string. 446 */ 447 void qtest_irq_intercept_out(QTestState *s, const char *string); 448 449 /** 450 * qtest_irq_intercept_out_named: 451 * @s: #QTestState instance to operate on. 452 * @qom_path: QOM path of a device. 453 * @name: Name of the GPIO out pin 454 * 455 * Associate a qtest irq with the named GPIO-out pin of the device 456 * whose path is specified by @string and whose name is @name. 457 */ 458 void qtest_irq_intercept_out_named(QTestState *s, const char *qom_path, const char *name); 459 460 /** 461 * qtest_set_irq_in: 462 * @s: QTestState instance to operate on. 463 * @string: QOM path of a device 464 * @name: IRQ name 465 * @irq: IRQ number 466 * @level: IRQ level 467 * 468 * Force given device/irq GPIO-in pin to the given level. 469 */ 470 void qtest_set_irq_in(QTestState *s, const char *string, const char *name, 471 int irq, int level); 472 473 /** 474 * qtest_outb: 475 * @s: #QTestState instance to operate on. 476 * @addr: I/O port to write to. 477 * @value: Value being written. 478 * 479 * Write an 8-bit value to an I/O port. 480 */ 481 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value); 482 483 /** 484 * qtest_outw: 485 * @s: #QTestState instance to operate on. 486 * @addr: I/O port to write to. 487 * @value: Value being written. 488 * 489 * Write a 16-bit value to an I/O port. 490 */ 491 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value); 492 493 /** 494 * qtest_outl: 495 * @s: #QTestState instance to operate on. 496 * @addr: I/O port to write to. 497 * @value: Value being written. 498 * 499 * Write a 32-bit value to an I/O port. 500 */ 501 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value); 502 503 /** 504 * qtest_inb: 505 * @s: #QTestState instance to operate on. 506 * @addr: I/O port to read from. 507 * 508 * Returns an 8-bit value from an I/O port. 509 */ 510 uint8_t qtest_inb(QTestState *s, uint16_t addr); 511 512 /** 513 * qtest_inw: 514 * @s: #QTestState instance to operate on. 515 * @addr: I/O port to read from. 516 * 517 * Returns a 16-bit value from an I/O port. 518 */ 519 uint16_t qtest_inw(QTestState *s, uint16_t addr); 520 521 /** 522 * qtest_inl: 523 * @s: #QTestState instance to operate on. 524 * @addr: I/O port to read from. 525 * 526 * Returns a 32-bit value from an I/O port. 527 */ 528 uint32_t qtest_inl(QTestState *s, uint16_t addr); 529 530 /** 531 * qtest_writeb: 532 * @s: #QTestState instance to operate on. 533 * @addr: Guest address to write to. 534 * @value: Value being written. 535 * 536 * Writes an 8-bit value to memory. 537 */ 538 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value); 539 540 /** 541 * qtest_writew: 542 * @s: #QTestState instance to operate on. 543 * @addr: Guest address to write to. 544 * @value: Value being written. 545 * 546 * Writes a 16-bit value to memory. 547 */ 548 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value); 549 550 /** 551 * qtest_writel: 552 * @s: #QTestState instance to operate on. 553 * @addr: Guest address to write to. 554 * @value: Value being written. 555 * 556 * Writes a 32-bit value to memory. 557 */ 558 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value); 559 560 /** 561 * qtest_writeq: 562 * @s: #QTestState instance to operate on. 563 * @addr: Guest address to write to. 564 * @value: Value being written. 565 * 566 * Writes a 64-bit value to memory. 567 */ 568 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value); 569 570 /** 571 * qtest_readb: 572 * @s: #QTestState instance to operate on. 573 * @addr: Guest address to read from. 574 * 575 * Reads an 8-bit value from memory. 576 * 577 * Returns: Value read. 578 */ 579 uint8_t qtest_readb(QTestState *s, uint64_t addr); 580 581 /** 582 * qtest_readw: 583 * @s: #QTestState instance to operate on. 584 * @addr: Guest address to read from. 585 * 586 * Reads a 16-bit value from memory. 587 * 588 * Returns: Value read. 589 */ 590 uint16_t qtest_readw(QTestState *s, uint64_t addr); 591 592 /** 593 * qtest_readl: 594 * @s: #QTestState instance to operate on. 595 * @addr: Guest address to read from. 596 * 597 * Reads a 32-bit value from memory. 598 * 599 * Returns: Value read. 600 */ 601 uint32_t qtest_readl(QTestState *s, uint64_t addr); 602 603 /** 604 * qtest_readq: 605 * @s: #QTestState instance to operate on. 606 * @addr: Guest address to read from. 607 * 608 * Reads a 64-bit value from memory. 609 * 610 * Returns: Value read. 611 */ 612 uint64_t qtest_readq(QTestState *s, uint64_t addr); 613 614 /** 615 * qtest_memread: 616 * @s: #QTestState instance to operate on. 617 * @addr: Guest address to read from. 618 * @data: Pointer to where memory contents will be stored. 619 * @size: Number of bytes to read. 620 * 621 * Read guest memory into a buffer. 622 */ 623 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size); 624 625 /** 626 * qtest_rtas_call: 627 * @s: #QTestState instance to operate on. 628 * @name: name of the command to call. 629 * @nargs: Number of args. 630 * @args: Guest address to read args from. 631 * @nret: Number of return value. 632 * @ret: Guest address to write return values to. 633 * 634 * Call an RTAS function 635 */ 636 uint64_t qtest_rtas_call(QTestState *s, const char *name, 637 uint32_t nargs, uint64_t args, 638 uint32_t nret, uint64_t ret); 639 640 /** 641 * qtest_csr_call: 642 * @s: #QTestState instance to operate on. 643 * @name: name of the command to call. 644 * @cpu: hart number. 645 * @csr: CSR number. 646 * @val: Value for reading/writing. 647 * 648 * Call an RISC-V CSR read/write function 649 */ 650 uint64_t qtest_csr_call(QTestState *s, const char *name, 651 uint64_t cpu, int csr, 652 uint64_t *val); 653 654 /** 655 * qtest_bufread: 656 * @s: #QTestState instance to operate on. 657 * @addr: Guest address to read from. 658 * @data: Pointer to where memory contents will be stored. 659 * @size: Number of bytes to read. 660 * 661 * Read guest memory into a buffer and receive using a base64 encoding. 662 */ 663 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size); 664 665 /** 666 * qtest_memwrite: 667 * @s: #QTestState instance to operate on. 668 * @addr: Guest address to write to. 669 * @data: Pointer to the bytes that will be written to guest memory. 670 * @size: Number of bytes to write. 671 * 672 * Write a buffer to guest memory. 673 */ 674 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size); 675 676 /** 677 * qtest_bufwrite: 678 * @s: #QTestState instance to operate on. 679 * @addr: Guest address to write to. 680 * @data: Pointer to the bytes that will be written to guest memory. 681 * @size: Number of bytes to write. 682 * 683 * Write a buffer to guest memory and transmit using a base64 encoding. 684 */ 685 void qtest_bufwrite(QTestState *s, uint64_t addr, 686 const void *data, size_t size); 687 688 /** 689 * qtest_memset: 690 * @s: #QTestState instance to operate on. 691 * @addr: Guest address to write to. 692 * @patt: Byte pattern to fill the guest memory region with. 693 * @size: Number of bytes to write. 694 * 695 * Write a pattern to guest memory. 696 */ 697 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size); 698 699 /** 700 * qtest_clock_step_next: 701 * @s: #QTestState instance to operate on. 702 * 703 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline. 704 * 705 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds. 706 */ 707 int64_t qtest_clock_step_next(QTestState *s); 708 709 /** 710 * qtest_clock_step: 711 * @s: QTestState instance to operate on. 712 * @step: Number of nanoseconds to advance the clock by. 713 * 714 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds. 715 * 716 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds. 717 */ 718 int64_t qtest_clock_step(QTestState *s, int64_t step); 719 720 /** 721 * qtest_clock_set: 722 * @s: QTestState instance to operate on. 723 * @val: Nanoseconds value to advance the clock to. 724 * 725 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched. 726 * 727 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds. 728 */ 729 int64_t qtest_clock_set(QTestState *s, int64_t val); 730 731 /** 732 * qtest_big_endian: 733 * @s: QTestState instance to operate on. 734 * 735 * Returns: True if the architecture under test has a big endian configuration. 736 */ 737 bool qtest_big_endian(QTestState *s); 738 739 /** 740 * qtest_get_arch: 741 * 742 * Returns: The architecture for the QEMU executable under test. 743 */ 744 const char *qtest_get_arch(void); 745 746 /** 747 * qtest_has_accel: 748 * @accel_name: Accelerator name to check for. 749 * 750 * Returns: true if the accelerator is built in. 751 */ 752 bool qtest_has_accel(const char *accel_name); 753 754 /** 755 * qtest_add_func: 756 * @str: Test case path. 757 * @fn: Test case function 758 * 759 * Add a GTester testcase with the given name and function. 760 * The path is prefixed with the architecture under test, as 761 * returned by qtest_get_arch(). 762 */ 763 void qtest_add_func(const char *str, void (*fn)(void)); 764 765 /** 766 * qtest_add_data_func: 767 * @str: Test case path. 768 * @data: Test case data 769 * @fn: Test case function 770 * 771 * Add a GTester testcase with the given name, data and function. 772 * The path is prefixed with the architecture under test, as 773 * returned by qtest_get_arch(). 774 */ 775 void qtest_add_data_func(const char *str, const void *data, 776 void (*fn)(const void *)); 777 778 /** 779 * qtest_add_data_func_full: 780 * @str: Test case path. 781 * @data: Test case data 782 * @fn: Test case function 783 * @data_free_func: GDestroyNotify for data 784 * 785 * Add a GTester testcase with the given name, data and function. 786 * The path is prefixed with the architecture under test, as 787 * returned by qtest_get_arch(). 788 * 789 * @data is passed to @data_free_func() on test completion. 790 */ 791 void qtest_add_data_func_full(const char *str, void *data, 792 void (*fn)(const void *), 793 GDestroyNotify data_free_func); 794 795 /** 796 * qtest_add: 797 * @testpath: Test case path 798 * @Fixture: Fixture type 799 * @tdata: Test case data 800 * @fsetup: Test case setup function 801 * @ftest: Test case function 802 * @fteardown: Test case teardown function 803 * 804 * Add a GTester testcase with the given name, data and functions. 805 * The path is prefixed with the architecture under test, as 806 * returned by qtest_get_arch(). 807 */ 808 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \ 809 do { \ 810 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \ 811 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \ 812 g_free(path); \ 813 } while (0) 814 815 /** 816 * qtest_add_abrt_handler: 817 * @fn: Handler function 818 * @data: Argument that is passed to the handler 819 * 820 * Add a handler function that is invoked on SIGABRT. This can be used to 821 * terminate processes and perform other cleanup. The handler can be removed 822 * with qtest_remove_abrt_handler(). 823 */ 824 void qtest_add_abrt_handler(GHookFunc fn, const void *data); 825 826 /** 827 * qtest_remove_abrt_handler: 828 * @data: Argument previously passed to qtest_add_abrt_handler() 829 * 830 * Remove an abrt handler that was previously added with 831 * qtest_add_abrt_handler(). 832 */ 833 void qtest_remove_abrt_handler(void *data); 834 835 /** 836 * qtest_vqmp_assert_success_ref: 837 * @qts: QTestState instance to operate on 838 * @fmt: QMP message to send to qemu, formatted like 839 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 840 * supported after '%'. 841 * @args: variable arguments for @fmt 842 * 843 * Sends a QMP message to QEMU, asserts that a 'return' key is present in 844 * the response, and returns the response. 845 */ 846 QDict *qtest_vqmp_assert_success_ref(QTestState *qts, 847 const char *fmt, va_list args) 848 G_GNUC_PRINTF(2, 0); 849 850 /** 851 * qtest_vqmp_assert_success: 852 * @qts: QTestState instance to operate on 853 * @fmt: QMP message to send to qemu, formatted like 854 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 855 * supported after '%'. 856 * @args: variable arguments for @fmt 857 * 858 * Sends a QMP message to QEMU and asserts that a 'return' key is present in 859 * the response. 860 */ 861 void qtest_vqmp_assert_success(QTestState *qts, 862 const char *fmt, va_list args) 863 G_GNUC_PRINTF(2, 0); 864 865 #ifndef _WIN32 866 /** 867 * qtest_vqmp_fds_assert_success_ref: 868 * @qts: QTestState instance to operate on 869 * @fds: the file descriptors to send 870 * @nfds: number of @fds to send 871 * @fmt: QMP message to send to qemu, formatted like 872 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 873 * supported after '%'. 874 * @args: variable arguments for @fmt 875 * 876 * Sends a QMP message with file descriptors to QEMU, 877 * asserts that a 'return' key is present in the response, 878 * and returns the response. 879 */ 880 QDict *qtest_vqmp_fds_assert_success_ref(QTestState *qts, int *fds, size_t nfds, 881 const char *fmt, va_list args) 882 G_GNUC_PRINTF(4, 0); 883 884 /** 885 * qtest_vqmp_fds_assert_success: 886 * @qts: QTestState instance to operate on 887 * @fds: the file descriptors to send 888 * @nfds: number of @fds to send 889 * @fmt: QMP message to send to qemu, formatted like 890 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 891 * supported after '%'. 892 * @args: variable arguments for @fmt 893 * 894 * Sends a QMP message with file descriptors to QEMU and 895 * asserts that a 'return' key is present in the response. 896 */ 897 void qtest_vqmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds, 898 const char *fmt, va_list args) 899 G_GNUC_PRINTF(4, 0); 900 #endif /* !_WIN32 */ 901 902 /** 903 * qtest_qmp_assert_failure_ref: 904 * @qts: QTestState instance to operate on 905 * @fmt: QMP message to send to qemu, formatted like 906 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 907 * supported after '%'. 908 * 909 * Sends a QMP message to QEMU, asserts that an 'error' key is present in 910 * the response, and returns the response. 911 */ 912 QDict *qtest_qmp_assert_failure_ref(QTestState *qts, const char *fmt, ...) 913 G_GNUC_PRINTF(2, 3); 914 915 /** 916 * qtest_vqmp_assert_failure_ref: 917 * @qts: QTestState instance to operate on 918 * @fmt: QMP message to send to qemu, formatted like 919 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 920 * supported after '%'. 921 * @args: variable arguments for @fmt 922 * 923 * Sends a QMP message to QEMU, asserts that an 'error' key is present in 924 * the response, and returns the response. 925 */ 926 QDict *qtest_vqmp_assert_failure_ref(QTestState *qts, 927 const char *fmt, va_list args) 928 G_GNUC_PRINTF(2, 0); 929 930 /** 931 * qtest_qmp_assert_success_ref: 932 * @qts: QTestState instance to operate on 933 * @fmt: QMP message to send to qemu, formatted like 934 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 935 * supported after '%'. 936 * 937 * Sends a QMP message to QEMU, asserts that a 'return' key is present in 938 * the response, and returns the response. 939 */ 940 QDict *qtest_qmp_assert_success_ref(QTestState *qts, const char *fmt, ...) 941 G_GNUC_PRINTF(2, 3); 942 943 /** 944 * qtest_qmp_assert_success: 945 * @qts: QTestState instance to operate on 946 * @fmt: QMP message to send to qemu, formatted like 947 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 948 * supported after '%'. 949 * 950 * Sends a QMP message to QEMU and asserts that a 'return' key is present in 951 * the response. 952 */ 953 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...) 954 G_GNUC_PRINTF(2, 3); 955 956 #ifndef _WIN32 957 /** 958 * qtest_qmp_fds_assert_success_ref: 959 * @qts: QTestState instance to operate on 960 * @fds: the file descriptors to send 961 * @nfds: number of @fds to send 962 * @fmt: QMP message to send to qemu, formatted like 963 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 964 * supported after '%'. 965 * 966 * Sends a QMP message with file descriptors to QEMU, 967 * asserts that a 'return' key is present in the response, 968 * and returns the response. 969 */ 970 QDict *qtest_qmp_fds_assert_success_ref(QTestState *qts, int *fds, size_t nfds, 971 const char *fmt, ...) 972 G_GNUC_PRINTF(4, 5); 973 974 /** 975 * qtest_qmp_fds_assert_success: 976 * @qts: QTestState instance to operate on 977 * @fds: the file descriptors to send 978 * @nfds: number of @fds to send 979 * @fmt: QMP message to send to qemu, formatted like 980 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 981 * supported after '%'. 982 * 983 * Sends a QMP message with file descriptors to QEMU and 984 * asserts that a 'return' key is present in the response. 985 */ 986 void qtest_qmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds, 987 const char *fmt, ...) 988 G_GNUC_PRINTF(4, 5); 989 #endif /* !_WIN32 */ 990 991 /** 992 * qtest_cb_for_every_machine: 993 * @cb: Pointer to the callback function 994 * @skip_old_versioned: true if versioned old machine types should be skipped 995 * 996 * Call a callback function for every name of all available machines. 997 */ 998 void qtest_cb_for_every_machine(void (*cb)(const char *machine), 999 bool skip_old_versioned); 1000 1001 /** 1002 * qtest_resolve_machine_alias: 1003 * @var: Environment variable from where to take the QEMU binary 1004 * @alias: The alias to resolve 1005 * 1006 * Returns: the machine type corresponding to the alias if any, 1007 * otherwise NULL. 1008 */ 1009 char *qtest_resolve_machine_alias(const char *var, const char *alias); 1010 1011 /** 1012 * qtest_has_machine: 1013 * @machine: The machine to look for 1014 * 1015 * Returns: true if the machine is available in the target binary. 1016 */ 1017 bool qtest_has_machine(const char *machine); 1018 1019 /** 1020 * qtest_has_machine_with_env: 1021 * @var: Environment variable from where to take the QEMU binary 1022 * @machine: The machine to look for 1023 * 1024 * Returns: true if the machine is available in the specified binary. 1025 */ 1026 bool qtest_has_machine_with_env(const char *var, const char *machine); 1027 1028 /** 1029 * qtest_has_cpu_model: 1030 * @cpu: The cpu to look for 1031 * 1032 * Returns: true if the cpu is available in the target binary. 1033 */ 1034 bool qtest_has_cpu_model(const char *cpu); 1035 1036 /** 1037 * qtest_has_device: 1038 * @device: The device to look for 1039 * 1040 * Returns: true if the device is available in the target binary. 1041 */ 1042 bool qtest_has_device(const char *device); 1043 1044 /** 1045 * qtest_qmp_device_add_qdict: 1046 * @qts: QTestState instance to operate on 1047 * @drv: Name of the device that should be added 1048 * @arguments: QDict with properties for the device to initialize 1049 * 1050 * Generic hot-plugging test via the device_add QMP command with properties 1051 * supplied in form of QDict. Use NULL for empty properties list. 1052 */ 1053 void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv, 1054 const QDict *arguments); 1055 1056 /** 1057 * qtest_qmp_device_add: 1058 * @qts: QTestState instance to operate on 1059 * @driver: Name of the device that should be added 1060 * @id: Identification string 1061 * @fmt: QMP message to send to qemu, formatted like 1062 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's 1063 * supported after '%'. 1064 * 1065 * Generic hot-plugging test via the device_add QMP command. 1066 */ 1067 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id, 1068 const char *fmt, ...) G_GNUC_PRINTF(4, 5); 1069 1070 /** 1071 * qtest_qmp_add_client: 1072 * @qts: QTestState instance to operate on 1073 * @protocol: the protocol to add to 1074 * @fd: the client file-descriptor 1075 * 1076 * Call QMP ``getfd`` (on Windows ``get-win32-socket``) followed by 1077 * ``add_client`` with the given @fd. 1078 */ 1079 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd); 1080 1081 /** 1082 * qtest_qmp_device_del_send: 1083 * @qts: QTestState instance to operate on 1084 * @id: Identification string 1085 * 1086 * Generic hot-unplugging test via the device_del QMP command. 1087 */ 1088 void qtest_qmp_device_del_send(QTestState *qts, const char *id); 1089 1090 /** 1091 * qtest_qmp_device_del: 1092 * @qts: QTestState instance to operate on 1093 * @id: Identification string 1094 * 1095 * Generic hot-unplugging test via the device_del QMP command. 1096 * Waiting for command completion event. 1097 */ 1098 void qtest_qmp_device_del(QTestState *qts, const char *id); 1099 1100 /** 1101 * qtest_probe_child: 1102 * @s: QTestState instance to operate on. 1103 * 1104 * Returns: true if the child is still alive. 1105 */ 1106 bool qtest_probe_child(QTestState *s); 1107 1108 /** 1109 * qtest_set_expected_status: 1110 * @s: QTestState instance to operate on. 1111 * @status: an expected exit status. 1112 * 1113 * Set expected exit status of the child. 1114 */ 1115 void qtest_set_expected_status(QTestState *s, int status); 1116 1117 QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch, 1118 void (*send)(void*, const char*)); 1119 1120 void qtest_client_inproc_recv(void *opaque, const char *str); 1121 1122 /** 1123 * qtest_qom_set_bool: 1124 * @s: QTestState instance to operate on. 1125 * @path: Path to the property being set. 1126 * @property: Property being set. 1127 * @value: Value to set the property. 1128 * 1129 * Set the property with passed in value. 1130 */ 1131 void qtest_qom_set_bool(QTestState *s, const char *path, const char *property, 1132 bool value); 1133 1134 /** 1135 * qtest_qom_get_bool: 1136 * @s: QTestState instance to operate on. 1137 * @path: Path to the property being retrieved. 1138 * @property: Property from where the value is being retrieved. 1139 * 1140 * Returns: Value retrieved from property. 1141 */ 1142 bool qtest_qom_get_bool(QTestState *s, const char *path, const char *property); 1143 1144 /** 1145 * qtest_pid: 1146 * @s: QTestState instance to operate on. 1147 * 1148 * Returns: the PID of the QEMU process, or <= 0 1149 */ 1150 pid_t qtest_pid(QTestState *s); 1151 1152 /** 1153 * have_qemu_img: 1154 * 1155 * Returns: true if "qemu-img" is available. 1156 */ 1157 bool have_qemu_img(void); 1158 1159 /** 1160 * mkimg: 1161 * @file: File name of the image that should be created 1162 * @fmt: Format, e.g. "qcow2" or "raw" 1163 * @size_mb: Size of the image in megabytes 1164 * 1165 * Create a disk image with qemu-img. Note that the QTEST_QEMU_IMG 1166 * environment variable must point to the qemu-img file. 1167 * 1168 * Returns: true if the image has been created successfully. 1169 */ 1170 bool mkimg(const char *file, const char *fmt, unsigned size_mb); 1171 1172 #endif 1173