Skip to content

Commit

Permalink
FAPI test: rework main.fapi.c
Browse files Browse the repository at this point in the history
* The tests of the TPM state is integrated.
* A global FAPI test state was introduced.
* The test dlopen-fapi-get-random was executed without
  the check of the TPM state. Because the esys function used
  in the test program could not be loaded.

Signed-off-by: Juergen Repp <[email protected]>
  • Loading branch information
JuergenReppSIT committed Nov 22, 2023
1 parent b35c1cf commit 232c0ec
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile-test.am
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ test_integration_sys_policy_authorizeNV_int_SOURCES = test/integration/main-sys.

if FAPI
test_integration_dlopen_fapi_get_random_fint_CFLAGS = $(TESTS_CFLAGS) \
-DENABLE_WARN=1
-DENABLE_WARN=1 -DDLOPEN=1
test_integration_dlopen_fapi_get_random_fint_LDADD = $(TESTS_LDADD) $(LIBADD_DL)
test_integration_dlopen_fapi_get_random_fint_LDFLAGS = $(TESTS_LDFLAGS)
test_integration_dlopen_fapi_get_random_fint_SOURCES = \
Expand Down
112 changes: 93 additions & 19 deletions test/integration/main-fapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@

#include "tss2_esys.h"
#include "tss2_fapi.h"

#include "test-fapi.h"
#include "fapi_int.h"

#define LOGDEFAULT LOGLEVEL_INFO
#define LOGMODULE test
#include "util/log.h"
#include "util/aux_util.h"

#include "test-common.h"

char *fapi_profile = NULL;
char *tmpdir = NULL;
FAPI_CONTEXT *global_fapi_context = NULL;
TSS2_TEST_FAPI_CONTEXT *fapi_test_ctx = NULL;

struct tpm_state {
TPMS_CAPABILITY_DATA capabilities[7];
};

bool file_exists (char *path) {
struct stat buffer;
Expand Down Expand Up @@ -206,8 +211,10 @@ int init_fapi(char *profile, FAPI_CONTEXT **fapi_context)
char *config_env = NULL;
char *config_bak = NULL;
FILE *config_file;
char *tmpdir;

fapi_profile = profile;
tmpdir = fapi_test_ctx->tmpdir;

/* First we construct a fapi config file */
#if defined(FAPI_NONTPM)
Expand Down Expand Up @@ -431,7 +438,7 @@ int init_fapi(char *profile, FAPI_CONTEXT **fapi_context)
ret = EXIT_FAILURE;
goto error;
}
global_fapi_context = *fapi_context;
fapi_test_ctx->fapi_ctx = *fapi_context;
SAFE_FREE(config_env);
SAFE_FREE(config);
SAFE_FREE(config_path);
Expand All @@ -447,6 +454,66 @@ int init_fapi(char *profile, FAPI_CONTEXT **fapi_context)
return ret;
}

int
test_fapi_setup(TSS2_TEST_FAPI_CONTEXT **test_ctx)
{
char template[] = "/tmp/fapi_tmpdir.XXXXXX";
char *tmpdir = NULL;
size_t size;
int ret;

size = sizeof(TSS2_TEST_FAPI_CONTEXT);
*test_ctx = calloc(size, 1);
if (test_ctx == NULL) {
LOG_ERROR("Failed to allocate 0x%zx bytes for the test context", size);
goto error;
}

tmpdir = strdup(template);
if (!tmpdir) {
LOG_ERROR("Failed to allocate name of temp dir.");
goto error;
}
(*test_ctx)->tmpdir = mkdtemp(tmpdir);
if (!(*test_ctx)->tmpdir) {
LOG_ERROR("No temp dir created");
goto error;
}
fapi_test_ctx = *test_ctx;

ret = init_fapi(FAPI_PROFILE, &(*test_ctx)->fapi_ctx);
if (ret != 0) {
LOG_ERROR("init fapi failed.");
goto error;
}
(*test_ctx)->test_esys_ctx.esys_ctx = (*test_ctx)->fapi_ctx->esys;
(*test_ctx)->test_esys_ctx.tpm_state = malloc(sizeof(tpm_state));
if (test_ctx == NULL) {
LOG_ERROR("Failed to allocate 0x%zx bytes for tpm_state.", size);
goto error;
}

return ret;

error:
SAFE_FREE(tmpdir);
SAFE_FREE(*test_ctx);
return EXIT_ERROR;
}

