Skip to content

Commit 8f84bdd

Browse files
tstrukJarkko Sakkinen
authored andcommitted
tpm: selftest: add test covering async mode
Add a test that sends a tpm cmd in an async mode. Currently there is a gap in test coverage with regards to this functionality. Signed-off-by: Tadeusz Struk <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent d23d124 commit 8f84bdd

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

tools/testing/selftests/tpm2/test_smoke.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
33

44
python -m unittest -v tpm2_tests.SmokeTest
5+
python -m unittest -v tpm2_tests.AsyncTest

tools/testing/selftests/tpm2/tpm2.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import struct
77
import sys
88
import unittest
9-
from fcntl import ioctl
10-
9+
import fcntl
10+
import select
1111

1212
TPM2_ST_NO_SESSIONS = 0x8001
1313
TPM2_ST_SESSIONS = 0x8002
@@ -352,6 +352,7 @@ def hex_dump(d):
352352
class Client:
353353
FLAG_DEBUG = 0x01
354354
FLAG_SPACE = 0x02
355+
FLAG_NONBLOCK = 0x04
355356
TPM_IOC_NEW_SPACE = 0xa200
356357

357358
def __init__(self, flags = 0):
@@ -362,13 +363,27 @@ def __init__(self, flags = 0):
362363
else:
363364
self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0)
364365

366+
if (self.flags & Client.FLAG_NONBLOCK):
367+
flags = fcntl.fcntl(self.tpm, fcntl.F_GETFL)
368+
flags |= os.O_NONBLOCK
369+
fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
370+
self.tpm_poll = select.poll()
371+
365372
def close(self):
366373
self.tpm.close()
367374

368375
def send_cmd(self, cmd):
369376
self.tpm.write(cmd)
377+
378+
if (self.flags & Client.FLAG_NONBLOCK):
379+
self.tpm_poll.register(self.tpm, select.POLLIN)
380+
self.tpm_poll.poll(10000)
381+
370382
rsp = self.tpm.read()
371383

384+
if (self.flags & Client.FLAG_NONBLOCK):
385+
self.tpm_poll.unregister(self.tpm)
386+
372387
if (self.flags & Client.FLAG_DEBUG) != 0:
373388
sys.stderr.write('cmd' + os.linesep)
374389
sys.stderr.write(hex_dump(cmd) + os.linesep)

tools/testing/selftests/tpm2/tpm2_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,16 @@ def test_invalid_cc(self):
288288

289289
self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE |
290290
tpm2.TSS2_RESMGR_TPM_RC_LAYER)
291+
292+
class AsyncTest(unittest.TestCase):
293+
def setUp(self):
294+
logging.basicConfig(filename='AsyncTest.log', level=logging.DEBUG)
295+
296+
def test_async(self):
297+
log = logging.getLogger(__name__)
298+
log.debug(sys._getframe().f_code.co_name)
299+
300+
async_client = tpm2.Client(tpm2.Client.FLAG_NONBLOCK)
301+
log.debug("Calling get_cap in a NON_BLOCKING mode")
302+
async_client.get_cap(tpm2.TPM2_CAP_HANDLES, tpm2.HR_LOADED_SESSION)
303+
async_client.close()

0 commit comments

Comments
 (0)