Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5763939ybl; Tue, 10 Dec 2019 11:06:15 -0800 (PST) X-Google-Smtp-Source: APXvYqzMpBBVRURfZGcgQwysuJdnUzDo8scOl7URqcB1wRH6r/2wP1zJGVUC+ZmGSBU5SD11oW1k X-Received: by 2002:aca:3a41:: with SMTP id h62mr326289oia.97.1576004775247; Tue, 10 Dec 2019 11:06:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576004775; cv=none; d=google.com; s=arc-20160816; b=oVcN57TINyU2zrrVlVi6aSqYJg5MSiPyA/qQ4DgiS+Z/iuSizvCPNPYBLwdInObSFU V8iiIVmpQP0bHuYsAWzT7ETF493YUaaDcL75QqI84QpsYrJwFpuiGX2mNbWtlyiYQbur sD973dotiIpqmAJI/uEXLs1ORne6px6EJvGQC1aQ0mhCe3x0H13Jengw82wt81iDKFsL B7k42p3xWAP9KXyZFAZiewx3PW8uBeZWBeFmJJHUKG3PIUlXaPIVioWJVeIclBGQ68dI VO/Qus2iQGXG+eh7KLyNUksbtF2HHOb0wPPrZowSIKb5Dr6H1zNCRpWhVgwEu2BAsTX9 lZqQ== 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=GYeVQYzYNxCFylIh5ekvBtLqLaUnuSVvjwIWaFT2KW8=; b=rfpnfZVaIgnWs8qAUtEYUPSe68v0olWz5Y4nzpk6UCvLTdYlbbsHokcJebeZy7tT01 UDvuE5dqX95hLZyCxufLoKTULtLv6V9U/oBG4rwPECTR0i6ZEfaqwVXOW4bb6KqAkjsY Y6Wl7V+IcrwrMflTeVdDq3mT5E2A+f0piTYw2xQVdbxuPHRzc8LtbP26ZMY3OO+LoB7a CTuciLDb0U62vVf1cD9WxwJzDotb01+k3xSWDkf3FBgjJPhQ3GEmVgrQNTF637FE4Y14 5f+71GP+p4KTXI55YT31n1wkj1JSvL3oauzL9eRXWoD49DSUmyDkGkIyqwTGUm1g652u Gg2Q== 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 m204si2423997oib.235.2019.12.10.11.06.01; Tue, 10 Dec 2019 11:06:15 -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 S1727652AbfLJTE7 (ORCPT + 99 others); Tue, 10 Dec 2019 14:04:59 -0500 Received: from mga09.intel.com ([134.134.136.24]:50106 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727603AbfLJTE6 (ORCPT ); Tue, 10 Dec 2019 14:04:58 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Dec 2019 11:04:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,300,1571727600"; d="scan'208";a="203294114" Received: from tstruk-mobl1.jf.intel.com (HELO [127.0.1.1]) ([10.7.196.67]) by orsmga007.jf.intel.com with ESMTP; 10 Dec 2019 11:04:58 -0800 Subject: [PATCH] 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: Tue, 10 Dec 2019 11:04:59 -0800 Message-ID: <157600469924.5042.14784541627191833405.stgit@tstruk-mobl1> In-Reply-To: <34e5340f-de75-f20e-7898-6142eac45c13@intel.com> References: <34e5340f-de75-f20e-7898-6142eac45c13@intel.com> 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 asyn 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()