[2/2] selftests: tpm2: Extend tests to cover partial reads
diff mbox series

Message ID 154939851969.18477.13778157467131576825.stgit@tstruk-mobl1.jf.intel.com
State New
Headers show
Series
  • [1/2] selftests: tpm2: Open tpm dev in unbuffered mode
Related show

Commit Message

Tadeusz Struk Feb. 5, 2019, 8:28 p.m. UTC
Three new tests added:
1. Send get random cmd, read header in 1st read, read the rest in second
   read - expect success
2. Send get random cmd, read only part of the response, send another
   get random command, read the response - expect success
3. Send get random cmd followed by another get random cmd, without
   reading the first response - expect the second cmd to fail with -EBUSY

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
---
 tools/testing/selftests/tpm2/tpm2.py       |    1 
 tools/testing/selftests/tpm2/tpm2_tests.py |   82 ++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)

Comments

Jarkko Sakkinen Feb. 11, 2019, 4:48 p.m. UTC | #1
You are missing a cover letter from this patch set. Please have it in
v2. Also use tag "selftests/tpm2" instead of having two tags in the
short summaries. Now they look a bit weird.

On Tue, Feb 05, 2019 at 12:28:39PM -0800, Tadeusz Struk wrote:
> Three new tests added:
> 1. Send get random cmd, read header in 1st read, read the rest in second
>    read - expect success
> 2. Send get random cmd, read only part of the response, send another
>    get random command, read the response - expect success
> 3. Send get random cmd followed by another get random cmd, without
>    reading the first response - expect the second cmd to fail with -EBUSY
> 
> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
> ---
>  tools/testing/selftests/tpm2/tpm2.py       |    1 
>  tools/testing/selftests/tpm2/tpm2_tests.py |   82 ++++++++++++++++++++++++++++
>  2 files changed, 83 insertions(+)
> 
> diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
> index c2b9f2b1a0ac..828c18584624 100644
> --- a/tools/testing/selftests/tpm2/tpm2.py
> +++ b/tools/testing/selftests/tpm2/tpm2.py
> @@ -22,6 +22,7 @@ TPM2_CC_UNSEAL = 0x015E
>  TPM2_CC_FLUSH_CONTEXT = 0x0165
>  TPM2_CC_START_AUTH_SESSION = 0x0176
>  TPM2_CC_GET_CAPABILITY	= 0x017A
> +TPM2_CC_GET_RANDOM = 0x017B
>  TPM2_CC_PCR_READ = 0x017E
>  TPM2_CC_POLICY_PCR = 0x017F
>  TPM2_CC_PCR_EXTEND = 0x0182
> diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py
> index 3bb066fea4a0..3f6a65cbdd39 100644
> --- a/tools/testing/selftests/tpm2/tpm2_tests.py
> +++ b/tools/testing/selftests/tpm2/tpm2_tests.py
> @@ -158,6 +158,88 @@ class SmokeTest(unittest.TestCase):
>              pass
>          self.assertEqual(rejected, True)
>  
> +    def test_read_partial_resp(self):
> +        """Reads random in two subsequent reads"""
> +        log = logging.getLogger(__name__)
> +        try:
> +            fmt = '>HIIH'
> +            cmd = struct.pack(fmt,
> +                              tpm2.TPM2_ST_NO_SESSIONS,
> +                              struct.calcsize(fmt),
> +                              tpm2.TPM2_CC_GET_RANDOM,
> +                              0x20)
> +            self.client.tpm.write(cmd)
> +            hdr = self.client.tpm.read(10)
> +            sz = struct.unpack('>I', hdr[2:6])[0]
> +            rsp = self.client.tpm.read()

8<

> +            log.debug("header:")
> +            log.debug(tpm2.hex_dump(hdr))
> +            log.debug("resp:")
> +            log.debug(tpm2.hex_dump(rsp))

Remove.

> +        except:

8<

> +            log.debug("Error:", sys.exc_info()[0])

Remove.

