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"
23 typedef struct QTestState QTestState
;
27 * @fmt: Format for creating other arguments to pass to QEMU, formatted
30 * Convenience wrapper around qtest_init().
32 * Returns: #QTestState instance.
34 QTestState
*qtest_initf(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
38 * @fmt: Format for creating other arguments to pass to QEMU, formatted
40 * @ap: Format arguments.
42 * Convenience wrapper around qtest_init().
44 * Returns: #QTestState instance.
46 QTestState
*qtest_vinitf(const char *fmt
, va_list ap
) GCC_FMT_ATTR(1, 0);
50 * @extra_args: other arguments to pass to QEMU. CAUTION: these
51 * arguments are subject to word splitting and shell evaluation.
53 * Returns: #QTestState instance.
55 QTestState
*qtest_init(const char *extra_args
);
58 * qtest_init_without_qmp_handshake:
59 * @extra_args: other arguments to pass to QEMU. CAUTION: these
60 * arguments are subject to word splitting and shell evaluation.
62 * Returns: #QTestState instance.
64 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
);
67 * qtest_init_with_serial:
68 * @extra_args: other arguments to pass to QEMU. CAUTION: these
69 * arguments are subject to word splitting and shell evaluation.
70 * @sock_fd: pointer to store the socket file descriptor for
71 * connection with serial.
73 * Returns: #QTestState instance.
75 QTestState
*qtest_init_with_serial(const char *extra_args
, int *sock_fd
);
79 * @s: #QTestState instance to operate on.
81 * Kill the QEMU process and wait for it to terminate. It is safe to call this
82 * function multiple times. Normally qtest_quit() is used instead because it
83 * also frees QTestState. Use qtest_kill_qemu() when you just want to kill QEMU
84 * and qtest_quit() will be called later.
86 void qtest_kill_qemu(QTestState
*s
);
90 * @s: #QTestState instance to operate on.
92 * Shut down the QEMU process associated to @s.
94 void qtest_quit(QTestState
*s
);
98 * @s: #QTestState instance to operate on.
99 * @fds: array of file descriptors
100 * @fds_num: number of elements in @fds
101 * @fmt: QMP message to send to qemu, formatted like
102 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
103 * supported after '%'.
105 * Sends a QMP message to QEMU with fds and returns the response.
107 QDict
*qtest_qmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
108 const char *fmt
, ...)
113 * @s: #QTestState instance to operate on.
114 * @fmt: QMP message to send to qemu, formatted like
115 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
116 * supported after '%'.
118 * Sends a QMP message to QEMU and returns the response.
120 QDict
*qtest_qmp(QTestState
*s
, 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 leaves the response in the stream.
132 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
136 * qtest_qmp_send_raw:
137 * @s: #QTestState instance to operate on.
138 * @fmt: text to send, formatted like sprintf()
140 * Sends text to the QMP monitor verbatim. Need not be valid JSON;
141 * this is useful for negative tests.
143 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
147 * qtest_socket_server:
148 * @socket_path: the UNIX domain socket path
150 * Create and return a listen socket file descriptor, or abort on failure.
152 int qtest_socket_server(const char *socket_path
);
156 * @s: #QTestState instance to operate on.
157 * @fds: array of file descriptors
158 * @fds_num: number of elements in @fds
159 * @fmt: QMP message to send to QEMU, formatted like
160 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
161 * supported after '%'.
162 * @ap: QMP message arguments
164 * Sends a QMP message to QEMU with fds and returns the response.
166 QDict
*qtest_vqmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
167 const char *fmt
, va_list ap
)
172 * @s: #QTestState instance to operate on.
173 * @fmt: QMP message to send to QEMU, formatted like
174 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
175 * supported after '%'.
176 * @ap: QMP message arguments
178 * Sends a QMP message to QEMU and returns the response.
180 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
184 * qtest_qmp_vsend_fds:
185 * @s: #QTestState instance to operate on.
186 * @fds: array of file descriptors
187 * @fds_num: number of elements in @fds
188 * @fmt: QMP message to send to QEMU, formatted like
189 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
190 * supported after '%'.
191 * @ap: QMP message arguments
193 * Sends a QMP message to QEMU and leaves the response in the stream.
195 void qtest_qmp_vsend_fds(QTestState
*s
, int *fds
, size_t fds_num
,
196 const char *fmt
, va_list ap
)
201 * @s: #QTestState instance to operate on.
202 * @fmt: QMP message to send to QEMU, formatted like
203 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
204 * supported after '%'.
205 * @ap: QMP message arguments
207 * Sends a QMP message to QEMU and leaves the response in the stream.
209 void qtest_qmp_vsend(QTestState
*s
, const char *fmt
, va_list ap
)
213 * qtest_qmp_receive_dict:
214 * @s: #QTestState instance to operate on.
216 * Reads a QMP message from QEMU and returns the response.
218 QDict
*qtest_qmp_receive_dict(QTestState
*s
);
222 * @s: #QTestState instance to operate on.
224 * Reads a QMP message from QEMU and returns the response.
225 * Buffers all the events received meanwhile, until a
226 * call to qtest_qmp_eventwait
228 QDict
*qtest_qmp_receive(QTestState
*s
);
231 * qtest_qmp_eventwait:
232 * @s: #QTestState instance to operate on.
233 * @event: event to wait for.
235 * Continuously polls for QMP responses until it receives the desired event.
237 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
240 * qtest_qmp_eventwait_ref:
241 * @s: #QTestState instance to operate on.
242 * @event: event to wait for.
244 * Continuously polls for QMP responses until it receives the desired event.
245 * Returns a copy of the event for further investigation.
247 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
250 * qtest_qmp_event_ref:
251 * @s: #QTestState instance to operate on.
252 * @event: event to return.
254 * Removes non-matching events from the buffer that was set by
255 * qtest_qmp_receive, until an event bearing the given name is found,
257 * If no event matches, clears the buffer and returns NULL.
260 QDict
*qtest_qmp_event_ref(QTestState
*s
, const char *event
);
264 * @s: #QTestState instance to operate on.
265 * @fmt: HMP command to send to QEMU, formats arguments like sprintf().
267 * Send HMP command to QEMU via QMP's human-monitor-command.
268 * QMP events are discarded.
270 * Returns: the command's output. The caller should g_free() it.
272 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
276 * @s: #QTestState instance to operate on.
277 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
278 * @ap: HMP command arguments
280 * Send HMP command to QEMU via QMP's human-monitor-command.
281 * QMP events are discarded.
283 * Returns: the command's output. The caller should g_free() it.
285 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
288 void qtest_module_load(QTestState
*s
, const char *prefix
, const char *libname
);
292 * @s: #QTestState instance to operate on.
293 * @num: Interrupt to observe.
295 * Returns: The level of the @num interrupt.
297 bool qtest_get_irq(QTestState
*s
, int num
);
300 * qtest_irq_intercept_in:
301 * @s: #QTestState instance to operate on.
302 * @string: QOM path of a device.
304 * Associate qtest irqs with the GPIO-in pins of the device
305 * whose path is specified by @string.
307 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
310 * qtest_irq_intercept_out:
311 * @s: #QTestState instance to operate on.
312 * @string: QOM path of a device.
314 * Associate qtest irqs with the GPIO-out pins of the device
315 * whose path is specified by @string.
317 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
321 * @s: QTestState instance to operate on.
322 * @string: QOM path of a device
327 * Force given device/irq GPIO-in pin to the given level.
329 void qtest_set_irq_in(QTestState
*s
, const char *string
, const char *name
,
334 * @s: #QTestState instance to operate on.
335 * @addr: I/O port to write to.
336 * @value: Value being written.
338 * Write an 8-bit value to an I/O port.
340 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
344 * @s: #QTestState instance to operate on.
345 * @addr: I/O port to write to.
346 * @value: Value being written.
348 * Write a 16-bit value to an I/O port.
350 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
354 * @s: #QTestState instance to operate on.
355 * @addr: I/O port to write to.
356 * @value: Value being written.
358 * Write a 32-bit value to an I/O port.
360 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
364 * @s: #QTestState instance to operate on.
365 * @addr: I/O port to read from.
367 * Returns an 8-bit value from an I/O port.
369 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
373 * @s: #QTestState instance to operate on.
374 * @addr: I/O port to read from.
376 * Returns a 16-bit value from an I/O port.
378 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
382 * @s: #QTestState instance to operate on.
383 * @addr: I/O port to read from.
385 * Returns a 32-bit value from an I/O port.
387 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
391 * @s: #QTestState instance to operate on.
392 * @addr: Guest address to write to.
393 * @value: Value being written.
395 * Writes an 8-bit value to memory.
397 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
401 * @s: #QTestState instance to operate on.
402 * @addr: Guest address to write to.
403 * @value: Value being written.
405 * Writes a 16-bit value to memory.
407 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
411 * @s: #QTestState instance to operate on.
412 * @addr: Guest address to write to.
413 * @value: Value being written.
415 * Writes a 32-bit value to memory.
417 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
421 * @s: #QTestState instance to operate on.
422 * @addr: Guest address to write to.
423 * @value: Value being written.
425 * Writes a 64-bit value to memory.
427 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
431 * @s: #QTestState instance to operate on.
432 * @addr: Guest address to read from.
434 * Reads an 8-bit value from memory.
436 * Returns: Value read.
438 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
442 * @s: #QTestState instance to operate on.
443 * @addr: Guest address to read from.
445 * Reads a 16-bit value from memory.
447 * Returns: Value read.
449 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
453 * @s: #QTestState instance to operate on.
454 * @addr: Guest address to read from.
456 * Reads a 32-bit value from memory.
458 * Returns: Value read.
460 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
464 * @s: #QTestState instance to operate on.
465 * @addr: Guest address to read from.
467 * Reads a 64-bit value from memory.
469 * Returns: Value read.
471 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
475 * @s: #QTestState instance to operate on.
476 * @addr: Guest address to read from.
477 * @data: Pointer to where memory contents will be stored.
478 * @size: Number of bytes to read.
480 * Read guest memory into a buffer.
482 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
486 * @s: #QTestState instance to operate on.
487 * @name: name of the command to call.
488 * @nargs: Number of args.
489 * @args: Guest address to read args from.
490 * @nret: Number of return value.
491 * @ret: Guest address to write return values to.
493 * Call an RTAS function
495 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
496 uint32_t nargs
, uint64_t args
,
497 uint32_t nret
, uint64_t ret
);
501 * @s: #QTestState instance to operate on.
502 * @addr: Guest address to read from.
503 * @data: Pointer to where memory contents will be stored.
504 * @size: Number of bytes to read.
506 * Read guest memory into a buffer and receive using a base64 encoding.
508 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
512 * @s: #QTestState instance to operate on.
513 * @addr: Guest address to write to.
514 * @data: Pointer to the bytes that will be written to guest memory.
515 * @size: Number of bytes to write.
517 * Write a buffer to guest memory.
519 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
523 * @s: #QTestState instance to operate on.
524 * @addr: Guest address to write to.
525 * @data: Pointer to the bytes that will be written to guest memory.
526 * @size: Number of bytes to write.
528 * Write a buffer to guest memory and transmit using a base64 encoding.
530 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
531 const void *data
, size_t size
);
535 * @s: #QTestState instance to operate on.
536 * @addr: Guest address to write to.
537 * @patt: Byte pattern to fill the guest memory region with.
538 * @size: Number of bytes to write.
540 * Write a pattern to guest memory.
542 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
545 * qtest_clock_step_next:
546 * @s: #QTestState instance to operate on.
548 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
550 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
552 int64_t qtest_clock_step_next(QTestState
*s
);
556 * @s: QTestState instance to operate on.
557 * @step: Number of nanoseconds to advance the clock by.
559 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
561 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
563 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
567 * @s: QTestState instance to operate on.
568 * @val: Nanoseconds value to advance the clock to.
570 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
572 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
574 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
578 * @s: QTestState instance to operate on.
580 * Returns: True if the architecture under test has a big endian configuration.
582 bool qtest_big_endian(QTestState
*s
);
587 * Returns: The architecture for the QEMU executable under test.
589 const char *qtest_get_arch(void);
593 * @accel_name: Accelerator name to check for.
595 * Returns: true if the accelerator is built in.
597 bool qtest_has_accel(const char *accel_name
);
601 * @str: Test case path.
602 * @fn: Test case function
604 * Add a GTester testcase with the given name and function.
605 * The path is prefixed with the architecture under test, as
606 * returned by qtest_get_arch().
608 void qtest_add_func(const char *str
, void (*fn
)(void));
611 * qtest_add_data_func:
612 * @str: Test case path.
613 * @data: Test case data
614 * @fn: Test case function
616 * Add a GTester testcase with the given name, data and function.
617 * The path is prefixed with the architecture under test, as
618 * returned by qtest_get_arch().
620 void qtest_add_data_func(const char *str
, const void *data
,
621 void (*fn
)(const void *));
624 * qtest_add_data_func_full:
625 * @str: Test case path.
626 * @data: Test case data
627 * @fn: Test case function
628 * @data_free_func: GDestroyNotify for data
630 * Add a GTester testcase with the given name, data and function.
631 * The path is prefixed with the architecture under test, as
632 * returned by qtest_get_arch().
634 * @data is passed to @data_free_func() on test completion.
636 void qtest_add_data_func_full(const char *str
, void *data
,
637 void (*fn
)(const void *),
638 GDestroyNotify data_free_func
);
642 * @testpath: Test case path
643 * @Fixture: Fixture type
644 * @tdata: Test case data
645 * @fsetup: Test case setup function
646 * @ftest: Test case function
647 * @fteardown: Test case teardown function
649 * Add a GTester testcase with the given name, data and functions.
650 * The path is prefixed with the architecture under test, as
651 * returned by qtest_get_arch().
653 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
655 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
656 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
661 * qtest_add_abrt_handler:
662 * @fn: Handler function
663 * @data: Argument that is passed to the handler
665 * Add a handler function that is invoked on SIGABRT. This can be used to
666 * terminate processes and perform other cleanup. The handler can be removed
667 * with qtest_remove_abrt_handler().
669 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
672 * qtest_remove_abrt_handler:
673 * @data: Argument previously passed to qtest_add_abrt_handler()
675 * Remove an abrt handler that was previously added with
676 * qtest_add_abrt_handler().
678 void qtest_remove_abrt_handler(void *data
);
681 * qtest_qmp_assert_success:
682 * @qts: QTestState instance to operate on
683 * @fmt: QMP message to send to qemu, formatted like
684 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
685 * supported after '%'.
687 * Sends a QMP message to QEMU and asserts that a 'return' key is present in
690 void qtest_qmp_assert_success(QTestState
*qts
, const char *fmt
, ...)
693 QDict
*qmp_fd_receive(int fd
);
694 void qmp_fd_vsend_fds(int fd
, int *fds
, size_t fds_num
,
695 const char *fmt
, va_list ap
) GCC_FMT_ATTR(4, 0);
696 void qmp_fd_vsend(int fd
, const char *fmt
, va_list ap
) GCC_FMT_ATTR(2, 0);
697 void qmp_fd_send(int fd
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
698 void qmp_fd_send_raw(int fd
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
699 void qmp_fd_vsend_raw(int fd
, const char *fmt
, va_list ap
) GCC_FMT_ATTR(2, 0);
700 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
) GCC_FMT_ATTR(2, 0);
701 QDict
*qmp_fd(int fd
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
704 * qtest_cb_for_every_machine:
705 * @cb: Pointer to the callback function
706 * @skip_old_versioned: true if versioned old machine types should be skipped
708 * Call a callback function for every name of all available machines.
710 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
711 bool skip_old_versioned
);
715 * @machine: The machine to look for
717 * Returns: true if the machine is available in the target binary.
719 bool qtest_has_machine(const char *machine
);
723 * @device: The device to look for
725 * Returns: true if the device is available in the target binary.
727 bool qtest_has_device(const char *device
);
730 * qtest_qmp_device_add_qdict:
731 * @qts: QTestState instance to operate on
732 * @drv: Name of the device that should be added
733 * @arguments: QDict with properties for the device to intialize
735 * Generic hot-plugging test via the device_add QMP command with properties
736 * supplied in form of QDict. Use NULL for empty properties list.
738 void qtest_qmp_device_add_qdict(QTestState
*qts
, const char *drv
,
739 const QDict
*arguments
);
742 * qtest_qmp_device_add:
743 * @qts: QTestState instance to operate on
744 * @driver: Name of the device that should be added
745 * @id: Identification string
746 * @fmt: QMP message to send to qemu, formatted like
747 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
748 * supported after '%'.
750 * Generic hot-plugging test via the device_add QMP command.
752 void qtest_qmp_device_add(QTestState
*qts
, const char *driver
, const char *id
,
753 const char *fmt
, ...) GCC_FMT_ATTR(4, 5);
756 * qtest_qmp_add_client:
757 * @qts: QTestState instance to operate on
758 * @protocol: the protocol to add to
759 * @fd: the client file-descriptor
761 * Call QMP ``getfd`` followed by ``add_client`` with the given @fd.
763 void qtest_qmp_add_client(QTestState
*qts
, const char *protocol
, int fd
);
766 * qtest_qmp_device_del:
767 * @qts: QTestState instance to operate on
768 * @id: Identification string
770 * Generic hot-unplugging test via the device_del QMP command.
772 void qtest_qmp_device_del(QTestState
*qts
, const char *id
);
776 * @rsp: QMP response to check for error
778 * Test @rsp for error and discard @rsp.
779 * Returns 'true' if there is error in @rsp and 'false' otherwise.
781 bool qmp_rsp_is_err(QDict
*rsp
);
784 * qmp_expect_error_and_unref:
785 * @rsp: QMP response to check for error
786 * @class: an error class
788 * Assert the response has the given error class and discard @rsp.
790 void qmp_expect_error_and_unref(QDict
*rsp
, const char *class);
794 * @s: QTestState instance to operate on.
796 * Returns: true if the child is still alive.
798 bool qtest_probe_child(QTestState
*s
);
801 * qtest_set_expected_status:
802 * @s: QTestState instance to operate on.
803 * @status: an expected exit status.
805 * Set expected exit status of the child.
807 void qtest_set_expected_status(QTestState
*s
, int status
);
809 QTestState
*qtest_inproc_init(QTestState
**s
, bool log
, const char* arch
,
810 void (*send
)(void*, const char*));
812 void qtest_client_inproc_recv(void *opaque
, const char *str
);