2012-03-28 17:42:05 +04:00
|
|
|
/*
|
|
|
|
* QTest
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2012
|
|
|
|
* Copyright Red Hat, Inc. 2012
|
2013-02-17 01:44:03 +04:00
|
|
|
* Copyright SUSE LINUX Products GmbH 2013
|
2012-03-28 17:42:05 +04:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
2013-02-17 01:44:03 +04:00
|
|
|
* Andreas Färber <afaerber@suse.de>
|
2012-03-28 17:42:05 +04:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
2018-02-01 14:18:38 +03:00
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2017-09-11 20:19:50 +03:00
|
|
|
#include "libqtest.h"
|
2018-08-23 19:40:01 +03:00
|
|
|
#include "qemu-common.h"
|
2019-05-23 17:35:06 +03:00
|
|
|
#include "qemu/ctype.h"
|
2017-09-11 20:19:50 +03:00
|
|
|
#include "qemu/cutils.h"
|
2017-03-01 00:26:55 +03:00
|
|
|
#include "qapi/error.h"
|
2018-08-23 19:40:20 +03:00
|
|
|
#include "qapi/qmp/json-parser.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2014-06-13 11:35:18 +04:00
|
|
|
#include "qapi/qmp/qjson.h"
|
2018-02-01 14:18:38 +03:00
|
|
|
#include "qapi/qmp/qlist.h"
|
2018-02-01 14:18:40 +03:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
#define MAX_IRQ 256
|
2016-06-13 14:16:44 +03:00
|
|
|
#define SOCKET_TIMEOUT 50
|
2019-05-28 12:16:33 +03:00
|
|
|
#define SOCKET_MAX_FDS 16
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
struct QTestState
|
|
|
|
{
|
|
|
|
int fd;
|
2012-04-26 21:07:55 +04:00
|
|
|
int qmp_fd;
|
2014-02-17 17:54:17 +04:00
|
|
|
pid_t qemu_pid; /* our child QEMU process */
|
2018-12-17 06:58:16 +03:00
|
|
|
int wstatus;
|
2019-09-03 19:22:45 +03:00
|
|
|
int expected_status;
|
2016-10-07 13:14:27 +03:00
|
|
|
bool big_endian;
|
2018-12-17 06:58:16 +03:00
|
|
|
bool irq_level[MAX_IRQ];
|
|
|
|
GString *rx;
|
2012-03-28 17:42:05 +04:00
|
|
|
};
|
|
|
|
|
2015-06-19 19:45:14 +03:00
|
|
|
static GHookList abrt_hooks;
|
2014-03-27 18:09:50 +04:00
|
|
|
static struct sigaction sigact_old;
|
|
|
|
|
2016-10-07 13:14:27 +03:00
|
|
|
static int qtest_query_target_endianness(QTestState *s);
|
|
|
|
|
2012-04-26 21:07:55 +04:00
|
|
|
static int init_socket(const char *socket_path)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int sock;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
2018-09-26 15:29:33 +03:00
|
|
|
g_assert_cmpint(sock, !=, -1);
|
2012-04-26 21:07:55 +04:00
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
|
|
|
|
qemu_set_cloexec(sock);
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
} while (ret == -1 && errno == EINTR);
|
2018-09-26 15:29:33 +03:00
|
|
|
g_assert_cmpint(ret, !=, -1);
|
2014-04-17 20:19:14 +04:00
|
|
|
ret = listen(sock, 1);
|
2018-09-26 15:29:33 +03:00
|
|
|
g_assert_cmpint(ret, !=, -1);
|
2012-04-26 21:07:55 +04:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int socket_accept(int sock)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int ret;
|
2014-03-11 17:00:34 +04:00
|
|
|
struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
|
|
|
|
.tv_usec = 0 };
|
|
|
|
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
|
|
|
|
sizeof(timeout));
|
2012-04-26 21:07:55 +04:00
|
|
|
|
|
|
|
do {
|
2014-04-17 21:21:12 +04:00
|
|
|
addrlen = sizeof(addr);
|
2012-04-26 21:07:55 +04:00
|
|
|
ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
|
|
|
|
} while (ret == -1 && errno == EINTR);
|
2014-04-17 20:38:25 +04:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
|
|
|
|
}
|
2012-04-26 21:07:55 +04:00
|
|
|
close(sock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-17 06:58:16 +03:00
|
|
|
bool qtest_probe_child(QTestState *s)
|
2014-02-17 19:33:35 +04:00
|
|
|
{
|
2018-12-17 06:58:16 +03:00
|
|
|
pid_t pid = s->qemu_pid;
|
|
|
|
|
|
|
|
if (pid != -1) {
|
|
|
|
pid = waitpid(pid, &s->wstatus, WNOHANG);
|
|
|
|
if (pid == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
s->qemu_pid = -1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-24 17:24:35 +03:00
|
|
|
|
2019-09-03 19:22:45 +03:00
|
|
|
void qtest_set_expected_status(QTestState *s, int status)
|
|
|
|
{
|
|
|
|
s->expected_status = status;
|
|
|
|
}
|
|
|
|
|
2018-12-17 06:58:16 +03:00
|
|
|
static void kill_qemu(QTestState *s)
|
|
|
|
{
|
|
|
|
pid_t pid = s->qemu_pid;
|
|
|
|
int wstatus;
|
tests/libqtest: Improve kill_qemu()
In kill_qemu() we have an assert that checks that the QEMU process
didn't dump core:
assert(!WCOREDUMP(wstatus));
Unfortunately the WCOREDUMP macro here means the resulting message
is not very easy to comprehend on at least some systems:
ahci-test: tests/libqtest.c:113: kill_qemu: Assertion `!(((__extension__ (((union { __typeof(wstatus) __in; int __i; }) { .__in = (wstatus) }).__i))) & 0x80)' failed.
and it doesn't identify what signal the process took. What's more,
WCOREDUMP is not reliable - in some cases, setrlimit() coupled with
kernel dump settings can result in the flag not being set. It's
better to log ALL death by signal, instead of caring whether a core
dump was attempted (although once we know a signal happened, also
mentioning if a core dump is present can be helpful).
Furthermore, we are NOT detecting EINTR (while EINTR shouldn't be
happening if we didn't install signal handlers, it's still better
to always be robust).
Finally, even non-signal death with a non-zero status is suspicious,
since qemu's SIGINT handler is supposed to result in exit(0).
Instead of using a raw assert, print the information in an
easier to understand way:
/i386/ahci/sanity: tests/libqtest.c:129: kill_qemu() detected QEMU death from signal 11 (Segmentation fault) (core dumped)
(Of course, the really useful information would be why the QEMU
process dumped core in the first place, but we don't have that
by the time the test program has picked up the exit status.)
Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180810132800.38549-1-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Core dump reporting and commit message tweaked]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
2018-08-10 16:28:00 +03:00
|
|
|
|
2018-12-17 06:58:16 +03:00
|
|
|
/* Skip wait if qtest_probe_child already reaped. */
|
|
|
|
if (pid != -1) {
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0));
|
tests/libqtest: Improve kill_qemu()
In kill_qemu() we have an assert that checks that the QEMU process
didn't dump core:
assert(!WCOREDUMP(wstatus));
Unfortunately the WCOREDUMP macro here means the resulting message
is not very easy to comprehend on at least some systems:
ahci-test: tests/libqtest.c:113: kill_qemu: Assertion `!(((__extension__ (((union { __typeof(wstatus) __in; int __i; }) { .__in = (wstatus) }).__i))) & 0x80)' failed.
and it doesn't identify what signal the process took. What's more,
WCOREDUMP is not reliable - in some cases, setrlimit() coupled with
kernel dump settings can result in the flag not being set. It's
better to log ALL death by signal, instead of caring whether a core
dump was attempted (although once we know a signal happened, also
mentioning if a core dump is present can be helpful).
Furthermore, we are NOT detecting EINTR (while EINTR shouldn't be
happening if we didn't install signal handlers, it's still better
to always be robust).
Finally, even non-signal death with a non-zero status is suspicious,
since qemu's SIGINT handler is supposed to result in exit(0).
Instead of using a raw assert, print the information in an
easier to understand way:
/i386/ahci/sanity: tests/libqtest.c:129: kill_qemu() detected QEMU death from signal 11 (Segmentation fault) (core dumped)
(Of course, the really useful information would be why the QEMU
process dumped core in the first place, but we don't have that
by the time the test program has picked up the exit status.)
Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180810132800.38549-1-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Core dump reporting and commit message tweaked]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
2018-08-10 16:28:00 +03:00
|
|
|
assert(pid == s->qemu_pid);
|
2018-12-17 06:58:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-03 19:22:45 +03:00
|
|
|
* Check whether qemu exited with expected exit status; anything else is
|
2018-12-17 06:58:16 +03:00
|
|
|
* fishy and should be logged with as much detail as possible.
|
|
|
|
*/
|
|
|
|
wstatus = s->wstatus;
|
2019-09-03 19:22:45 +03:00
|
|
|
if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != s->expected_status) {
|
|
|
|
fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
|
|
|
|
"process but encountered exit status %d (expected %d)\n",
|
|
|
|
__FILE__, __LINE__, WEXITSTATUS(wstatus), s->expected_status);
|
|
|
|
abort();
|
|
|
|
} else if (WIFSIGNALED(wstatus)) {
|
|
|
|
int sig = WTERMSIG(wstatus);
|
|
|
|
const char *signame = strsignal(sig) ?: "unknown ???";
|
|
|
|
const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : "";
|
|
|
|
|
|
|
|
fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death "
|
|
|
|
"from signal %d (%s)%s\n",
|
|
|
|
__FILE__, __LINE__, sig, signame, dump);
|
2018-12-17 06:58:16 +03:00
|
|
|
abort();
|
2014-02-17 19:33:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 23:20:33 +03:00
|
|
|
static void kill_qemu_hook_func(void *s)
|
|
|
|
{
|
|
|
|
kill_qemu(s);
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:33:35 +04:00
|
|
|
static void sigabrt_handler(int signo)
|
|
|
|
{
|
2015-06-19 19:45:14 +03:00
|
|
|
g_hook_list_invoke(&abrt_hooks, FALSE);
|
2014-03-27 18:09:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_sigabrt_handler(void)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
/* Catch SIGABRT to clean up on g_assert() failure */
|
|
|
|
sigact = (struct sigaction){
|
|
|
|
.sa_handler = sigabrt_handler,
|
|
|
|
.sa_flags = SA_RESETHAND,
|
|
|
|
};
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigaction(SIGABRT, &sigact, &sigact_old);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_sigabrt_handler(void)
|
|
|
|
{
|
|
|
|
sigaction(SIGABRT, &sigact_old, NULL);
|
2014-02-17 19:33:35 +04:00
|
|
|
}
|
|
|
|
|
2015-12-02 23:20:33 +03:00
|
|
|
void qtest_add_abrt_handler(GHookFunc fn, const void *data)
|
2015-06-19 19:45:14 +03:00
|
|
|
{
|
|
|
|
GHook *hook;
|
|
|
|
|
|
|
|
/* Only install SIGABRT handler once */
|
|
|
|
if (!abrt_hooks.is_setup) {
|
|
|
|
g_hook_list_init(&abrt_hooks, sizeof(GHook));
|
|
|
|
}
|
2017-08-08 23:38:59 +03:00
|
|
|
setup_sigabrt_handler();
|
2015-06-19 19:45:14 +03:00
|
|
|
|
|
|
|
hook = g_hook_alloc(&abrt_hooks);
|
|
|
|
hook->func = fn;
|
|
|
|
hook->data = (void *)data;
|
|
|
|
|
|
|
|
g_hook_prepend(&abrt_hooks, hook);
|
|
|
|
}
|
|
|
|
|
2017-08-28 13:25:45 +03:00
|
|
|
static const char *qtest_qemu_binary(void)
|
|
|
|
{
|
|
|
|
const char *qemu_bin;
|
|
|
|
|
|
|
|
qemu_bin = getenv("QTEST_QEMU_BINARY");
|
|
|
|
if (!qemu_bin) {
|
|
|
|
fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qemu_bin;
|
|
|
|
}
|
|
|
|
|
2018-10-09 09:27:16 +03:00
|
|
|
QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
|
2012-03-28 17:42:05 +04:00
|
|
|
{
|
|
|
|
QTestState *s;
|
2013-04-16 18:45:15 +04:00
|
|
|
int sock, qmpsock, i;
|
2013-11-21 14:37:25 +04:00
|
|
|
gchar *socket_path;
|
|
|
|
gchar *qmp_socket_path;
|
2012-03-28 17:42:05 +04:00
|
|
|
gchar *command;
|
2017-08-28 13:25:45 +03:00
|
|
|
const char *qemu_binary = qtest_qemu_binary();
|
2012-03-28 17:42:05 +04:00
|
|
|
|
2017-10-07 02:49:56 +03:00
|
|
|
s = g_new(QTestState, 1);
|
2012-03-28 17:42:05 +04:00
|
|
|
|
2013-11-21 14:37:25 +04:00
|
|
|
socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
|
|
|
|
qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
|
2012-06-29 19:34:27 +04:00
|
|
|
|
2017-03-31 15:36:41 +03:00
|
|
|
/* It's possible that if an earlier test run crashed it might
|
|
|
|
* have left a stale unix socket lying around. Delete any
|
|
|
|
* stale old socket to avoid spurious test failures with
|
|
|
|
* tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
|
|
|
|
*/
|
|
|
|
unlink(socket_path);
|
|
|
|
unlink(qmp_socket_path);
|
|
|
|
|
2013-11-21 14:37:25 +04:00
|
|
|
sock = init_socket(socket_path);
|
|
|
|
qmpsock = init_socket(qmp_socket_path);
|
2012-03-28 17:42:05 +04:00
|
|
|
|
2015-12-02 23:20:33 +03:00
|
|
|
qtest_add_abrt_handler(kill_qemu_hook_func, s);
|
2014-02-17 19:33:35 +04:00
|
|
|
|
2018-11-15 15:29:30 +03:00
|
|
|
command = g_strdup_printf("exec %s "
|
2019-02-11 21:24:31 +03:00
|
|
|
"-qtest unix:%s "
|
2018-11-15 15:29:30 +03:00
|
|
|
"-qtest-log %s "
|
2019-02-11 21:24:31 +03:00
|
|
|
"-chardev socket,path=%s,id=char0 "
|
2018-10-09 09:27:16 +03:00
|
|
|
"-mon chardev=char0,mode=control "
|
2019-09-04 08:27:39 +03:00
|
|
|
"-accel qtest "
|
2018-11-15 15:29:30 +03:00
|
|
|
"-display none "
|
|
|
|
"%s", qemu_binary, socket_path,
|
|
|
|
getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
|
2018-10-09 09:27:16 +03:00
|
|
|
qmp_socket_path,
|
2018-11-15 15:29:30 +03:00
|
|
|
extra_args ?: "");
|
|
|
|
|
|
|
|
g_test_message("starting QEMU: %s", command);
|
|
|
|
|
2018-12-17 06:58:16 +03:00
|
|
|
s->wstatus = 0;
|
2019-09-03 19:22:45 +03:00
|
|
|
s->expected_status = 0;
|
2014-02-17 17:54:17 +04:00
|
|
|
s->qemu_pid = fork();
|
|
|
|
if (s->qemu_pid == 0) {
|
2014-08-27 15:08:52 +04:00
|
|
|
setenv("QEMU_AUDIO_DRV", "none", true);
|
2013-04-16 18:45:15 +04:00
|
|
|
execlp("/bin/sh", "sh", "-c", command, NULL);
|
|
|
|
exit(1);
|
2012-03-28 17:42:05 +04:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:29:30 +03:00
|
|
|
g_free(command);
|
2012-04-26 21:07:55 +04:00
|
|
|
s->fd = socket_accept(sock);
|
2014-03-11 17:00:34 +04:00
|
|
|
if (s->fd >= 0) {
|
|
|
|
s->qmp_fd = socket_accept(qmpsock);
|
|
|
|
}
|
2013-11-21 14:37:25 +04:00
|
|
|
unlink(socket_path);
|
|
|
|
unlink(qmp_socket_path);
|
|
|
|
g_free(socket_path);
|
|
|
|
g_free(qmp_socket_path);
|
2012-03-28 17:42:05 +04:00
|
|
|
|
2014-03-11 17:00:34 +04:00
|
|
|
g_assert(s->fd >= 0 && s->qmp_fd >= 0);
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
s->rx = g_string_new("");
|
|
|
|
for (i = 0; i < MAX_IRQ; i++) {
|
|
|
|
s->irq_level[i] = false;
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:18:07 +04:00
|
|
|
if (getenv("QTEST_STOP")) {
|
2013-11-21 14:33:17 +04:00
|
|
|
kill(s->qemu_pid, SIGSTOP);
|
2012-10-01 16:18:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-07 13:14:27 +03:00
|
|
|
/* ask endianness of the target */
|
|
|
|
|
|
|
|
s->big_endian = qtest_query_target_endianness(s);
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-03-03 15:32:23 +03:00
|
|
|
QTestState *qtest_init(const char *extra_args)
|
|
|
|
{
|
2018-10-09 09:27:16 +03:00
|
|
|
QTestState *s = qtest_init_without_qmp_handshake(extra_args);
|
2018-08-06 09:53:24 +03:00
|
|
|
QDict *greeting;
|
2017-03-03 15:32:23 +03:00
|
|
|
|
|
|
|
/* Read the QMP greeting and then do the handshake */
|
2018-08-06 09:53:24 +03:00
|
|
|
greeting = qtest_qmp_receive(s);
|
|
|
|
qobject_unref(greeting);
|
2018-08-06 09:53:25 +03:00
|
|
|
qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }"));
|
2017-03-03 15:32:23 +03:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:43 +03:00
|
|
|
QTestState *qtest_vinitf(const char *fmt, va_list ap)
|
2017-10-18 17:20:27 +03:00
|
|
|
{
|
|
|
|
char *args = g_strdup_vprintf(fmt, ap);
|
|
|
|
QTestState *s;
|
|
|
|
|
2018-08-06 09:53:43 +03:00
|
|
|
s = qtest_init(args);
|
2017-10-18 17:20:27 +03:00
|
|
|
g_free(args);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:43 +03:00
|
|
|
QTestState *qtest_initf(const char *fmt, ...)
|
2017-10-18 17:20:27 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QTestState *s;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 09:53:43 +03:00
|
|
|
s = qtest_vinitf(fmt, ap);
|
2017-10-18 17:20:27 +03:00
|
|
|
va_end(ap);
|
|
|
|
return s;
|
2019-01-29 14:46:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd)
|
|
|
|
{
|
|
|
|
int sock_fd_init;
|
|
|
|
char *sock_path, sock_dir[] = "/tmp/qtest-serial-XXXXXX";
|
|
|
|
QTestState *qts;
|
|
|
|
|
|
|
|
g_assert_true(mkdtemp(sock_dir) != NULL);
|
|
|
|
sock_path = g_strdup_printf("%s/sock", sock_dir);
|
|
|
|
|
|
|
|
sock_fd_init = init_socket(sock_path);
|
|
|
|
|
|
|
|
qts = qtest_initf("-chardev socket,id=s0,path=%s -serial chardev:s0 %s",
|
|
|
|
sock_path, extra_args);
|
|
|
|
|
|
|
|
*sock_fd = socket_accept(sock_fd_init);
|
|
|
|
|
|
|
|
unlink(sock_path);
|
|
|
|
g_free(sock_path);
|
|
|
|
rmdir(sock_dir);
|
|
|
|
|
|
|
|
g_assert_true(*sock_fd >= 0);
|
|
|
|
|
|
|
|
return qts;
|
2017-10-18 17:20:27 +03:00
|
|
|
}
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
void qtest_quit(QTestState *s)
|
|
|
|
{
|
2015-06-19 19:45:14 +03:00
|
|
|
g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
|
|
|
|
|
2014-03-27 18:09:50 +04:00
|
|
|
/* Uninstall SIGABRT handler on last instance */
|
2017-09-11 20:19:49 +03:00
|
|
|
cleanup_sigabrt_handler();
|
2014-03-27 18:09:50 +04:00
|
|
|
|
2014-02-17 19:33:35 +04:00
|
|
|
kill_qemu(s);
|
2013-06-20 10:55:28 +04:00
|
|
|
close(s->fd);
|
|
|
|
close(s->qmp_fd);
|
|
|
|
g_string_free(s->rx, true);
|
|
|
|
g_free(s);
|
2012-03-28 17:42:05 +04:00
|
|
|
}
|
|
|
|
|
2014-06-13 11:35:18 +04:00
|
|
|
static void socket_send(int fd, const char *buf, size_t size)
|
2012-03-28 17:42:05 +04:00
|
|
|
{
|
2014-06-13 11:35:18 +04:00
|
|
|
size_t offset;
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
while (offset < size) {
|
|
|
|
ssize_t len;
|
|
|
|
|
2014-06-13 11:35:18 +04:00
|
|
|
len = write(fd, buf + offset, size - offset);
|
2012-03-28 17:42:05 +04:00
|
|
|
if (len == -1 && errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_cmpint(len, >, 0);
|
|
|
|
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 11:35:18 +04:00
|
|
|
static void socket_sendf(int fd, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
gchar *str = g_strdup_vprintf(fmt, ap);
|
|
|
|
size_t size = strlen(str);
|
|
|
|
|
|
|
|
socket_send(fd, str, size);
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:07:55 +04:00
|
|
|
static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
socket_sendf(s->fd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2019-05-28 12:16:33 +03:00
|
|
|
/* Sends a message and file descriptors to the socket.
|
|
|
|
* It's needed for qmp-commands like getfd/add-fd */
|
|
|
|
static void socket_send_fds(int socket_fd, int *fds, size_t fds_num,
|
|
|
|
const char *buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
struct msghdr msg = { 0 };
|
|
|
|
char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)] = { 0 };
|
|
|
|
size_t fdsize = sizeof(int) * fds_num;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct iovec iov = { .iov_base = (char *)buf, .iov_len = buf_size };
|
|
|
|
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
if (fds && fds_num > 0) {
|
|
|
|
g_assert_cmpuint(fds_num, <, SOCKET_MAX_FDS);
|
|
|
|
|
|
|
|
msg.msg_control = control;
|
|
|
|
msg.msg_controllen = CMSG_SPACE(fdsize);
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(fdsize);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
memcpy(CMSG_DATA(cmsg), fds, fdsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = sendmsg(socket_fd, &msg, 0);
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
g_assert_cmpint(ret, >, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
static GString *qtest_recv_line(QTestState *s)
|
|
|
|
{
|
|
|
|
GString *line;
|
|
|
|
size_t offset;
|
|
|
|
char *eol;
|
|
|
|
|
|
|
|
while ((eol = strchr(s->rx->str, '\n')) == NULL) {
|
|
|
|
ssize_t len;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
len = read(s->fd, buffer, sizeof(buffer));
|
|
|
|
if (len == -1 && errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == -1 || len == 0) {
|
|
|
|
fprintf(stderr, "Broken pipe\n");
|
2018-08-15 17:19:45 +03:00
|
|
|
abort();
|
2012-03-28 17:42:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append_len(s->rx, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = eol - s->rx->str;
|
|
|
|
line = g_string_new_len(s->rx->str, offset);
|
|
|
|
g_string_erase(s->rx, 0, offset + 1);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar **qtest_rsp(QTestState *s, int expected_args)
|
|
|
|
{
|
|
|
|
GString *line;
|
|
|
|
gchar **words;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
redo:
|
|
|
|
line = qtest_recv_line(s);
|
|
|
|
words = g_strsplit(line->str, " ", 0);
|
|
|
|
g_string_free(line, TRUE);
|
|
|
|
|
|
|
|
if (strcmp(words[0], "IRQ") == 0) {
|
2017-09-11 20:19:50 +03:00
|
|
|
long irq;
|
|
|
|
int ret;
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
g_assert(words[1] != NULL);
|
|
|
|
g_assert(words[2] != NULL);
|
|
|
|
|
2017-09-11 20:19:50 +03:00
|
|
|
ret = qemu_strtol(words[2], NULL, 0, &irq);
|
|
|
|
g_assert(!ret);
|
2012-03-28 17:42:05 +04:00
|
|
|
g_assert_cmpint(irq, >=, 0);
|
|
|
|
g_assert_cmpint(irq, <, MAX_IRQ);
|
|
|
|
|
|
|
|
if (strcmp(words[1], "raise") == 0) {
|
|
|
|
s->irq_level[irq] = true;
|
|
|
|
} else {
|
|
|
|
s->irq_level[irq] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(words);
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(words[0] != NULL);
|
|
|
|
g_assert_cmpstr(words[0], ==, "OK");
|
|
|
|
|
|
|
|
if (expected_args) {
|
|
|
|
for (i = 0; i < expected_args; i++) {
|
|
|
|
g_assert(words[i] != NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_strfreev(words);
|
|
|
|
}
|
|
|
|
|
|
|
|
return words;
|
|
|
|
}
|
|
|
|
|
2016-10-07 13:14:27 +03:00
|
|
|
static int qtest_query_target_endianness(QTestState *s)
|
|
|
|
{
|
|
|
|
gchar **args;
|
|
|
|
int big_endian;
|
|
|
|
|
|
|
|
qtest_sendf(s, "endianness\n");
|
|
|
|
args = qtest_rsp(s, 1);
|
|
|
|
g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
|
|
|
|
big_endian = strcmp(args[1], "big") == 0;
|
|
|
|
g_strfreev(args);
|
|
|
|
|
|
|
|
return big_endian;
|
|
|
|
}
|
|
|
|
|
2013-10-30 17:54:33 +04:00
|
|
|
typedef struct {
|
|
|
|
JSONMessageParser parser;
|
|
|
|
QDict *response;
|
|
|
|
} QMPResponseParser;
|
|
|
|
|
2018-08-23 19:40:01 +03:00
|
|
|
static void qmp_response(void *opaque, QObject *obj, Error *err)
|
2012-04-26 21:07:55 +04:00
|
|
|
{
|
2018-08-23 19:40:01 +03:00
|
|
|
QMPResponseParser *qmp = opaque;
|
2013-10-30 17:54:33 +04:00
|
|
|
|
2018-08-23 19:40:06 +03:00
|
|
|
assert(!obj != !err);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
error_prepend(&err, "QMP JSON response parsing failed: ");
|
|
|
|
error_report_err(err);
|
2018-08-15 17:19:45 +03:00
|
|
|
abort();
|
2013-10-30 17:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(!qmp->response);
|
2018-02-24 18:40:29 +03:00
|
|
|
qmp->response = qobject_to(QDict, obj);
|
2017-02-17 23:38:16 +03:00
|
|
|
g_assert(qmp->response);
|
2013-10-30 17:54:33 +04:00
|
|
|
}
|
|
|
|
|
2015-10-02 15:58:16 +03:00
|
|
|
QDict *qmp_fd_receive(int fd)
|
2013-10-30 17:54:33 +04:00
|
|
|
{
|
|
|
|
QMPResponseParser qmp;
|
2014-08-05 01:11:03 +04:00
|
|
|
bool log = getenv("QTEST_LOG") != NULL;
|
2012-04-26 21:07:55 +04:00
|
|
|
|
2013-10-30 17:54:33 +04:00
|
|
|
qmp.response = NULL;
|
2018-08-23 19:40:01 +03:00
|
|
|
json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL);
|
2013-10-30 17:54:33 +04:00
|
|
|
while (!qmp.response) {
|
2012-04-26 21:07:55 +04:00
|
|
|
ssize_t len;
|
|
|
|
char c;
|
|
|
|
|
2015-10-02 15:58:16 +03:00
|
|
|
len = read(fd, &c, 1);
|
2012-04-26 21:07:55 +04:00
|
|
|
if (len == -1 && errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-27 17:20:43 +04:00
|
|
|
if (len == -1 || len == 0) {
|
|
|
|
fprintf(stderr, "Broken pipe\n");
|
2018-08-15 17:19:45 +03:00
|
|
|
abort();
|
2012-06-27 17:20:43 +04:00
|
|
|
}
|
|
|
|
|
2014-08-05 01:11:03 +04:00
|
|
|
if (log) {
|
|
|
|
len = write(2, &c, 1);
|
|
|
|
}
|
2013-10-30 17:54:33 +04:00
|
|
|
json_message_parser_feed(&qmp.parser, &c, 1);
|
2012-04-26 21:07:55 +04:00
|
|
|
}
|
2013-10-30 17:54:33 +04:00
|
|
|
json_message_parser_destroy(&qmp.parser);
|
|
|
|
|
|
|
|
return qmp.response;
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:58:16 +03:00
|
|
|
QDict *qtest_qmp_receive(QTestState *s)
|
|
|
|
{
|
|
|
|
return qmp_fd_receive(s->qmp_fd);
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:27:51 +03:00
|
|
|
/**
|
|
|
|
* Allow users to send a message without waiting for the reply,
|
|
|
|
* in the case that they choose to discard all replies up until
|
|
|
|
* a particular EVENT is received.
|
|
|
|
*/
|
2019-05-28 12:16:33 +03:00
|
|
|
void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, va_list ap)
|
2014-02-21 23:55:30 +04:00
|
|
|
{
|
2014-06-13 11:35:18 +04:00
|
|
|
QObject *qobj;
|
|
|
|
|
2018-08-06 09:53:29 +03:00
|
|
|
/* Going through qobject ensures we escape strings properly */
|
|
|
|
qobj = qobject_from_vjsonf_nofail(fmt, ap);
|
2014-06-13 11:35:18 +04:00
|
|
|
|
|
|
|
/* No need to send anything for an empty QObject. */
|
|
|
|
if (qobj) {
|
2014-08-12 15:41:48 +04:00
|
|
|
int log = getenv("QTEST_LOG") != NULL;
|
2014-06-13 11:35:18 +04:00
|
|
|
QString *qstr = qobject_to_json(qobj);
|
libqtest: Work around a "QMP wants a newline" bug
The next commit is going to add a test that calls qmp("null").
Curiously, this hangs. Here's why.
qmp_fd_sendv() doesn't send newlines. Not even when @fmt contains
some. At first glance, the QMP parser seems to be fine with that.
However, it turns out that it fails to react to input until it sees
either a newline, an object or an array. To reproduce, feed to a QMP
monitor like this:
$ echo -n 'null' | socat UNIX:/work/armbru/images/test-qmp STDIO
{"QMP": {"version": {"qemu": {"micro": 50, "minor": 8, "major": 2}, "package": " (v2.8.0-1195-gf84141e-dirty)"}, "capabilities": []}}
No output after the greeting.
Add a newline:
$ echo 'null' | socat UNIX:/work/armbru/images/test-qmp STDIO
{"QMP": {"version": {"qemu": {"micro": 50, "minor": 8, "major": 2}, "package": " (v2.8.0-1195-gf84141e-dirty)"}, "capabilities": []}}
{"error": {"class": "GenericError", "desc": "Expected 'object' in QMP input"}}
Correct output for input 'null'.
Add an object instead:
$ echo -n 'null { "execute": "qmp_capabilities" }' | socat UNIX:qmp-socket STDIO
{"QMP": {"version": {"qemu": {"micro": 50, "minor": 8, "major": 2}, "package": " (v2.8.0-1195-gf84141e-dirty)"}, "capabilities": []}}
{"error": {"class": "GenericError", "desc": "Expected 'object' in QMP input"}}
{"return": {}}
Also correct output.
Work around this QMP bug by having qmp_fd_sendv() append a newline.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <1488544368-30622-3-git-send-email-armbru@redhat.com>
2017-03-03 15:32:22 +03:00
|
|
|
const char *str;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BUG: QMP doesn't react to input until it sees a newline, an
|
|
|
|
* object, or an array. Work-around: give it a newline.
|
|
|
|
*/
|
|
|
|
qstring_append_chr(qstr, '\n');
|
|
|
|
str = qstring_get_str(qstr);
|
2014-06-13 11:35:18 +04:00
|
|
|
|
2014-08-12 15:41:48 +04:00
|
|
|
if (log) {
|
|
|
|
fprintf(stderr, "%s", str);
|
|
|
|
}
|
2014-06-13 11:35:18 +04:00
|
|
|
/* Send QMP request */
|
2019-05-28 12:16:33 +03:00
|
|
|
if (fds && fds_num > 0) {
|
|
|
|
socket_send_fds(fd, fds, fds_num, str, qstring_get_length(qstr));
|
|
|
|
} else {
|
|
|
|
socket_send(fd, str, qstring_get_length(qstr));
|
|
|
|
}
|
2014-06-13 11:35:18 +04:00
|
|
|
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(qstr);
|
|
|
|
qobject_unref(qobj);
|
2014-06-13 11:35:18 +04:00
|
|
|
}
|
2015-04-28 22:27:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 12:16:33 +03:00
|
|
|
void qmp_fd_vsend(int fd, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
qmp_fd_vsend_fds(s->qmp_fd, fds, fds_num, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:22 +03:00
|
|
|
void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
|
2015-10-02 15:58:16 +03:00
|
|
|
{
|
2019-05-28 12:16:33 +03:00
|
|
|
qmp_fd_vsend_fds(s->qmp_fd, NULL, 0, fmt, ap);
|
2015-10-02 15:58:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
|
|
|
|
{
|
2019-05-28 12:16:33 +03:00
|
|
|
qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
|
2015-10-02 15:58:16 +03:00
|
|
|
|
|
|
|
return qmp_fd_receive(fd);
|
|
|
|
}
|
|
|
|
|
2019-05-28 12:16:33 +03:00
|
|
|
QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
qtest_qmp_vsend_fds(s, fds, fds_num, fmt, ap);
|
|
|
|
|
|
|
|
/* Receive reply */
|
|
|
|
return qtest_qmp_receive(s);
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:44 +03:00
|
|
|
QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
|
2015-04-28 22:27:51 +03:00
|
|
|
{
|
2018-08-06 09:53:22 +03:00
|
|
|
qtest_qmp_vsend(s, fmt, ap);
|
2014-02-21 23:55:30 +04:00
|
|
|
|
|
|
|
/* Receive reply */
|
|
|
|
return qtest_qmp_receive(s);
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:58:16 +03:00
|
|
|
QDict *qmp_fd(int fd, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
response = qmp_fdv(fd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qmp_fd_send(int fd, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 09:53:22 +03:00
|
|
|
qmp_fd_vsend(fd, fmt, ap);
|
2015-10-02 15:58:16 +03:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2019-05-28 12:16:33 +03:00
|
|
|
QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
response = qtest_vqmp_fds(s, fds, fds_num, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2013-10-30 17:54:33 +04:00
|
|
|
QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 09:53:44 +03:00
|
|
|
response = qtest_vqmp(s, fmt, ap);
|
2013-10-30 17:54:33 +04:00
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:22 +03:00
|
|
|
void qtest_qmp_send(QTestState *s, const char *fmt, ...)
|
2015-04-28 22:27:51 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 09:53:22 +03:00
|
|
|
qtest_qmp_vsend(s, fmt, ap);
|
2015-04-28 22:27:51 +03:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:39:34 +03:00
|
|
|
void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap)
|
2018-08-23 19:39:33 +03:00
|
|
|
{
|
|
|
|
bool log = getenv("QTEST_LOG") != NULL;
|
2018-08-23 19:39:34 +03:00
|
|
|
char *str = g_strdup_vprintf(fmt, ap);
|
2018-08-23 19:39:33 +03:00
|
|
|
|
|
|
|
if (log) {
|
|
|
|
fprintf(stderr, "%s", str);
|
|
|
|
}
|
2018-08-23 19:39:34 +03:00
|
|
|
socket_send(fd, str, strlen(str));
|
2018-08-23 19:39:33 +03:00
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:39:34 +03:00
|
|
|
void qmp_fd_send_raw(int fd, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
qmp_fd_vsend_raw(fd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
qmp_fd_vsend_raw(s->qmp_fd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2016-11-14 19:15:54 +03:00
|
|
|
QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
|
2015-04-28 22:27:51 +03:00
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
response = qtest_qmp_receive(s);
|
|
|
|
if ((qdict_haskey(response, "event")) &&
|
|
|
|
(strcmp(qdict_get_str(response, "event"), event) == 0)) {
|
2016-11-14 19:15:54 +03:00
|
|
|
return response;
|
2015-04-28 22:27:51 +03:00
|
|
|
}
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(response);
|
2015-04-28 22:27:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 19:15:54 +03:00
|
|
|
void qtest_qmp_eventwait(QTestState *s, const char *event)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
response = qtest_qmp_eventwait_ref(s, event);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(response);
|
2016-11-14 19:15:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:44 +03:00
|
|
|
char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
|
2015-10-01 11:59:55 +03:00
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
QDict *resp;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
cmd = g_strdup_vprintf(fmt, ap);
|
|
|
|
resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
|
|
|
|
" 'arguments': {'command-line': %s}}",
|
|
|
|
cmd);
|
|
|
|
ret = g_strdup(qdict_get_try_str(resp, "return"));
|
2017-03-30 10:50:05 +03:00
|
|
|
while (ret == NULL && qdict_get_try_str(resp, "event")) {
|
|
|
|
/* Ignore asynchronous QMP events */
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(resp);
|
2017-03-30 10:50:05 +03:00
|
|
|
resp = qtest_qmp_receive(s);
|
|
|
|
ret = g_strdup(qdict_get_try_str(resp, "return"));
|
|
|
|
}
|
2015-10-01 11:59:55 +03:00
|
|
|
g_assert(ret);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(resp);
|
2015-10-01 11:59:55 +03:00
|
|
|
g_free(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *qtest_hmp(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 09:53:44 +03:00
|
|
|
ret = qtest_vhmp(s, fmt, ap);
|
2015-10-01 11:59:55 +03:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-04-28 22:27:51 +03:00
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
const char *qtest_get_arch(void)
|
|
|
|
{
|
2017-08-28 13:25:45 +03:00
|
|
|
const char *qemu = qtest_qemu_binary();
|
2012-03-28 17:42:05 +04:00
|
|
|
const char *end = strrchr(qemu, '/');
|
|
|
|
|
|
|
|
return end + strlen("/qemu-system-");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool qtest_get_irq(QTestState *s, int num)
|
|
|
|
{
|
|
|
|
/* dummy operation in order to make sure irq is up to date */
|
|
|
|
qtest_inb(s, 0);
|
|
|
|
|
|
|
|
return s->irq_level[num];
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:51:40 +03:00
|
|
|
void qtest_module_load(QTestState *s, const char *prefix, const char *libname)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "module_load %s %s\n", prefix, libname);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
static int64_t qtest_clock_rsp(QTestState *s)
|
|
|
|
{
|
|
|
|
gchar **words;
|
|
|
|
int64_t clock;
|
|
|
|
words = qtest_rsp(s, 2);
|
|
|
|
clock = g_ascii_strtoll(words[1], NULL, 0);
|
|
|
|
g_strfreev(words);
|
|
|
|
return clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qtest_clock_step_next(QTestState *s)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "clock_step\n");
|
|
|
|
return qtest_clock_rsp(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qtest_clock_step(QTestState *s, int64_t step)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "clock_step %"PRIi64"\n", step);
|
|
|
|
return qtest_clock_rsp(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qtest_clock_set(QTestState *s, int64_t val)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "clock_set %"PRIi64"\n", val);
|
|
|
|
return qtest_clock_rsp(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:23:47 +03:00
|
|
|
void qtest_set_irq_in(QTestState *s, const char *qom_path, const char *name,
|
|
|
|
int num, int level)
|
|
|
|
{
|
|
|
|
if (!name) {
|
|
|
|
name = "unnamed-gpio-in";
|
|
|
|
}
|
|
|
|
qtest_sendf(s, "set_irq_in %s %s %d %d\n", qom_path, name, num, level);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
qtest_out(s, "outb", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
|
|
|
|
{
|
|
|
|
qtest_out(s, "outw", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
qtest_out(s, "outl", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
|
|
|
|
{
|
|
|
|
gchar **args;
|
2017-09-11 20:19:50 +03:00
|
|
|
int ret;
|
|
|
|
unsigned long value;
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
qtest_sendf(s, "%s 0x%x\n", cmd, addr);
|
|
|
|
args = qtest_rsp(s, 2);
|
2017-09-11 20:19:50 +03:00
|
|
|
ret = qemu_strtoul(args[1], NULL, 0, &value);
|
|
|
|
g_assert(!ret && value <= UINT32_MAX);
|
2012-03-28 17:42:05 +04:00
|
|
|
g_strfreev(args);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t qtest_inb(QTestState *s, uint16_t addr)
|
|
|
|
{
|
|
|
|
return qtest_in(s, "inb", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t qtest_inw(QTestState *s, uint16_t addr)
|
|
|
|
{
|
|
|
|
return qtest_in(s, "inw", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t qtest_inl(QTestState *s, uint16_t addr)
|
|
|
|
{
|
|
|
|
return qtest_in(s, "inl", addr);
|
|
|
|
}
|
|
|
|
|
2013-02-17 01:44:03 +04:00
|
|
|
static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
|
|
|
|
uint64_t value)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writeb", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writew", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writel", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writeq", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
|
|
|
|
{
|
|
|
|
gchar **args;
|
2017-09-11 20:19:50 +03:00
|
|
|
int ret;
|
2013-02-17 01:44:03 +04:00
|
|
|
uint64_t value;
|
|
|
|
|
|
|
|
qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
|
|
|
|
args = qtest_rsp(s, 2);
|
2017-09-11 20:19:50 +03:00
|
|
|
ret = qemu_strtou64(args[1], NULL, 0, &value);
|
|
|
|
g_assert(!ret);
|
2013-02-17 01:44:03 +04:00
|
|
|
g_strfreev(args);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t qtest_readb(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readb", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t qtest_readw(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readw", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t qtest_readl(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readl", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t qtest_readq(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readq", addr);
|
|
|
|
}
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
static int hex2nib(char ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
return ch - '0';
|
|
|
|
} else if (ch >= 'a' && ch <= 'f') {
|
|
|
|
return 10 + (ch - 'a');
|
|
|
|
} else if (ch >= 'A' && ch <= 'F') {
|
|
|
|
return 10 + (ch - 'a');
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = data;
|
|
|
|
gchar **args;
|
|
|
|
size_t i;
|
|
|
|
|
2017-01-11 11:49:32 +03:00
|
|
|
if (!size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-23 23:40:08 +04:00
|
|
|
qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
|
2012-03-28 17:42:05 +04:00
|
|
|
args = qtest_rsp(s, 2);
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
|
|
|
|
ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(args);
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:52:45 +03:00
|
|
|
uint64_t qtest_rtas_call(QTestState *s, const char *name,
|
|
|
|
uint32_t nargs, uint64_t args,
|
|
|
|
uint32_t nret, uint64_t ret)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
|
|
|
|
name, nargs, args, nret, ret);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-02 23:20:33 +03:00
|
|
|
void qtest_add_func(const char *str, void (*fn)(void))
|
2012-03-28 17:42:05 +04:00
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
|
|
|
g_test_add_func(path, fn);
|
2014-08-05 01:11:22 +04:00
|
|
|
g_free(path);
|
2012-03-28 17:42:05 +04:00
|
|
|
}
|
|
|
|
|
2016-07-19 14:42:01 +03:00
|
|
|
void qtest_add_data_func_full(const char *str, void *data,
|
|
|
|
void (*fn)(const void *),
|
|
|
|
GDestroyNotify data_free_func)
|
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
|
|
|
g_test_add_data_func_full(path, data, fn, data_free_func);
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2015-12-02 23:20:33 +03:00
|
|
|
void qtest_add_data_func(const char *str, const void *data,
|
|
|
|
void (*fn)(const void *))
|
2015-03-24 21:33:34 +03:00
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
|
|
|
g_test_add_data_func(path, data, fn);
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2015-05-22 21:13:44 +03:00
|
|
|
void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
gchar *bdata;
|
|
|
|
|
|
|
|
bdata = g_base64_encode(data, size);
|
|
|
|
qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
|
|
|
|
socket_send(s->fd, bdata, strlen(bdata));
|
|
|
|
socket_send(s->fd, "\n", 1);
|
|
|
|
qtest_rsp(s, 0);
|
|
|
|
g_free(bdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
|
|
|
|
{
|
|
|
|
gchar **args;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
|
|
|
|
args = qtest_rsp(s, 2);
|
|
|
|
|
|
|
|
g_base64_decode_inplace(args[1], &len);
|
|
|
|
if (size != len) {
|
|
|
|
fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
|
|
|
|
size, len);
|
|
|
|
len = MIN(len, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, args[1], len);
|
|
|
|
g_strfreev(args);
|
|
|
|
}
|
|
|
|
|
2012-03-28 17:42:05 +04:00
|
|
|
void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
const uint8_t *ptr = data;
|
|
|
|
size_t i;
|
2017-01-11 11:49:32 +03:00
|
|
|
char *enc;
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
enc = g_malloc(2 * size + 1);
|
2012-03-28 17:42:05 +04:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2015-05-22 21:13:44 +03:00
|
|
|
sprintf(&enc[i * 2], "%02x", ptr[i]);
|
2012-03-28 17:42:05 +04:00
|
|
|
}
|
2015-05-22 21:13:44 +03:00
|
|
|
|
|
|
|
qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
|
2012-03-28 17:42:05 +04:00
|
|
|
qtest_rsp(s, 0);
|
2015-05-22 21:13:44 +03:00
|
|
|
g_free(enc);
|
2012-03-28 17:42:05 +04:00
|
|
|
}
|
2014-03-13 15:24:15 +04:00
|
|
|
|
2014-08-05 01:11:20 +04:00
|
|
|
void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
|
|
|
|
{
|
2015-05-22 21:13:44 +03:00
|
|
|
qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
|
2014-08-05 01:11:20 +04:00
|
|
|
qtest_rsp(s, 0);
|
2014-03-13 15:24:15 +04:00
|
|
|
}
|
|
|
|
|
2019-07-23 22:22:39 +03:00
|
|
|
void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
|
2019-05-08 12:58:45 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-07-23 22:22:39 +03:00
|
|
|
response = qtest_vqmp(qts, fmt, ap);
|
2019-05-08 12:58:45 +03:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
g_assert(response);
|
|
|
|
if (!qdict_haskey(response, "return")) {
|
|
|
|
QString *s = qobject_to_json_pretty(QOBJECT(response));
|
|
|
|
g_test_message("%s", qstring_get_str(s));
|
|
|
|
qobject_unref(s);
|
|
|
|
}
|
|
|
|
g_assert(qdict_haskey(response, "return"));
|
|
|
|
qobject_unref(response);
|
|
|
|
}
|
|
|
|
|
2016-10-07 13:14:27 +03:00
|
|
|
bool qtest_big_endian(QTestState *s)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2016-10-07 13:14:27 +03:00
|
|
|
return s->big_endian;
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
2017-03-30 10:50:06 +03:00
|
|
|
|
2018-08-16 14:35:55 +03:00
|
|
|
static bool qtest_check_machine_version(const char *mname, const char *basename,
|
|
|
|
int major, int minor)
|
|
|
|
{
|
|
|
|
char *newname;
|
|
|
|
bool is_equal;
|
|
|
|
|
|
|
|
newname = g_strdup_printf("%s-%i.%i", basename, major, minor);
|
|
|
|
is_equal = g_str_equal(mname, newname);
|
|
|
|
g_free(newname);
|
|
|
|
|
|
|
|
return is_equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qtest_is_old_versioned_machine(const char *mname)
|
|
|
|
{
|
|
|
|
const char *dash = strrchr(mname, '-');
|
|
|
|
const char *dot = strrchr(mname, '.');
|
|
|
|
const char *chr;
|
|
|
|
char *bname;
|
|
|
|
const int major = QEMU_VERSION_MAJOR;
|
|
|
|
const int minor = QEMU_VERSION_MINOR;
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
if (dash && dot && dot > dash) {
|
|
|
|
for (chr = dash + 1; *chr; chr++) {
|
|
|
|
if (!qemu_isdigit(*chr) && *chr != '.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now check if it is one of the latest versions. Check major + 1
|
|
|
|
* and minor + 1 versions as well, since they might already exist
|
|
|
|
* in the development branch.
|
|
|
|
*/
|
|
|
|
bname = g_strdup(mname);
|
|
|
|
bname[dash - mname] = 0;
|
|
|
|
res = !qtest_check_machine_version(mname, bname, major + 1, 0) &&
|
|
|
|
!qtest_check_machine_version(mname, bname, major, minor + 1) &&
|
|
|
|
!qtest_check_machine_version(mname, bname, major, minor);
|
|
|
|
g_free(bname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_cb_for_every_machine(void (*cb)(const char *machine),
|
|
|
|
bool skip_old_versioned)
|
2017-03-30 10:50:06 +03:00
|
|
|
{
|
|
|
|
QDict *response, *minfo;
|
|
|
|
QList *list;
|
|
|
|
const QListEntry *p;
|
|
|
|
QObject *qobj;
|
|
|
|
QString *qstr;
|
|
|
|
const char *mname;
|
2019-07-22 17:34:42 +03:00
|
|
|
QTestState *qts;
|
2017-03-30 10:50:06 +03:00
|
|
|
|
2019-07-22 17:34:42 +03:00
|
|
|
qts = qtest_init("-machine none");
|
|
|
|
response = qtest_qmp(qts, "{ 'execute': 'query-machines' }");
|
2017-03-30 10:50:06 +03:00
|
|
|
g_assert(response);
|
|
|
|
list = qdict_get_qlist(response, "return");
|
|
|
|
g_assert(list);
|
|
|
|
|
|
|
|
for (p = qlist_first(list); p; p = qlist_next(p)) {
|
2018-02-24 18:40:29 +03:00
|
|
|
minfo = qobject_to(QDict, qlist_entry_obj(p));
|
2017-03-30 10:50:06 +03:00
|
|
|
g_assert(minfo);
|
|
|
|
qobj = qdict_get(minfo, "name");
|
|
|
|
g_assert(qobj);
|
2018-02-24 18:40:29 +03:00
|
|
|
qstr = qobject_to(QString, qobj);
|
2017-03-30 10:50:06 +03:00
|
|
|
g_assert(qstr);
|
|
|
|
mname = qstring_get_str(qstr);
|
2018-08-16 14:35:55 +03:00
|
|
|
if (!skip_old_versioned || !qtest_is_old_versioned_machine(mname)) {
|
|
|
|
cb(mname);
|
|
|
|
}
|
2017-03-30 10:50:06 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 17:34:42 +03:00
|
|
|
qtest_quit(qts);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(response);
|
2017-03-30 10:50:06 +03:00
|
|
|
}
|
2017-08-15 09:58:54 +03:00
|
|
|
|
2018-08-06 09:53:37 +03:00
|
|
|
QDict *qtest_qmp_receive_success(QTestState *s,
|
|
|
|
void (*event_cb)(void *opaque,
|
|
|
|
const char *event,
|
|
|
|
QDict *data),
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
QDict *response, *ret, *data;
|
|
|
|
const char *event;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
response = qtest_qmp_receive(s);
|
|
|
|
g_assert(!qdict_haskey(response, "error"));
|
|
|
|
ret = qdict_get_qdict(response, "return");
|
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
event = qdict_get_str(response, "event");
|
|
|
|
data = qdict_get_qdict(response, "data");
|
|
|
|
if (event_cb) {
|
|
|
|
event_cb(opaque, event, data);
|
|
|
|
}
|
|
|
|
qobject_unref(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
qobject_ref(ret);
|
|
|
|
qobject_unref(response);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-15 09:58:54 +03:00
|
|
|
/*
|
|
|
|
* Generic hot-plugging test via the device_add QMP command.
|
|
|
|
*/
|
2019-07-22 18:10:55 +03:00
|
|
|
void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
|
2018-08-06 09:53:35 +03:00
|
|
|
const char *fmt, ...)
|
2017-08-15 09:58:54 +03:00
|
|
|
{
|
2018-08-06 09:53:35 +03:00
|
|
|
QDict *args, *response;
|
|
|
|
va_list ap;
|
2017-08-15 09:58:54 +03:00
|
|
|
|
2018-08-06 09:53:35 +03:00
|
|
|
va_start(ap, fmt);
|
|
|
|
args = qdict_from_vjsonf_nofail(fmt, ap);
|
|
|
|
va_end(ap);
|
2017-08-15 09:58:54 +03:00
|
|
|
|
2018-08-06 09:53:35 +03:00
|
|
|
g_assert(!qdict_haskey(args, "driver") && !qdict_haskey(args, "id"));
|
|
|
|
qdict_put_str(args, "driver", driver);
|
|
|
|
qdict_put_str(args, "id", id);
|
2017-08-15 09:58:54 +03:00
|
|
|
|
2019-07-22 18:10:55 +03:00
|
|
|
response = qtest_qmp(qts, "{'execute': 'device_add', 'arguments': %p}",
|
|
|
|
args);
|
2017-08-15 09:58:54 +03:00
|
|
|
g_assert(response);
|
|
|
|
g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
|
|
|
|
g_assert(!qdict_haskey(response, "error"));
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(response);
|
2017-08-15 09:58:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-06 09:53:37 +03:00
|
|
|
static void device_deleted_cb(void *opaque, const char *name, QDict *data)
|
|
|
|
{
|
|
|
|
bool *got_event = opaque;
|
|
|
|
|
|
|
|
g_assert_cmpstr(name, ==, "DEVICE_DELETED");
|
|
|
|
*got_event = true;
|
|
|
|
}
|
|
|
|
|
2017-08-15 09:58:54 +03:00
|
|
|
/*
|
|
|
|
* Generic hot-unplugging test via the device_del QMP command.
|
|
|
|
* Device deletion will get one response and one event. For example:
|
|
|
|
*
|
|
|
|
* {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
|
|
|
|
*
|
|
|
|
* will get this one:
|
|
|
|
*
|
|
|
|
* {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
|
|
|
|
* "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
|
|
|
|
* "path": "/machine/peripheral/scsi-hd"}}
|
|
|
|
*
|
|
|
|
* and this one:
|
|
|
|
*
|
|
|
|
* {"return": {}}
|
|
|
|
*
|
|
|
|
* But the order of arrival may vary - so we've got to detect both.
|
|
|
|
*/
|
2019-07-22 18:10:55 +03:00
|
|
|
void qtest_qmp_device_del(QTestState *qts, const char *id)
|
2017-08-15 09:58:54 +03:00
|
|
|
{
|
2018-08-06 09:53:37 +03:00
|
|
|
bool got_event = false;
|
|
|
|
QDict *rsp;
|
2017-08-15 09:58:54 +03:00
|
|
|
|
2019-07-22 18:10:55 +03:00
|
|
|
qtest_qmp_send(qts, "{'execute': 'device_del', 'arguments': {'id': %s}}",
|
2018-08-06 09:53:37 +03:00
|
|
|
id);
|
2019-07-22 18:10:55 +03:00
|
|
|
rsp = qtest_qmp_receive_success(qts, device_deleted_cb, &got_event);
|
2018-08-06 09:53:37 +03:00
|
|
|
qobject_unref(rsp);
|
|
|
|
if (!got_event) {
|
2019-07-22 18:10:55 +03:00
|
|
|
rsp = qtest_qmp_receive(qts);
|
2018-08-06 09:53:37 +03:00
|
|
|
g_assert_cmpstr(qdict_get_try_str(rsp, "event"),
|
|
|
|
==, "DEVICE_DELETED");
|
|
|
|
qobject_unref(rsp);
|
2017-08-15 09:58:54 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-17 14:30:07 +03:00
|
|
|
|
|
|
|
bool qmp_rsp_is_err(QDict *rsp)
|
|
|
|
{
|
|
|
|
QDict *error = qdict_get_qdict(rsp, "error");
|
|
|
|
qobject_unref(rsp);
|
|
|
|
return !!error;
|
|
|
|
}
|
2018-08-30 18:58:07 +03:00
|
|
|
|
|
|
|
void qmp_assert_error_class(QDict *rsp, const char *class)
|
|
|
|
{
|
|
|
|
QDict *error = qdict_get_qdict(rsp, "error");
|
|
|
|
|
|
|
|
g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class);
|
|
|
|
g_assert_nonnull(qdict_get_try_str(error, "desc"));
|
|
|
|
g_assert(!qdict_haskey(rsp, "return"));
|
|
|
|
|
|
|
|
qobject_unref(rsp);
|
|
|
|
}
|