1 /*
2  * QTest testcase for Sysbus TPM TIS talking to external swtpm and swtpm
3  * migration
4  *
5  * Copyright (c) 2018 IBM Corporation
6  *  with parts borrowed from migration-test.c that is:
7  *     Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
8  *
9  * Authors:
10  *   Stefan Berger <stefanb@linux.vnet.ibm.com>
11  *
12  * This work is licensed under the terms of the GNU GPL, version 2 or later.
13  * See the COPYING file in the top-level directory.
14  */
15 
16 #include "qemu/osdep.h"
17 
18 #include "libqtest.h"
19 #include "qemu/module.h"
20 #include "tpm-tests.h"
21 #include "hw/acpi/tpm.h"
22 
23 uint64_t tpm_tis_base_addr = 0xc000000;
24 #define MACHINE_OPTIONS "-machine virt,gic-version=max -accel tcg"
25 
26 typedef struct TestState {
27     char *src_tpm_path;
28     char *dst_tpm_path;
29     char *uri;
30 } TestState;
31 
32 static void tpm_tis_swtpm_test(const void *data)
33 {
34     const TestState *ts = data;
35 
36     tpm_test_swtpm_test(ts->src_tpm_path, tpm_util_tis_transfer,
37                         "tpm-tis-device", MACHINE_OPTIONS);
38 }
39 
40 static void tpm_tis_swtpm_migration_test(const void *data)
41 {
42     const TestState *ts = data;
43 
44     tpm_test_swtpm_migration_test(ts->src_tpm_path, ts->dst_tpm_path, ts->uri,
45                                   tpm_util_tis_transfer, "tpm-tis-device",
46                                   MACHINE_OPTIONS);
47 }
48 
49 int main(int argc, char **argv)
50 {
51     int ret;
52     TestState ts = { 0 };
53 
54     ts.src_tpm_path = g_dir_make_tmp("qemu-tpm-tis-device-swtpm-test.XXXXXX",
55                                      NULL);
56     ts.dst_tpm_path = g_dir_make_tmp("qemu-tpm-tis-device-swtpm-test.XXXXXX",
57                                      NULL);
58     ts.uri = g_strdup_printf("unix:%s/migsocket", ts.src_tpm_path);
59 
60     module_call_init(MODULE_INIT_QOM);
61     g_test_init(&argc, &argv, NULL);
62 
63     qtest_add_data_func("/tpm/tis-swtpm/test", &ts, tpm_tis_swtpm_test);
64     qtest_add_data_func("/tpm/tis-swtpm-migration/test", &ts,
65                         tpm_tis_swtpm_migration_test);
66     ret = g_test_run();
67 
68     tpm_util_rmdir(ts.dst_tpm_path);
69     g_free(ts.dst_tpm_path);
70     tpm_util_rmdir(ts.src_tpm_path);
71     g_free(ts.src_tpm_path);
72     g_free(ts.uri);
73 
74     return ret;
75 }
76