Re: [PATCH v4] selftests: tpm: upgrade TPM2 tests from Python 2 to Python 3

2020-07-02 Thread Pengfei Xu
Thanks a lot Jarkko and Shuah!

BR.
Thanks!

On 2020-07-02 at 15:32:49 -0600, Shuah Khan wrote:
> On 7/2/20 1:44 PM, Jarkko Sakkinen wrote:
> > On Fri, Jun 26, 2020 at 11:40:52AM +0800, Pengfei Xu wrote:
> > > Python 2 is no longer supported by the Python upstream project, so
> > > upgrade TPM2 tests to Python 3.
> > > 
> > > Signed-off-by: Pengfei Xu 
> > 
> > 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
> > 
> > Reviewed-by: Jarkko Sakkinen 
> > Tested-by: Jarkko Sakkinen 
> > 
> > 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
> 


[PATCH v4] selftests: tpm: upgrade TPM2 tests from Python 2 to Python 3

2020-06-25 Thread 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 
---
 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(-)

diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
b/tools/testing/selftests/tpm2/test_smoke.sh
index 663062701d5a..d05467f6d258 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/test_space.sh 
b/tools/testing/selftests/tpm2/test_space.sh
index 36c9d030a1c6..151c64e8ee9f 100755
--- a/tools/testing/selftests/tpm2/test_space.sh
+++ b/tools/testing/selftests/tpm2/test_space.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/tpm2.py 
b/tools/testing/selftests/tpm2/tpm2.py
index d0fcb66a88a6..f34486cd7342 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -247,14 +247,14 @@ class ProtocolError(Exception):
 class AuthCommand(object):
 """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=bytes(),
+ session_attributes=0, hmac=bytes()):
 self.session_handle = session_handle
 self.nonce = nonce
 self.session_attributes = session_attributes
 self.hmac = hmac
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
 return struct.pack(fmt, self.session_handle, len(self.nonce),
self.nonce, self.session_attributes, len(self.hmac),
@@ -268,11 +268,11 @@ class AuthCommand(object):
 class SensitiveCreate(object):
 """TPMS_SENSITIVE_CREATE"""
 
-def __init__(self, user_auth='', data=''):
+def __init__(self, user_auth=bytes(), data=bytes()):
 self.user_auth = user_auth
 self.data = data
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
 return struct.pack(fmt, len(self.user_auth), self.user_auth,
len(self.data), self.data)
@@ -296,8 +296,9 @@ class Public(object):
 return '>HHIH%us%usH%us' % \
 (len(self.auth_policy), len(self.parameters), len(self.unique))
 
-def __init__(self, object_type, name_alg, object_attributes, 
auth_policy='',
- parameters='', unique=''):
+def __init__(self, object_type, name_alg, object_attributes,
+ auth_policy=bytes(), parameters=bytes(),
+ unique=bytes()):
 self.object_type = object_type
 self.name_alg = name_alg
 self.object_attributes = object_attributes
@@ -305,7 +306,7 @@ class Public(object):
 self.parameters = parameters
 self.unique = unique
 
-def __str__(self):
+def __bytes__(self):
 return struct.pack(self.__fmt(),
self.object_type,
self.name_alg,
@@ -343,7 +344,7 @@ def get_algorithm(name):
 
 def hex_dump(d):
 d = [format(ord(x), '02x') for x in d]
-d = [d[i: i + 16] for i in xrange(0, len(d), 16)]
+d = [d[i: i + 16] for i in range(0, len(d), 16)]
 d = [' '.join(x) for x in d]
 d = os.linesep.join(d)
 
@@ -401,7 +402,7 @@ class Client:
 pcrsel_len = max((i >> 3) + 1, 3)
 pcrsel = [0] * pcrsel_len
 pcrsel[i >> 3] = 1 << (i & 7)
-pcrsel = ''.join(map(chr, pcrsel))
+pcrsel = ''.join(map(chr, pcrsel)).encode()
 
 fmt = '>HII IHB%us' % (pcrsel_len)
 cmd = struct.pack(fmt,
@@ -443,7 +444,7 @@ class Client:
 TPM2_CC_PCR_EXTEND,
 i,
 len(auth_cmd),
-str(auth_cmd),
+bytes(auth_cmd),
 1, bank_alg, dig)
 
 self.send_cmd(cmd)
@@ -457,7 +458,7 @@ class Client:
   TPM2_RH_NULL,
   TPM2_RH_NULL,
   16,
-

Re: [PATCH v3] selftests: tpm: upgrade TPM2 tests from Python 2 to Python 3

2020-06-25 Thread Pengfei Xu
Hi Jarkko,
  I missed this improvement in previous mail.
  Will do.

Thanks!
BR.

On 2020-06-26 at 00:51:45 +0300, Jarkko Sakkinen wrote:
> On Fri, Jun 26, 2020 at 12:37:54AM +0800, Pengfei Xu wrote:
> > Python 2 is no longer supported by the Python upstream project, so
> > upgrade TPM2 tests to Python 3.
> > 
> > Signed-off-by: Pengfei Xu 
> > ---
> >  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(-)
> > 
> > diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
> > b/tools/testing/selftests/tpm2/test_smoke.sh
> > index 663062701d5a..d05467f6d258 100755
> > --- a/tools/testing/selftests/tpm2/test_smoke.sh
> > +++ b/tools/testing/selftests/tpm2/test_smoke.sh
> > @@ -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
> > diff --git a/tools/testing/selftests/tpm2/test_space.sh 
> > b/tools/testing/selftests/tpm2/test_space.sh
> > index 36c9d030a1c6..151c64e8ee9f 100755
> > --- a/tools/testing/selftests/tpm2/test_space.sh
> > +++ b/tools/testing/selftests/tpm2/test_space.sh
> > @@ -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
> > diff --git a/tools/testing/selftests/tpm2/tpm2.py 
> > b/tools/testing/selftests/tpm2/tpm2.py
> > index d0fcb66a88a6..88a0e7776a23 100644
> > --- a/tools/testing/selftests/tpm2/tpm2.py
> > +++ b/tools/testing/selftests/tpm2/tpm2.py
> > @@ -247,14 +247,14 @@ class ProtocolError(Exception):
> >  class AuthCommand(object):
> >  """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()):
> 
> Initialize these just directly as empty bytes objects (e.g.
> nonce=bytes())
> 
> >  self.session_handle = session_handle
> >  self.nonce = nonce
> >  self.session_attributes = session_attributes
> >  self.hmac = hmac
> >  
> > -def __str__(self):
> > +def __bytes__(self):
> >  fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
> >  return struct.pack(fmt, self.session_handle, len(self.nonce),
> > self.nonce, self.session_attributes, 
> > len(self.hmac),
> > @@ -268,11 +268,11 @@ class AuthCommand(object):
> >  class SensitiveCreate(object):
> >  """TPMS_SENSITIVE_CREATE"""
> >  
> > -def __init__(self, user_auth='', data=''):
> > +def __init__(self, user_auth=''.encode(), data=''.encode()):
> 
> Ditto.
> 
> >  self.user_auth = user_auth
> >  self.data = data
> >  
> > -def __str__(self):
> > +def __bytes__(self):
> >  fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
> >  return struct.pack(fmt, len(self.user_auth), self.user_auth,
> > len(self.data), self.data)
> > @@ -296,8 +296,9 @@ class Public(object):
> >  return '>HHIH%us%usH%us' % \
> >  (len(self.auth_policy), len(self.parameters), len(self.unique))
> >  
> > -def __init__(self, object_type, name_alg, object_attributes, 
> > auth_policy='',
> > - parameters='', unique=''):
> > +def __init__(self, object_type, name_alg, object_attributes,
> > + auth_policy=''.encode(), parameters=''.encode(),
> > + unique=''.encode()):
> 
> Ditto.
> 
> >  self.object_type = object_type
> >  self.name_alg = name_alg
> >  self.object_attributes = object_attributes

[PATCH v3] selftests: tpm: upgrade TPM2 tests from Python 2 to Python 3

2020-06-25 Thread 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 
---
 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(-)

diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
b/tools/testing/selftests/tpm2/test_smoke.sh
index 663062701d5a..d05467f6d258 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/test_space.sh 
b/tools/testing/selftests/tpm2/test_space.sh
index 36c9d030a1c6..151c64e8ee9f 100755
--- a/tools/testing/selftests/tpm2/test_space.sh
+++ b/tools/testing/selftests/tpm2/test_space.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/tpm2.py 
b/tools/testing/selftests/tpm2/tpm2.py
index d0fcb66a88a6..88a0e7776a23 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -247,14 +247,14 @@ class ProtocolError(Exception):
 class AuthCommand(object):
 """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()):
 self.session_handle = session_handle
 self.nonce = nonce
 self.session_attributes = session_attributes
 self.hmac = hmac
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
 return struct.pack(fmt, self.session_handle, len(self.nonce),
self.nonce, self.session_attributes, len(self.hmac),
@@ -268,11 +268,11 @@ class AuthCommand(object):
 class SensitiveCreate(object):
 """TPMS_SENSITIVE_CREATE"""
 
-def __init__(self, user_auth='', data=''):
+def __init__(self, user_auth=''.encode(), data=''.encode()):
 self.user_auth = user_auth
 self.data = data
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
 return struct.pack(fmt, len(self.user_auth), self.user_auth,
len(self.data), self.data)
@@ -296,8 +296,9 @@ class Public(object):
 return '>HHIH%us%usH%us' % \
 (len(self.auth_policy), len(self.parameters), len(self.unique))
 
-def __init__(self, object_type, name_alg, object_attributes, 
auth_policy='',
- parameters='', unique=''):
+def __init__(self, object_type, name_alg, object_attributes,
+ auth_policy=''.encode(), parameters=''.encode(),
+ unique=''.encode()):
 self.object_type = object_type
 self.name_alg = name_alg
 self.object_attributes = object_attributes
@@ -305,7 +306,7 @@ class Public(object):
 self.parameters = parameters
 self.unique = unique
 
-def __str__(self):
+def __bytes__(self):
 return struct.pack(self.__fmt(),
self.object_type,
self.name_alg,
@@ -343,7 +344,7 @@ def get_algorithm(name):
 
 def hex_dump(d):
 d = [format(ord(x), '02x') for x in d]
-d = [d[i: i + 16] for i in xrange(0, len(d), 16)]
+d = [d[i: i + 16] for i in range(0, len(d), 16)]
 d = [' '.join(x) for x in d]
 d = os.linesep.join(d)
 
@@ -401,7 +402,7 @@ class Client:
 pcrsel_len = max((i >> 3) + 1, 3)
 pcrsel = [0] * pcrsel_len
 pcrsel[i >> 3] = 1 << (i & 7)
-pcrsel = ''.join(map(chr, pcrsel))
+pcrsel = ''.join(map(chr, pcrsel)).encode()
 
 fmt = '>HII IHB%us' % (pcrsel_len)
 cmd = struct.pack(fmt,
@@ -443,7 +444,7 @@ class Client:
 TPM2_CC_PCR_EXTEND,
 i,
 len(auth_cmd),
-str(auth_cmd),
+bytes(auth_cmd),
 1, bank_alg, dig)
 
 self.send_cmd(cmd)
@@ -457,7 +458,7 @@ class Client:

Re: [PATCH v2] Kernel selftests: TPM2: upgrade TPM2 tests from Python 2 to Python 3

2020-06-25 Thread Pengfei Xu
On 2020-06-25 at 02:14:57 +0300, Jarkko Sakkinen wrote:
> On Tue, Jun 23, 2020 at 07:41:55PM +0800, Pengfei Xu wrote:
> >   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!

> 
> /Jarkko


Re: [PATCH v2] Kernel selftests: TPM2: upgrade TPM2 tests from Python 2 to Python 3

2020-06-23 Thread Pengfei Xu
Hi Jarkk,
  Thanks for your comments!
  My feedback is as below.

  BR.

On 2020-06-23 at 00:47:39 +0300, Jarkko Sakkinen wrote:
> On Tue, Jun 23, 2020 at 12:46:18AM +0300, Jarkko Sakkinen wrote:
> > On Thu, Jun 18, 2020 at 04:15:02PM +0800, Pengfei Xu wrote:
> > > Python 2 is no longer supported by the Python upstream project, so
> > > upgrade TPM2 tests to Python 3.
> > > 
> > > Signed-off-by: Pengfei Xu 
> > 
> > Use "selftests: tpm: ".
> > 

   Will do.

> > > ---
> > >  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(-)
> > > 
> > > diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
> > > b/tools/testing/selftests/tpm2/test_smoke.sh
> > > index 663062701d5a..d05467f6d258 100755
> > > --- a/tools/testing/selftests/tpm2/test_smoke.sh
> > > +++ b/tools/testing/selftests/tpm2/test_smoke.sh
> > > @@ -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
> > > diff --git a/tools/testing/selftests/tpm2/test_space.sh 
> > > b/tools/testing/selftests/tpm2/test_space.sh
> > > index 36c9d030a1c6..151c64e8ee9f 100755
> > > --- a/tools/testing/selftests/tpm2/test_space.sh
> > > +++ b/tools/testing/selftests/tpm2/test_space.sh
> > > @@ -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
> > > diff --git a/tools/testing/selftests/tpm2/tpm2.py 
> > > b/tools/testing/selftests/tpm2/tpm2.py
> > > index d0fcb66a88a6..b0ccc1499c53 100644
> > > --- a/tools/testing/selftests/tpm2/tpm2.py
> > > +++ b/tools/testing/selftests/tpm2/tpm2.py
> > > @@ -247,14 +247,18 @@ class ProtocolError(Exception):
> > >  class AuthCommand(object):
> > >  """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.
> 
> 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!

> /Jarkko


[PATCH v2] Kernel selftests: TPM2: upgrade TPM2 tests from Python 2 to Python 3

2020-06-18 Thread 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 
---
 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(-)

diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
b/tools/testing/selftests/tpm2/test_smoke.sh
index 663062701d5a..d05467f6d258 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/test_space.sh 
b/tools/testing/selftests/tpm2/test_space.sh
index 36c9d030a1c6..151c64e8ee9f 100755
--- a/tools/testing/selftests/tpm2/test_space.sh
+++ b/tools/testing/selftests/tpm2/test_space.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/tpm2.py 
b/tools/testing/selftests/tpm2/tpm2.py
index d0fcb66a88a6..b0ccc1499c53 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -247,14 +247,18 @@ class ProtocolError(Exception):
 class AuthCommand(object):
 """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()
 self.session_handle = session_handle
 self.nonce = nonce
 self.session_attributes = session_attributes
 self.hmac = hmac
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
 return struct.pack(fmt, self.session_handle, len(self.nonce),
self.nonce, self.session_attributes, len(self.hmac),
@@ -268,11 +272,15 @@ class AuthCommand(object):
 class SensitiveCreate(object):
 """TPMS_SENSITIVE_CREATE"""
 
-def __init__(self, user_auth='', data=''):
+def __init__(self, user_auth=''.encode(), data=''.encode()):
+if not isinstance(user_auth, bytes):
+user_auth = user_auth.encode()
+if not isinstance(data, bytes):
+data = data.encode()
 self.user_auth = user_auth
 self.data = data
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
 return struct.pack(fmt, len(self.user_auth), self.user_auth,
len(self.data), self.data)
@@ -296,8 +304,15 @@ class Public(object):
 return '>HHIH%us%usH%us' % \
 (len(self.auth_policy), len(self.parameters), len(self.unique))
 
-def __init__(self, object_type, name_alg, object_attributes, 
auth_policy='',
- parameters='', unique=''):
+def __init__(self, object_type, name_alg, object_attributes,
+ auth_policy=''.encode(), parameters=''.encode(),
+ unique=''.encode()):
+if not isinstance(auth_policy, bytes):
+auth_policy = auth_policy.encode()
+if not isinstance(parameters, bytes):
+parameters = parameters.encode()
+if not isinstance(unique, bytes):
+unique = unique.encode()
 self.object_type = object_type
 self.name_alg = name_alg
 self.object_attributes = object_attributes
@@ -305,7 +320,7 @@ class Public(object):
 self.parameters = parameters
 self.unique = unique
 
-def __str__(self):
+def __bytes__(self):
 return struct.pack(self.__fmt(),
self.object_type,
self.name_alg,
@@ -343,7 +358,7 @@ def get_algorithm(name):
 
 def hex_dump(d):
 d = [format(ord(x), '02x') for x in d]
-d = [d[i: i + 16] for i in xrange(0, len(d), 16)]
+d = [d[i: i + 16] for i in range(0, len(d), 16)]
 d = [' '.join(x) for x in d]
 d = os.linesep.join(d)
 
@@ -401,7 +416,7 @@ class Client:
 pcrsel_len = max((i >> 3) + 1, 3)

Re: [Linux] [PATCH] Kernel selftests: tpm2: upgrade tpm2 tests from python2 to python3

2020-06-18 Thread Pengfei Xu
Hi Jarkko,
  Thanks for your advice.
  My feedback is as below.

  BR.
  Thanks!

On 2020-06-18 at 10:18:58 +0300, Jarkko Sakkinen wrote:
> On Thu, Jun 18, 2020 at 11:02:45AM +0800, Pengfei Xu wrote:
> > Some Linux OS will never support python2 anymore, so upgrade tpm2 selftests
> > to python3.
> > 
> > Signed-off-by: Pengfei Xu 
> 
> Linux is a kernel, not a full operating system. Perhaps you mean Linux
> distributions?
> 

  Will remove [Linux], thanks!

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

  Will do, thanks!

> 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!

> /Jarkko


[Linux] [PATCH] Kernel selftests: tpm2: upgrade tpm2 tests from python2 to python3

2020-06-17 Thread Pengfei Xu
Some Linux OS will never support python2 anymore, so upgrade tpm2 selftests
to python3.

Signed-off-by: Pengfei Xu 
---
 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(-)

diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
b/tools/testing/selftests/tpm2/test_smoke.sh
index 663062701d5a..d05467f6d258 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/test_space.sh 
b/tools/testing/selftests/tpm2/test_space.sh
index 36c9d030a1c6..151c64e8ee9f 100755
--- a/tools/testing/selftests/tpm2/test_space.sh
+++ b/tools/testing/selftests/tpm2/test_space.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/tpm2.py 
b/tools/testing/selftests/tpm2/tpm2.py
index d0fcb66a88a6..b0ccc1499c53 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -247,14 +247,18 @@ class ProtocolError(Exception):
 class AuthCommand(object):
 """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()
 self.session_handle = session_handle
 self.nonce = nonce
 self.session_attributes = session_attributes
 self.hmac = hmac
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
 return struct.pack(fmt, self.session_handle, len(self.nonce),
self.nonce, self.session_attributes, len(self.hmac),
@@ -268,11 +272,15 @@ class AuthCommand(object):
 class SensitiveCreate(object):
 """TPMS_SENSITIVE_CREATE"""
 
-def __init__(self, user_auth='', data=''):
+def __init__(self, user_auth=''.encode(), data=''.encode()):
+if not isinstance(user_auth, bytes):
+user_auth = user_auth.encode()
+if not isinstance(data, bytes):
+data = data.encode()
 self.user_auth = user_auth
 self.data = data
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
 return struct.pack(fmt, len(self.user_auth), self.user_auth,
len(self.data), self.data)
@@ -296,8 +304,15 @@ class Public(object):
 return '>HHIH%us%usH%us' % \
 (len(self.auth_policy), len(self.parameters), len(self.unique))
 
-def __init__(self, object_type, name_alg, object_attributes, 
auth_policy='',
- parameters='', unique=''):
+def __init__(self, object_type, name_alg, object_attributes,
+ auth_policy=''.encode(), parameters=''.encode(),
+ unique=''.encode()):
+if not isinstance(auth_policy, bytes):
+auth_policy = auth_policy.encode()
+if not isinstance(parameters, bytes):
+parameters = parameters.encode()
+if not isinstance(unique, bytes):
+unique = unique.encode()
 self.object_type = object_type
 self.name_alg = name_alg
 self.object_attributes = object_attributes
@@ -305,7 +320,7 @@ class Public(object):
 self.parameters = parameters
 self.unique = unique
 
-def __str__(self):
+def __bytes__(self):
 return struct.pack(self.__fmt(),
self.object_type,
self.name_alg,
@@ -343,7 +358,7 @@ def get_algorithm(name):
 
 def hex_dump(d):
 d = [format(ord(x), '02x') for x in d]
-d = [d[i: i + 16] for i in xrange(0, len(d), 16)]
+d = [d[i: i + 16] for i in range(0, len(d), 16)]
 d = [' '.join(x) for x in d]
 d = os.linesep.join(d)
 
@@ -401,7 +416,7 @@ class Client:
 pcrsel_len = max((i >> 3) + 1, 3)
 pcrsel 

[Linux] [PATCH] Kernel selftests: tpm2: upgrade tpm2 tests from python2 to python3

2020-06-16 Thread Pengfei Xu
Some Linux OS will never support python2 anymore, so upgrade tpm2 selftests
to python3.

Signed-off-by: Pengfei Xu 
---
 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(-)

diff --git a/tools/testing/selftests/tpm2/test_smoke.sh 
b/tools/testing/selftests/tpm2/test_smoke.sh
index 663062701d5a..d05467f6d258 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/test_space.sh 
b/tools/testing/selftests/tpm2/test_space.sh
index 36c9d030a1c6..151c64e8ee9f 100755
--- a/tools/testing/selftests/tpm2/test_space.sh
+++ b/tools/testing/selftests/tpm2/test_space.sh
@@ -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
diff --git a/tools/testing/selftests/tpm2/tpm2.py 
b/tools/testing/selftests/tpm2/tpm2.py
index d0fcb66a88a6..b0ccc1499c53 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -247,14 +247,18 @@ class ProtocolError(Exception):
 class AuthCommand(object):
 """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()
 self.session_handle = session_handle
 self.nonce = nonce
 self.session_attributes = session_attributes
 self.hmac = hmac
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
 return struct.pack(fmt, self.session_handle, len(self.nonce),
self.nonce, self.session_attributes, len(self.hmac),
@@ -268,11 +272,15 @@ class AuthCommand(object):
 class SensitiveCreate(object):
 """TPMS_SENSITIVE_CREATE"""
 
-def __init__(self, user_auth='', data=''):
+def __init__(self, user_auth=''.encode(), data=''.encode()):
+if not isinstance(user_auth, bytes):
+user_auth = user_auth.encode()
+if not isinstance(data, bytes):
+data = data.encode()
 self.user_auth = user_auth
 self.data = data
 
-def __str__(self):
+def __bytes__(self):
 fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
 return struct.pack(fmt, len(self.user_auth), self.user_auth,
len(self.data), self.data)
@@ -296,8 +304,15 @@ class Public(object):
 return '>HHIH%us%usH%us' % \
 (len(self.auth_policy), len(self.parameters), len(self.unique))
 
-def __init__(self, object_type, name_alg, object_attributes, 
auth_policy='',
- parameters='', unique=''):
+def __init__(self, object_type, name_alg, object_attributes,
+ auth_policy=''.encode(), parameters=''.encode(),
+ unique=''.encode()):
+if not isinstance(auth_policy, bytes):
+auth_policy = auth_policy.encode()
+if not isinstance(parameters, bytes):
+parameters = parameters.encode()
+if not isinstance(unique, bytes):
+unique = unique.encode()
 self.object_type = object_type
 self.name_alg = name_alg
 self.object_attributes = object_attributes
@@ -305,7 +320,7 @@ class Public(object):
 self.parameters = parameters
 self.unique = unique
 
-def __str__(self):
+def __bytes__(self):
 return struct.pack(self.__fmt(),
self.object_type,
self.name_alg,
@@ -343,7 +358,7 @@ def get_algorithm(name):
 
 def hex_dump(d):
 d = [format(ord(x), '02x') for x in d]
-d = [d[i: i + 16] for i in xrange(0, len(d), 16)]
+d = [d[i: i + 16] for i in range(0, len(d), 16)]
 d = [' '.join(x) for x in d]
 d = os.linesep.join(d)
 
@@ -401,7 +416,7 @@ class Client:
 pcrsel_len = max((i >> 3) + 1, 3)
 pcrsel 

Re: [Kernel BUG?] SMSW operation get success on UMIP KVM guest

2019-07-02 Thread Pengfei Xu
Seems no issue now.

Thanks all.

On 2019-07-02 at 09:52:39 +0800, Li Wang wrote:
> On Tue, Jul 2, 2019 at 12:04 AM Ricardo Neri <
> ricardo.neri-calde...@linux.intel.com> wrote:
> 
> > On Mon, Jul 01, 2019 at 08:57:28PM +0800, Li Wang wrote:
> > > On Mon, Jul 1, 2019 at 8:02 PM Paolo Bonzini 
> > wrote:
> > >
> > > > On 01/07/19 09:50, Li Wang wrote:
> > > > > Hello there,
> > > > >
> > > > > LTP/umip_basic_test get failed on KVM UMIP
> > > > > system(kernel-v5.2-rc4.x86_64). The test is only trying to do
> > > > >  asm volatile("smsw %0\n" : "=m" (val));
> > > > > and expect to get SIGSEGV in this SMSW operation, but it exits with 0
> > > > > unexpectedly.
> > > >
> > > > In addition to what Thomas said, perhaps you are using a host that does
> > > > *not* have UMIP, and configuring KVM to emulate it(*).  In that case,
> > it
> > > > is not possible to intercept SMSW, and therefore it will incorrectly
> > > > succeed.
> > > >
> > >
> > > Right, I checked the host system, and confirmed that CPU doesn't support
> > > UMIP.
> > >
> > > >
> > > > Paolo
> > > >
> > > > (*) before the x86 people jump at me, this won't happen unless you
> > > > explicitly pass an option to QEMU, such as "-cpu host,+umip". :)  The
> > > > incorrect emulation of SMSW when CR4.UMIP=1 is why.
> > > >
> > > Good to know this, is there any document for that declaration? It seems
> > > neither LTP issue nor kernel bug here. But anyway we'd better do
> > something
> > > to avoid the error in the test.
> >
> > The test case already checks for umip in /proc/cpuinfo, right? And in
> > long mode it always expects a SIGSEGV signal. If you did not add -cpu
> > host,+umip,
> > how come umip was present in /proc/cpuinfo?
> >
> 
> Yes, right.
> 
> But the KVM guest is not customized in manual, I reserved that system for
> automation test and did not aware of the '-cpu host,+umip,' parameter until
> Paolo points it out. In the last email, I was hoping to find a way to
> recognize this situation for the LTP test intelligently.
> 
> Thank you all for a reply to this.
> 
> -- 
> Regards,
> Li Wang