test: Allow tpm2 tests to run in parallel
authorSimon Glass <sjg@chromium.org>
Sun, 19 Sep 2021 21:14:50 +0000 (15:14 -0600)
committerTom Rini <trini@konsulko.com>
Tue, 5 Oct 2021 12:50:15 +0000 (08:50 -0400)
These tests currently run in a particular sequence, with some of them
depending on the actions of earlier tests.

Add a check for sandbox and reset to a known state at the start of each
test, so that all tests can run in parallel.

Signed-off-by: Simon Glass <sjg@chromium.org>
test/py/tests/test_tpm2.py

index c7a9dc1..7c89f5f 100644 (file)
@@ -52,14 +52,17 @@ def force_init(u_boot_console, force=False):
             u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
         u_boot_console.run_command('echo --- end of init ---')
 
             u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
         u_boot_console.run_command('echo --- end of init ---')
 
+def is_sandbox(cons):
+    # Array slice removes leading/trailing quotes.
+    sys_arch = cons.config.buildconfig.get('config_sys_arch', '"sandbox"')[1:-1]
+    return sys_arch == 'sandbox'
+
 @pytest.mark.buildconfigspec('cmd_tpm_v2')
 def test_tpm2_init(u_boot_console):
     """Init the software stack to use TPMv2 commands."""
 @pytest.mark.buildconfigspec('cmd_tpm_v2')
 def test_tpm2_init(u_boot_console):
     """Init the software stack to use TPMv2 commands."""
-
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
         pytest.skip('skip TPM device test')
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
         pytest.skip('skip TPM device test')
-
     u_boot_console.run_command('tpm2 init')
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
     u_boot_console.run_command('tpm2 init')
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
@@ -70,6 +73,19 @@ def test_tpm2_startup(u_boot_console):
 
     Initiate the TPM internal state machine.
     """
 
     Initiate the TPM internal state machine.
     """
+    u_boot_console.run_command('tpm2 startup TPM2_SU_CLEAR')
+    output = u_boot_console.run_command('echo $?')
+    assert output.endswith('0')
+
+def tpm2_sandbox_init(u_boot_console):
+    """Put sandbox back into a known state so we can run a test
+
+    This allows all tests to run in parallel, since no test depends on another.
+    """
+    u_boot_console.restart_uboot()
+    u_boot_console.run_command('tpm2 init')
+    output = u_boot_console.run_command('echo $?')
+    assert output.endswith('0')
 
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
 
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
@@ -78,12 +94,25 @@ def test_tpm2_startup(u_boot_console):
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
 
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
 
+    u_boot_console.run_command('tpm2 self_test full')
+    output = u_boot_console.run_command('echo $?')
+    assert output.endswith('0')
+
 @pytest.mark.buildconfigspec('cmd_tpm_v2')
 @pytest.mark.buildconfigspec('cmd_tpm_v2')
-def test_tpm2_self_test_full(u_boot_console):
+def test_tpm2_sandbox_self_test_full(u_boot_console):
     """Execute a TPM2_SelfTest (full) command.
 
     Ask the TPM to perform all self tests to also enable full capabilities.
     """
     """Execute a TPM2_SelfTest (full) command.
 
     Ask the TPM to perform all self tests to also enable full capabilities.
     """
+    if is_sandbox(u_boot_console):
+        u_boot_console.restart_uboot()
+        u_boot_console.run_command('tpm2 init')
+        output = u_boot_console.run_command('echo $?')
+        assert output.endswith('0')
+
+        u_boot_console.run_command('tpm2 startup TPM2_SU_CLEAR')
+        output = u_boot_console.run_command('echo $?')
+        assert output.endswith('0')
 
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
 
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
@@ -103,6 +132,8 @@ def test_tpm2_continue_self_test(u_boot_console):
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
         pytest.skip('skip TPM device test')
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
         pytest.skip('skip TPM device test')
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
     u_boot_console.run_command('tpm2 self_test continue')
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
     u_boot_console.run_command('tpm2 self_test continue')
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
@@ -119,6 +150,8 @@ def test_tpm2_clear(u_boot_console):
     not have a password set, otherwise this test will fail. ENDORSEMENT and
     PLATFORM hierarchies are also available.
     """
     not have a password set, otherwise this test will fail. ENDORSEMENT and
     PLATFORM hierarchies are also available.
     """
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
 
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
 
     skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
     if skip_test:
@@ -140,7 +173,8 @@ def test_tpm2_change_auth(u_boot_console):
     Use the LOCKOUT hierarchy for this. ENDORSEMENT and PLATFORM hierarchies are
     also available.
     """
     Use the LOCKOUT hierarchy for this. ENDORSEMENT and PLATFORM hierarchies are
     also available.
     """
-
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
     force_init(u_boot_console)
 
     u_boot_console.run_command('tpm2 change_auth TPM2_RH_LOCKOUT unicorn')
     force_init(u_boot_console)
 
     u_boot_console.run_command('tpm2 change_auth TPM2_RH_LOCKOUT unicorn')
@@ -164,6 +198,8 @@ def test_tpm2_get_capability(u_boot_console):
     There is no expected default values because it would depend on the chip
     used. We can still save them in order to check they have changed later.
     """
     There is no expected default values because it would depend on the chip
     used. We can still save them in order to check they have changed later.
     """
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
 
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
@@ -186,7 +222,8 @@ def test_tpm2_dam_parameters(u_boot_console):
     the authentication, otherwise the lockout will be engaged after the first
     failed authentication attempt.
     """
     the authentication, otherwise the lockout will be engaged after the first
     failed authentication attempt.
     """
-
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
@@ -209,6 +246,8 @@ def test_tpm2_pcr_read(u_boot_console):
 
     Perform a PCR read of the 0th PCR. Must be zero.
     """
 
     Perform a PCR read of the 0th PCR. Must be zero.
     """
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
 
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
@@ -236,7 +275,8 @@ def test_tpm2_pcr_extend(u_boot_console):
     No authentication mechanism is used here, not protecting against packet
     replay, yet.
     """
     No authentication mechanism is used here, not protecting against packet
     replay, yet.
     """
-
+    if is_sandbox(u_boot_console):
+        tpm2_sandbox_init(u_boot_console)
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)