Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / tests / qtest / tpm-tests.c
blob25073d1f9e9f3303b1e4099736f769a9b2d25f98
1 /*
2 * QTest TPM commont test code
4 * Copyright (c) 2018 IBM Corporation
5 * Copyright (c) 2018 Red Hat, Inc.
7 * Authors:
8 * Stefan Berger <stefanb@linux.vnet.ibm.com>
9 * Marc-André Lureau <marcandre.lureau@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include <glib/gstdio.h>
18 #include "libqtest-single.h"
19 #include "tpm-tests.h"
21 static bool
22 tpm_test_swtpm_skip(void)
24 if (!tpm_util_swtpm_has_tpm2()) {
25 g_test_skip("swtpm not in PATH or missing --tpm2 support");
26 return true;
29 return false;
32 void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx,
33 const char *ifmodel, const char *machine_options)
35 char *args = NULL;
36 QTestState *s;
37 SocketAddress *addr = NULL;
38 gboolean succ;
39 GPid swtpm_pid;
40 GError *error = NULL;
42 if (tpm_test_swtpm_skip()) {
43 return;
46 succ = tpm_util_swtpm_start(src_tpm_path, &swtpm_pid, &addr, &error);
47 g_assert_true(succ);
49 args = g_strdup_printf(
50 "%s "
51 "-chardev socket,id=chr,path=%s "
52 "-tpmdev emulator,id=dev,chardev=chr "
53 "-device %s,tpmdev=dev",
54 machine_options ? : "", addr->u.q_unix.path, ifmodel);
56 s = qtest_start(args);
57 g_free(args);
59 tpm_util_startup(s, tx);
60 tpm_util_pcrextend(s, tx);
62 static const unsigned char tpm_pcrread_resp[] =
63 "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
64 "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
65 "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
66 "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
67 tpm_util_pcrread(s, tx, tpm_pcrread_resp,
68 sizeof(tpm_pcrread_resp));
70 qtest_end();
71 tpm_util_swtpm_kill(swtpm_pid);
73 g_unlink(addr->u.q_unix.path);
74 qapi_free_SocketAddress(addr);
77 void tpm_test_swtpm_migration_test(const char *src_tpm_path,
78 const char *dst_tpm_path,
79 const char *uri, tx_func *tx,
80 const char *ifmodel,
81 const char *machine_options)
83 gboolean succ;
84 GPid src_tpm_pid, dst_tpm_pid;
85 SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL;
86 GError *error = NULL;
87 QTestState *src_qemu, *dst_qemu;
89 if (tpm_test_swtpm_skip()) {
90 return;
93 succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid,
94 &src_tpm_addr, &error);
95 g_assert_true(succ);
97 succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid,
98 &dst_tpm_addr, &error);
99 g_assert_true(succ);
101 tpm_util_migration_start_qemu(&src_qemu, &dst_qemu,
102 src_tpm_addr, dst_tpm_addr, uri,
103 ifmodel, machine_options);
105 tpm_util_startup(src_qemu, tx);
106 tpm_util_pcrextend(src_qemu, tx);
108 static const unsigned char tpm_pcrread_resp[] =
109 "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
110 "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
111 "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
112 "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
113 tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp,
114 sizeof(tpm_pcrread_resp));
116 tpm_util_migrate(src_qemu, uri);
117 tpm_util_wait_for_migration_complete(src_qemu);
119 tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp,
120 sizeof(tpm_pcrread_resp));
122 qtest_quit(dst_qemu);
123 qtest_quit(src_qemu);
125 tpm_util_swtpm_kill(dst_tpm_pid);
126 g_unlink(dst_tpm_addr->u.q_unix.path);
127 qapi_free_SocketAddress(dst_tpm_addr);
129 tpm_util_swtpm_kill(src_tpm_pid);
130 g_unlink(src_tpm_addr->u.q_unix.path);
131 qapi_free_SocketAddress(src_tpm_addr);