xref: /qemu/tests/qtest/tpm-tis-device-test.c (revision 27a4a30e)
1 /*
2  * QTest testcase for SYSBUS 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 "io/channel-socket.h"
19 #include "libqtest-single.h"
20 #include "qemu/module.h"
21 #include "tpm-emu.h"
22 #include "tpm-util.h"
23 #include "tpm-tis-util.h"
24 
25 /*
26  * As the Sysbus tpm-tis-device is instantiated on the ARM virt
27  * platform bus and it is the only sysbus device dynamically
28  * instantiated, it gets plugged at its base address
29  */
30 uint64_t tpm_tis_base_addr = 0xc000000;
31 
32 int main(int argc, char **argv)
33 {
34     char *tmp_path = g_dir_make_tmp("qemu-tpm-tis-device-test.XXXXXX", NULL);
35     GThread *thread;
36     TestState test;
37     char *args;
38     int ret;
39 
40     module_call_init(MODULE_INIT_QOM);
41     g_test_init(&argc, &argv, NULL);
42 
43     test.addr = g_new0(SocketAddress, 1);
44     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
45     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
46     g_mutex_init(&test.data_mutex);
47     g_cond_init(&test.data_cond);
48     test.data_cond_signal = false;
49 
50     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
51     tpm_emu_test_wait_cond(&test);
52 
53     args = g_strdup_printf(
54         "-machine virt,gic-version=max -accel tcg "
55         "-chardev socket,id=chr,path=%s "
56         "-tpmdev emulator,id=dev,chardev=chr "
57         "-device tpm-tis-device,tpmdev=dev",
58         test.addr->u.q_unix.path);
59     qtest_start(args);
60 
61     qtest_add_data_func("/tpm-tis/test_check_localities", &test,
62                         tpm_tis_test_check_localities);
63 
64     qtest_add_data_func("/tpm-tis/test_check_access_reg", &test,
65                         tpm_tis_test_check_access_reg);
66 
67     qtest_add_data_func("/tpm-tis/test_check_access_reg_seize", &test,
68                         tpm_tis_test_check_access_reg_seize);
69 
70     qtest_add_data_func("/tpm-tis/test_check_access_reg_release", &test,
71                         tpm_tis_test_check_access_reg_release);
72 
73     qtest_add_data_func("/tpm-tis/test_check_transmit", &test,
74                         tpm_tis_test_check_transmit);
75 
76     ret = g_test_run();
77 
78     qtest_end();
79 
80     g_thread_join(thread);
81     g_unlink(test.addr->u.q_unix.path);
82     qapi_free_SocketAddress(test.addr);
83     g_rmdir(tmp_path);
84     g_free(tmp_path);
85     g_free(args);
86     return ret;
87 }
88