1# Copyright (C) 2019 - 2022 Armin Kuster <akuster808@gmail.com>
2#
3from oeqa.runtime.case import OERuntimeTestCase
4from oeqa.core.decorator.depends import OETestDepends
5from oeqa.runtime.decorator.package import OEHasPackage
6from oeqa.core.decorator.data import skipIfNotFeature
7
8class Tpm2Test(OERuntimeTestCase):
9    @classmethod
10    def setUpClass(cls):
11        cls.tc.target.run('swtpm_ioctl -s --tcp :2322')
12        cls.tc.target.run('mkdir /tmp/myvtpm2')
13
14    @classmethod
15    def tearDownClass(cls):
16        cls.tc.target.run('swtpm_ioctl -s --tcp :2322')
17        cls.tc.target.run('rm -fr /tmp/myvtpm2')
18
19    def check_endlines(self, results,  expected_endlines):
20        for line in results.splitlines():
21            for el in expected_endlines:
22                if line == el:
23                    expected_endlines.remove(el)
24                    break
25
26        if expected_endlines:
27            self.fail('Missing expected line endings:\n  %s' % '\n  '.join(expected_endlines))
28
29    @OEHasPackage(['tpm2-tools'])
30    @OEHasPackage(['tpm2-abrmd'])
31    @OEHasPackage(['swtpm'])
32    @skipIfNotFeature('tpm2','Test tpm2_startup requires tpm2 to be in DISTRO_FEATURES')
33    @OETestDepends(['ssh.SSHTest.test_ssh'])
34    def test_tpm2_startup(self):
35        cmds = [
36                'swtpm socket -d --tpmstate dir=/tmp/myvtpm2 --tpm2 --ctrl type=tcp,port=2322 --server type=tcp,port=2321 --flags not-need-init',
37                'tpm2_startup -c -T "swtpm:port=2321"',
38               ]
39
40        for cmd in cmds:
41            status, output = self.target.run(cmd)
42            self.assertEqual(status, 0, msg='\n'.join([cmd, output]))
43
44    @OETestDepends(['tpm2.Tpm2Test.test_tpm2_startup'])
45    def test_tpm2_pcrread(self):
46         (status, output) = self.target.run('tpm2_pcrread')
47         expected_endlines = []
48         expected_endlines.append('  sha1:')
49         expected_endlines.append('    0 : 0x0000000000000000000000000000000000000000')
50         expected_endlines.append('    1 : 0x0000000000000000000000000000000000000000')
51         expected_endlines.append('  sha256:')
52         expected_endlines.append('    0 : 0x0000000000000000000000000000000000000000000000000000000000000000')
53         expected_endlines.append('    1 : 0x0000000000000000000000000000000000000000000000000000000000000000')
54
55
56         self.check_endlines(output, expected_endlines)
57
58
59    @OEHasPackage(['p11-kit'])
60    @OEHasPackage(['tpm2-pkcs11'])
61    @OETestDepends(['tpm2.Tpm2Test.test_tpm2_pcrread'])
62    def test_tpm2_pkcs11(self):
63         (status, output) = self.target.run('p11-kit list-modules -v')
64         self.assertEqual(status, 0, msg="Modules missing: %s" % output)
65
66    @OETestDepends(['tpm2.Tpm2Test.test_tpm2_pkcs11'])
67    def test_tpm2_swtpm_reset(self):
68         (status, output) = self.target.run('swtpm_ioctl -i --tcp :2322')
69         self.assertEqual(status, 0, msg="swtpm reset failed: %s" % output)
70