From 47a7a4d404366cf6efc89f7ea1bfe30b8a8d0299 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 13 Nov 2023 16:41:16 +0100 Subject: [PATCH 1/3] tests: add macro for generating function enter log message The test-tpm2 test multiplexes a bunch of tests from a single entrypoint test that creates the TPM2 connection. This means we only get the nice log output which test we are looking for once for the entrypoint. Let's add a macro that allows it to nicely generate it for the inner tests too and use it. --- src/shared/tests.h | 3 +++ src/test/test-tpm2.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/shared/tests.h b/src/shared/tests.h index 2a6d9d9d30..d76cf2edbe 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -104,6 +104,9 @@ extern const TestFunc _weak_ __stop_SYSTEMD_TEST_TABLE[]; REGISTER_TEST(test_##name, ##__VA_ARGS__); \ static int test_##name(void) +#define TEST_LOG_FUNC() \ + log_info("/* %s() */", __func__) + static inline int run_test_table(void) { _cleanup_strv_free_ char **tests = NULL; int r = EXIT_SUCCESS; diff --git a/src/test/test-tpm2.c b/src/test/test-tpm2.c index 0469625ab1..c3506936e4 100644 --- a/src/test/test-tpm2.c +++ b/src/test/test-tpm2.c @@ -995,6 +995,8 @@ TEST(tpm2_get_srk_template) { } static void check_best_srk_template(Tpm2Context *c) { + TEST_LOG_FUNC(); + TPMT_PUBLIC template; assert_se(tpm2_get_best_srk_template(c, &template) >= 0); @@ -1009,6 +1011,8 @@ static void check_best_srk_template(Tpm2Context *c) { static void check_test_parms(Tpm2Context *c) { assert(c); + TEST_LOG_FUNC(); + TPMU_PUBLIC_PARMS parms = { .symDetail.sym = { .algorithm = TPM2_ALG_AES, @@ -1031,6 +1035,8 @@ static void check_test_parms(Tpm2Context *c) { static void check_supports_alg(Tpm2Context *c) { assert(c); + TEST_LOG_FUNC(); + /* Test invalid algs */ assert_se(!tpm2_supports_alg(c, TPM2_ALG_ERROR)); assert_se(!tpm2_supports_alg(c, TPM2_ALG_LAST + 1)); @@ -1044,6 +1050,8 @@ static void check_supports_alg(Tpm2Context *c) { static void check_supports_command(Tpm2Context *c) { assert(c); + TEST_LOG_FUNC(); + /* Test invalid commands. TPM specification Part 2 ("Structures") section "TPM_CC (Command Codes)" * states bits 31:30 and 28:16 are reserved and must be 0. */ assert_se(!tpm2_supports_command(c, UINT32_C(0x80000000))); @@ -1062,6 +1070,8 @@ static void check_supports_command(Tpm2Context *c) { } static void check_get_or_create_srk(Tpm2Context *c) { + TEST_LOG_FUNC(); + _cleanup_free_ TPM2B_PUBLIC *public = NULL; _cleanup_free_ TPM2B_NAME *name = NULL, *qname = NULL; _cleanup_(tpm2_handle_freep) Tpm2Handle *handle = NULL; @@ -1132,6 +1142,8 @@ static int check_calculate_seal(Tpm2Context *c) { assert(c); int r; + TEST_LOG_FUNC(); + _cleanup_free_ TPM2B_PUBLIC *srk_public = NULL; assert_se(tpm2_get_srk(c, NULL, &srk_public, NULL, NULL, NULL) >= 0); calculate_seal_and_unseal(c, TPM2_SRK_HANDLE, srk_public); @@ -1204,6 +1216,8 @@ static void check_seal_unseal(Tpm2Context *c) { assert(c); + TEST_LOG_FUNC(); + check_seal_unseal_for_handle(c, 0); check_seal_unseal_for_handle(c, TPM2_SRK_HANDLE); From 3aea672bff95bbebaa80bc21b79eb1275157e0f4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 13 Nov 2023 16:43:30 +0100 Subject: [PATCH 2/3] test-tpm2: skip RSA generating TPM2 tests on physical hw MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TPM2 tests that genreate an RSA primary key are fast on vtpms, but very slow on physical TPMs, simply because TPMs aren't precisely fast devices. It makes sense to keep the tests around however. Hence hide the test behind the "slow test" logic by default – but only if we run on physical hw, and keep them in place on VMs (where we'd expect a vtpm, if any). --- src/test/test-tpm2.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/test/test-tpm2.c b/src/test/test-tpm2.c index c3506936e4..06b9800dec 100644 --- a/src/test/test-tpm2.c +++ b/src/test/test-tpm2.c @@ -2,8 +2,9 @@ #include "hexdecoct.h" #include "macro.h" -#include "tpm2-util.h" #include "tests.h" +#include "tpm2-util.h" +#include "virt.h" TEST(tpm2_pcr_index_from_string) { assert_se(tpm2_pcr_index_from_string("platform-code") == 0); @@ -1142,6 +1143,11 @@ static int check_calculate_seal(Tpm2Context *c) { assert(c); int r; + if (detect_virtualization() == VIRTUALIZATION_NONE && !slow_tests_enabled()) { + log_notice("Skipping slow calculate seal TPM2 tests. Physical system detected, and slow tests disabled."); + return 0; + } + TEST_LOG_FUNC(); _cleanup_free_ TPM2B_PUBLIC *srk_public = NULL; @@ -1216,6 +1222,11 @@ static void check_seal_unseal(Tpm2Context *c) { assert(c); + if (detect_virtualization() == VIRTUALIZATION_NONE && !slow_tests_enabled()) { + log_notice("Skipping slow seal/unseal TPM2 tests. Physical system detected, and slow tests disabled."); + return; + } + TEST_LOG_FUNC(); check_seal_unseal_for_handle(c, 0); From cc8c67928159273e4f56c11a21faac7e80408c3c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 13 Nov 2023 16:53:56 +0100 Subject: [PATCH 3/3] test-tpm2: raise timeout, as RSA is slow --- src/test/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/meson.build b/src/test/meson.build index 3f1b7afbf7..a59461acc4 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -408,6 +408,7 @@ executables += [ test_template + { 'sources' : files('test-tpm2.c'), 'dependencies' : libopenssl, + 'timeout' : 120, }, test_template + { 'sources' : files('test-utmp.c'),