void
test_fapi_teardown(TSS2_TEST_FAPI_CONTEXT *test_ctx)
{
if (test_ctx) {
if (test_ctx->fapi_ctx) {
Fapi_Finalize(&test_ctx->fapi_ctx);
}
SAFE_FREE(test_ctx->tmpdir);
SAFE_FREE(test_ctx->test_esys_ctx.tpm_state);
SAFE_FREE(test_ctx);
}
}

/**
* This program is a template for integration tests (ones that use the TCTI,
* the ESAPI, and FAPI contexts / API directly). It does nothing more than
Expand All @@ -461,40 +528,47 @@ main(int argc, char *argv[])
char *config_path = NULL;
char *config_env = NULL;
char *remove_cmd = NULL;
TSS2_TEST_FAPI_CONTEXT *test_ctx = NULL;

char template[] = "/tmp/fapi_tmpdir.XXXXXX";

tmpdir = mkdtemp(template);

if (!tmpdir) {
LOG_ERROR("No temp dir created");
return EXIT_ERROR;
ret = test_fapi_setup(&test_ctx);
if (ret != 0) {
goto error;
}
ret = init_fapi(FAPI_PROFILE, &global_fapi_context);
if (ret)

#if !defined(FAPI_NONTPM) && !defined(DLOPEN)
ret = test_fapi_checks_pre(test_ctx);
if (ret != 0) {
goto error;
}
#endif

ret = test_invoke_fapi(global_fapi_context);
ret = test_invoke_fapi(test_ctx->fapi_ctx);

LOG_INFO("Test returned %i", ret);
if (ret) goto error;

size = asprintf(&remove_cmd, "rm -r -f %s", tmpdir);
#if !defined(FAPI_NONTPM) && !defined(DLOPEN)
test_ctx->test_esys_ctx.esys_ctx = test_ctx->fapi_ctx->esys;
ret = test_fapi_checks_post(test_ctx);
if (ret != 0) {
goto error;
}
#endif

size = asprintf(&remove_cmd, "rm -r -f %s", test_ctx->tmpdir);
if (size < 0) {
LOG_ERROR("Out of memory");
ret = EXIT_ERROR;
goto error;
}
if (system(remove_cmd) != 0) {
LOG_ERROR("Directory %s can't be deleted.", tmpdir);
LOG_ERROR("Directory %s can't be deleted.", test_ctx->tmpdir);
ret = EXIT_ERROR;
goto error;
}

error:

if (global_fapi_context) Fapi_Finalize(&global_fapi_context);

test_fapi_teardown(test_ctx);
if (config) free(config);
if (config_path) free(config_path);
if (config_env) free(config_env);
Expand Down
15 changes: 14 additions & 1 deletion test/integration/test-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ struct tpm_state {
TPMS_CAPABILITY_DATA capabilities[7];
};


/** Define a proxy tcti that returns yielded on every second invocation
* thus the corresponding handling code in ESYS can be tested.
* The first invocation will be Tss2_Sys_StartUp.
Expand Down Expand Up @@ -557,6 +556,20 @@ test_esys_checks_post(TSS2_TEST_ESYS_CONTEXT *test_ctx)
return EXIT_SUCCESS;
}

int
test_fapi_checks_pre(TSS2_TEST_FAPI_CONTEXT *test_ctx)
{
return test_esys_checks_pre(&test_ctx->test_esys_ctx);
}

int
test_fapi_checks_post(TSS2_TEST_FAPI_CONTEXT *test_ctx)
{
return test_esys_checks_post(&test_ctx->test_esys_ctx);
}



void
test_esys_teardown(TSS2_TEST_ESYS_CONTEXT *test_ctx)
{
Expand Down
2 changes: 2 additions & 0 deletions test/integration/test-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ void test_esys_teardown(TSS2_TEST_ESYS_CONTEXT *test_ctx);
typedef struct {
FAPI_CONTEXT *fapi_ctx;
char *tmpdir;
char *fapi_profile;
TSS2_TEST_ESYS_CONTEXT test_esys_ctx;
tpm_state *tpm_state;
} TSS2_TEST_FAPI_CONTEXT;

Expand Down

0 comments on commit 232c0ec

Please sign in to comment.