> +            raise
> +
> +        self.assertEqual(sz, 10 + 2 + 32)
> +        self.assertEqual(len(rsp), 2 + 32)
> +
> +    def test_read_partial_overwrite(self):
> +        """Reads only part of the response and issue a new cmd"""
> +        log = logging.getLogger(__name__)
> +        try:
> +            fmt = '>HIIH'
> +            cmd = struct.pack(fmt,
> +                              tpm2.TPM2_ST_NO_SESSIONS,
> +                              struct.calcsize(fmt),
> +                              tpm2.TPM2_CC_GET_RANDOM,
> +                              0x20)
> +            self.client.tpm.write(cmd)
> +            # Read part of the respone
> +            rsp1 = self.client.tpm.read(15)

8<

> +            log.debug("rsp 1:")
> +            log.debug(tpm2.hex_dump(rsp1))

Remove.

> +
> +            # Send a new cmd
> +            self.client.tpm.write(cmd)
> +
> +            # Read the whole respone
> +            rsp2 = self.client.tpm.read()

8<

> +            log.debug("rsp 2:")
> +            log.debug(tpm2.hex_dump(rsp2))

Remove.

> +
> +        except:

8<
> +            log.debug("Error:", sys.exc_info()[0])

Remove.

> +            raise
> +
> +        self.assertEqual(len(rsp1), 15)
> +        self.assertEqual(len(rsp2), 10 + 2 + 32)
> +
> +    def test_send_two_cmds(self):
> +        """Send two cmds without reading a response"""
> +        rejected = False
> +        try:
> +            fmt = '>HIIH'
> +            cmd = struct.pack(fmt,
> +                              tpm2.TPM2_ST_NO_SESSIONS,
> +                              struct.calcsize(fmt),
> +                              tpm2.TPM2_CC_GET_RANDOM,
> +                              0x20)
> +            self.client.tpm.write(cmd)
> +
> +            # expect the second one to raise -EBUSY error
> +            self.client.tpm.write(cmd)
> +            rsp = self.client.tpm.read()
> +
> +        except IOError, e:
> +            # read the response
> +            rsp = self.client.tpm.read()
> +            rejected = True
> +            pass
> +        except:
> +            raise
> +
> +        self.assertEqual(rejected, True)
> +
>  class SpaceTest(unittest.TestCase):
>      def setUp(self):
>          logging.basicConfig(filename='SpaceTest.log', level=logging.DEBUG)
> 

/Jarkko
Tadeusz Struk Feb. 11, 2019, 5:55 p.m. UTC | #2
On 2/11/19 8:48 AM, Jarkko Sakkinen wrote:
> You are missing a cover letter from this patch set. Please have it in
> v2. Also use tag "selftests/tpm2" instead of having two tags in the
> short summaries. Now they look a bit weird.

Since when is the cover letter mandatory?
I understand that is helps for a complicated patch set
to explain the problem and solution in the cover letter,
but for this simple test case addition what's the point?
And there is nothing forcing a cover letter in
https://www.kernel.org/doc/html/v4.20/process/submitting-patches.html

Also double tags seams to be quite common for selftest.
See git log tools/testing/selftests/
Thanks,
Jarkko Sakkinen Feb. 12, 2019, 11:06 p.m. UTC | #3
On Mon, Feb 11, 2019 at 09:55:36AM -0800, Tadeusz Struk wrote:
> On 2/11/19 8:48 AM, Jarkko Sakkinen wrote:
> > You are missing a cover letter from this patch set. Please have it in
> > v2. Also use tag "selftests/tpm2" instead of having two tags in the
> > short summaries. Now they look a bit weird.
> 
> Since when is the cover letter mandatory?
> I understand that is helps for a complicated patch set
> to explain the problem and solution in the cover letter,
> but for this simple test case addition what's the point?
> And there is nothing forcing a cover letter in
> https://www.kernel.org/doc/html/v4.20/process/submitting-patches.html

OK, cool.

> Also double tags seams to be quite common for selftest.
> See git log tools/testing/selftests/

Prefer slash for TPM specific commits.

I can merge this after those extra logs are removed.

/Jarkko

