xref: /openbmc/qemu/tests/qtest/tpm-tis-test.c (revision a9a5c473)
1 /*
2  * QTest testcase for ISA TPM TIS
3  *
4  * Copyright (c) 2018 Red Hat, Inc.
5  * Copyright (c) 2018 IBM Corporation
6  *
7  * Authors:
8  *   Marc-André Lureau <marcandre.lureau@redhat.com>
9  *   Stefan Berger <stefanb@linux.vnet.ibm.com>
10  *
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.
13  */
14 
15 #include "qemu/osdep.h"
16 #include <glib/gstdio.h>
17 
18 #include "hw/acpi/tpm.h"
19 #include "io/channel-socket.h"
20 #include "libqtest-single.h"
21 #include "qemu/module.h"
22 #include "tpm-emu.h"
23 #include "tpm-tis-util.h"
24 
25 uint64_t tpm_tis_base_addr = TPM_TIS_ADDR_BASE;
26 
27 int main(int argc, char **argv)
28 {
29     int ret;
30     char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-tis-test.XXXXXX", NULL);
31     GThread *thread;
32     TestState test;
33 
34     module_call_init(MODULE_INIT_QOM);
35     g_test_init(&argc, &argv, NULL);
36 
37     test.addr = g_new0(SocketAddress, 1);
38     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
39     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
40     g_mutex_init(&test.data_mutex);
41     g_cond_init(&test.data_cond);
42     test.data_cond_signal = false;
43 
44     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
45     tpm_emu_test_wait_cond(&test);
46 
47     args = g_strdup_printf(
48         "-chardev socket,id=chr,path=%s "
49         "-tpmdev emulator,id=dev,chardev=chr "
50         "-device tpm-tis,tpmdev=dev",
51         test.addr->u.q_unix.path);
52     qtest_start(args);
53 
54     qtest_add_data_func("/tpm-tis/test_check_localities", &test,
55                         tpm_tis_test_check_localities);
56 
57     qtest_add_data_func("/tpm-tis/test_check_access_reg", &test,
58                         tpm_tis_test_check_access_reg);
59 
60     qtest_add_data_func("/tpm-tis/test_check_access_reg_seize", &test,
61                         tpm_tis_test_check_access_reg_seize);
62 
63     qtest_add_data_func("/tpm-tis/test_check_access_reg_release", &test,
64                         tpm_tis_test_check_access_reg_release);
65 
66     qtest_add_data_func("/tpm-tis/test_check_transmit", &test,
67                         tpm_tis_test_check_transmit);
68 
69     ret = g_test_run();
70 
71     qtest_end();
72 
73     g_thread_join(thread);
74     g_unlink(test.addr->u.q_unix.path);
75     qapi_free_SocketAddress(test.addr);
76     g_rmdir(tmp_path);
77     g_free(tmp_path);
78     g_free(args);
79     return ret;
80 }
81