tpm: Convert sandbox-focussed tests to C
authorSimon Glass <sjg@chromium.org>
Sat, 24 May 2025 13:06:35 +0000 (07:06 -0600)
committerIlias Apalodimas <ilias.apalodimas@linaro.org>
Tue, 10 Jun 2025 16:35:26 +0000 (19:35 +0300)
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.

Rename and update the Python tpm_init test to use 'tpm autostart',
since this fully initializes the TPM and performs the self tests.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
test/dm/tpm.c
test/py/tests/test_tpm2.py

index 962a3fd..87c5c41 100644 (file)
@@ -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)
index 064651c..e55adfe 100644 (file)
@@ -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')
 
-@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')
 
-@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.