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