aboutsummaryrefslogtreecommitdiff
path: root/tests/tpm-util.c
diff options
context:
space:
mode:
authorStefan Berger <stefanb@linux.vnet.ibm.com>2018-05-30 12:12:50 -0400
committerStefan Berger <stefanb@linux.vnet.ibm.com>2018-06-06 15:43:54 -0400
commitb1e4b7c6b299b8aba1cef9a5ac7221d9eef4e949 (patch)
tree2413569c357cf209f04bece9522ec59fecaa4131 /tests/tpm-util.c
parentc1c2a435905ae76b159c573b0c0d6f095b45ebc6 (diff)
test: Move reusable code from tpm-crb-swtpm-test.c to tpm-util.c
Move code we can reuse from tpm-crb-swtpm-test.c into tpm-util.c and prefix functions with 'tpm_util_'. Remove some unnecessary #include's. Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Diffstat (limited to 'tests/tpm-util.c')
-rw-r--r--tests/tpm-util.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/tpm-util.c b/tests/tpm-util.c
index c9b3947c1c..e6e3b922fa 100644
--- a/tests/tpm-util.c
+++ b/tests/tpm-util.c
@@ -17,6 +17,9 @@
#include "hw/acpi/tpm.h"
#include "libqtest.h"
#include "tpm-util.h"
+#include "qapi/qmp/qdict.h"
+
+static bool got_stop;
void tpm_util_crb_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
@@ -184,3 +187,89 @@ void tpm_util_swtpm_kill(GPid pid)
kill(pid, SIGKILL);
}
+
+void tpm_util_migrate(QTestState *who, const char *uri)
+{
+ QDict *rsp;
+ gchar *cmd;
+
+ cmd = g_strdup_printf("{ 'execute': 'migrate',"
+ "'arguments': { 'uri': '%s' } }",
+ uri);
+ rsp = qtest_qmp(who, cmd);
+ g_free(cmd);
+ g_assert(qdict_haskey(rsp, "return"));
+ qobject_unref(rsp);
+}
+
+/*
+ * Events can get in the way of responses we are actually waiting for.
+ */
+static QDict *tpm_util_wait_command(QTestState *who, const char *command)
+{
+ const char *event_string;
+ QDict *response;
+
+ response = qtest_qmp(who, command);
+
+ while (qdict_haskey(response, "event")) {
+ /* OK, it was an event */
+ event_string = qdict_get_str(response, "event");
+ if (!strcmp(event_string, "STOP")) {
+ got_stop = true;
+ }
+ qobject_unref(response);
+ response = qtest_qmp_receive(who);
+ }
+ return response;
+}
+
+void tpm_util_wait_for_migration_complete(QTestState *who)
+{
+ while (true) {
+ QDict *rsp, *rsp_return;
+ bool completed;
+ const char *status;
+
+ rsp = tpm_util_wait_command(who, "{ 'execute': 'query-migrate' }");
+ rsp_return = qdict_get_qdict(rsp, "return");
+ status = qdict_get_str(rsp_return, "status");
+ completed = strcmp(status, "completed") == 0;
+ g_assert_cmpstr(status, !=, "failed");
+ qobject_unref(rsp);
+ if (completed) {
+ return;
+ }
+ usleep(1000);
+ }
+}
+
+void tpm_util_migration_start_qemu(QTestState **src_qemu,
+ QTestState **dst_qemu,
+ SocketAddress *src_tpm_addr,
+ SocketAddress *dst_tpm_addr,
+ const char *miguri)
+{
+ char *src_qemu_args, *dst_qemu_args;
+
+ src_qemu_args = g_strdup_printf(
+ "-chardev socket,id=chr,path=%s "
+ "-tpmdev emulator,id=dev,chardev=chr "
+ "-device tpm-crb,tpmdev=dev ",
+ src_tpm_addr->u.q_unix.path);
+
+ *src_qemu = qtest_init(src_qemu_args);
+
+ dst_qemu_args = g_strdup_printf(
+ "-chardev socket,id=chr,path=%s "
+ "-tpmdev emulator,id=dev,chardev=chr "
+ "-device tpm-crb,tpmdev=dev "
+ "-incoming %s",
+ dst_tpm_addr->u.q_unix.path,
+ miguri);
+
+ *dst_qemu = qtest_init(dst_qemu_args);
+
+ free(src_qemu_args);
+ free(dst_qemu_args);
+}