xref: /qemu/tests/qtest/libqtest.h (revision 1addf57177a5646f86ede4eee385932b0214ab72)
1 /*
2  * QTest
3  *
4  * Copyright IBM, Corp. 2012
5  * Copyright Red Hat, Inc. 2012
6  * Copyright SUSE LINUX Products GmbH 2013
7  *
8  * Authors:
9  *  Anthony Liguori   <aliguori@us.ibm.com>
10  *  Paolo Bonzini     <pbonzini@redhat.com>
11  *  Andreas Färber    <afaerber@suse.de>
12  *
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.
15  *
16  */
17 #ifndef LIBQTEST_H
18 #define LIBQTEST_H
19 
20 #include "qapi/qmp/qobject.h"
21 #include "qapi/qmp/qdict.h"
22 #include "libqmp.h"
23 
24 typedef struct QTestState QTestState;
25 
26 /**
27  * qtest_initf:
28  * @fmt: Format for creating other arguments to pass to QEMU, formatted
29  * like sprintf().
30  *
31  * Convenience wrapper around qtest_init().
32  *
33  * Returns: #QTestState instance.
34  */
35 QTestState *qtest_initf(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
36 
37 /**
38  * qtest_vinitf:
39  * @fmt: Format for creating other arguments to pass to QEMU, formatted
40  * like vsprintf().
41  * @ap: Format arguments.
42  *
43  * Convenience wrapper around qtest_init().
44  *
45  * Returns: #QTestState instance.
46  */
47 QTestState *qtest_vinitf(const char *fmt, va_list ap) G_GNUC_PRINTF(1, 0);
48 
49 /**
50  * qtest_init:
51  * @extra_args: other arguments to pass to QEMU.  CAUTION: these
52  * arguments are subject to word splitting and shell evaluation.
53  *
54  * Returns: #QTestState instance.
55  */
56 QTestState *qtest_init(const char *extra_args);
57 
58 /**
59  * qtest_init_with_env:
60  * @var: Environment variable from where to take the QEMU binary
61  * @extra_args: Other arguments to pass to QEMU.  CAUTION: these
62  * arguments are subject to word splitting and shell evaluation.
63  *
64  * Like qtest_init(), but use a different environment variable for the
65  * QEMU binary.
66  *
67  * Returns: #QTestState instance.
68  */
69 QTestState *qtest_init_with_env(const char *var, const char *extra_args);
70 
71 /**
72  * qtest_init_without_qmp_handshake:
73  * @extra_args: other arguments to pass to QEMU.  CAUTION: these
74  * arguments are subject to word splitting and shell evaluation.
75  *
76  * Returns: #QTestState instance.
77  */
78 QTestState *qtest_init_without_qmp_handshake(const char *extra_args);
79 
80 /**
81  * qtest_init_with_serial:
82  * @extra_args: other arguments to pass to QEMU.  CAUTION: these
83  * arguments are subject to word splitting and shell evaluation.
84  * @sock_fd: pointer to store the socket file descriptor for
85  * connection with serial.
86  *
87  * Returns: #QTestState instance.
88  */
89 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd);
90 
91 /**
92  * qtest_system_reset:
93  * @s: #QTestState instance to operate on.
94  *
95  * Send a "system_reset" command to the QEMU under test, and wait for
96  * the reset to complete before returning.
97  */
98 void qtest_system_reset(QTestState *s);
99 
100 /**
101  * qtest_system_reset_nowait:
102  * @s: #QTestState instance to operate on.
103  *
104  * Send a "system_reset" command to the QEMU under test, but do not
105  * wait for the reset to complete before returning. The caller is
106  * responsible for waiting for either the RESET event or some other
107  * event of interest to them before proceeding.
108  *
109  * This function should only be used if you're specifically testing
110  * for some other event; in that case you can't use qtest_system_reset()
111  * because it will read and discard any other QMP events that arrive
112  * before the RESET event.
113  */
114 void qtest_system_reset_nowait(QTestState *s);
115 
116 /**
117  * qtest_wait_qemu:
118  * @s: #QTestState instance to operate on.
119  *
120  * Wait for the QEMU process to terminate. It is safe to call this function
121  * multiple times.
122  */
123 void qtest_wait_qemu(QTestState *s);
124 
125 /**
126  * qtest_kill_qemu:
127  * @s: #QTestState instance to operate on.
128  *
129  * Kill the QEMU process and wait for it to terminate. It is safe to call this
130  * function multiple times. Normally qtest_quit() is used instead because it
131  * also frees QTestState. Use qtest_kill_qemu() when you just want to kill QEMU
132  * and qtest_quit() will be called later.
133  */
134 void qtest_kill_qemu(QTestState *s);
135 
136 /**
137  * qtest_quit:
138  * @s: #QTestState instance to operate on.
139  *
140  * Shut down the QEMU process associated to @s.
141  */
142 void qtest_quit(QTestState *s);
143 
144 #ifndef _WIN32
145 /**
146  * qtest_qmp_fds:
147  * @s: #QTestState instance to operate on.
148  * @fds: array of file descriptors
149  * @fds_num: number of elements in @fds
150  * @fmt: QMP message to send to qemu, formatted like
151  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
152  * supported after '%'.
153  *
154  * Sends a QMP message to QEMU with fds and returns the response.
155  */
156 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
157                      const char *fmt, ...)
158     G_GNUC_PRINTF(4, 5);
159 #endif /* _WIN32 */
160 
161 /**
162  * qtest_qmp:
163  * @s: #QTestState instance to operate on.
164  * @fmt: QMP message to send to qemu, formatted like
165  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
166  * supported after '%'.
167  *
168  * Sends a QMP message to QEMU and returns the response.
169  */
170 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
171     G_GNUC_PRINTF(2, 3);
172 
173 /**
174  * qtest_qmp_send:
175  * @s: #QTestState instance to operate on.
176  * @fmt: QMP message to send to qemu, formatted like
177  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
178  * supported after '%'.
179  *
180  * Sends a QMP message to QEMU and leaves the response in the stream.
181  */
182 void qtest_qmp_send(QTestState *s, const char *fmt, ...)
183     G_GNUC_PRINTF(2, 3);
184 
185 /**
186  * qtest_qmp_send_raw:
187  * @s: #QTestState instance to operate on.
188  * @fmt: text to send, formatted like sprintf()
189  *
190  * Sends text to the QMP monitor verbatim.  Need not be valid JSON;
191  * this is useful for negative tests.
192  */
193 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
194     G_GNUC_PRINTF(2, 3);
195 
196 /**
197  * qtest_socket_server:
198  * @socket_path: the UNIX domain socket path
199  *
200  * Create and return a listen socket file descriptor, or abort on failure.
201  */
202 int qtest_socket_server(const char *socket_path);
203 
204 #ifndef _WIN32
205 /**
206  * qtest_vqmp_fds:
207  * @s: #QTestState instance to operate on.
208  * @fds: array of file descriptors
209  * @fds_num: number of elements in @fds
210  * @fmt: QMP message to send to QEMU, formatted like
211  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
212  * supported after '%'.
213  * @ap: QMP message arguments
214  *
215  * Sends a QMP message to QEMU with fds and returns the response.
216  */
217 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
218                       const char *fmt, va_list ap)
219     G_GNUC_PRINTF(4, 0);
220 #endif /* _WIN32 */
221 
222 /**
223  * qtest_vqmp:
224  * @s: #QTestState instance to operate on.
225  * @fmt: QMP message to send to QEMU, formatted like
226  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
227  * supported after '%'.
228  * @ap: QMP message arguments
229  *
230  * Sends a QMP message to QEMU and returns the response.
231  */
232 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
233     G_GNUC_PRINTF(2, 0);
234 
235 #ifndef _WIN32
236 /**
237  * qtest_qmp_vsend_fds:
238  * @s: #QTestState instance to operate on.
239  * @fds: array of file descriptors
240  * @fds_num: number of elements in @fds
241  * @fmt: QMP message to send to QEMU, formatted like
242  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
243  * supported after '%'.
244  * @ap: QMP message arguments
245  *
246  * Sends a QMP message to QEMU and leaves the response in the stream.
247  */
248 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
249                          const char *fmt, va_list ap)
250     G_GNUC_PRINTF(4, 0);
251 #endif /* _WIN32 */
252 
253 /**
254  * qtest_qmp_vsend:
255  * @s: #QTestState instance to operate on.
256  * @fmt: QMP message to send to QEMU, formatted like
257  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
258  * supported after '%'.
259  * @ap: QMP message arguments
260  *
261  * Sends a QMP message to QEMU and leaves the response in the stream.
262  */
263 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
264     G_GNUC_PRINTF(2, 0);
265 
266 /**
267  * qtest_qmp_receive_dict:
268  * @s: #QTestState instance to operate on.
269  *
270  * Reads a QMP message from QEMU and returns the response.
271  */
272 QDict *qtest_qmp_receive_dict(QTestState *s);
273 
274 /**
275  * qtest_qmp_receive:
276  * @s: #QTestState instance to operate on.
277  *
278  * Reads a QMP message from QEMU and returns the response.
279  *
280  * If a callback is registered with qtest_qmp_set_event_callback,
281  * it will be invoked for every event seen, otherwise events
282  * will be buffered until a call to one of the qtest_qmp_eventwait
283  * family of functions.
284  */
285 QDict *qtest_qmp_receive(QTestState *s);
286 
287 /*
288  * QTestQMPEventCallback:
289  * @s: #QTestState instance event was received on
290  * @name: name of the event type
291  * @event: #QDict for the event details
292  * @opaque: opaque data from time of callback registration
293  *
294  * This callback will be invoked whenever an event is received.
295  * If the callback returns true the event will be consumed,
296  * otherwise it will be put on the list of pending events.
297  * Pending events can be later handled by calling either
298  * qtest_qmp_eventwait or qtest_qmp_eventwait_ref.
299  *
300  * Return: true to consume the event, false to let it be queued
301  */
302 typedef bool (*QTestQMPEventCallback)(QTestState *s, const char *name,
303                                       QDict *event, void *opaque);
304 
305 /**
306  * qtest_qmp_set_event_callback:
307  * @s: #QTestSTate instance to operate on
308  * @cb: callback to invoke for events
309  * @opaque: data to pass to @cb
310  *
311  * Register a callback to be invoked whenever an event arrives
312  */
313 void qtest_qmp_set_event_callback(QTestState *s,
314                                   QTestQMPEventCallback cb, void *opaque);
315 
316 /**
317  * qtest_qmp_eventwait:
318  * @s: #QTestState instance to operate on.
319  * @event: event to wait for.
320  *
321  * Continuously polls for QMP responses until it receives the desired event.
322  *
323  * Any callback registered with qtest_qmp_set_event_callback will
324  * be invoked for every event seen.
325  */
326 void qtest_qmp_eventwait(QTestState *s, const char *event);
327 
328 /**
329  * qtest_qmp_eventwait_ref:
330  * @s: #QTestState instance to operate on.
331  * @event: event to wait for.
332  *
333  * Continuously polls for QMP responses until it receives the desired event.
334  *
335  * Any callback registered with qtest_qmp_set_event_callback will
336  * be invoked for every event seen.
337  *
338  * Returns a copy of the event for further investigation.
339  */
340 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
341 
342 /**
343  * qtest_qmp_event_ref:
344  * @s: #QTestState instance to operate on.
345  * @event: event to return.
346  *
347  * Removes non-matching events from the buffer that was set by
348  * qtest_qmp_receive, until an event bearing the given name is found,
349  * and returns it.
350  * If no event matches, clears the buffer and returns NULL.
351  *
352  */
353 QDict *qtest_qmp_event_ref(QTestState *s, const char *event);
354 
355 /**
356  * qtest_hmp:
357  * @s: #QTestState instance to operate on.
358  * @fmt: HMP command to send to QEMU, formats arguments like sprintf().
359  *
360  * Send HMP command to QEMU via QMP's human-monitor-command.
361  * QMP events are discarded.
362  *
363  * Returns: the command's output.  The caller should g_free() it.
364  */
365 char *qtest_hmp(QTestState *s, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
366 
367 /**
368  * qtest_hmpv:
369  * @s: #QTestState instance to operate on.
370  * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
371  * @ap: HMP command arguments
372  *
373  * Send HMP command to QEMU via QMP's human-monitor-command.
374  * QMP events are discarded.
375  *
376  * Returns: the command's output.  The caller should g_free() it.
377  */
378 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
379     G_GNUC_PRINTF(2, 0);
380 
381 void qtest_module_load(QTestState *s, const char *prefix, const char *libname);
382 
383 /**
384  * qtest_get_irq:
385  * @s: #QTestState instance to operate on.
386  * @num: Interrupt to observe.
387  *
388  * Returns: The level of the @num interrupt.
389  */
390 bool qtest_get_irq(QTestState *s, int num);
391 
392 /**
393  * qtest_irq_intercept_in:
394  * @s: #QTestState instance to operate on.
395  * @string: QOM path of a device.
396  *
397  * Associate qtest irqs with the GPIO-in pins of the device
398  * whose path is specified by @string.
399  */
400 void qtest_irq_intercept_in(QTestState *s, const char *string);
401 
402 /**
403  * qtest_irq_intercept_out:
404  * @s: #QTestState instance to operate on.
405  * @string: QOM path of a device.
406  *
407  * Associate qtest irqs with the GPIO-out pins of the device
408  * whose path is specified by @string.
409  */
410 void qtest_irq_intercept_out(QTestState *s, const char *string);
411 
412 /**
413  * qtest_irq_intercept_out_named:
414  * @s: #QTestState instance to operate on.
415  * @qom_path: QOM path of a device.
416  * @name: Name of the GPIO out pin
417  *
418  * Associate a qtest irq with the named GPIO-out pin of the device
419  * whose path is specified by @string and whose name is @name.
420  */
421 void qtest_irq_intercept_out_named(QTestState *s, const char *qom_path, const char *name);
422 
423 /**
424  * qtest_set_irq_in:
425  * @s: QTestState instance to operate on.
426  * @string: QOM path of a device
427  * @name: IRQ name
428  * @irq: IRQ number
429  * @level: IRQ level
430  *
431  * Force given device/irq GPIO-in pin to the given level.
432  */
433 void qtest_set_irq_in(QTestState *s, const char *string, const char *name,
434                       int irq, int level);
435 
436 /**
437  * qtest_outb:
438  * @s: #QTestState instance to operate on.
439  * @addr: I/O port to write to.
440  * @value: Value being written.
441  *
442  * Write an 8-bit value to an I/O port.
443  */
444 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
445 
446 /**
447  * qtest_outw:
448  * @s: #QTestState instance to operate on.
449  * @addr: I/O port to write to.
450  * @value: Value being written.
451  *
452  * Write a 16-bit value to an I/O port.
453  */
454 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
455 
456 /**
457  * qtest_outl:
458  * @s: #QTestState instance to operate on.
459  * @addr: I/O port to write to.
460  * @value: Value being written.
461  *
462  * Write a 32-bit value to an I/O port.
463  */
464 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
465 
466 /**
467  * qtest_inb:
468  * @s: #QTestState instance to operate on.
469  * @addr: I/O port to read from.
470  *
471  * Returns an 8-bit value from an I/O port.
472  */
473 uint8_t qtest_inb(QTestState *s, uint16_t addr);
474 
475 /**
476  * qtest_inw:
477  * @s: #QTestState instance to operate on.
478  * @addr: I/O port to read from.
479  *
480  * Returns a 16-bit value from an I/O port.
481  */
482 uint16_t qtest_inw(QTestState *s, uint16_t addr);
483 
484 /**
485  * qtest_inl:
486  * @s: #QTestState instance to operate on.
487  * @addr: I/O port to read from.
488  *
489  * Returns a 32-bit value from an I/O port.
490  */
491 uint32_t qtest_inl(QTestState *s, uint16_t addr);
492 
493 /**
494  * qtest_writeb:
495  * @s: #QTestState instance to operate on.
496  * @addr: Guest address to write to.
497  * @value: Value being written.
498  *
499  * Writes an 8-bit value to memory.
500  */
501 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
502 
503 /**
504  * qtest_writew:
505  * @s: #QTestState instance to operate on.
506  * @addr: Guest address to write to.
507  * @value: Value being written.
508  *
509  * Writes a 16-bit value to memory.
510  */
511 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
512 
513 /**
514  * qtest_writel:
515  * @s: #QTestState instance to operate on.
516  * @addr: Guest address to write to.
517  * @value: Value being written.
518  *
519  * Writes a 32-bit value to memory.
520  */
521 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
522 
523 /**
524  * qtest_writeq:
525  * @s: #QTestState instance to operate on.
526  * @addr: Guest address to write to.
527  * @value: Value being written.
528  *
529  * Writes a 64-bit value to memory.
530  */
531 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
532 
533 /**
534  * qtest_readb:
535  * @s: #QTestState instance to operate on.
536  * @addr: Guest address to read from.
537  *
538  * Reads an 8-bit value from memory.
539  *
540  * Returns: Value read.
541  */
542 uint8_t qtest_readb(QTestState *s, uint64_t addr);
543 
544 /**
545  * qtest_readw:
546  * @s: #QTestState instance to operate on.
547  * @addr: Guest address to read from.
548  *
549  * Reads a 16-bit value from memory.
550  *
551  * Returns: Value read.
552  */
553 uint16_t qtest_readw(QTestState *s, uint64_t addr);
554 
555 /**
556  * qtest_readl:
557  * @s: #QTestState instance to operate on.
558  * @addr: Guest address to read from.
559  *
560  * Reads a 32-bit value from memory.
561  *
562  * Returns: Value read.
563  */
564 uint32_t qtest_readl(QTestState *s, uint64_t addr);
565 
566 /**
567  * qtest_readq:
568  * @s: #QTestState instance to operate on.
569  * @addr: Guest address to read from.
570  *
571  * Reads a 64-bit value from memory.
572  *
573  * Returns: Value read.
574  */
575 uint64_t qtest_readq(QTestState *s, uint64_t addr);
576 
577 /**
578  * qtest_memread:
579  * @s: #QTestState instance to operate on.
580  * @addr: Guest address to read from.
581  * @data: Pointer to where memory contents will be stored.
582  * @size: Number of bytes to read.
583  *
584  * Read guest memory into a buffer.
585  */
586 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
587 
588 /**
589  * qtest_rtas_call:
590  * @s: #QTestState instance to operate on.
591  * @name: name of the command to call.
592  * @nargs: Number of args.
593  * @args: Guest address to read args from.
594  * @nret: Number of return value.
595  * @ret: Guest address to write return values to.
596  *
597  * Call an RTAS function
598  */
599 uint64_t qtest_rtas_call(QTestState *s, const char *name,
600                          uint32_t nargs, uint64_t args,
601                          uint32_t nret, uint64_t ret);
602 
603 /**
604  * qtest_csr_call:
605  * @s: #QTestState instance to operate on.
606  * @name: name of the command to call.
607  * @cpu: hart number.
608  * @csr: CSR number.
609  * @val: Value for reading/writing.
610  *
611  * Call an RISC-V CSR read/write function
612  */
613 uint64_t qtest_csr_call(QTestState *s, const char *name,
614                          uint64_t cpu, int csr,
615                          uint64_t *val);
616 
617 /**
618  * qtest_bufread:
619  * @s: #QTestState instance to operate on.
620  * @addr: Guest address to read from.
621  * @data: Pointer to where memory contents will be stored.
622  * @size: Number of bytes to read.
623  *
624  * Read guest memory into a buffer and receive using a base64 encoding.
625  */
626 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
627 
628 /**
629  * qtest_memwrite:
630  * @s: #QTestState instance to operate on.
631  * @addr: Guest address to write to.
632  * @data: Pointer to the bytes that will be written to guest memory.
633  * @size: Number of bytes to write.
634  *
635  * Write a buffer to guest memory.
636  */
637 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
638 
639 /**
640  * qtest_bufwrite:
641  * @s: #QTestState instance to operate on.
642  * @addr: Guest address to write to.
643  * @data: Pointer to the bytes that will be written to guest memory.
644  * @size: Number of bytes to write.
645  *
646  * Write a buffer to guest memory and transmit using a base64 encoding.
647  */
648 void qtest_bufwrite(QTestState *s, uint64_t addr,
649                     const void *data, size_t size);
650 
651 /**
652  * qtest_memset:
653  * @s: #QTestState instance to operate on.
654  * @addr: Guest address to write to.
655  * @patt: Byte pattern to fill the guest memory region with.
656  * @size: Number of bytes to write.
657  *
658  * Write a pattern to guest memory.
659  */
660 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
661 
662 /**
663  * qtest_clock_step_next:
664  * @s: #QTestState instance to operate on.
665  *
666  * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
667  *
668  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
669  */
670 int64_t qtest_clock_step_next(QTestState *s);
671 
672 /**
673  * qtest_clock_step:
674  * @s: QTestState instance to operate on.
675  * @step: Number of nanoseconds to advance the clock by.
676  *
677  * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
678  *
679  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
680  */
681 int64_t qtest_clock_step(QTestState *s, int64_t step);
682 
683 /**
684  * qtest_clock_set:
685  * @s: QTestState instance to operate on.
686  * @val: Nanoseconds value to advance the clock to.
687  *
688  * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
689  *
690  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
691  */
692 int64_t qtest_clock_set(QTestState *s, int64_t val);
693 
694 /**
695  * qtest_big_endian:
696  * @s: QTestState instance to operate on.
697  *
698  * Returns: True if the architecture under test has a big endian configuration.
699  */
700 bool qtest_big_endian(QTestState *s);
701 
702 /**
703  * qtest_get_arch:
704  *
705  * Returns: The architecture for the QEMU executable under test.
706  */
707 const char *qtest_get_arch(void);
708 
709 /**
710  * qtest_has_accel:
711  * @accel_name: Accelerator name to check for.
712  *
713  * Returns: true if the accelerator is built in.
714  */
715 bool qtest_has_accel(const char *accel_name);
716 
717 /**
718  * qtest_add_func:
719  * @str: Test case path.
720  * @fn: Test case function
721  *
722  * Add a GTester testcase with the given name and function.
723  * The path is prefixed with the architecture under test, as
724  * returned by qtest_get_arch().
725  */
726 void qtest_add_func(const char *str, void (*fn)(void));
727 
728 /**
729  * qtest_add_data_func:
730  * @str: Test case path.
731  * @data: Test case data
732  * @fn: Test case function
733  *
734  * Add a GTester testcase with the given name, data and function.
735  * The path is prefixed with the architecture under test, as
736  * returned by qtest_get_arch().
737  */
738 void qtest_add_data_func(const char *str, const void *data,
739                          void (*fn)(const void *));
740 
741 /**
742  * qtest_add_data_func_full:
743  * @str: Test case path.
744  * @data: Test case data
745  * @fn: Test case function
746  * @data_free_func: GDestroyNotify for data
747  *
748  * Add a GTester testcase with the given name, data and function.
749  * The path is prefixed with the architecture under test, as
750  * returned by qtest_get_arch().
751  *
752  * @data is passed to @data_free_func() on test completion.
753  */
754 void qtest_add_data_func_full(const char *str, void *data,
755                               void (*fn)(const void *),
756                               GDestroyNotify data_free_func);
757 
758 /**
759  * qtest_add:
760  * @testpath: Test case path
761  * @Fixture: Fixture type
762  * @tdata: Test case data
763  * @fsetup: Test case setup function
764  * @ftest: Test case function
765  * @fteardown: Test case teardown function
766  *
767  * Add a GTester testcase with the given name, data and functions.
768  * The path is prefixed with the architecture under test, as
769  * returned by qtest_get_arch().
770  */
771 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
772     do { \
773         char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
774         g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
775         g_free(path); \
776     } while (0)
777 
778 /**
779  * qtest_add_abrt_handler:
780  * @fn: Handler function
781  * @data: Argument that is passed to the handler
782  *
783  * Add a handler function that is invoked on SIGABRT. This can be used to
784  * terminate processes and perform other cleanup. The handler can be removed
785  * with qtest_remove_abrt_handler().
786  */
787 void qtest_add_abrt_handler(GHookFunc fn, const void *data);
788 
789 /**
790  * qtest_remove_abrt_handler:
791  * @data: Argument previously passed to qtest_add_abrt_handler()
792  *
793  * Remove an abrt handler that was previously added with
794  * qtest_add_abrt_handler().
795  */
796 void qtest_remove_abrt_handler(void *data);
797 
798 /**
799  * qtest_vqmp_assert_success_ref:
800  * @qts: QTestState instance to operate on
801  * @fmt: QMP message to send to qemu, formatted like
802  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
803  * supported after '%'.
804  * @args: variable arguments for @fmt
805  *
806  * Sends a QMP message to QEMU, asserts that a 'return' key is present in
807  * the response, and returns the response.
808  */
809 QDict *qtest_vqmp_assert_success_ref(QTestState *qts,
810                                      const char *fmt, va_list args)
811     G_GNUC_PRINTF(2, 0);
812 
813 /**
814  * qtest_vqmp_assert_success:
815  * @qts: QTestState instance to operate on
816  * @fmt: QMP message to send to qemu, formatted like
817  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
818  * supported after '%'.
819  * @args: variable arguments for @fmt
820  *
821  * Sends a QMP message to QEMU and asserts that a 'return' key is present in
822  * the response.
823  */
824 void qtest_vqmp_assert_success(QTestState *qts,
825                                const char *fmt, va_list args)
826     G_GNUC_PRINTF(2, 0);
827 
828 #ifndef _WIN32
829 /**
830  * qtest_vqmp_fds_assert_success_ref:
831  * @qts: QTestState instance to operate on
832  * @fds: the file descriptors to send
833  * @nfds: number of @fds to send
834  * @fmt: QMP message to send to qemu, formatted like
835  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
836  * supported after '%'.
837  * @args: variable arguments for @fmt
838  *
839  * Sends a QMP message with file descriptors to QEMU,
840  * asserts that a 'return' key is present in the response,
841  * and returns the response.
842  */
843 QDict *qtest_vqmp_fds_assert_success_ref(QTestState *qts, int *fds, size_t nfds,
844                                          const char *fmt, va_list args)
845     G_GNUC_PRINTF(4, 0);
846 
847 /**
848  * qtest_vqmp_fds_assert_success:
849  * @qts: QTestState instance to operate on
850  * @fds: the file descriptors to send
851  * @nfds: number of @fds to send
852  * @fmt: QMP message to send to qemu, formatted like
853  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
854  * supported after '%'.
855  * @args: variable arguments for @fmt
856  *
857  * Sends a QMP message with file descriptors to QEMU and
858  * asserts that a 'return' key is present in the response.
859  */
860 void qtest_vqmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds,
861                                    const char *fmt, va_list args)
862     G_GNUC_PRINTF(4, 0);
863 #endif /* !_WIN32 */
864 
865 /**
866  * qtest_qmp_assert_failure_ref:
867  * @qts: QTestState instance to operate on
868  * @fmt: QMP message to send to qemu, formatted like
869  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
870  * supported after '%'.
871  *
872  * Sends a QMP message to QEMU, asserts that an 'error' key is present in
873  * the response, and returns the response.
874  */
875 QDict *qtest_qmp_assert_failure_ref(QTestState *qts, const char *fmt, ...)
876     G_GNUC_PRINTF(2, 3);
877 
878 /**
879  * qtest_vqmp_assert_failure_ref:
880  * @qts: QTestState instance to operate on
881  * @fmt: QMP message to send to qemu, formatted like
882  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
883  * supported after '%'.
884  * @args: variable arguments for @fmt
885  *
886  * Sends a QMP message to QEMU, asserts that an 'error' key is present in
887  * the response, and returns the response.
888  */
889 QDict *qtest_vqmp_assert_failure_ref(QTestState *qts,
890                                      const char *fmt, va_list args)
891     G_GNUC_PRINTF(2, 0);
892 
893 /**
894  * qtest_qmp_assert_success_ref:
895  * @qts: QTestState instance to operate on
896  * @fmt: QMP message to send to qemu, formatted like
897  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
898  * supported after '%'.
899  *
900  * Sends a QMP message to QEMU, asserts that a 'return' key is present in
901  * the response, and returns the response.
902  */
903 QDict *qtest_qmp_assert_success_ref(QTestState *qts, const char *fmt, ...)
904     G_GNUC_PRINTF(2, 3);
905 
906 /**
907  * qtest_qmp_assert_success:
908  * @qts: QTestState instance to operate on
909  * @fmt: QMP message to send to qemu, formatted like
910  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
911  * supported after '%'.
912  *
913  * Sends a QMP message to QEMU and asserts that a 'return' key is present in
914  * the response.
915  */
916 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
917     G_GNUC_PRINTF(2, 3);
918 
919 #ifndef _WIN32
920 /**
921  * qtest_qmp_fd_assert_success_ref:
922  * @qts: QTestState instance to operate on
923  * @fds: the file descriptors to send
924  * @nfds: number of @fds to send
925  * @fmt: QMP message to send to qemu, formatted like
926  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
927  * supported after '%'.
928  *
929  * Sends a QMP message with file descriptors to QEMU,
930  * asserts that a 'return' key is present in the response,
931  * and returns the response.
932  */
933 QDict *qtest_qmp_fds_assert_success_ref(QTestState *qts, int *fds, size_t nfds,
934                                         const char *fmt, ...)
935     G_GNUC_PRINTF(4, 5);
936 
937 /**
938  * qtest_qmp_fd_assert_success:
939  * @qts: QTestState instance to operate on
940  * @fds: the file descriptors to send
941  * @nfds: number of @fds to send
942  * @fmt: QMP message to send to qemu, formatted like
943  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
944  * supported after '%'.
945  *
946  * Sends a QMP message with file descriptors to QEMU and
947  * asserts that a 'return' key is present in the response.
948  */
949 void qtest_qmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds,
950                                   const char *fmt, ...)
951     G_GNUC_PRINTF(4, 5);
952 #endif /* !_WIN32 */
953 
954 /**
955  * qtest_cb_for_every_machine:
956  * @cb: Pointer to the callback function
957  * @skip_old_versioned: true if versioned old machine types should be skipped
958  *
959  *  Call a callback function for every name of all available machines.
960  */
961 void qtest_cb_for_every_machine(void (*cb)(const char *machine),
962                                 bool skip_old_versioned);
963 
964 /**
965  * qtest_resolve_machine_alias:
966  * @var: Environment variable from where to take the QEMU binary
967  * @alias: The alias to resolve
968  *
969  * Returns: the machine type corresponding to the alias if any,
970  * otherwise NULL.
971  */
972 char *qtest_resolve_machine_alias(const char *var, const char *alias);
973 
974 /**
975  * qtest_has_machine:
976  * @machine: The machine to look for
977  *
978  * Returns: true if the machine is available in the target binary.
979  */
980 bool qtest_has_machine(const char *machine);
981 
982 /**
983  * qtest_has_machine_with_env:
984  * @var: Environment variable from where to take the QEMU binary
985  * @machine: The machine to look for
986  *
987  * Returns: true if the machine is available in the specified binary.
988  */
989 bool qtest_has_machine_with_env(const char *var, const char *machine);
990 
991 /**
992  * qtest_has_cpu_model:
993  * @cpu: The cpu to look for
994  *
995  * Returns: true if the cpu is available in the target binary.
996  */
997 bool qtest_has_cpu_model(const char *cpu);
998 
999 /**
1000  * qtest_has_device:
1001  * @device: The device to look for
1002  *
1003  * Returns: true if the device is available in the target binary.
1004  */
1005 bool qtest_has_device(const char *device);
1006 
1007 /**
1008  * qtest_qmp_device_add_qdict:
1009  * @qts: QTestState instance to operate on
1010  * @drv: Name of the device that should be added
1011  * @arguments: QDict with properties for the device to initialize
1012  *
1013  * Generic hot-plugging test via the device_add QMP command with properties
1014  * supplied in form of QDict. Use NULL for empty properties list.
1015  */
1016 void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv,
1017                                 const QDict *arguments);
1018 
1019 /**
1020  * qtest_qmp_device_add:
1021  * @qts: QTestState instance to operate on
1022  * @driver: Name of the device that should be added
1023  * @id: Identification string
1024  * @fmt: QMP message to send to qemu, formatted like
1025  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
1026  * supported after '%'.
1027  *
1028  * Generic hot-plugging test via the device_add QMP command.
1029  */
1030 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
1031                           const char *fmt, ...) G_GNUC_PRINTF(4, 5);
1032 
1033 /**
1034  * qtest_qmp_add_client:
1035  * @qts: QTestState instance to operate on
1036  * @protocol: the protocol to add to
1037  * @fd: the client file-descriptor
1038  *
1039  * Call QMP ``getfd`` (on Windows ``get-win32-socket``) followed by
1040  * ``add_client`` with the given @fd.
1041  */
1042 void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd);
1043 
1044 /**
1045  * qtest_qmp_device_del_send:
1046  * @qts: QTestState instance to operate on
1047  * @id: Identification string
1048  *
1049  * Generic hot-unplugging test via the device_del QMP command.
1050  */
1051 void qtest_qmp_device_del_send(QTestState *qts, const char *id);
1052 
1053 /**
1054  * qtest_qmp_device_del:
1055  * @qts: QTestState instance to operate on
1056  * @id: Identification string
1057  *
1058  * Generic hot-unplugging test via the device_del QMP command.
1059  * Waiting for command completion event.
1060  */
1061 void qtest_qmp_device_del(QTestState *qts, const char *id);
1062 
1063 /**
1064  * qtest_probe_child:
1065  * @s: QTestState instance to operate on.
1066  *
1067  * Returns: true if the child is still alive.
1068  */
1069 bool qtest_probe_child(QTestState *s);
1070 
1071 /**
1072  * qtest_set_expected_status:
1073  * @s: QTestState instance to operate on.
1074  * @status: an expected exit status.
1075  *
1076  * Set expected exit status of the child.
1077  */
1078 void qtest_set_expected_status(QTestState *s, int status);
1079 
1080 QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch,
1081                     void (*send)(void*, const char*));
1082 
1083 void qtest_client_inproc_recv(void *opaque, const char *str);
1084 
1085 /**
1086  * qtest_qom_set_bool:
1087  * @s: QTestState instance to operate on.
1088  * @path: Path to the property being set.
1089  * @property: Property being set.
1090  * @value: Value to set the property.
1091  *
1092  * Set the property with passed in value.
1093  */
1094 void qtest_qom_set_bool(QTestState *s, const char *path, const char *property,
1095                          bool value);
1096 
1097 /**
1098  * qtest_qom_get_bool:
1099  * @s: QTestState instance to operate on.
1100  * @path: Path to the property being retrieved.
1101  * @property: Property from where the value is being retrieved.
1102  *
1103  * Returns: Value retrieved from property.
1104  */
1105 bool qtest_qom_get_bool(QTestState *s, const char *path, const char *property);
1106 
1107 /**
1108  * qtest_pid:
1109  * @s: QTestState instance to operate on.
1110  *
1111  * Returns: the PID of the QEMU process, or <= 0
1112  */
1113 pid_t qtest_pid(QTestState *s);
1114 
1115 /**
1116  * have_qemu_img:
1117  *
1118  * Returns: true if "qemu-img" is available.
1119  */
1120 bool have_qemu_img(void);
1121 
1122 /**
1123  * mkimg:
1124  * @file: File name of the image that should be created
1125  * @fmt: Format, e.g. "qcow2" or "raw"
1126  * @size_mb: Size of the image in megabytes
1127  *
1128  * Create a disk image with qemu-img. Note that the QTEST_QEMU_IMG
1129  * environment variable must point to the qemu-img file.
1130  *
1131  * Returns: true if the image has been created successfully.
1132  */
1133 bool mkimg(const char *file, const char *fmt, unsigned size_mb);
1134 
1135 #endif
1136