Add a TPM specific selftest for the riscv-server-ref board.  The test
will be skipped if there's no 'swtpm' in the host.

The test has been basically cloned from our Aspeed ast2600 friends.
Shoutout to monsieur Cédric Le Goater for the code.

Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
 tests/functional/riscv64/test_server_ref.py | 31 ++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/tests/functional/riscv64/test_server_ref.py 
b/tests/functional/riscv64/test_server_ref.py
index 2ecfcf60ad..9b120628dc 100755
--- a/tests/functional/riscv64/test_server_ref.py
+++ b/tests/functional/riscv64/test_server_ref.py
@@ -8,8 +8,13 @@
 riscv-server-ref board test
 """
 
+import os
+import tempfile
+import subprocess
+
 from qemu_test import QemuSystemTest, Asset
 from qemu_test import wait_for_console_pattern
+from qemu_test import skipIfMissingCommands
 
 class RiscvServerRefTest(QemuSystemTest):
     """
@@ -26,7 +31,7 @@ class RiscvServerRefTest(QemuSystemTest):
          'master/riscv/images/virt64/buildroot/rootfs.ext2'),
         'f00bb88749f945d80675540a1338bd1ccb226574685a5b6c65ab44027d0411a8')
 
-    def test_boot_linux_test(self):
+    def _test_boot_linux_test(self, tpmstate_dir=None):
         self.set_machine('riscv-server-ref')
         kernel_path = self.ASSET_KERNEL.fetch()
         rootfs_path = self.ASSET_ROOTFS.fetch()
@@ -38,6 +43,22 @@ def test_boot_linux_test(self):
         self.vm.add_args('-device', 'ahci,id=ahci')
         self.vm.add_args('-device', 'ide-hd,drive=hd0,bus=ahci.0')
 
+        if tpmstate_dir is not None:
+            # Note: code taken verbatim from
+            # tests/functional/arm/test_aspeed_ast2600_buildroot_tpm.py
+
+            # We must put the TPM state dir in /tmp/, not the build dir,
+            # because some distros use AppArmor to lock down swtpm and
+            # restrict the set of locations it can access files in.
+            socket = os.path.join(tpmstate_dir, 'swtpm-socket')
+            subprocess.run(['swtpm', 'socket', '-d', '--tpm2',
+                            '--tpmstate', f'dir={tpmstate_dir}',
+                            '--ctrl', f'type=unixio,path={socket}'],
+                           check=True)
+            self.vm.add_args('-chardev', f'socket,id=chrtpm,path={socket}')
+            self.vm.add_args('-tpmdev', 'emulator,id=tpm0,chardev=chrtpm')
+            self.vm.add_args('-device', 'tpm-tis-device,tpmdev=tpm0')
+
         self.vm.set_console()
         self.vm.launch()
 
@@ -55,5 +76,13 @@ def test_boot_linux_test(self):
         # Wait for boot to complete - system reaches login prompt
         wait_for_console_pattern(self, 'Run /sbin/init as init process')
 
+    def test_boot_linux_test(self):
+        self._test_boot_linux_test()
+
+    @skipIfMissingCommands('swtpm')
+    def test_boot_linux_test_tpm(self):
+        with tempfile.TemporaryDirectory(prefix="qemu_") as tpmstate_dir:
+            self._test_boot_linux_test(tpmstate_dir)
+
 if __name__ == '__main__':
     QemuSystemTest.main()
-- 
2.43.0


Reply via email to