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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <pengfei.xu@xxxxxxxxx>
> > 
> > Use "selftests: tpm: <short summary>".
> > 

   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



[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux