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     TPMTestState 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     test.tpm_version = TPM_VERSION_2_0;
50 
51     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
52     tpm_emu_test_wait_cond(&test);
53 
54     args = g_strdup_printf(
55         "-machine virt,gic-version=max -accel tcg "
56         "-chardev socket,id=chr,path=%s "
57         "-tpmdev emulator,id=dev,chardev=chr "
58         "-device tpm-tis-device,tpmdev=dev",
59         test.addr->u.q_unix.path);
60     qtest_start(args);
61 
62     qtest_add_data_func("/tpm-tis/test_check_localities", &test,
63                         tpm_tis_test_check_localities);
64 
65     qtest_add_data_func("/tpm-tis/test_check_access_reg", &test,
66                         tpm_tis_test_check_access_reg);
67 
68     qtest_add_data_func("/tpm-tis/test_check_access_reg_seize", &test,
69                         tpm_tis_test_check_access_reg_seize);
70 
71     qtest_add_data_func("/tpm-tis/test_check_access_reg_release", &test,
72                         tpm_tis_test_check_access_reg_release);
73 
74     qtest_add_data_func("/tpm-tis/test_check_transmit", &test,
75                         tpm_tis_test_check_transmit);
76 
77     ret = g_test_run();
78 
79     qtest_end();
80 
81     g_thread_join(thread);
82     g_unlink(test.addr->u.q_unix.path);
83     qapi_free_SocketAddress(test.addr);
84     g_rmdir(tmp_path);
85     g_free(tmp_path);
86     g_free(args);
87     return ret;
88 }
89