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