Skip to content

Commit

Permalink
tpm: selftest: add test covering async mode
Browse files Browse the repository at this point in the history
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 <tadeusz.struk@intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  • Loading branch information
Tadeusz Struk authored and Jarkko Sakkinen committed Dec 17, 2019
1 parent d23d124 commit 8f84bdd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/tpm2/test_smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)

python -m unittest -v tpm2_tests.SmokeTest
python -m unittest -v tpm2_tests.AsyncTest
19 changes: 17 additions & 2 deletions tools/testing/selftests/tpm2/tpm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import struct
import sys
import unittest
from fcntl import ioctl

import fcntl
import select

TPM2_ST_NO_SESSIONS = 0x8001
TPM2_ST_SESSIONS = 0x8002
Expand Down Expand Up @@ -352,6 +352,7 @@ def hex_dump(d):
class Client:
FLAG_DEBUG = 0x01
FLAG_SPACE = 0x02
FLAG_NONBLOCK = 0x04
TPM_IOC_NEW_SPACE = 0xa200

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

if (self.flags & Client.FLAG_NONBLOCK):
flags = fcntl.fcntl(self.tpm, fcntl.F_GETFL)
flags |= os.O_NONBLOCK
fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
self.tpm_poll = select.poll()

def close(self):
self.tpm.close()

def send_cmd(self, cmd):
self.tpm.write(cmd)

if (self.flags & Client.FLAG_NONBLOCK):
self.tpm_poll.register(self.tpm, select.POLLIN)
self.tpm_poll.poll(10000)

rsp = self.tpm.read()

if (self.flags & Client.FLAG_NONBLOCK):
self.tpm_poll.unregister(self.tpm)

if (self.flags & Client.FLAG_DEBUG) != 0:
sys.stderr.write('cmd' + os.linesep)
sys.stderr.write(hex_dump(cmd) + os.linesep)
Expand Down
13 changes: 13 additions & 0 deletions tools/testing/selftests/tpm2/tpm2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,16 @@ def test_invalid_cc(self):

self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE |
tpm2.TSS2_RESMGR_TPM_RC_LAYER)

class AsyncTest(unittest.TestCase):
def setUp(self):
logging.basicConfig(filename='AsyncTest.log', level=logging.DEBUG)

def test_async(self):
log = logging.getLogger(__name__)
log.debug(sys._getframe().f_code.co_name)

async_client = tpm2.Client(tpm2.Client.FLAG_NONBLOCK)
log.debug("Calling get_cap in a NON_BLOCKING mode")
async_client.get_cap(tpm2.TPM2_CAP_HANDLES, tpm2.HR_LOADED_SESSION)
async_client.close()

0 comments on commit 8f84bdd

Please sign in to comment.