Skip to contentSkip to conversation

Kernel selftests: tpm2: upgrade tpm2 tests from python2 to python3

PATCH In review Pengfei Xu 2 participants 3 messages Jun 18, 2020

Subsystems (2)
lore
Share & tools
lore

Pengfei Xu

author
#0 lore raw

PATCH 1/1 Kernel selftests: tpm2: upgrade tpm2 tests from python2 to python3

Some Linux OS will never support python2 anymore, so upgrade tpm2 selftests to python3.

---
 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
index 663062701d5a..d05467f6d258 100755
@@ -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
index 36c9d030a1c6..151c64e8ee9f 100755
@@ -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
index 728be7c69b76..e134033e6f67 100644
@@ -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
#1 lore raw
Quoted 4 lines · Pengfei Xu

Some Linux OS will never support python2 anymore, so upgrade tpm2 selftests to python3.

Linux is a kernel, not a full operating system. Perhaps you mean Linux distributions?

Please capitalize abbrevations correctly (TPM2, not tpm2). Please write 'Python 2' instead of python2.

With that said the commit message is inaccurate. The root reason for moving to Python 3 is that Python 2 is no longer supported by the Python upstream project. Nothing to do with Linux or Linux distributions for that matter.

/Jarkko

author
#2 lore raw

Hi Jarkko,
Thanks for your advice.
My feedback is as below.

BR.
Thanks!

Quoted 9 lines · Jarkko Sakkinen
Quoted 4 lines · Pengfei Xu

Some Linux OS will never support python2 anymore, so upgrade tpm2 selftests to python3.

Linux is a kernel, not a full operating system. Perhaps you mean Linux distributions?

Will remove [Linux], thanks!

Quoted 3 lines

Please capitalize abbrevations correctly (TPM2, not tpm2). Please write 'Python 2' instead of python2.

Will do, thanks!

Quoted 5 lines

With that said the commit message is inaccurate. The root reason for moving to Python 3 is that Python 2 is no longer supported by the Python upstream project. Nothing to do with Linux or Linux distributions for that matter.

Will do, thanks!

Quoted 1 line

/Jarkko