Skip to content

Commit f1a0ba6

Browse files
tstrukJames Morris
authored and
James Morris
committed
selftests/tpm2: Extend tests to cover partial reads
Three new tests added: 1. Send get random cmd, read header in 1st read, read the rest in second read - expect success 2. Send get random cmd, read only part of the response, send another get random command, read the response - expect success 3. Send get random cmd followed by another get random cmd, without reading the first response - expect the second cmd to fail with -EBUSY Signed-off-by: Tadeusz Struk <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]> Signed-off-by: James Morris <[email protected]>
1 parent be24b37 commit f1a0ba6

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

tools/testing/selftests/tpm2/tpm2.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
TPM2_CC_FLUSH_CONTEXT = 0x0165
2323
TPM2_CC_START_AUTH_SESSION = 0x0176
2424
TPM2_CC_GET_CAPABILITY = 0x017A
25+
TPM2_CC_GET_RANDOM = 0x017B
2526
TPM2_CC_PCR_READ = 0x017E
2627
TPM2_CC_POLICY_PCR = 0x017F
2728
TPM2_CC_PCR_EXTEND = 0x0182

tools/testing/selftests/tpm2/tpm2_tests.py

+63
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,69 @@ def test_too_short_cmd(self):
158158
pass
159159
self.assertEqual(rejected, True)
160160

161+
def test_read_partial_resp(self):
162+
try:
163+
fmt = '>HIIH'
164+
cmd = struct.pack(fmt,
165+
tpm2.TPM2_ST_NO_SESSIONS,
166+
struct.calcsize(fmt),
167+
tpm2.TPM2_CC_GET_RANDOM,
168+
0x20)
169+
self.client.tpm.write(cmd)
170+
hdr = self.client.tpm.read(10)
171+
sz = struct.unpack('>I', hdr[2:6])[0]
172+
rsp = self.client.tpm.read()
173+
except:
174+
pass
175+
self.assertEqual(sz, 10 + 2 + 32)
176+
self.assertEqual(len(rsp), 2 + 32)
177+
178+
def test_read_partial_overwrite(self):
179+
try:
180+
fmt = '>HIIH'
181+
cmd = struct.pack(fmt,
182+
tpm2.TPM2_ST_NO_SESSIONS,
183+
struct.calcsize(fmt),
184+
tpm2.TPM2_CC_GET_RANDOM,
185+
0x20)
186+
self.client.tpm.write(cmd)
187+
# Read part of the respone
188+
rsp1 = self.client.tpm.read(15)
189+
190+
# Send a new cmd
191+
self.client.tpm.write(cmd)
192+
193+
# Read the whole respone
194+
rsp2 = self.client.tpm.read()
195+
except:
196+
pass
197+
self.assertEqual(len(rsp1), 15)
198+
self.assertEqual(len(rsp2), 10 + 2 + 32)
199+
200+
def test_send_two_cmds(self):
201+
rejected = False
202+
try:
203+
fmt = '>HIIH'
204+
cmd = struct.pack(fmt,
205+
tpm2.TPM2_ST_NO_SESSIONS,
206+
struct.calcsize(fmt),
207+
tpm2.TPM2_CC_GET_RANDOM,
208+
0x20)
209+
self.client.tpm.write(cmd)
210+
211+
# expect the second one to raise -EBUSY error
212+
self.client.tpm.write(cmd)
213+
rsp = self.client.tpm.read()
214+
215+
except IOError, e:
216+
# read the response
217+
rsp = self.client.tpm.read()
218+
rejected = True
219+
pass
220+
except:
221+
pass
222+
self.assertEqual(rejected, True)
223+
161224
class SpaceTest(unittest.TestCase):
162225
def setUp(self):
163226
logging.basicConfig(filename='SpaceTest.log', level=logging.DEBUG)

0 commit comments

Comments
 (0)