Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1113549ybl; Thu, 12 Dec 2019 09:49:47 -0800 (PST) X-Google-Smtp-Source: APXvYqyXidlu3yv8i2rLBsIHPwEuFG8J1EL6YXCY/rWt4y3YvdLxfPAAfRvzoPyqNV6nsw7Mvoqc X-Received: by 2002:a9d:6a8f:: with SMTP id l15mr9164187otq.59.1576172987423; Thu, 12 Dec 2019 09:49:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576172987; cv=none; d=google.com; s=arc-20160816; b=LfyMxhxHN/MLfttRoM+29qSFk2s5gE8DoIbZ1YSG8+zlsHWlyY9khRD/FBLfWrYWhk aCR+ERHujxvGsbplOhhKXk+BPaUmwZBixRyrNpJKlv3qcgW8GA3PiYaWMUlWcUuuAXpT Ey7Rnw0JfEQVCtj7IYC4fsTrNwhTsrnqRJJKsuzWEVlrojDks8cFs/hAJhiOknqPtoPX R2lVfhXO10C8stuMthY1esmIYbzDeBBJqymAauDET3olz3aInMzpfSv7XfaJLTTRgcQl 6dx2XHe6JvOgqtwAyo9c8zPcntM+ytF/LgORXRcRDVdgJx6aITzgeKIkz04SSaKHezCZ /Nqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject; bh=puXJ58oMZtnAeifJaLSfu4hNqGgkDUVEmqU1zQlmdvY=; b=TVPFy+gN8/20ZaU5gh35igD5vy8JM35KNzmTUpfRRDcLaN8GKilX38LfKol4qpiz/0 03Pylsp12h8Ptndqq0OIN3V6KgrX7HHuVwxATcwsj3DIy/Ea1SmPyvBTCx/GgaA8I6SG VeoJ2siCed2AlKi2NwaB/G4vybvu/zZEQddMHcox8t26GSH01Dzuzjgm4Zia6FY+jA6I KugLR8Ii8ncoWYa5WvzlM8uB9h5oEvkK87vtfBNc0j4c707FUdbNnfG9cr4AIsjBbUD3 goI7OY5cxFPVw3HvwTOg4aHJ8HGYC/mLtzoIytjuiqg7M9zfF+XvS44HOlST4gUPL7Ch xq9g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v7si5169168otr.44.2019.12.12.09.49.32; Thu, 12 Dec 2019 09:49:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730234AbfLLRsv (ORCPT + 99 others); Thu, 12 Dec 2019 12:48:51 -0500 Received: from mga12.intel.com ([192.55.52.136]:15317 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729771AbfLLRsv (ORCPT ); Thu, 12 Dec 2019 12:48:51 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Dec 2019 09:48:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,306,1571727600"; d="scan'208";a="414006040" Received: from tstruk-mobl1.jf.intel.com (HELO [127.0.1.1]) ([10.7.196.67]) by fmsmga005.fm.intel.com with ESMTP; 12 Dec 2019 09:48:50 -0800 Subject: [PATCH =v2 2/3] tpm: selftest: add test covering async mode From: Tadeusz Struk To: jarkko.sakkinen@linux.intel.com Cc: tadeusz.struk@intel.com, peterz@infradead.org, linux-kernel@vger.kernel.org, jgg@ziepe.ca, mingo@redhat.com, jeffrin@rajagiritech.edu.in, linux-integrity@vger.kernel.org, will@kernel.org, peterhuewe@gmx.de Date: Thu, 12 Dec 2019 09:48:53 -0800 Message-ID: <157617293389.8172.8156104731485294664.stgit@tstruk-mobl1> In-Reply-To: <157617292787.8172.9586296287013438621.stgit@tstruk-mobl1> References: <157617292787.8172.9586296287013438621.stgit@tstruk-mobl1> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a test that sends a tpm cmd in an async mode. Currently there is a gap in test coverage with regards to this functionality. Signed-off-by: Tadeusz Struk --- tools/testing/selftests/tpm2/test_smoke.sh | 1 + tools/testing/selftests/tpm2/tpm2.py | 19 +++++++++++++++++-- tools/testing/selftests/tpm2/tpm2_tests.py | 13 +++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/tpm2/test_smoke.sh b/tools/testing/selftests/tpm2/test_smoke.sh index 80521d46220c..cb54ab637ea6 100755 --- a/tools/testing/selftests/tpm2/test_smoke.sh +++ b/tools/testing/selftests/tpm2/test_smoke.sh @@ -2,3 +2,4 @@ # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) python -m unittest -v tpm2_tests.SmokeTest +python -m unittest -v tpm2_tests.AsyncTest diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py index 828c18584624..d0fcb66a88a6 100644 --- a/tools/testing/selftests/tpm2/tpm2.py +++ b/tools/testing/selftests/tpm2/tpm2.py @@ -6,8 +6,8 @@ import socket import struct import sys import unittest -from fcntl import ioctl - +import fcntl +import select TPM2_ST_NO_SESSIONS = 0x8001 TPM2_ST_SESSIONS = 0x8002 @@ -352,6 +352,7 @@ def hex_dump(d): class Client: FLAG_DEBUG = 0x01 FLAG_SPACE = 0x02 + FLAG_NONBLOCK = 0x04 TPM_IOC_NEW_SPACE = 0xa200 def __init__(self, flags = 0): @@ -362,13 +363,27 @@ class Client: else: self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0) + if (self.flags & Client.FLAG_NONBLOCK): + flags = fcntl.fcntl(self.tpm, fcntl.F_GETFL) + flags |= os.O_NONBLOCK + fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags) + self.tpm_poll = select.poll() + def close(self): self.tpm.close() def send_cmd(self, cmd): self.tpm.write(cmd) + + if (self.flags & Client.FLAG_NONBLOCK): + self.tpm_poll.register(self.tpm, select.POLLIN) + self.tpm_poll.poll(10000) + rsp = self.tpm.read() + if (self.flags & Client.FLAG_NONBLOCK): + self.tpm_poll.unregister(self.tpm) + if (self.flags & Client.FLAG_DEBUG) != 0: sys.stderr.write('cmd' + os.linesep) sys.stderr.write(hex_dump(cmd) + os.linesep) diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py index d4973be53493..728be7c69b76 100644 --- a/tools/testing/selftests/tpm2/tpm2_tests.py +++ b/tools/testing/selftests/tpm2/tpm2_tests.py @@ -288,3 +288,16 @@ class SpaceTest(unittest.TestCase): self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE | tpm2.TSS2_RESMGR_TPM_RC_LAYER) + +class AsyncTest(unittest.TestCase): + def setUp(self): + logging.basicConfig(filename='AsyncTest.log', level=logging.DEBUG) + + def test_async(self): + log = logging.getLogger(__name__) + log.debug(sys._getframe().f_code.co_name) + + async_client = tpm2.Client(tpm2.Client.FLAG_NONBLOCK) + log.debug("Calling get_cap in a NON_BLOCKING mode") + async_client.get_cap(tpm2.TPM2_CAP_HANDLES, tpm2.HR_LOADED_SESSION) + async_client.close()