Skip to contentSkip to conversation

selftests: tpm: upgrade TPM2 tests from Python 2 to Python 3

PATCH In review Pengfei Xu 3 participants 7 messages Jun 26, 2020 – Jul 6, 2020

Subsystems (2)
lore
Share & tools
lore

Pengfei Xu

author
#0 lore raw

PATCH v4 1/1 selftests: tpm: 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       | 56 +++++++++++-----------
 tools/testing/selftests/tpm2/tpm2_tests.py | 39 +++++++--------
 4 files changed, 52 insertions(+), 49 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 +29 -27

182 lines not on this page · raw message

tools/testing/selftests/tpm2/tpm2_tests.py +20 -19
index 728be7c69b76..9d764306887b 100644
@@ -20,8 +20,8 @@ class SmokeTest(unittest.TestCase):
         self.client.close()
 
     def test_seal_with_auth(self):
-        data = 'X' * 64
-        auth = 'A' * 15
+        data = ('X' * 64).encode()
+        auth = ('A' * 15).encode()
 
         blob = self.client.seal(self.root_key, data, auth, None)
         result = self.client.unseal(self.root_key, blob, auth, None)
@@ -30,8 +30,8 @@ class SmokeTest(unittest.TestCase):
     def test_seal_with_policy(self):
         handle = self.client.start_auth_session(tpm2.TPM2_SE_TRIAL)
 
-        data = 'X' * 64
-        auth = 'A' * 15
+        data = ('X' * 64).encode()
+        auth = ('A' * 15).encode()
         pcrs = [16]
 
         try:
@@ -58,14 +58,15 @@ class SmokeTest(unittest.TestCase):
         self.assertEqual(data, result)
 
     def test_unseal_with_wrong_auth(self):
-        data = 'X' * 64
-        auth = 'A' * 20
+        data = ('X' * 64).encode()
+        auth = ('A' * 20).encode()
         rc = 0
 
         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:
+            result = self.client.unseal(self.root_key, blob,
+                        auth[:-1] + 'B'.encode(), None)
+        except ProtocolError as e:
             rc = e.rc
 
         self.assertEqual(rc, tpm2.TPM2_RC_AUTH_FAIL)
@@ -73,8 +74,8 @@ class SmokeTest(unittest.TestCase):
     def test_unseal_with_wrong_policy(self):
         handle = self.client.start_auth_session(tpm2.TPM2_SE_TRIAL)
 
-        data = 'X' * 64
-        auth = 'A' * 17
+        data = ('X' * 64).encode()
+        auth = ('A' * 17).encode()
         pcrs = [16]
 
         try:
@@ -91,7 +92,7 @@ class SmokeTest(unittest.TestCase):
         # This should succeed.
 
         ds = tpm2.get_digest_size(tpm2.TPM2_ALG_SHA1)
-        self.client.extend_pcr(1, 'X' * ds)
+        self.client.extend_pcr(1, ('X' * ds).encode())
 
         handle = self.client.start_auth_session(tpm2.TPM2_SE_POLICY)
 
@@ -108,7 +109,7 @@ class SmokeTest(unittest.TestCase):
 
         # Then, extend a PCR that is part of the policy and try to unseal.
         # This should fail.
-        self.client.extend_pcr(16, 'X' * ds)
+        self.client.extend_pcr(16, ('X' * ds).encode())
 
         handle = self.client.start_auth_session(tpm2.TPM2_SE_POLICY)
 
@@ -119,7 +120,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:
@@ -130,13 +131,13 @@ class SmokeTest(unittest.TestCase):
 
     def test_seal_with_too_long_auth(self):
         ds = tpm2.get_digest_size(tpm2.TPM2_ALG_SHA1)
-        data = 'X' * 64
-        auth = 'A' * (ds + 1)
+        data = ('X' * 64).encode()
+        auth = ('A' * (ds + 1)).encode()
 
         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 +153,7 @@ class SmokeTest(unittest.TestCase):
                               0xDEADBEEF)
 
             self.client.send_cmd(cmd)
-        except IOError, e:
+        except IOError as e:
             rejected = True
         except:
             pass
@@ -212,7 +213,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 +284,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
Reviewed-by Tested-by on v4 1/1
#1 lore raw
Quoted 4 lines · Pengfei Xu

Python 2 is no longer supported by the Python upstream project, so upgrade TPM2 tests to Python 3.

I think that it's perfect now. Thank you.

Also

