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 * Kill the QEMU process and wait for it to terminate. It is safe to call this
83 * function multiple times. Normally qtest_quit() is used instead because it
84 * also frees QTestState. Use qtest_kill_qemu() when you just want to kill QEMU
85 * and qtest_quit() will be called later.
87 void qtest_kill_qemu(QTestState
*s
);
91 * @s: #QTestState instance to operate on.
93 * Shut down the QEMU process associated to @s.
95 void qtest_quit(QTestState
*s
);
99 * @s: #QTestState instance to operate on.
100 * @fds: array of file descriptors
101 * @fds_num: number of elements in @fds
102 * @fmt: QMP message to send to qemu, formatted like
103 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
104 * supported after '%'.
106 * Sends a QMP message to QEMU with fds and returns the response.
108 QDict
*qtest_qmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
109 const char *fmt
, ...)
114 * @s: #QTestState instance to operate on.
115 * @fmt: QMP message to send to qemu, formatted like
116 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
117 * supported after '%'.
119 * Sends a QMP message to QEMU and returns the response.
121 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
126 * @s: #QTestState instance to operate on.
127 * @fmt: QMP message to send to qemu, formatted like
128 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
129 * supported after '%'.
131 * Sends a QMP message to QEMU and leaves the response in the stream.
133 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
137 * qtest_qmp_send_raw:
138 * @s: #QTestState instance to operate on.
139 * @fmt: text to send, formatted like sprintf()
141 * Sends text to the QMP monitor verbatim. Need not be valid JSON;
142 * this is useful for negative tests.
144 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
148 * qtest_socket_server:
149 * @socket_path: the UNIX domain socket path
151 * Create and return a listen socket file descriptor, or abort on failure.
153 int qtest_socket_server(const char *socket_path
);
157 * @s: #QTestState instance to operate on.
158 * @fds: array of file descriptors
159 * @fds_num: number of elements in @fds
160 * @fmt: QMP message to send to QEMU, formatted like
161 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
162 * supported after '%'.
163 * @ap: QMP message arguments
165 * Sends a QMP message to QEMU with fds and returns the response.
167 QDict
*qtest_vqmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
168 const char *fmt
, va_list ap
)
173 * @s: #QTestState instance to operate on.
174 * @fmt: QMP message to send to QEMU, formatted like
175 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
176 * supported after '%'.
177 * @ap: QMP message arguments
179 * Sends a QMP message to QEMU and returns the response.
181 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
185 * qtest_qmp_vsend_fds:
186 * @s: #QTestState instance to operate on.
187 * @fds: array of file descriptors
188 * @fds_num: number of elements in @fds
189 * @fmt: QMP message to send to QEMU, formatted like
190 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
191 * supported after '%'.
192 * @ap: QMP message arguments
194 * Sends a QMP message to QEMU and leaves the response in the stream.
196 void qtest_qmp_vsend_fds(QTestState
*s
, int *fds
, size_t fds_num
,
197 const char *fmt
, va_list ap
)
202 * @s: #QTestState instance to operate on.
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(QTestState
*s
, const char *fmt
, va_list ap
)
214 * qtest_qmp_receive_dict:
215 * @s: #QTestState instance to operate on.
217 * Reads a QMP message from QEMU and returns the response.
219 QDict
*qtest_qmp_receive_dict(QTestState
*s
);
223 * @s: #QTestState instance to operate on.
225 * Reads a QMP message from QEMU and returns the response.
226 * Buffers all the events received meanwhile, until a
227 * call to qtest_qmp_eventwait
229 QDict
*qtest_qmp_receive(QTestState
*s
);
232 * qtest_qmp_eventwait:
233 * @s: #QTestState instance to operate on.
234 * @event: event to wait for.
236 * Continuously polls for QMP responses until it receives the desired event.
238 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
241 * qtest_qmp_eventwait_ref:
242 * @s: #QTestState instance to operate on.
243 * @event: event to wait for.
245 * Continuously polls for QMP responses until it receives the desired event.
246 * Returns a copy of the event for further investigation.
248 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
251 * qtest_qmp_event_ref:
252 * @s: #QTestState instance to operate on.
253 * @event: event to return.
255 * Removes non-matching events from the buffer that was set by
256 * qtest_qmp_receive, until an event bearing the given name is found,
258 * If no event matches, clears the buffer and returns NULL.
261 QDict
*qtest_qmp_event_ref(QTestState
*s
, const char *event
);
265 * @s: #QTestState instance to operate on.
266 * @fmt: HMP command to send to QEMU, formats arguments like sprintf().
268 * Send HMP command to QEMU via QMP's human-monitor-command.
269 * QMP events are discarded.
271 * Returns: the command's output. The caller should g_free() it.
273 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...) G_GNUC_PRINTF(2, 3);
277 * @s: #QTestState instance to operate on.
278 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
279 * @ap: HMP command arguments
281 * Send HMP command to QEMU via QMP's human-monitor-command.
282 * QMP events are discarded.
284 * Returns: the command's output. The caller should g_free() it.
286 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
289 void qtest_module_load(QTestState
*s
, const char *prefix
, const char *libname
);
293 * @s: #QTestState instance to operate on.
294 * @num: Interrupt to observe.
296 * Returns: The level of the @num interrupt.
298 bool qtest_get_irq(QTestState
*s
, int num
);
301 * qtest_irq_intercept_in:
302 * @s: #QTestState instance to operate on.
303 * @string: QOM path of a device.
305 * Associate qtest irqs with the GPIO-in pins of the device
306 * whose path is specified by @string.
308 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
311 * qtest_irq_intercept_out:
312 * @s: #QTestState instance to operate on.
313 * @string: QOM path of a device.
315 * Associate qtest irqs with the GPIO-out pins of the device
316 * whose path is specified by @string.
318 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
322 * @s: QTestState instance to operate on.
323 * @string: QOM path of a device
328 * Force given device/irq GPIO-in pin to the given level.
330 void qtest_set_irq_in(QTestState
*s
, const char *string
, const char *name
,
335 * @s: #QTestState instance to operate on.
336 * @addr: I/O port to write to.
337 * @value: Value being written.
339 * Write an 8-bit value to an I/O port.
341 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
345 * @s: #QTestState instance to operate on.
346 * @addr: I/O port to write to.
347 * @value: Value being written.
349 * Write a 16-bit value to an I/O port.
351 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
355 * @s: #QTestState instance to operate on.
356 * @addr: I/O port to write to.
357 * @value: Value being written.
359 * Write a 32-bit value to an I/O port.
361 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
365 * @s: #QTestState instance to operate on.
366 * @addr: I/O port to read from.
368 * Returns an 8-bit value from an I/O port.
370 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
374 * @s: #QTestState instance to operate on.
375 * @addr: I/O port to read from.
377 * Returns a 16-bit value from an I/O port.
379 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
383 * @s: #QTestState instance to operate on.
384 * @addr: I/O port to read from.
386 * Returns a 32-bit value from an I/O port.
388 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
392 * @s: #QTestState instance to operate on.
393 * @addr: Guest address to write to.
394 * @value: Value being written.
396 * Writes an 8-bit value to memory.
398 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
402 * @s: #QTestState instance to operate on.
403 * @addr: Guest address to write to.
404 * @value: Value being written.
406 * Writes a 16-bit value to memory.
408 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
412 * @s: #QTestState instance to operate on.
413 * @addr: Guest address to write to.
414 * @value: Value being written.
416 * Writes a 32-bit value to memory.
418 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
422 * @s: #QTestState instance to operate on.
423 * @addr: Guest address to write to.
424 * @value: Value being written.
426 * Writes a 64-bit value to memory.
428 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
432 * @s: #QTestState instance to operate on.
433 * @addr: Guest address to read from.
435 * Reads an 8-bit value from memory.
437 * Returns: Value read.
439 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
443 * @s: #QTestState instance to operate on.
444 * @addr: Guest address to read from.
446 * Reads a 16-bit value from memory.
448 * Returns: Value read.
450 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
454 * @s: #QTestState instance to operate on.
455 * @addr: Guest address to read from.
457 * Reads a 32-bit value from memory.
459 * Returns: Value read.
461 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
465 * @s: #QTestState instance to operate on.
466 * @addr: Guest address to read from.
468 * Reads a 64-bit value from memory.
470 * Returns: Value read.
472 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
476 * @s: #QTestState instance to operate on.
477 * @addr: Guest address to read from.
478 * @data: Pointer to where memory contents will be stored.
479 * @size: Number of bytes to read.
481 * Read guest memory into a buffer.
483 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
487 * @s: #QTestState instance to operate on.
488 * @name: name of the command to call.
489 * @nargs: Number of args.
490 * @args: Guest address to read args from.
491 * @nret: Number of return value.
492 * @ret: Guest address to write return values to.
494 * Call an RTAS function
496 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
497 uint32_t nargs
, uint64_t args
,
498 uint32_t nret
, uint64_t ret
);
502 * @s: #QTestState instance to operate on.
503 * @addr: Guest address to read from.
504 * @data: Pointer to where memory contents will be stored.
505 * @size: Number of bytes to read.
507 * Read guest memory into a buffer and receive using a base64 encoding.
509 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
513 * @s: #QTestState instance to operate on.
514 * @addr: Guest address to write to.
515 * @data: Pointer to the bytes that will be written to guest memory.
516 * @size: Number of bytes to write.
518 * Write a buffer to guest memory.
520 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
524 * @s: #QTestState instance to operate on.
525 * @addr: Guest address to write to.
526 * @data: Pointer to the bytes that will be written to guest memory.
527 * @size: Number of bytes to write.
529 * Write a buffer to guest memory and transmit using a base64 encoding.
531 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
532 const void *data
, size_t size
);
536 * @s: #QTestState instance to operate on.
537 * @addr: Guest address to write to.
538 * @patt: Byte pattern to fill the guest memory region with.
539 * @size: Number of bytes to write.
541 * Write a pattern to guest memory.
543 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
546 * qtest_clock_step_next:
547 * @s: #QTestState instance to operate on.
549 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
551 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
553 int64_t qtest_clock_step_next(QTestState
*s
);
557 * @s: QTestState instance to operate on.
558 * @step: Number of nanoseconds to advance the clock by.
560 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
562 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
564 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
568 * @s: QTestState instance to operate on.
569 * @val: Nanoseconds value to advance the clock to.
571 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
573 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
575 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
579 * @s: QTestState instance to operate on.
581 * Returns: True if the architecture under test has a big endian configuration.
583 bool qtest_big_endian(QTestState
*s
);
588 * Returns: The architecture for the QEMU executable under test.
590 const char *qtest_get_arch(void);
594 * @accel_name: Accelerator name to check for.
596 * Returns: true if the accelerator is built in.
598 bool qtest_has_accel(const char *accel_name
);
602 * @str: Test case path.
603 * @fn: Test case function
605 * Add a GTester testcase with the given name and function.
606 * The path is prefixed with the architecture under test, as
607 * returned by qtest_get_arch().
609 void qtest_add_func(const char *str
, void (*fn
)(void));
612 * qtest_add_data_func:
613 * @str: Test case path.
614 * @data: Test case data
615 * @fn: Test case function
617 * Add a GTester testcase with the given name, data and function.
618 * The path is prefixed with the architecture under test, as
619 * returned by qtest_get_arch().
621 void qtest_add_data_func(const char *str
, const void *data
,
622 void (*fn
)(const void *));
625 * qtest_add_data_func_full:
626 * @str: Test case path.
627 * @data: Test case data
628 * @fn: Test case function
629 * @data_free_func: GDestroyNotify for data
631 * Add a GTester testcase with the given name, data and function.
632 * The path is prefixed with the architecture under test, as
633 * returned by qtest_get_arch().
635 * @data is passed to @data_free_func() on test completion.
637 void qtest_add_data_func_full(const char *str
, void *data
,
638 void (*fn
)(const void *),
639 GDestroyNotify data_free_func
);
643 * @testpath: Test case path
644 * @Fixture: Fixture type
645 * @tdata: Test case data
646 * @fsetup: Test case setup function
647 * @ftest: Test case function
648 * @fteardown: Test case teardown function
650 * Add a GTester testcase with the given name, data and functions.
651 * The path is prefixed with the architecture under test, as
652 * returned by qtest_get_arch().
654 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
656 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
657 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
662 * qtest_add_abrt_handler:
663 * @fn: Handler function
664 * @data: Argument that is passed to the handler
666 * Add a handler function that is invoked on SIGABRT. This can be used to
667 * terminate processes and perform other cleanup. The handler can be removed
668 * with qtest_remove_abrt_handler().
670 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
673 * qtest_remove_abrt_handler:
674 * @data: Argument previously passed to qtest_add_abrt_handler()
676 * Remove an abrt handler that was previously added with
677 * qtest_add_abrt_handler().
679 void qtest_remove_abrt_handler(void *data
);
682 * qtest_qmp_assert_success:
683 * @qts: QTestState instance to operate on
684 * @fmt: QMP message to send to qemu, formatted like
685 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
686 * supported after '%'.
688 * Sends a QMP message to QEMU and asserts that a 'return' key is present in
691 void qtest_qmp_assert_success(QTestState
*qts
, const char *fmt
, ...)
695 * qtest_cb_for_every_machine:
696 * @cb: Pointer to the callback function
697 * @skip_old_versioned: true if versioned old machine types should be skipped
699 * Call a callback function for every name of all available machines.
701 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
702 bool skip_old_versioned
);
706 * @machine: The machine to look for
708 * Returns: true if the machine is available in the target binary.
710 bool qtest_has_machine(const char *machine
);
714 * @device: The device to look for
716 * Returns: true if the device is available in the target binary.
718 bool qtest_has_device(const char *device
);
721 * qtest_qmp_device_add_qdict:
722 * @qts: QTestState instance to operate on
723 * @drv: Name of the device that should be added
724 * @arguments: QDict with properties for the device to intialize
726 * Generic hot-plugging test via the device_add QMP command with properties
727 * supplied in form of QDict. Use NULL for empty properties list.
729 void qtest_qmp_device_add_qdict(QTestState
*qts
, const char *drv
,
730 const QDict
*arguments
);
733 * qtest_qmp_device_add:
734 * @qts: QTestState instance to operate on
735 * @driver: Name of the device that should be added
736 * @id: Identification string
737 * @fmt: QMP message to send to qemu, formatted like
738 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
739 * supported after '%'.
741 * Generic hot-plugging test via the device_add QMP command.
743 void qtest_qmp_device_add(QTestState
*qts
, const char *driver
, const char *id
,
744 const char *fmt
, ...) G_GNUC_PRINTF(4, 5);
747 * qtest_qmp_add_client:
748 * @qts: QTestState instance to operate on
749 * @protocol: the protocol to add to
750 * @fd: the client file-descriptor
752 * Call QMP ``getfd`` followed by ``add_client`` with the given @fd.
754 void qtest_qmp_add_client(QTestState
*qts
, const char *protocol
, int fd
);
757 * qtest_qmp_device_del:
758 * @qts: QTestState instance to operate on
759 * @id: Identification string
761 * Generic hot-unplugging test via the device_del QMP command.
763 void qtest_qmp_device_del(QTestState
*qts
, const char *id
);
767 * @s: QTestState instance to operate on.
769 * Returns: true if the child is still alive.
771 bool qtest_probe_child(QTestState
*s
);
774 * qtest_set_expected_status:
775 * @s: QTestState instance to operate on.
776 * @status: an expected exit status.
778 * Set expected exit status of the child.
780 void qtest_set_expected_status(QTestState
*s
, int status
);
782 QTestState
*qtest_inproc_init(QTestState
**s
, bool log
, const char* arch
,
783 void (*send
)(void*, const char*));
785 void qtest_client_inproc_recv(void *opaque
, const char *str
);
788 * qtest_qom_set_bool:
789 * @s: QTestState instance to operate on.
790 * @path: Path to the property being set.
791 * @property: Property being set.
792 * @value: Value to set the property.
794 * Set the property with passed in value.
796 void qtest_qom_set_bool(QTestState
*s
, const char *path
, const char *property
,
800 * qtest_qom_get_bool:
801 * @s: QTestState instance to operate on.
802 * @path: Path to the property being retrieved.
803 * @property: Property from where the value is being retrieved.
805 * Returns: Value retrieved from property.
807 bool qtest_qom_get_bool(QTestState
*s
, const char *path
, const char *property
);