sd/milkymist-memcard: Fix format string
[qemu/armbru.git] / tests / qtest / tpm-tests.c
bloba2f2838e15b9b49b892b3c755e1d45e8bb2a2fa1
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 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 if (addr) {
74 g_unlink(addr->u.q_unix.path);
75 qapi_free_SocketAddress(addr);
79 void tpm_test_swtpm_migration_test(const char *src_tpm_path,
80 const char *dst_tpm_path,
81 const char *uri, tx_func *tx,
82 const char *ifmodel,
83 const char *machine_options)
85 gboolean succ;
86 GPid src_tpm_pid, dst_tpm_pid;
87 SocketAddress *src_tpm_addr = NULL, *dst_tpm_addr = NULL;
88 GError *error = NULL;
89 QTestState *src_qemu, *dst_qemu;
91 if (tpm_test_swtpm_skip()) {
92 return;
95 succ = tpm_util_swtpm_start(src_tpm_path, &src_tpm_pid,
96 &src_tpm_addr, &error);
97 g_assert_true(succ);
99 succ = tpm_util_swtpm_start(dst_tpm_path, &dst_tpm_pid,
100 &dst_tpm_addr, &error);
101 g_assert_true(succ);
103 tpm_util_migration_start_qemu(&src_qemu, &dst_qemu,
104 src_tpm_addr, dst_tpm_addr, uri,
105 ifmodel, machine_options);
107 tpm_util_startup(src_qemu, tx);
108 tpm_util_pcrextend(src_qemu, tx);
110 unsigned char tpm_pcrread_resp[] =
111 "\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00"
112 "\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85"
113 "\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89"
114 "\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde";
115 tpm_util_pcrread(src_qemu, tx, tpm_pcrread_resp,
116 sizeof(tpm_pcrread_resp));
118 tpm_util_migrate(src_qemu, uri);
119 tpm_util_wait_for_migration_complete(src_qemu);
121 tpm_util_pcrread(dst_qemu, tx, tpm_pcrread_resp,
122 sizeof(tpm_pcrread_resp));
124 qtest_quit(dst_qemu);
125 qtest_quit(src_qemu);
127 tpm_util_swtpm_kill(dst_tpm_pid);
128 if (dst_tpm_addr) {
129 g_unlink(dst_tpm_addr->u.q_unix.path);
130 qapi_free_SocketAddress(dst_tpm_addr);
133 tpm_util_swtpm_kill(src_tpm_pid);
134 if (src_tpm_addr) {
135 g_unlink(src_tpm_addr->u.q_unix.path);
136 qapi_free_SocketAddress(src_tpm_addr);