[PATCH v3 2/4] tpm: Convert sandbox-focussed tests to C

Ilias Apalodimas ilias.apalodimas at linaro.org
Mon May 26 11:40:42 CEST 2025


Hi Simon,

On Sat May 24, 2025 at 4:06 PM EEST, Simon Glass wrote:
> Some of the Python tests are a pain because they don't reset the TPM
> state before each test. Driver model tests do this, so convert the
> tests to C.
>
> This means that these tests won't run on real hardware, but we have
> tests which do TPM init, so there is still enough coverage.
>

What's needed to run DM tests on real hardware?

We have full coverage. The auto start command, which is not removed from the
python tests, will run the selftest command so I think overall this is ok.

> Rename and update the Python tpm_init test to use 'tpm autostart',
> since this deals with starting up ready for the tests below.

may 'since this fully initializes the TPM and performs the self tests'?

Other than that it looks good to me.
I can rewrite the commit message on my PR to Tom if you don't want to send a v4.

With the changes above
Reviewed-by: Ilias Apalodimas <ilias.apalodimas at linaro.org>

Thanks
/Ilias
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
>
> Changes in v3:
> - Use 'check' instead of 'test' when naming test helpers
> - Add missing tpm_self_test_full() call
>
> Changes in v2:
> - Keep test_tpm2_continue_self_test()
>
>  test/dm/tpm.c              | 77 +++++++++++++++++++++++++++++++++++++-
>  test/py/tests/test_tpm2.py | 38 +------------------
>  2 files changed, 76 insertions(+), 39 deletions(-)
>
> diff --git a/test/dm/tpm.c b/test/dm/tpm.c
> index 962a3fd1943..87c5c416daa 100644
> --- a/test/dm/tpm.c
> +++ b/test/dm/tpm.c
> @@ -49,14 +49,87 @@ static int test_tpm_init(struct unit_test_state *uts, enum tpm_version version)
>  	return 0;
>  }
>
> -static int dm_test_tpm(struct unit_test_state *uts)
> +static int dm_test_tpm_init(struct unit_test_state *uts)
>  {
>  	ut_assertok(test_tpm_init(uts, TPM_V1));
>  	ut_assertok(test_tpm_init(uts, TPM_V2));
>
>  	return 0;
>  }
> -DM_TEST(dm_test_tpm, UTF_SCAN_FDT);
> +DM_TEST(dm_test_tpm_init, UTF_SCAN_FDT);
> +
> +/* check TPM startup */
> +static int check_tpm_startup(struct unit_test_state *uts,
> +			     enum tpm_version version)
> +{
> +	struct udevice *dev;
> +
> +	/* check probe success */
> +	ut_assertok(get_tpm_version(version, &dev));
> +
> +	ut_assertok(tpm_init(dev));
> +	ut_assertok(tpm_startup(dev, TPM_ST_CLEAR));
> +
> +	return 0;
> +}
> +
> +/* test TPM startup */
> +static int dm_test_tpm_startup(struct unit_test_state *uts)
> +{
> +	ut_assertok(check_tpm_startup(uts, TPM_V1));
> +	ut_assertok(check_tpm_startup(uts, TPM_V2));
> +
> +	return 0;
> +}
> +DM_TEST(dm_test_tpm_startup, UTF_SCAN_FDT);
> +
> +static int check_tpm_self_test_full(struct unit_test_state *uts,
> +				    enum tpm_version version)
> +{
> +	struct udevice *dev;
> +
> +	ut_assertok(check_tpm_startup(uts, version));
> +
> +	ut_assertok(get_tpm_version(version, &dev));
> +	ut_assertok(tpm_self_test_full(dev));
> +
> +	return 0;
> +}
> +
> +/* Test TPM self-test full */
> +static int dm_test_tpm_self_test_full(struct unit_test_state *uts)
> +{
> +	ut_assertok(check_tpm_self_test_full(uts, TPM_V1));
> +	ut_assertok(check_tpm_self_test_full(uts, TPM_V2));
> +
> +	return 0;
> +}
> +DM_TEST(dm_test_tpm_self_test_full, UTF_SCAN_FDT);
> +
> +/* Test TPM self-test continue */
> +static int test_tpm_self_test_cont(struct unit_test_state *uts,
> +				   enum tpm_version version)
> +{
> +	struct udevice *dev;
> +
> +	/* check probe success */
> +	ut_assertok(get_tpm_version(version, &dev));
> +
> +	ut_assertok(tpm_init(dev));
> +	ut_assertok(tpm_startup(dev, TPM_ST_CLEAR));
> +	ut_assertok(tpm_continue_self_test(dev));
> +
> +	return 0;
> +}
> +
> +static int dm_test_tpm_self_test_cont(struct unit_test_state *uts)
> +{
> +	ut_assertok(test_tpm_self_test_cont(uts, TPM_V1));
> +	ut_assertok(test_tpm_self_test_cont(uts, TPM_V2));
> +
> +	return 0;
> +}
> +DM_TEST(dm_test_tpm_self_test_cont, UTF_SCAN_FDT);
>
>  /* Test report_state */
>  static int dm_test_tpm_report_state(struct unit_test_state *uts)
> diff --git a/test/py/tests/test_tpm2.py b/test/py/tests/test_tpm2.py
> index 064651c3e23..e55adfe784c 100644
> --- a/test/py/tests/test_tpm2.py
> +++ b/test/py/tests/test_tpm2.py
> @@ -56,7 +56,7 @@ def is_sandbox(ubman):
>      return sys_arch == 'sandbox'
>
>  @pytest.mark.buildconfigspec('cmd_tpm_v2')
> -def test_tpm2_init(ubman):
> +def test_tpm2_autostart(ubman):
>      """Init the software stack to use TPMv2 commands."""
>      skip_test = ubman.config.env.get('env__tpm_device_test_skip', False)
>      if skip_test:
> @@ -65,19 +65,6 @@ def test_tpm2_init(ubman):
>      output = ubman.run_command('echo $?')
>      assert output.endswith('0')
>
> - at pytest.mark.buildconfigspec('cmd_tpm_v2')
> -def test_tpm2_startup(ubman):
> -    """Execute a TPM2_Startup command.
> -
> -    Initiate the TPM internal state machine.
> -    """
> -    skip_test = ubman.config.env.get('env__tpm_device_test_skip', False)
> -    if skip_test:
> -        pytest.skip('skip TPM device test')
> -    ubman.run_command('tpm2 startup TPM2_SU_CLEAR')
> -    output = ubman.run_command('echo $?')
> -    assert output.endswith('0')
> -
>  def tpm2_sandbox_init(ubman):
>      """Put sandbox back into a known state so we can run a test
>
> @@ -92,29 +79,6 @@ def tpm2_sandbox_init(ubman):
>      if skip_test:
>          pytest.skip('skip TPM device test')
>
> - at pytest.mark.buildconfigspec('cmd_tpm_v2')
> -def test_tpm2_sandbox_self_test_full(ubman):
> -    """Execute a TPM2_SelfTest (full) command.
> -
> -    Ask the TPM to perform all self tests to also enable full capabilities.
> -    """
> -    if is_sandbox(ubman):
> -        ubman.restart_uboot()
> -        ubman.run_command('tpm2 autostart')
> -        output = ubman.run_command('echo $?')
> -        assert output.endswith('0')
> -
> -        ubman.run_command('tpm2 startup TPM2_SU_CLEAR')
> -        output = ubman.run_command('echo $?')
> -        assert output.endswith('0')
> -
> -    skip_test = ubman.config.env.get('env__tpm_device_test_skip', False)
> -    if skip_test:
> -        pytest.skip('skip TPM device test')
> -    ubman.run_command('tpm2 self_test full')
> -    output = ubman.run_command('echo $?')
> -    assert output.endswith('0')
> -
>  @pytest.mark.buildconfigspec('cmd_tpm_v2')
>  def test_tpm2_continue_self_test(ubman):
>      """Execute a TPM2_SelfTest (continued) command.



More information about the U-Boot mailing list