Kernel selftests: TPM2: upgrade TPM2 tests from Python 2 to Python 3
Subsystems (2)
PATCH v2 1/1 Kernel selftests: TPM2: upgrade TPM2 tests from Python 2 to Python 3
Python 2 is no longer supported by the Python upstream project, so upgrade TPM2 tests to Python 3.
--- tools/testing/selftests/tpm2/test_smoke.sh | 4 +- tools/testing/selftests/tpm2/test_space.sh | 2 +- tools/testing/selftests/tpm2/tpm2.py | 68 ++++++++++++++-------- tools/testing/selftests/tpm2/tpm2_tests.py | 24 +++++--- 4 files changed, 61 insertions(+), 37 deletions(-)
tools/testing/selftests/tpm2/test_smoke.sh +2 -2
@@ -6,8 +6,8 @@ ksft_skip=4 [ -f /dev/tpm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SmokeTest -python -m unittest -v tpm2_tests.AsyncTest +python3 -m unittest -v tpm2_tests.SmokeTest +python3 -m unittest -v tpm2_tests.AsyncTest CLEAR_CMD=$(which tpm2_clear) if [ -n $CLEAR_CMD ]; then
tools/testing/selftests/tpm2/test_space.sh +1 -1
@@ -6,4 +6,4 @@ ksft_skip=4 [ -f /dev/tpmrm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SpaceTest +python3 -m unittest -v tpm2_tests.SpaceTest
tools/testing/selftests/tpm2/tpm2.py +43 -25
188 lines not on this page · raw message
tools/testing/selftests/tpm2/tpm2_tests.py +15 -9
@@ -25,7 +25,9 @@ class SmokeTest(unittest.TestCase): blob = self.client.seal(self.root_key, data, auth, None) result = self.client.unseal(self.root_key, blob, auth, None) - self.assertEqual(data, result) + if not isinstance(result, bytes): + result = result.encode() + self.assertEqual(data.encode(), result) def test_seal_with_policy(self): handle = self.client.start_auth_session(tpm2.TPM2_SE_TRIAL)
@@ -51,11 +53,13 @@ class SmokeTest(unittest.TestCase): self.client.policy_password(handle) result = self.client.unseal(self.root_key, blob, auth, handle) + if not isinstance(result, bytes): + result = result.encode() except: self.client.flush_context(handle) raise - self.assertEqual(data, result) + self.assertEqual(data.encode(), result) def test_unseal_with_wrong_auth(self): data = 'X' * 64
@@ -65,7 +69,7 @@ class SmokeTest(unittest.TestCase): blob = self.client.seal(self.root_key, data, auth, None) try: result = self.client.unseal(self.root_key, blob, auth[:-1] + 'B', None) - except ProtocolError, e: + except ProtocolError as e: rc = e.rc self.assertEqual(rc, tpm2.TPM2_RC_AUTH_FAIL)
@@ -100,11 +104,13 @@ class SmokeTest(unittest.TestCase): self.client.policy_password(handle) result = self.client.unseal(self.root_key, blob, auth, handle) + if not isinstance(result, bytes): + result = result.encode() except: self.client.flush_context(handle) raise - self.assertEqual(data, result) + self.assertEqual(data.encode(), result) # Then, extend a PCR that is part of the policy and try to unseal. # This should fail.
@@ -119,7 +125,7 @@ class SmokeTest(unittest.TestCase): self.client.policy_password(handle) result = self.client.unseal(self.root_key, blob, auth, handle) - except ProtocolError, e: + except ProtocolError as e: rc = e.rc self.client.flush_context(handle) except:
@@ -136,7 +142,7 @@ class SmokeTest(unittest.TestCase): rc = 0 try: blob = self.client.seal(self.root_key, data, auth, None) - except ProtocolError, e: + except ProtocolError as e: rc = e.rc self.assertEqual(rc, tpm2.TPM2_RC_SIZE)
@@ -152,7 +158,7 @@ class SmokeTest(unittest.TestCase): 0xDEADBEEF) self.client.send_cmd(cmd) - except IOError, e: + except IOError as e: rejected = True except: pass
@@ -212,7 +218,7 @@ class SmokeTest(unittest.TestCase): self.client.tpm.write(cmd) rsp = self.client.tpm.read() - except IOError, e: + except IOError as e: # read the response rsp = self.client.tpm.read() rejected = True
@@ -283,7 +289,7 @@ class SpaceTest(unittest.TestCase): rc = 0 try: space1.send_cmd(cmd) - except ProtocolError, e: + except ProtocolError as e: rc = e.rc self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE |
Signature · 1 line
2.17.1
Quoted 4 lines · Pengfei Xu
Python 2 is no longer supported by the Python upstream project, so upgrade TPM2 tests to Python 3.
Signed-off-by Pengfei Xu <[email protected]>
Use "selftests: tpm: <short summary>".
Quoted 38 lines
--- tools/testing/selftests/tpm2/test_smoke.sh | 4 +- tools/testing/selftests/tpm2/test_space.sh | 2 +- tools/testing/selftests/tpm2/tpm2.py | 68 ++++++++++++++-------- tools/testing/selftests/tpm2/tpm2_tests.py | 24 +++++--- 4 files changed, 61 insertions(+), 37 deletions(-)tools/testing/selftests/tpm2/test_smoke.sh +2
-2@@ -6,8 +6,8 @@ ksft_skip=4 [ -f /dev/tpm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SmokeTest -python -m unittest -v tpm2_tests.AsyncTest +python3 -m unittest -v tpm2_tests.SmokeTest +python3 -m unittest -v tpm2_tests.AsyncTest CLEAR_CMD=$(which tpm2_clear) if [ -n $CLEAR_CMD ]; thentools/testing/selftests/tpm2/test_space.sh +1
-1@@ -6,4 +6,4 @@ ksft_skip=4 [ -f /dev/tpmrm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SpaceTest +python3 -m unittest -v tpm2_tests.SpaceTesttools/testing/selftests/tpm2/tpm2.py +0
-0@@ -247,14 +247,18 @@ class ProtocolError(Exception): class AuthCommand(object):
tools/testing/selftests/tpm2/tpm2.py class ProtocolError(Exception): · line 255
Quoted 10 lines
tools/testing/selftests/tpm2/tpm2.py +6
-2"""TPMS_AUTH_COMMAND""" - def __init__(self, session_handle=TPM2_RS_PW, nonce='', session_attributes=0, - hmac=''): + def __init__(self, session_handle=TPM2_RS_PW, nonce=''.encode(), + session_attributes=0, hmac=''.encode()): + if not isinstance(nonce, bytes): + nonce = nonce.encode() + if not isinstance(hmac, bytes): + hmac = hmac.encode()
This looks messy. Please, instead
def __init__(self, session_handle=TPM2_RS_PW, nonce=bytes(),
session_attributes=0, hmac=bytes()):
self.session_handle = session_handle self.nonce = nonce
self.session_attributes = session_attributes
Applies also to other places.
/Jarkko
Quoted 59 lines · Jarkko Sakkinen
Quoted 4 lines · Pengfei Xu
Python 2 is no longer supported by the Python upstream project, so upgrade TPM2 tests to Python 3.
Signed-off-by Pengfei Xu <[email protected]>Use "selftests: tpm: <short summary>".
Quoted 38 lines
--- tools/testing/selftests/tpm2/test_smoke.sh | 4 +- tools/testing/selftests/tpm2/test_space.sh | 2 +- tools/testing/selftests/tpm2/tpm2.py | 68 ++++++++++++++-------- tools/testing/selftests/tpm2/tpm2_tests.py | 24 +++++--- 4 files changed, 61 insertions(+), 37 deletions(-)tools/testing/selftests/tpm2/test_smoke.sh +2
-2@@ -6,8 +6,8 @@ ksft_skip=4 [ -f /dev/tpm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SmokeTest -python -m unittest -v tpm2_tests.AsyncTest +python3 -m unittest -v tpm2_tests.SmokeTest +python3 -m unittest -v tpm2_tests.AsyncTest CLEAR_CMD=$(which tpm2_clear) if [ -n $CLEAR_CMD ]; thentools/testing/selftests/tpm2/test_space.sh +1
-1@@ -6,4 +6,4 @@ ksft_skip=4 [ -f /dev/tpmrm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SpaceTest +python3 -m unittest -v tpm2_tests.SpaceTesttools/testing/selftests/tpm2/tpm2.py +0
-0@@ -247,14 +247,18 @@ class ProtocolError(Exception): class AuthCommand(object):tools/testing/selftests/tpm2/tpm2.py class ProtocolError(Exception): · line 255
Quoted 10 lines
tools/testing/selftests/tpm2/tpm2.py +6
-2"""TPMS_AUTH_COMMAND""" - def __init__(self, session_handle=TPM2_RS_PW, nonce='', session_attributes=0, - hmac=''): + def __init__(self, session_handle=TPM2_RS_PW, nonce=''.encode(), + session_attributes=0, hmac=''.encode()): + if not isinstance(nonce, bytes): + nonce = nonce.encode() + if not isinstance(hmac, bytes): + hmac = hmac.encode()This looks messy. Please, instead
Quoted 7 lines
def __init__(self, session_handle=TPM2_RS_PW, nonce=bytes(),
session_attributes=0, hmac=bytes()):
self.session_handle =session_handleself.nonce = nonce
self.session_attributes =session_attributesApplies also to other places.
I.e. use '.encode()' in the call site.
/Jarkko
Hi Jarkk,
Thanks for your comments!
My feedback is as below.
BR.
Quoted 9 lines · Jarkko Sakkinen
Quoted 8 lines · Jarkko Sakkinen
Quoted 4 lines · Pengfei Xu
Python 2 is no longer supported by the Python upstream project, so upgrade TPM2 tests to Python 3.
Signed-off-by Pengfei Xu <[email protected]>Use "selftests: tpm: <short summary>".
Will do.
Quoted 57 lines
Quoted 57 lines
Quoted 38 lines
--- tools/testing/selftests/tpm2/test_smoke.sh | 4 +- tools/testing/selftests/tpm2/test_space.sh | 2 +- tools/testing/selftests/tpm2/tpm2.py | 68 ++++++++++++++-------- tools/testing/selftests/tpm2/tpm2_tests.py | 24 +++++--- 4 files changed, 61 insertions(+), 37 deletions(-)tools/testing/selftests/tpm2/test_smoke.sh +2
-2@@ -6,8 +6,8 @@ ksft_skip=4 [ -f /dev/tpm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SmokeTest -python -m unittest -v tpm2_tests.AsyncTest +python3 -m unittest -v tpm2_tests.SmokeTest +python3 -m unittest -v tpm2_tests.AsyncTest CLEAR_CMD=$(which tpm2_clear) if [ -n $CLEAR_CMD ]; thentools/testing/selftests/tpm2/test_space.sh +1
-1@@ -6,4 +6,4 @@ ksft_skip=4 [ -f /dev/tpmrm0 ] || exit $ksft_skip -python -m unittest -v tpm2_tests.SpaceTest +python3 -m unittest -v tpm2_tests.SpaceTesttools/testing/selftests/tpm2/tpm2.py +0
-0@@ -247,14 +247,18 @@ class ProtocolError(Exception): class AuthCommand(object):tools/testing/selftests/tpm2/tpm2.py class ProtocolError(Exception): · line 255
Quoted 10 lines
tools/testing/selftests/tpm2/tpm2.py +6
-2"""TPMS_AUTH_COMMAND""" - def __init__(self, session_handle=TPM2_RS_PW, nonce='', session_attributes=0, - hmac=''): + def __init__(self, session_handle=TPM2_RS_PW, nonce=''.encode(), + session_attributes=0, hmac=''.encode()): + if not isinstance(nonce, bytes): + nonce = nonce.encode() + if not isinstance(hmac, bytes): + hmac = hmac.encode()This looks messy. Please, instead
def __init__(self, session_handle=TPM2_RS_PW, nonce=bytes(),
session_attributes=0, hmac=bytes()):
self.session_handle =session_handleself.nonce = nonce
self.session_attributes =session_attributes
Quoted 4 lines
Quoted 1 line
Applies also to other places.
I.e. use '.encode()' in the call site.
I tried to use bytes(parm, encoding='UTF-8') way, it met the situation that sometimes parm is string, sometimes parm is bytes. And it's better we could convert all types value to bytes in __init__ function.
Could we modify it as below:
def ConvertToBytes(parm):
if not isinstance(parm, bytes):
parm = parm.encode()
return parm
class AuthCommand(object):
"""TPMS_AUTH_COMMAND"""
def __init__(self, session_handle=TPM2_RS_PW, nonce='',
session_attributes=0, hmac=''):
self.session_handle = session_handle self.nonce = ConvertToBytes(nonce) self.session_attributes = session_attributes self.hmac = ConvertToBytes(hmac)
Thanks!
Quoted 1 line
/Jarkko
Quoted 2 lines · Pengfei Xu
I tried to use
bytes(parm, encoding='UTF-8')way, it met the situation that sometimes parm is string, sometimes parm is bytes.
We need to do right things right, i.e. you should fix in the call site, not in the implementation.
/Jarkko
Quoted 6 lines · Jarkko Sakkinen
Quoted 2 lines · Pengfei Xu
I tried to use
bytes(parm, encoding='UTF-8')way, it met the situation that sometimes parm is string, sometimes parm is bytes.We need to do right things right, i.e. you should fix in the call site, not in the implementation.
Ok, I agree, I will have a try. :)
Thanks!
Quoted 2 lines
/Jarkko