1. I checked that scripts/checkpatch.pl did not report any errors.
2. sudo python3 -m unittest -v tpm2_tests.SmokeTest
3. sudo python3 -m unittest -v tpm2_tests.SpaceTest

Shuah, I could pick this up after your PR (with my earlier fixes) lands to mainline, and sort out possible merge conflicts if they uprise. Is this fine by you?

/Jarkko

maintainer
#2 lore raw
Quoted 14 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.

I think that it's perfect now. Thank you.

Also

1. I checked that scripts/checkpatch.pl did not report any errors.
2. sudo python3 -m unittest -v tpm2_tests.SmokeTest
3. sudo python3 -m unittest -v tpm2_tests.SpaceTest

Quoted 7 lines

Shuah, I could pick this up after your PR (with my earlier fixes) lands to mainline, and sort out possible merge conflicts if they uprise. Is this fine by you?

Yes. I will apply them as soon as PR clears and hopefully they can go into rc5.

thanks,
-- Shuah

author
#3 lore raw

Thanks a lot Jarkko and Shuah!

BR.
Thanks!

Quoted 29 lines · Shuah Khan
Quoted 14 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.

I think that it's perfect now. Thank you.

Also

1. I checked that scripts/checkpatch.pl did not report any errors.
2. sudo python3 -m unittest -v tpm2_tests.SmokeTest
3. sudo python3 -m unittest -v tpm2_tests.SpaceTest

Quoted 7 lines

Shuah, I could pick this up after your PR (with my earlier fixes) lands to mainline, and sort out possible merge conflicts if they uprise. Is this fine by you?

Yes. I will apply them as soon as PR clears and hopefully they can go into rc5.

thanks,
-- Shuah

maintainer
#4 lore raw
Quoted 29 lines · Pengfei Xu

Thanks a lot Jarkko and Shuah!

BR.
Thanks!

Quoted 23 lines · Shuah Khan
Quoted 21 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.

I think that it's perfect now. Thank you.

Also

1. I checked that scripts/checkpatch.pl did not report any errors.
2. sudo python3 -m unittest -v tpm2_tests.SmokeTest
3. sudo python3 -m unittest -v tpm2_tests.SpaceTest

Shuah, I could pick this up after your PR (with my earlier fixes) lands to mainline, and sort out possible merge conflicts if they uprise. Is this fine by you?

I started applying this and then passed.

Doesn't this test fail if python3 isn't installed? Do you have to support both versions?

thanks,
-- Shuah

maintainer
#5 lore raw
Quoted 31 lines · Shuah Khan
Quoted 29 lines · Pengfei Xu

Thanks a lot Jarkko and Shuah!

BR.
Thanks!

Quoted 23 lines · Shuah Khan
Quoted 21 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.

I think that it's perfect now. Thank you.

Also

1. I checked that scripts/checkpatch.pl did not report any errors.
2. sudo python3 -m unittest -v tpm2_tests.SmokeTest
3. sudo python3 -m unittest -v tpm2_tests.SpaceTest

Shuah, I could pick this up after your PR (with my earlier fixes) lands to mainline, and sort out possible merge conflicts if they uprise. Is this fine by you?

Quoted 5 lines

I started applying this and then passed.

Doesn't this test fail if python3 isn't installed? Do you have to support both versions?

Never mind. Tested it on with python2. All is well. Applied to linux-kselftest fixes for Linux 5.8-rc5

thanks,
-- Shuah

#6 lore raw
Quoted 38 lines · Shuah Khan
Quoted 31 lines · Shuah Khan
Quoted 29 lines · Pengfei Xu

Thanks a lot Jarkko and Shuah!

BR.
Thanks!

Quoted 23 lines · Shuah Khan
Quoted 21 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.

I think that it's perfect now. Thank you.

Also

1. I checked that scripts/checkpatch.pl did not report any errors.
2. sudo python3 -m unittest -v tpm2_tests.SmokeTest
3. sudo python3 -m unittest -v tpm2_tests.SpaceTest

Shuah, I could pick this up after your PR (with my earlier fixes) lands to mainline, and sort out possible merge conflicts if they uprise. Is this fine by you?

Quoted 5 lines

I started applying this and then passed.

Doesn't this test fail if python3 isn't installed? Do you have to support both versions?

Quoted 5 lines

Never mind. Tested it on with python2. All is well. Applied to linux-kselftest fixes for Linux 5.8-rc5

thanks,
-- Shuah

OK, great, thanks a lot!

/Jarkko