Patch
diff mbox series

diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
index c2b9f2b1a0ac..828c18584624 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -22,6 +22,7 @@  TPM2_CC_UNSEAL = 0x015E
 TPM2_CC_FLUSH_CONTEXT = 0x0165
 TPM2_CC_START_AUTH_SESSION = 0x0176
 TPM2_CC_GET_CAPABILITY	= 0x017A
+TPM2_CC_GET_RANDOM = 0x017B
 TPM2_CC_PCR_READ = 0x017E
 TPM2_CC_POLICY_PCR = 0x017F
 TPM2_CC_PCR_EXTEND = 0x0182
diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py
index 3bb066fea4a0..3f6a65cbdd39 100644
--- a/tools/testing/selftests/tpm2/tpm2_tests.py
+++ b/tools/testing/selftests/tpm2/tpm2_tests.py
@@ -158,6 +158,88 @@  class SmokeTest(unittest.TestCase):
             pass
         self.assertEqual(rejected, True)
 
+    def test_read_partial_resp(self):
+        """Reads random in two subsequent reads"""
+        log = logging.getLogger(__name__)
+        try:
+            fmt = '>HIIH'
+            cmd = struct.pack(fmt,
+                              tpm2.TPM2_ST_NO_SESSIONS,
+                              struct.calcsize(fmt),
+                              tpm2.TPM2_CC_GET_RANDOM,
+                              0x20)
+            self.client.tpm.write(cmd)
+            hdr = self.client.tpm.read(10)
+            sz = struct.unpack('>I', hdr[2:6])[0]
+            rsp = self.client.tpm.read()
+            log.debug("header:")
+            log.debug(tpm2.hex_dump(hdr))
+            log.debug("resp:")
+            log.debug(tpm2.hex_dump(rsp))
+        except:
+            log.debug("Error:", sys.exc_info()[0])
+            raise
+
+        self.assertEqual(sz, 10 + 2 + 32)
+        self.assertEqual(len(rsp), 2 + 32)
+
+    def test_read_partial_overwrite(self):
+        """Reads only part of the response and issue a new cmd"""
+        log = logging.getLogger(__name__)
+        try:
+            fmt = '>HIIH'
+            cmd = struct.pack(fmt,
+                              tpm2.TPM2_ST_NO_SESSIONS,
+                              struct.calcsize(fmt),
+                              tpm2.TPM2_CC_GET_RANDOM,
+                              0x20)
+            self.client.tpm.write(cmd)
+            # Read part of the respone
+            rsp1 = self.client.tpm.read(15)
+            log.debug("rsp 1:")
+            log.debug(tpm2.hex_dump(rsp1))
+
+            # Send a new cmd
+            self.client.tpm.write(cmd)
+
+            # Read the whole respone
+            rsp2 = self.client.tpm.read()
+            log.debug("rsp 2:")
+            log.debug(tpm2.hex_dump(rsp2))
+
+        except:
+            log.debug("Error:", sys.exc_info()[0])
+            raise
+
+        self.assertEqual(len(rsp1), 15)
+        self.assertEqual(len(rsp2), 10 + 2 + 32)
+
+    def test_send_two_cmds(self):
+        """Send two cmds without reading a response"""
+        rejected = False
+        try:
+            fmt = '>HIIH'
+            cmd = struct.pack(fmt,
+                              tpm2.TPM2_ST_NO_SESSIONS,
+                              struct.calcsize(fmt),
+                              tpm2.TPM2_CC_GET_RANDOM,
+                              0x20)
+            self.client.tpm.write(cmd)
+
+            # expect the second one to raise -EBUSY error
+            self.client.tpm.write(cmd)
+            rsp = self.client.tpm.read()
+
+        except IOError, e:
+            # read the response
+            rsp = self.client.tpm.read()
+            rejected = True
+            pass
+        except:
+            raise
+
+        self.assertEqual(rejected, True)
+
 class SpaceTest(unittest.TestCase):
     def setUp(self):
         logging.basicConfig(filename='SpaceTest.log', level=logging.DEBUG)