blob: d3d3f2e0ce9bbf1cc4dfaeaeb129c8f2ae1ef186 [file] [log] [blame]
Andrew Geisslerd5838332022-05-27 11:33:10 -05001# Copyright (C) 2022 Armin Kuster <akuster808@gmail.com>
2# Copyright (C) 2022 Anton Antonov <Anton.Antonov@arm.com>
3#
4import re
5from tempfile import mkstemp
6
7from oeqa.runtime.case import OERuntimeTestCase
8from oeqa.core.decorator.depends import OETestDepends
9from oeqa.runtime.decorator.package import OEHasPackage
10from oeqa.core.decorator.data import skipIfNotFeature
11
12class ParsecTest(OERuntimeTestCase):
13 @classmethod
14 def setUpClass(cls):
15 cls.toml_file = '/etc/parsec/config.toml'
16
17 def setUp(self):
18 super(ParsecTest, self).setUp()
19 if 'systemd' in self.tc.td['DISTRO_FEATURES']:
20 self.parsec_status='systemctl status -l parsec'
21 self.parsec_reload='systemctl restart parsec'
22 else:
23 self.parsec_status='pgrep -l parsec'
24 self.parsec_reload='/etc/init.d/parsec reload'
25
26 def copy_subconfig(self, cfg, provider):
27 """ Copy a provider configuration to target and append it to Parsec config """
28
29 tmp_fd, tmp_path = mkstemp()
30 with os.fdopen(tmp_fd, 'w') as f:
31 f.write('\n'.join(cfg))
32
33 (status, output) = self.target.copyTo(tmp_path, "%s-%s" % (self.toml_file, provider))
34 self.assertEqual(status, 0, msg='File could not be copied.\n%s' % output)
35 status, output = self.target.run('cat %s-%s >>%s' % (self.toml_file, provider, self.toml_file))
36 os.remove(tmp_path)
37
38 def check_parsec_providers(self, provider=None, prov_id=None):
39 """ Get Parsec providers list and check for one if defined """
40
41 status, output = self.target.run(self.parsec_status)
42 self.assertEqual(status, 0, msg='Parsec service is not running.\n%s' % output)
43
44 status, output = self.target.run('parsec-tool list-providers')
45 self.assertEqual(status, 0, msg='Cannot get a list of Parsec providers.\n%s' % output)
46 if provider and prov_id:
47 self.assertIn("ID: 0x0%d (%s provider)" % (prov_id, provider),
48 output, msg='%s provider is not configured.' % provider)
49
50 def run_cli_tests(self, prov_id=None):
51 """ Run Parsec CLI end-to-end tests against one or all providers """
52
53 status, output = self.target.run('parsec-cli-tests.sh %s' % ("-%d" % prov_id if prov_id else ""))
54 self.assertEqual(status, 0, msg='Parsec CLI tests failed.\n %s' % output)
55
56 @OEHasPackage(['parsec-service'])
57 @OETestDepends(['ssh.SSHTest.test_ssh'])
58 def test_all_providers(self):
59 """ Test Parsec service with all pre-defined providers """
60
61 self.check_parsec_providers()
62 self.run_cli_tests()
63
64 def configure_tpm_provider(self):
65 """ Create Parsec TPM provider configuration """
66
67 cfg = [
68 '',
69 '[[provider]]',
70 'name = "tpm-provider"',
71 'provider_type = "Tpm"',
72 'key_info_manager = "sqlite-manager"',
73 'tcti = "swtpm:port=2321"',
74 'owner_hierarchy_auth = ""',
75 ]
76 self.copy_subconfig(cfg, "TPM")
77
78 cmds = [
79 'mkdir /tmp/myvtpm',
80 'swtpm socket -d --tpmstate dir=/tmp/myvtpm --tpm2 --ctrl type=tcp,port=2322 --server type=tcp,port=2321 --flags not-need-init',
81 'tpm2_startup -c -T "swtpm:port=2321"',
82 self.parsec_reload,
83 ]
84
85 for cmd in cmds:
86 status, output = self.target.run(cmd)
87 self.assertEqual(status, 0, msg='\n'.join([cmd, output]))
88
89 @OEHasPackage(['parsec-service'])
90 @OEHasPackage(['swtpm'])
91 @skipIfNotFeature('tpm2','Test parsec_tpm_provider requires tpm2 to be in DISTRO_FEATURES')
92 @OETestDepends(['ssh.SSHTest.test_ssh', 'parsec.ParsecTest.test_all_providers'])
93 def test_tpm_provider(self):
94 """ Configure and test Parsec TPM provider with swtpm as a backend """
95
96 prov_id = 3
97 self.configure_tpm_provider()
98 self.check_parsec_providers("TPM", prov_id)
99 self.run_cli_tests(prov_id)
100
101 def configure_pkcs11_provider(self):
102 """ Create Parsec PKCS11 provider configuration """
103
104 status, output = self.target.run('softhsm2-util --init-token --free --label "Parsec Service" --pin 123456 --so-pin 123456')
105 self.assertEqual(status, 0, msg='Failed to init PKCS11 token.\n%s' % output)
106
107 slot = re.search('The token has been initialized and is reassigned to slot (\d*)', output)
108 if slot is None:
109 self.fail('Failed to get PKCS11 slot serial number.\n%s' % output)
110 self.assertNotEqual(slot.group(1), None, msg='Failed to get PKCS11 slot serial number.\n%s' % output)
111
112 cfg = [
113 '',
114 '[[provider]]',
115 'name = "pkcs11-provider"',
116 'provider_type = "Pkcs11"',
117 'key_info_manager = "sqlite-manager"',
118 'library_path = "/usr/lib/softhsm/libsofthsm2.so"',
119 'slot_number = %s' % slot.group(1),
120 'user_pin = "123456"',
121 'allow_export = true',
122 ]
123 self.copy_subconfig(cfg, "PKCS11")
124
125 status, output = self.target.run('for d in /var/lib/softhsm/tokens/*; do chown -R parsec $d; done')
126 status, output = self.target.run(self.parsec_reload)
127 self.assertEqual(status, 0, msg='Failed to reload Parsec.\n%s' % output)
128
129 @OEHasPackage(['parsec-service'])
130 @OEHasPackage(['softhsm'])
131 @OETestDepends(['ssh.SSHTest.test_ssh', 'parsec.ParsecTest.test_all_providers'])
132 def test_pkcs11_provider(self):
133 """ Configure and test Parsec PKCS11 provider with softhsm as a backend """
134
135 prov_id = 2
136 self.configure_pkcs11_provider()
137 self.check_parsec_providers("PKCS #11", prov_id)
138 self.run_cli_tests(prov_id)