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 * @str: Test case path.
594 * @fn: Test case function
596 * Add a GTester testcase with the given name and function.
597 * The path is prefixed with the architecture under test, as
598 * returned by qtest_get_arch().
600 void qtest_add_func(const char *str
, void (*fn
)(void));
603 * qtest_add_data_func:
604 * @str: Test case path.
605 * @data: Test case data
606 * @fn: Test case function
608 * Add a GTester testcase with the given name, data and function.
609 * The path is prefixed with the architecture under test, as
610 * returned by qtest_get_arch().
612 void qtest_add_data_func(const char *str
, const void *data
,
613 void (*fn
)(const void *));
616 * qtest_add_data_func_full:
617 * @str: Test case path.
618 * @data: Test case data
619 * @fn: Test case function
620 * @data_free_func: GDestroyNotify for data
622 * Add a GTester testcase with the given name, data and function.
623 * The path is prefixed with the architecture under test, as
624 * returned by qtest_get_arch().
626 * @data is passed to @data_free_func() on test completion.
628 void qtest_add_data_func_full(const char *str
, void *data
,
629 void (*fn
)(const void *),
630 GDestroyNotify data_free_func
);
634 * @testpath: Test case path
635 * @Fixture: Fixture type
636 * @tdata: Test case data
637 * @fsetup: Test case setup function
638 * @ftest: Test case function
639 * @fteardown: Test case teardown function
641 * Add a GTester testcase with the given name, data and functions.
642 * The path is prefixed with the architecture under test, as
643 * returned by qtest_get_arch().
645 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
647 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
648 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
653 * qtest_add_abrt_handler:
654 * @fn: Handler function
655 * @data: Argument that is passed to the handler
657 * Add a handler function that is invoked on SIGABRT. This can be used to
658 * terminate processes and perform other cleanup. The handler can be removed
659 * with qtest_remove_abrt_handler().
661 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
664 * qtest_remove_abrt_handler:
665 * @data: Argument previously passed to qtest_add_abrt_handler()
667 * Remove an abrt handler that was previously added with
668 * qtest_add_abrt_handler().
670 void qtest_remove_abrt_handler(void *data
);
673 * qtest_qmp_assert_success:
674 * @qts: QTestState instance to operate on
675 * @fmt: QMP message to send to qemu, formatted like
676 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
677 * supported after '%'.
679 * Sends a QMP message to QEMU and asserts that a 'return' key is present in
682 void qtest_qmp_assert_success(QTestState
*qts
, const char *fmt
, ...)
685 QDict
*qmp_fd_receive(int fd
);
686 void qmp_fd_vsend_fds(int fd
, int *fds
, size_t fds_num
,
687 const char *fmt
, va_list ap
) GCC_FMT_ATTR(4, 0);
688 void qmp_fd_vsend(int fd
, const char *fmt
, va_list ap
) GCC_FMT_ATTR(2, 0);
689 void qmp_fd_send(int fd
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
690 void qmp_fd_send_raw(int fd
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
691 void qmp_fd_vsend_raw(int fd
, const char *fmt
, va_list ap
) GCC_FMT_ATTR(2, 0);
692 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
) GCC_FMT_ATTR(2, 0);
693 QDict
*qmp_fd(int fd
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
696 * qtest_cb_for_every_machine:
697 * @cb: Pointer to the callback function
698 * @skip_old_versioned: true if versioned old machine types should be skipped
700 * Call a callback function for every name of all available machines.
702 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
703 bool skip_old_versioned
);
706 * qtest_qmp_device_add_qdict:
707 * @qts: QTestState instance to operate on
708 * @drv: Name of the device that should be added
709 * @arguments: QDict with properties for the device to intialize
711 * Generic hot-plugging test via the device_add QMP command with properties
712 * supplied in form of QDict. Use NULL for empty properties list.
714 void qtest_qmp_device_add_qdict(QTestState
*qts
, const char *drv
,
715 const QDict
*arguments
);
718 * qtest_qmp_device_add:
719 * @qts: QTestState instance to operate on
720 * @driver: Name of the device that should be added
721 * @id: Identification string
722 * @fmt: QMP message to send to qemu, formatted like
723 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
724 * supported after '%'.
726 * Generic hot-plugging test via the device_add QMP command.
728 void qtest_qmp_device_add(QTestState
*qts
, const char *driver
, const char *id
,
729 const char *fmt
, ...) GCC_FMT_ATTR(4, 5);
732 * qtest_qmp_device_del:
733 * @qts: QTestState instance to operate on
734 * @id: Identification string
736 * Generic hot-unplugging test via the device_del QMP command.
738 void qtest_qmp_device_del(QTestState
*qts
, const char *id
);
742 * @rsp: QMP response to check for error
744 * Test @rsp for error and discard @rsp.
745 * Returns 'true' if there is error in @rsp and 'false' otherwise.
747 bool qmp_rsp_is_err(QDict
*rsp
);
750 * qmp_expect_error_and_unref:
751 * @rsp: QMP response to check for error
752 * @class: an error class
754 * Assert the response has the given error class and discard @rsp.
756 void qmp_expect_error_and_unref(QDict
*rsp
, const char *class);
760 * @s: QTestState instance to operate on.
762 * Returns: true if the child is still alive.
764 bool qtest_probe_child(QTestState
*s
);
767 * qtest_set_expected_status:
768 * @s: QTestState instance to operate on.
769 * @status: an expected exit status.
771 * Set expected exit status of the child.
773 void qtest_set_expected_status(QTestState
*s
, int status
);
775 QTestState
*qtest_inproc_init(QTestState
**s
, bool log
, const char* arch
,
776 void (*send
)(void*, const char*));
778 void qtest_client_inproc_recv(void *opaque
, const char *str
);