File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed
tools/testing/selftests/tpm2 Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 2
2
# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3
3
4
4
python -m unittest -v tpm2_tests.SmokeTest
5
+ python -m unittest -v tpm2_tests.AsyncTest
Original file line number Diff line number Diff line change 6
6
import struct
7
7
import sys
8
8
import unittest
9
- from fcntl import ioctl
10
-
9
+ import fcntl
10
+ import select
11
11
12
12
TPM2_ST_NO_SESSIONS = 0x8001
13
13
TPM2_ST_SESSIONS = 0x8002
@@ -352,6 +352,7 @@ def hex_dump(d):
352
352
class Client :
353
353
FLAG_DEBUG = 0x01
354
354
FLAG_SPACE = 0x02
355
+ FLAG_NONBLOCK = 0x04
355
356
TPM_IOC_NEW_SPACE = 0xa200
356
357
357
358
def __init__ (self , flags = 0 ):
@@ -362,13 +363,27 @@ def __init__(self, flags = 0):
362
363
else :
363
364
self .tpm = open ('/dev/tpmrm0' , 'r+b' , buffering = 0 )
364
365
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
+
365
372
def close (self ):
366
373
self .tpm .close ()
367
374
368
375
def send_cmd (self , cmd ):
369
376
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
+
370
382
rsp = self .tpm .read ()
371
383
384
+ if (self .flags & Client .FLAG_NONBLOCK ):
385
+ self .tpm_poll .unregister (self .tpm )
386
+
372
387
if (self .flags & Client .FLAG_DEBUG ) != 0 :
373
388
sys .stderr .write ('cmd' + os .linesep )
374
389
sys .stderr .write (hex_dump (cmd ) + os .linesep )
Original file line number Diff line number Diff line change @@ -288,3 +288,16 @@ def test_invalid_cc(self):
288
288
289
289
self .assertEqual (rc , tpm2 .TPM2_RC_COMMAND_CODE |
290
290
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 ()
You can’t perform that action at this time.
0 commit comments