4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
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.
20 #include "qapi/qmp/qobject.h"
21 #include "qapi/qmp/qdict.h"
24 typedef struct QTestState QTestState
;
28 * @fmt: Format for creating other arguments to pass to QEMU, formatted
31 * Convenience wrapper around qtest_init().
33 * Returns: #QTestState instance.
35 QTestState
*qtest_initf(const char *fmt
, ...) G_GNUC_PRINTF(1, 2);
39 * @fmt: Format for creating other arguments to pass to QEMU, formatted
41 * @ap: Format arguments.
43 * Convenience wrapper around qtest_init().
45 * Returns: #QTestState instance.
47 QTestState
*qtest_vinitf(const char *fmt
, va_list ap
) G_GNUC_PRINTF(1, 0);
51 * @extra_args: other arguments to pass to QEMU. CAUTION: these
52 * arguments are subject to word splitting and shell evaluation.
54 * Returns: #QTestState instance.
56 QTestState
*qtest_init(const char *extra_args
);
59 * qtest_init_without_qmp_handshake:
60 * @extra_args: other arguments to pass to QEMU. CAUTION: these
61 * arguments are subject to word splitting and shell evaluation.
63 * Returns: #QTestState instance.
65 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
);
68 * qtest_init_with_serial:
69 * @extra_args: other arguments to pass to QEMU. CAUTION: these
70 * arguments are subject to word splitting and shell evaluation.
71 * @sock_fd: pointer to store the socket file descriptor for
72 * connection with serial.
74 * Returns: #QTestState instance.
76 QTestState
*qtest_init_with_serial(const char *extra_args
, int *sock_fd
);
80 * @s: #QTestState instance to operate on.
82 * Wait for the QEMU process to terminate. It is safe to call this function
85 void qtest_wait_qemu(QTestState
*s
);
89 * @s: #QTestState instance to operate on.
91 * Kill the QEMU process and wait for it to terminate. It is safe to call this
92 * function multiple times. Normally qtest_quit() is used instead because it
93 * also frees QTestState. Use qtest_kill_qemu() when you just want to kill QEMU
94 * and qtest_quit() will be called later.
96 void qtest_kill_qemu(QTestState
*s
);
100 * @s: #QTestState instance to operate on.
102 * Shut down the QEMU process associated to @s.
104 void qtest_quit(QTestState
*s
);
109 * @s: #QTestState instance to operate on.
110 * @fds: array of file descriptors
111 * @fds_num: number of elements in @fds
112 * @fmt: QMP message to send to qemu, formatted like
113 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
114 * supported after '%'.
116 * Sends a QMP message to QEMU with fds and returns the response.
118 QDict
*qtest_qmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
119 const char *fmt
, ...)
125 * @s: #QTestState instance to operate on.
126 * @fmt: QMP message to send to qemu, formatted like
127 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
128 * supported after '%'.
130 * Sends a QMP message to QEMU and returns the response.
132 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
137 * @s: #QTestState instance to operate on.
138 * @fmt: QMP message to send to qemu, formatted like
139 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
140 * supported after '%'.
142 * Sends a QMP message to QEMU and leaves the response in the stream.
144 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
148 * qtest_qmp_send_raw:
149 * @s: #QTestState instance to operate on.
150 * @fmt: text to send, formatted like sprintf()
152 * Sends text to the QMP monitor verbatim. Need not be valid JSON;
153 * this is useful for negative tests.
155 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
159 * qtest_socket_server:
160 * @socket_path: the UNIX domain socket path
162 * Create and return a listen socket file descriptor, or abort on failure.
164 int qtest_socket_server(const char *socket_path
);
169 * @s: #QTestState instance to operate on.
170 * @fds: array of file descriptors
171 * @fds_num: number of elements in @fds
172 * @fmt: QMP message to send to QEMU, formatted like
173 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
174 * supported after '%'.
175 * @ap: QMP message arguments
177 * Sends a QMP message to QEMU with fds and returns the response.
179 QDict
*qtest_vqmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
180 const char *fmt
, va_list ap
)
186 * @s: #QTestState instance to operate on.
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 * @ap: QMP message arguments
192 * Sends a QMP message to QEMU and returns the response.
194 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
199 * qtest_qmp_vsend_fds:
200 * @s: #QTestState instance to operate on.
201 * @fds: array of file descriptors
202 * @fds_num: number of elements in @fds
203 * @fmt: QMP message to send to QEMU, formatted like
204 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
205 * supported after '%'.
206 * @ap: QMP message arguments
208 * Sends a QMP message to QEMU and leaves the response in the stream.
210 void qtest_qmp_vsend_fds(QTestState
*s
, int *fds
, size_t fds_num
,
211 const char *fmt
, va_list ap
)
217 * @s: #QTestState instance to operate on.
218 * @fmt: QMP message to send to QEMU, formatted like
219 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
220 * supported after '%'.
221 * @ap: QMP message arguments
223 * Sends a QMP message to QEMU and leaves the response in the stream.
225 void qtest_qmp_vsend(QTestState
*s
, const char *fmt
, va_list ap
)
229 * qtest_qmp_receive_dict:
230 * @s: #QTestState instance to operate on.
232 * Reads a QMP message from QEMU and returns the response.
234 QDict
*qtest_qmp_receive_dict(QTestState
*s
);
238 * @s: #QTestState instance to operate on.
240 * Reads a QMP message from QEMU and returns the response.
241 * Buffers all the events received meanwhile, until a
242 * call to qtest_qmp_eventwait
244 QDict
*qtest_qmp_receive(QTestState
*s
);
247 * qtest_qmp_eventwait:
248 * @s: #QTestState instance to operate on.
249 * @event: event to wait for.
251 * Continuously polls for QMP responses until it receives the desired event.
253 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
256 * qtest_qmp_eventwait_ref:
257 * @s: #QTestState instance to operate on.
258 * @event: event to wait for.
260 * Continuously polls for QMP responses until it receives the desired event.
261 * Returns a copy of the event for further investigation.
263 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
266 * qtest_qmp_event_ref:
267 * @s: #QTestState instance to operate on.
268 * @event: event to return.
270 * Removes non-matching events from the buffer that was set by
271 * qtest_qmp_receive, until an event bearing the given name is found,
273 * If no event matches, clears the buffer and returns NULL.
276 QDict
*qtest_qmp_event_ref(QTestState
*s
, const char *event
);
280 * @s: #QTestState instance to operate on.
281 * @fmt: HMP command to send to QEMU, formats arguments like sprintf().
283 * Send HMP command to QEMU via QMP's human-monitor-command.
284 * QMP events are discarded.
286 * Returns: the command's output. The caller should g_free() it.
288 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...) G_GNUC_PRINTF(2, 3);
292 * @s: #QTestState instance to operate on.
293 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
294 * @ap: HMP command arguments
296 * Send HMP command to QEMU via QMP's human-monitor-command.
297 * QMP events are discarded.
299 * Returns: the command's output. The caller should g_free() it.
301 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
304 void qtest_module_load(QTestState
*s
, const char *prefix
, const char *libname
);
308 * @s: #QTestState instance to operate on.
309 * @num: Interrupt to observe.
311 * Returns: The level of the @num interrupt.
313 bool qtest_get_irq(QTestState
*s
, int num
);
316 * qtest_irq_intercept_in:
317 * @s: #QTestState instance to operate on.
318 * @string: QOM path of a device.
320 * Associate qtest irqs with the GPIO-in pins of the device
321 * whose path is specified by @string.
323 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
326 * qtest_irq_intercept_out:
327 * @s: #QTestState instance to operate on.
328 * @string: QOM path of a device.
330 * Associate qtest irqs with the GPIO-out pins of the device
331 * whose path is specified by @string.
333 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
337 * @s: QTestState instance to operate on.
338 * @string: QOM path of a device
343 * Force given device/irq GPIO-in pin to the given level.
345 void qtest_set_irq_in(QTestState
*s
, const char *string
, const char *name
,
350 * @s: #QTestState instance to operate on.
351 * @addr: I/O port to write to.
352 * @value: Value being written.
354 * Write an 8-bit value to an I/O port.
356 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
360 * @s: #QTestState instance to operate on.
361 * @addr: I/O port to write to.
362 * @value: Value being written.
364 * Write a 16-bit value to an I/O port.
366 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
370 * @s: #QTestState instance to operate on.
371 * @addr: I/O port to write to.
372 * @value: Value being written.
374 * Write a 32-bit value to an I/O port.
376 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
380 * @s: #QTestState instance to operate on.
381 * @addr: I/O port to read from.
383 * Returns an 8-bit value from an I/O port.
385 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
389 * @s: #QTestState instance to operate on.
390 * @addr: I/O port to read from.
392 * Returns a 16-bit value from an I/O port.
394 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
398 * @s: #QTestState instance to operate on.
399 * @addr: I/O port to read from.
401 * Returns a 32-bit value from an I/O port.
403 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
407 * @s: #QTestState instance to operate on.
408 * @addr: Guest address to write to.
409 * @value: Value being written.
411 * Writes an 8-bit value to memory.
413 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
417 * @s: #QTestState instance to operate on.
418 * @addr: Guest address to write to.
419 * @value: Value being written.
421 * Writes a 16-bit value to memory.
423 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
427 * @s: #QTestState instance to operate on.
428 * @addr: Guest address to write to.
429 * @value: Value being written.
431 * Writes a 32-bit value to memory.
433 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
437 * @s: #QTestState instance to operate on.
438 * @addr: Guest address to write to.
439 * @value: Value being written.
441 * Writes a 64-bit value to memory.
443 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
447 * @s: #QTestState instance to operate on.
448 * @addr: Guest address to read from.
450 * Reads an 8-bit value from memory.
452 * Returns: Value read.
454 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
458 * @s: #QTestState instance to operate on.
459 * @addr: Guest address to read from.
461 * Reads a 16-bit value from memory.
463 * Returns: Value read.
465 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
469 * @s: #QTestState instance to operate on.
470 * @addr: Guest address to read from.
472 * Reads a 32-bit value from memory.
474 * Returns: Value read.
476 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
480 * @s: #QTestState instance to operate on.
481 * @addr: Guest address to read from.
483 * Reads a 64-bit value from memory.
485 * Returns: Value read.
487 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
491 * @s: #QTestState instance to operate on.
492 * @addr: Guest address to read from.
493 * @data: Pointer to where memory contents will be stored.
494 * @size: Number of bytes to read.
496 * Read guest memory into a buffer.
498 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
502 * @s: #QTestState instance to operate on.
503 * @name: name of the command to call.
504 * @nargs: Number of args.
505 * @args: Guest address to read args from.
506 * @nret: Number of return value.
507 * @ret: Guest address to write return values to.
509 * Call an RTAS function
511 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
512 uint32_t nargs
, uint64_t args
,
513 uint32_t nret
, uint64_t ret
);
517 * @s: #QTestState instance to operate on.
518 * @addr: Guest address to read from.
519 * @data: Pointer to where memory contents will be stored.
520 * @size: Number of bytes to read.
522 * Read guest memory into a buffer and receive using a base64 encoding.
524 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
528 * @s: #QTestState instance to operate on.
529 * @addr: Guest address to write to.
530 * @data: Pointer to the bytes that will be written to guest memory.
531 * @size: Number of bytes to write.
533 * Write a buffer to guest memory.
535 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
539 * @s: #QTestState instance to operate on.
540 * @addr: Guest address to write to.
541 * @data: Pointer to the bytes that will be written to guest memory.
542 * @size: Number of bytes to write.
544 * Write a buffer to guest memory and transmit using a base64 encoding.
546 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
547 const void *data
, size_t size
);
551 * @s: #QTestState instance to operate on.
552 * @addr: Guest address to write to.
553 * @patt: Byte pattern to fill the guest memory region with.
554 * @size: Number of bytes to write.
556 * Write a pattern to guest memory.
558 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
561 * qtest_clock_step_next:
562 * @s: #QTestState instance to operate on.
564 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
566 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
568 int64_t qtest_clock_step_next(QTestState
*s
);
572 * @s: QTestState instance to operate on.
573 * @step: Number of nanoseconds to advance the clock by.
575 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
577 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
579 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
583 * @s: QTestState instance to operate on.
584 * @val: Nanoseconds value to advance the clock to.
586 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
588 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
590 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
594 * @s: QTestState instance to operate on.
596 * Returns: True if the architecture under test has a big endian configuration.
598 bool qtest_big_endian(QTestState
*s
);
603 * Returns: The architecture for the QEMU executable under test.
605 const char *qtest_get_arch(void);
609 * @accel_name: Accelerator name to check for.
611 * Returns: true if the accelerator is built in.
613 bool qtest_has_accel(const char *accel_name
);
617 * @str: Test case path.
618 * @fn: Test case function
620 * Add a GTester testcase with the given name and function.
621 * The path is prefixed with the architecture under test, as
622 * returned by qtest_get_arch().
624 void qtest_add_func(const char *str
, void (*fn
)(void));
627 * qtest_add_data_func:
628 * @str: Test case path.
629 * @data: Test case data
630 * @fn: Test case function
632 * Add a GTester testcase with the given name, data and function.
633 * The path is prefixed with the architecture under test, as
634 * returned by qtest_get_arch().
636 void qtest_add_data_func(const char *str
, const void *data
,
637 void (*fn
)(const void *));
640 * qtest_add_data_func_full:
641 * @str: Test case path.
642 * @data: Test case data
643 * @fn: Test case function
644 * @data_free_func: GDestroyNotify for data
646 * Add a GTester testcase with the given name, data and function.
647 * The path is prefixed with the architecture under test, as
648 * returned by qtest_get_arch().
650 * @data is passed to @data_free_func() on test completion.
652 void qtest_add_data_func_full(const char *str
, void *data
,
653 void (*fn
)(const void *),
654 GDestroyNotify data_free_func
);
658 * @testpath: Test case path
659 * @Fixture: Fixture type
660 * @tdata: Test case data
661 * @fsetup: Test case setup function
662 * @ftest: Test case function
663 * @fteardown: Test case teardown function
665 * Add a GTester testcase with the given name, data and functions.
666 * The path is prefixed with the architecture under test, as
667 * returned by qtest_get_arch().
669 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
671 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
672 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
677 * qtest_add_abrt_handler:
678 * @fn: Handler function
679 * @data: Argument that is passed to the handler
681 * Add a handler function that is invoked on SIGABRT. This can be used to
682 * terminate processes and perform other cleanup. The handler can be removed
683 * with qtest_remove_abrt_handler().
685 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
688 * qtest_remove_abrt_handler:
689 * @data: Argument previously passed to qtest_add_abrt_handler()
691 * Remove an abrt handler that was previously added with
692 * qtest_add_abrt_handler().
694 void qtest_remove_abrt_handler(void *data
);
697 * qtest_qmp_assert_success:
698 * @qts: QTestState instance to operate on
699 * @fmt: QMP message to send to qemu, formatted like
700 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
701 * supported after '%'.
703 * Sends a QMP message to QEMU and asserts that a 'return' key is present in
706 void qtest_qmp_assert_success(QTestState
*qts
, const char *fmt
, ...)
710 * qtest_cb_for_every_machine:
711 * @cb: Pointer to the callback function
712 * @skip_old_versioned: true if versioned old machine types should be skipped
714 * Call a callback function for every name of all available machines.
716 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
717 bool skip_old_versioned
);
721 * @machine: The machine to look for
723 * Returns: true if the machine is available in the target binary.
725 bool qtest_has_machine(const char *machine
);
729 * @device: The device to look for
731 * Returns: true if the device is available in the target binary.
733 bool qtest_has_device(const char *device
);
736 * qtest_qmp_device_add_qdict:
737 * @qts: QTestState instance to operate on
738 * @drv: Name of the device that should be added
739 * @arguments: QDict with properties for the device to initialize
741 * Generic hot-plugging test via the device_add QMP command with properties
742 * supplied in form of QDict. Use NULL for empty properties list.
744 void qtest_qmp_device_add_qdict(QTestState
*qts
, const char *drv
,
745 const QDict
*arguments
);
748 * qtest_qmp_device_add:
749 * @qts: QTestState instance to operate on
750 * @driver: Name of the device that should be added
751 * @id: Identification string
752 * @fmt: QMP message to send to qemu, formatted like
753 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
754 * supported after '%'.
756 * Generic hot-plugging test via the device_add QMP command.
758 void qtest_qmp_device_add(QTestState
*qts
, const char *driver
, const char *id
,
759 const char *fmt
, ...) G_GNUC_PRINTF(4, 5);
762 * qtest_qmp_add_client:
763 * @qts: QTestState instance to operate on
764 * @protocol: the protocol to add to
765 * @fd: the client file-descriptor
767 * Call QMP ``getfd`` (on Windows ``get-win32-socket``) followed by
768 * ``add_client`` with the given @fd.
770 void qtest_qmp_add_client(QTestState
*qts
, const char *protocol
, int fd
);
773 * qtest_qmp_device_del_send:
774 * @qts: QTestState instance to operate on
775 * @id: Identification string
777 * Generic hot-unplugging test via the device_del QMP command.
779 void qtest_qmp_device_del_send(QTestState
*qts
, const char *id
);
782 * qtest_qmp_device_del:
783 * @qts: QTestState instance to operate on
784 * @id: Identification string
786 * Generic hot-unplugging test via the device_del QMP command.
787 * Waiting for command completion event.
789 void qtest_qmp_device_del(QTestState
*qts
, const char *id
);
793 * @s: QTestState instance to operate on.
795 * Returns: true if the child is still alive.
797 bool qtest_probe_child(QTestState
*s
);
800 * qtest_set_expected_status:
801 * @s: QTestState instance to operate on.
802 * @status: an expected exit status.
804 * Set expected exit status of the child.
806 void qtest_set_expected_status(QTestState
*s
, int status
);
808 QTestState
*qtest_inproc_init(QTestState
**s
, bool log
, const char* arch
,
809 void (*send
)(void*, const char*));
811 void qtest_client_inproc_recv(void *opaque
, const char *str
);
814 * qtest_qom_set_bool:
815 * @s: QTestState instance to operate on.
816 * @path: Path to the property being set.
817 * @property: Property being set.
818 * @value: Value to set the property.
820 * Set the property with passed in value.
822 void qtest_qom_set_bool(QTestState
*s
, const char *path
, const char *property
,
826 * qtest_qom_get_bool:
827 * @s: QTestState instance to operate on.
828 * @path: Path to the property being retrieved.
829 * @property: Property from where the value is being retrieved.
831 * Returns: Value retrieved from property.
833 bool qtest_qom_get_bool(QTestState
*s
, const char *path
, const char *property
);