From a3f9cd27cdf53b58b6a6637f432fb6902fdbecab Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 25 May 2021 23:40:10 +0200 Subject: [PATCH] test: add simple test for PCR list parsing --- src/test/meson.build | 2 ++ src/test/test-tpm2.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/test/test-tpm2.c diff --git a/src/test/meson.build b/src/test/meson.build index e02eaf78ff9..c7b2bbfedc3 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -422,6 +422,8 @@ tests += [ [['src/test/test-sleep.c']], + [['src/test/test-tpm2.c']], + [['src/test/test-replace-var.c']], [['src/test/test-calendarspec.c']], diff --git a/src/test/test-tpm2.c b/src/test/test-tpm2.c new file mode 100644 index 00000000000..a1fd0aa03be --- /dev/null +++ b/src/test/test-tpm2.c @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "tpm2-util.h" +#include "tests.h" + +static void test_tpm2_parse_pcrs(const char *s, uint32_t mask, int ret) { + uint32_t m; + + assert_se(tpm2_parse_pcrs(s, &m) == ret); + + if (ret >= 0) + assert_se(m == mask); +} + +int main(int argc, char *argv[]) { + + test_setup_logging(LOG_DEBUG); + + test_tpm2_parse_pcrs("", 0, 0); + test_tpm2_parse_pcrs("0", 1, 0); + test_tpm2_parse_pcrs("1", 2, 0); + test_tpm2_parse_pcrs("0,1", 3, 0); + test_tpm2_parse_pcrs("0+1", 3, 0); + test_tpm2_parse_pcrs("0-1", 0, -EINVAL); + test_tpm2_parse_pcrs("0,1,2", 7, 0); + test_tpm2_parse_pcrs("0+1+2", 7, 0); + test_tpm2_parse_pcrs("0+1,2", 7, 0); + test_tpm2_parse_pcrs("0,1+2", 7, 0); + test_tpm2_parse_pcrs("0,2", 5, 0); + test_tpm2_parse_pcrs("0+2", 5, 0); + test_tpm2_parse_pcrs("foo", 0, -EINVAL); + + return 0; +} -- 2.47.3