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