Skip to content

Commit

Permalink
test: Add swtpm migration test for the TPM TIS interface
Browse files Browse the repository at this point in the history
Add a test case for testing swtpm migration with the TPM TIS
interface.

Signed-off-by: Stefan Berger <[email protected]>
Reviewed-by: Marc-André Lureau <[email protected]>
  • Loading branch information
stefanberger committed Jun 6, 2018
1 parent ea71a33 commit 7066385
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ check-qtest-x86_64-$(CONFIG_VHOST_USER_NET_TEST_x86_64) += tests/vhost-user-test
endif
check-qtest-i386-$(CONFIG_TPM) += tests/tpm-crb-swtpm-test$(EXESUF)
check-qtest-i386-$(CONFIG_TPM) += tests/tpm-crb-test$(EXESUF)
check-qtest-i386-$(CONFIG_TPM) += tests/tpm-tis-swtpm-test$(EXESUF)
check-qtest-i386-$(CONFIG_TPM) += tests/tpm-tis-test$(EXESUF)
check-qtest-i386-$(CONFIG_SLIRP) += tests/test-netfilter$(EXESUF)
check-qtest-i386-$(CONFIG_POSIX) += tests/test-filter-mirror$(EXESUF)
Expand Down Expand Up @@ -726,6 +727,8 @@ tests/test-io-channel-socket$(EXESUF): tests/test-io-channel-socket.o \
tests/tpm-crb-swtpm-test$(EXESUF): tests/tpm-crb-swtpm-test.o tests/tpm-emu.o \
tests/tpm-util.o tests/tpm-tests.o $(test-io-obj-y)
tests/tpm-crb-test$(EXESUF): tests/tpm-crb-test.o tests/tpm-emu.o $(test-io-obj-y)
tests/tpm-tis-swtpm-test$(EXESUF): tests/tpm-tis-swtpm-test.o tests/tpm-emu.o \
tests/tpm-util.o tests/tpm-tests.o $(test-io-obj-y)
tests/tpm-tis-test$(EXESUF): tests/tpm-tis-test.o tests/tpm-emu.o $(test-io-obj-y)
tests/test-io-channel-file$(EXESUF): tests/test-io-channel-file.o \
tests/io-channel-helpers.o $(test-io-obj-y)
Expand Down
66 changes: 66 additions & 0 deletions tests/tpm-tis-swtpm-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* QTest testcase for TPM TIS talking to external swtpm and swtpm migration
*
* Copyright (c) 2018 IBM Corporation
* with parts borrowed from migration-test.c that is:
* Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
*
* Authors:
* Stefan Berger <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/

#include "qemu/osdep.h"
#include <glib/gstdio.h>

#include "libqtest.h"
#include "tpm-tests.h"

typedef struct TestState {
char *src_tpm_path;
char *dst_tpm_path;
char *uri;
} TestState;

static void tpm_tis_swtpm_test(const void *data)
{
const TestState *ts = data;

tpm_test_swtpm_test(ts->src_tpm_path, tpm_util_tis_transfer, "tpm-tis");
}

static void tpm_tis_swtpm_migration_test(const void *data)
{
const TestState *ts = data;

tpm_test_swtpm_migration_test(ts->src_tpm_path, ts->dst_tpm_path, ts->uri,
tpm_util_tis_transfer, "tpm-tis");
}

int main(int argc, char **argv)
{
int ret;
TestState ts = { 0 };

ts.src_tpm_path = g_dir_make_tmp("qemu-tpm-tis-swtpm-test.XXXXXX", NULL);
ts.dst_tpm_path = g_dir_make_tmp("qemu-tpm-tis-swtpm-test.XXXXXX", NULL);
ts.uri = g_strdup_printf("unix:%s/migsocket", ts.src_tpm_path);

module_call_init(MODULE_INIT_QOM);
g_test_init(&argc, &argv, NULL);

qtest_add_data_func("/tpm/tis-swtpm/test", &ts, tpm_tis_swtpm_test);
qtest_add_data_func("/tpm/tis-swtpm-migration/test", &ts,
tpm_tis_swtpm_migration_test);
ret = g_test_run();

g_rmdir(ts.dst_tpm_path);
g_free(ts.dst_tpm_path);
g_rmdir(ts.src_tpm_path);
g_free(ts.src_tpm_path);
g_free(ts.uri);

return ret;
}
48 changes: 48 additions & 0 deletions tests/tpm-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "tpm-util.h"
#include "qapi/qmp/qdict.h"

#define TIS_REG(LOCTY, REG) \
(TPM_TIS_ADDR_BASE + ((LOCTY) << 12) + REG)

static bool got_stop;

void tpm_util_crb_transfer(QTestState *s,
Expand Down Expand Up @@ -52,6 +55,51 @@ void tpm_util_crb_transfer(QTestState *s,
qtest_memread(s, raddr, rsp, rsp_size);
}

void tpm_util_tis_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size)
{
uint32_t sts;
uint16_t bcount;
size_t i;

/* request use of locality 0 */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
qtest_writel(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY);

sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
bcount = (sts >> 8) & 0xffff;
g_assert_cmpint(bcount, >=, req_size);

/* transmit command */
for (i = 0; i < req_size; i++) {
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO), req[i]);
}

/* start processing */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO);

uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND;
do {
sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
break;
}
} while (g_get_monotonic_time() < end_time);

sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
bcount = (sts >> 8) & 0xffff;

memset(rsp, 0, rsp_size);
for (i = 0; i < bcount; i++) {
rsp[i] = qtest_readb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO));
}

/* relinquish use of locality 0 */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS),
TPM_TIS_ACCESS_ACTIVE_LOCALITY);
}

void tpm_util_startup(QTestState *s, tx_func *tx)
{
unsigned char buffer[1024];
Expand Down
3 changes: 3 additions & 0 deletions tests/tpm-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ typedef void (tx_func)(QTestState *s,
void tpm_util_crb_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size);
void tpm_util_tis_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size);

void tpm_util_startup(QTestState *s, tx_func *tx);
void tpm_util_pcrextend(QTestState *s, tx_func *tx);
Expand Down

0 comments on commit 7066385

Please sign in to comment.