libqtest: add qtest_socket_server()
[qemu/kevin.git] / tests / qtest / libqos / libqtest.h
blobe5f1ec590cc6ed7999e7b49e9e2fb03c929c3f30
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
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.
17 #ifndef LIBQTEST_H
18 #define LIBQTEST_H
20 #include "qapi/qmp/qobject.h"
21 #include "qapi/qmp/qdict.h"
23 typedef struct QTestState QTestState;
25 /**
26 * qtest_initf:
27 * @fmt: Format for creating other arguments to pass to QEMU, formatted
28 * like sprintf().
30 * Convenience wrapper around qtest_init().
32 * Returns: #QTestState instance.
34 QTestState *qtest_initf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
36 /**
37 * qtest_vinitf:
38 * @fmt: Format for creating other arguments to pass to QEMU, formatted
39 * like vsprintf().
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);
48 /**
49 * qtest_init:
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);
57 /**
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);
66 /**
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);
77 /**
78 * qtest_quit:
79 * @s: #QTestState instance to operate on.
81 * Shut down the QEMU process associated to @s.
83 void qtest_quit(QTestState *s);
85 /**
86 * qtest_qmp_fds:
87 * @s: #QTestState instance to operate on.
88 * @fds: array of file descriptors
89 * @fds_num: number of elements in @fds
90 * @fmt: QMP message to send to qemu, formatted like
91 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
92 * supported after '%'.
94 * Sends a QMP message to QEMU with fds and returns the response.
96 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
97 const char *fmt, ...)
98 GCC_FMT_ATTR(4, 5);
101 * qtest_qmp:
102 * @s: #QTestState instance to operate on.
103 * @fmt: QMP message to send to qemu, formatted like
104 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
105 * supported after '%'.
107 * Sends a QMP message to QEMU and returns the response.
109 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
110 GCC_FMT_ATTR(2, 3);
113 * qtest_qmp_send:
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 leaves the response in the stream.
121 void qtest_qmp_send(QTestState *s, const char *fmt, ...)
122 GCC_FMT_ATTR(2, 3);
125 * qtest_qmp_send_raw:
126 * @s: #QTestState instance to operate on.
127 * @fmt: text to send, formatted like sprintf()
129 * Sends text to the QMP monitor verbatim. Need not be valid JSON;
130 * this is useful for negative tests.
132 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
133 GCC_FMT_ATTR(2, 3);
136 * qtest_socket_server:
137 * @socket_path: the UNIX domain socket path
139 * Create and return a listen socket file descriptor, or abort on failure.
141 int qtest_socket_server(const char *socket_path);
144 * qtest_vqmp_fds:
145 * @s: #QTestState instance to operate on.
146 * @fds: array of file descriptors
147 * @fds_num: number of elements in @fds
148 * @fmt: QMP message to send to QEMU, formatted like
149 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
150 * supported after '%'.
151 * @ap: QMP message arguments
153 * Sends a QMP message to QEMU with fds and returns the response.
155 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
156 const char *fmt, va_list ap)
157 GCC_FMT_ATTR(4, 0);
160 * qtest_vqmp:
161 * @s: #QTestState instance to operate on.
162 * @fmt: QMP message to send to QEMU, formatted like
163 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
164 * supported after '%'.
165 * @ap: QMP message arguments
167 * Sends a QMP message to QEMU and returns the response.
169 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
170 GCC_FMT_ATTR(2, 0);
173 * qtest_qmp_vsend_fds:
174 * @s: #QTestState instance to operate on.
175 * @fds: array of file descriptors
176 * @fds_num: number of elements in @fds
177 * @fmt: QMP message to send to QEMU, formatted like
178 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
179 * supported after '%'.
180 * @ap: QMP message arguments
182 * Sends a QMP message to QEMU and leaves the response in the stream.
184 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
185 const char *fmt, va_list ap)
186 GCC_FMT_ATTR(4, 0);
189 * qtest_qmp_vsend:
190 * @s: #QTestState instance to operate on.
191 * @fmt: QMP message to send to QEMU, formatted like
192 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
193 * supported after '%'.
194 * @ap: QMP message arguments
196 * Sends a QMP message to QEMU and leaves the response in the stream.
198 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
199 GCC_FMT_ATTR(2, 0);
202 * qtest_qmp_receive_dict:
203 * @s: #QTestState instance to operate on.
205 * Reads a QMP message from QEMU and returns the response.
207 QDict *qtest_qmp_receive_dict(QTestState *s);
210 * qtest_qmp_receive:
211 * @s: #QTestState instance to operate on.
213 * Reads a QMP message from QEMU and returns the response.
214 * Buffers all the events received meanwhile, until a
215 * call to qtest_qmp_eventwait
217 QDict *qtest_qmp_receive(QTestState *s);
220 * qtest_qmp_eventwait:
221 * @s: #QTestState instance to operate on.
222 * @event: event to wait for.
224 * Continuously polls for QMP responses until it receives the desired event.
226 void qtest_qmp_eventwait(QTestState *s, const char *event);
229 * qtest_qmp_eventwait_ref:
230 * @s: #QTestState instance to operate on.
231 * @event: event to wait for.
233 * Continuously polls for QMP responses until it receives the desired event.
234 * Returns a copy of the event for further investigation.
236 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
239 * qtest_qmp_event_ref:
240 * @s: #QTestState instance to operate on.
241 * @event: event to return.
243 * Removes non-matching events from the buffer that was set by
244 * qtest_qmp_receive, until an event bearing the given name is found,
245 * and returns it.
246 * If no event matches, clears the buffer and returns NULL.
249 QDict *qtest_qmp_event_ref(QTestState *s, const char *event);
252 * qtest_hmp:
253 * @s: #QTestState instance to operate on.
254 * @fmt: HMP command to send to QEMU, formats arguments like sprintf().
256 * Send HMP command to QEMU via QMP's human-monitor-command.
257 * QMP events are discarded.
259 * Returns: the command's output. The caller should g_free() it.
261 char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
264 * qtest_hmpv:
265 * @s: #QTestState instance to operate on.
266 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
267 * @ap: HMP command arguments
269 * Send HMP command to QEMU via QMP's human-monitor-command.
270 * QMP events are discarded.
272 * Returns: the command's output. The caller should g_free() it.
274 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
275 GCC_FMT_ATTR(2, 0);
277 void qtest_module_load(QTestState *s, const char *prefix, const char *libname);
280 * qtest_get_irq:
281 * @s: #QTestState instance to operate on.
282 * @num: Interrupt to observe.
284 * Returns: The level of the @num interrupt.
286 bool qtest_get_irq(QTestState *s, int num);
289 * qtest_irq_intercept_in:
290 * @s: #QTestState instance to operate on.
291 * @string: QOM path of a device.
293 * Associate qtest irqs with the GPIO-in pins of the device
294 * whose path is specified by @string.
296 void qtest_irq_intercept_in(QTestState *s, const char *string);
299 * qtest_irq_intercept_out:
300 * @s: #QTestState instance to operate on.
301 * @string: QOM path of a device.
303 * Associate qtest irqs with the GPIO-out pins of the device
304 * whose path is specified by @string.
306 void qtest_irq_intercept_out(QTestState *s, const char *string);
309 * qtest_set_irq_in:
310 * @s: QTestState instance to operate on.
311 * @string: QOM path of a device
312 * @name: IRQ name
313 * @irq: IRQ number
314 * @level: IRQ level
316 * Force given device/irq GPIO-in pin to the given level.
318 void qtest_set_irq_in(QTestState *s, const char *string, const char *name,
319 int irq, int level);
322 * qtest_outb:
323 * @s: #QTestState instance to operate on.
324 * @addr: I/O port to write to.
325 * @value: Value being written.
327 * Write an 8-bit value to an I/O port.
329 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
332 * qtest_outw:
333 * @s: #QTestState instance to operate on.
334 * @addr: I/O port to write to.
335 * @value: Value being written.
337 * Write a 16-bit value to an I/O port.
339 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
342 * qtest_outl:
343 * @s: #QTestState instance to operate on.
344 * @addr: I/O port to write to.
345 * @value: Value being written.
347 * Write a 32-bit value to an I/O port.
349 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
352 * qtest_inb:
353 * @s: #QTestState instance to operate on.
354 * @addr: I/O port to read from.
356 * Returns an 8-bit value from an I/O port.
358 uint8_t qtest_inb(QTestState *s, uint16_t addr);
361 * qtest_inw:
362 * @s: #QTestState instance to operate on.
363 * @addr: I/O port to read from.
365 * Returns a 16-bit value from an I/O port.
367 uint16_t qtest_inw(QTestState *s, uint16_t addr);
370 * qtest_inl:
371 * @s: #QTestState instance to operate on.
372 * @addr: I/O port to read from.
374 * Returns a 32-bit value from an I/O port.
376 uint32_t qtest_inl(QTestState *s, uint16_t addr);
379 * qtest_writeb:
380 * @s: #QTestState instance to operate on.
381 * @addr: Guest address to write to.
382 * @value: Value being written.
384 * Writes an 8-bit value to memory.
386 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
389 * qtest_writew:
390 * @s: #QTestState instance to operate on.
391 * @addr: Guest address to write to.
392 * @value: Value being written.
394 * Writes a 16-bit value to memory.
396 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
399 * qtest_writel:
400 * @s: #QTestState instance to operate on.
401 * @addr: Guest address to write to.
402 * @value: Value being written.
404 * Writes a 32-bit value to memory.
406 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
409 * qtest_writeq:
410 * @s: #QTestState instance to operate on.
411 * @addr: Guest address to write to.
412 * @value: Value being written.
414 * Writes a 64-bit value to memory.
416 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
419 * qtest_readb:
420 * @s: #QTestState instance to operate on.
421 * @addr: Guest address to read from.
423 * Reads an 8-bit value from memory.
425 * Returns: Value read.
427 uint8_t qtest_readb(QTestState *s, uint64_t addr);
430 * qtest_readw:
431 * @s: #QTestState instance to operate on.
432 * @addr: Guest address to read from.
434 * Reads a 16-bit value from memory.
436 * Returns: Value read.
438 uint16_t qtest_readw(QTestState *s, uint64_t addr);
441 * qtest_readl:
442 * @s: #QTestState instance to operate on.
443 * @addr: Guest address to read from.
445 * Reads a 32-bit value from memory.
447 * Returns: Value read.
449 uint32_t qtest_readl(QTestState *s, uint64_t addr);
452 * qtest_readq:
453 * @s: #QTestState instance to operate on.
454 * @addr: Guest address to read from.
456 * Reads a 64-bit value from memory.
458 * Returns: Value read.
460 uint64_t qtest_readq(QTestState *s, uint64_t addr);
463 * qtest_memread:
464 * @s: #QTestState instance to operate on.
465 * @addr: Guest address to read from.
466 * @data: Pointer to where memory contents will be stored.
467 * @size: Number of bytes to read.
469 * Read guest memory into a buffer.
471 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
474 * qtest_rtas_call:
475 * @s: #QTestState instance to operate on.
476 * @name: name of the command to call.
477 * @nargs: Number of args.
478 * @args: Guest address to read args from.
479 * @nret: Number of return value.
480 * @ret: Guest address to write return values to.
482 * Call an RTAS function
484 uint64_t qtest_rtas_call(QTestState *s, const char *name,
485 uint32_t nargs, uint64_t args,
486 uint32_t nret, uint64_t ret);
489 * qtest_bufread:
490 * @s: #QTestState instance to operate on.
491 * @addr: Guest address to read from.
492 * @data: Pointer to where memory contents will be stored.
493 * @size: Number of bytes to read.
495 * Read guest memory into a buffer and receive using a base64 encoding.
497 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
500 * qtest_memwrite:
501 * @s: #QTestState instance to operate on.
502 * @addr: Guest address to write to.
503 * @data: Pointer to the bytes that will be written to guest memory.
504 * @size: Number of bytes to write.
506 * Write a buffer to guest memory.
508 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
511 * qtest_bufwrite:
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 and transmit using a base64 encoding.
519 void qtest_bufwrite(QTestState *s, uint64_t addr,
520 const void *data, size_t size);
523 * qtest_memset:
524 * @s: #QTestState instance to operate on.
525 * @addr: Guest address to write to.
526 * @patt: Byte pattern to fill the guest memory region with.
527 * @size: Number of bytes to write.
529 * Write a pattern to guest memory.
531 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
534 * qtest_clock_step_next:
535 * @s: #QTestState instance to operate on.
537 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
539 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
541 int64_t qtest_clock_step_next(QTestState *s);
544 * qtest_clock_step:
545 * @s: QTestState instance to operate on.
546 * @step: Number of nanoseconds to advance the clock by.
548 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
550 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
552 int64_t qtest_clock_step(QTestState *s, int64_t step);
555 * qtest_clock_set:
556 * @s: QTestState instance to operate on.
557 * @val: Nanoseconds value to advance the clock to.
559 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
561 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
563 int64_t qtest_clock_set(QTestState *s, int64_t val);
566 * qtest_big_endian:
567 * @s: QTestState instance to operate on.
569 * Returns: True if the architecture under test has a big endian configuration.
571 bool qtest_big_endian(QTestState *s);
574 * qtest_get_arch:
576 * Returns: The architecture for the QEMU executable under test.
578 const char *qtest_get_arch(void);
581 * qtest_add_func:
582 * @str: Test case path.
583 * @fn: Test case function
585 * Add a GTester testcase with the given name and function.
586 * The path is prefixed with the architecture under test, as
587 * returned by qtest_get_arch().
589 void qtest_add_func(const char *str, void (*fn)(void));
592 * qtest_add_data_func:
593 * @str: Test case path.
594 * @data: Test case data
595 * @fn: Test case function
597 * Add a GTester testcase with the given name, data and function.
598 * The path is prefixed with the architecture under test, as
599 * returned by qtest_get_arch().
601 void qtest_add_data_func(const char *str, const void *data,
602 void (*fn)(const void *));
605 * qtest_add_data_func_full:
606 * @str: Test case path.
607 * @data: Test case data
608 * @fn: Test case function
609 * @data_free_func: GDestroyNotify for data
611 * Add a GTester testcase with the given name, data and function.
612 * The path is prefixed with the architecture under test, as
613 * returned by qtest_get_arch().
615 * @data is passed to @data_free_func() on test completion.
617 void qtest_add_data_func_full(const char *str, void *data,
618 void (*fn)(const void *),
619 GDestroyNotify data_free_func);
622 * qtest_add:
623 * @testpath: Test case path
624 * @Fixture: Fixture type
625 * @tdata: Test case data
626 * @fsetup: Test case setup function
627 * @ftest: Test case function
628 * @fteardown: Test case teardown function
630 * Add a GTester testcase with the given name, data and functions.
631 * The path is prefixed with the architecture under test, as
632 * returned by qtest_get_arch().
634 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
635 do { \
636 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
637 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
638 g_free(path); \
639 } while (0)
641 void qtest_add_abrt_handler(GHookFunc fn, const void *data);
644 * qtest_qmp_assert_success:
645 * @qts: QTestState instance to operate on
646 * @fmt: QMP message to send to qemu, formatted like
647 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
648 * supported after '%'.
650 * Sends a QMP message to QEMU and asserts that a 'return' key is present in
651 * the response.
653 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
654 GCC_FMT_ATTR(2, 3);
656 QDict *qmp_fd_receive(int fd);
657 void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
658 const char *fmt, va_list ap) GCC_FMT_ATTR(4, 0);
659 void qmp_fd_vsend(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
660 void qmp_fd_send(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
661 void qmp_fd_send_raw(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
662 void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
663 QDict *qmp_fdv(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
664 QDict *qmp_fd(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
667 * qtest_cb_for_every_machine:
668 * @cb: Pointer to the callback function
669 * @skip_old_versioned: true if versioned old machine types should be skipped
671 * Call a callback function for every name of all available machines.
673 void qtest_cb_for_every_machine(void (*cb)(const char *machine),
674 bool skip_old_versioned);
677 * qtest_qmp_device_add_qdict:
678 * @qts: QTestState instance to operate on
679 * @drv: Name of the device that should be added
680 * @arguments: QDict with properties for the device to intialize
682 * Generic hot-plugging test via the device_add QMP command with properties
683 * supplied in form of QDict. Use NULL for empty properties list.
685 void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv,
686 const QDict *arguments);
689 * qtest_qmp_device_add:
690 * @qts: QTestState instance to operate on
691 * @driver: Name of the device that should be added
692 * @id: Identification string
693 * @fmt: QMP message to send to qemu, formatted like
694 * qobject_from_jsonf_nofail(). See parse_interpolation() for what's
695 * supported after '%'.
697 * Generic hot-plugging test via the device_add QMP command.
699 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
700 const char *fmt, ...) GCC_FMT_ATTR(4, 5);
703 * qtest_qmp_device_del:
704 * @qts: QTestState instance to operate on
705 * @id: Identification string
707 * Generic hot-unplugging test via the device_del QMP command.
709 void qtest_qmp_device_del(QTestState *qts, const char *id);
712 * qmp_rsp_is_err:
713 * @rsp: QMP response to check for error
715 * Test @rsp for error and discard @rsp.
716 * Returns 'true' if there is error in @rsp and 'false' otherwise.
718 bool qmp_rsp_is_err(QDict *rsp);
721 * qmp_expect_error_and_unref:
722 * @rsp: QMP response to check for error
723 * @class: an error class
725 * Assert the response has the given error class and discard @rsp.
727 void qmp_expect_error_and_unref(QDict *rsp, const char *class);
730 * qtest_probe_child:
731 * @s: QTestState instance to operate on.
733 * Returns: true if the child is still alive.
735 bool qtest_probe_child(QTestState *s);
738 * qtest_set_expected_status:
739 * @s: QTestState instance to operate on.
740 * @status: an expected exit status.
742 * Set expected exit status of the child.
744 void qtest_set_expected_status(QTestState *s, int status);
746 QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch,
747 void (*send)(void*, const char*));
749 void qtest_client_inproc_recv(void *opaque, const char *str);
750 #endif