From a0ee706819037df1107ba7498fe3bfbf8b59540b Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Wed, 10 Feb 2016 10:33:21 -0800 Subject: [PATCH] cr50: test: consolidate test exceptions There is no point in defining tpm test exception classes per test type, one common class is enough, especially if the source module of the exception is reported. BRANCH=none BUG=none TEST=tried running the test without the USB FTDI cable plugged in, got the following error message: $ ./test/tpm_test/tpmtest.py Starting MPSSE at 800 kHz Error in tpmtest.py:54: Failed to connect $ Change-Id: I5642aa70c8a581099887b58e3a436d7f8d7608a1 Signed-off-by: Vadim Bendebury Reviewed-on: https://chromium-review.googlesource.com/327300 Reviewed-by: Nagendra Modadugu --- test/tpm_test/crypto_test.py | 44 ++++++++++++++++++------------------ test/tpm_test/ecc_test.py | 10 +++----- test/tpm_test/hash_test.py | 12 ++++------ test/tpm_test/rsa_test.py | 8 ++----- test/tpm_test/subcmd.py | 5 ++++ test/tpm_test/tpmtest.py | 37 ++++++++++++++---------------- 6 files changed, 53 insertions(+), 63 deletions(-) diff --git a/test/tpm_test/crypto_test.py b/test/tpm_test/crypto_test.py index 7fa4ea6146..929df7c48c 100644 --- a/test/tpm_test/crypto_test.py +++ b/test/tpm_test/crypto_test.py @@ -17,9 +17,6 @@ import utils DECRYPT = 0 ENCRYPT = 1 -class CryptoError(Exception): - pass - def get_attribute(tdesc, attr_name, required=True): """Retrieve an attribute value from an XML node. @@ -33,8 +30,8 @@ def get_attribute(tdesc, attr_name, required=True): Returns: The attribute value as a string (ascii or binary) Raises: - CryptoError: on various format errors, or in case a required attribute is - not found, the error message describes the problem. + subcmd.TpmTestError: on various format errors, or in case a required + attribute is not found, the error message describes the problem. """ # Fields stored in hex format by default. @@ -43,7 +40,7 @@ def get_attribute(tdesc, attr_name, required=True): data = tdesc.find(attr_name) if data is None: if required: - raise CryptoError('node "%s" does not have attribute "%s"' % + raise subcmd.TpmTestError('node "%s" does not have attribute "%s"' % (tdesc.get('name'), attr_name)) return '' @@ -55,7 +52,7 @@ def get_attribute(tdesc, attr_name, required=True): else: cell_format = 'ascii' elif cell_format not in ('hex', 'ascii'): - raise CryptoError('%s:%s, unrecognizable format "%s"' % + raise subcmd.TpmTestError('%s:%s, unrecognizable format "%s"' % (tdesc.get('name'), attr_name, cell_format)) text = ' '.join(x.strip() for x in data.text.splitlines() if x) @@ -65,7 +62,7 @@ def get_attribute(tdesc, attr_name, required=True): # Drop spaces from hex representation. text = text.replace(' ', '') if len(text) & 3: - raise CryptoError('%s:%s %swrong hex number size' % + raise subcmd.TpmTestError('%s:%s %swrong hex number size' % (tdesc.get('name'), attr_name, utils.hex_dump(text))) # Convert text to binary value = '' @@ -73,7 +70,7 @@ def get_attribute(tdesc, attr_name, required=True): try: value += struct.pack(' 4096: - raise TpmError(prefix + 'invalid size %d' % size) + raise subcmd.TpmTestError(prefix + 'invalid size %d' % size) if response_mode: return if cmd_code >= 0x11f and cmd_code <= 0x18f: @@ -80,7 +77,7 @@ class TPM(object): if cmd_code == EXT_CMD: return # This is an extension command - raise TpmError(prefix + 'invalid command code 0x%x' % cmd_code) + raise subcmd.TpmTestError(prefix + 'invalid command code 0x%x' % cmd_code) def command(self, cmd_data): # Verify command header @@ -108,21 +105,22 @@ class TPM(object): Returns: the binary string of the response payload, if validation succeeded. Raises: - TpmError: in case there are any validation problems, the error message - describes the problem. + subcmd.TpmTestError: in case there are any validation problems, the + error message describes the problem. """ header_size = struct.calcsize(self.HEADER_FMT) tag, size, cmd, subcmd = struct.unpack(self.HEADER_FMT, response[:header_size]) if tag != 0x8001: - raise TpmError('Wrong response tag: %4.4x' % tag) + raise subcmd.TpmTestError('Wrong response tag: %4.4x' % tag) if cmd != EXT_CMD: - raise TpmError('Unexpected response command field: %8.8x' % cmd) + raise subcmd.TpmTestError('Unexpected response command field: %8.8x' % + cmd) if subcmd != expected_subcmd: - raise TpmError('Unexpected response subcommand field: %2.2x' % + raise subcmd.TpmTestError('Unexpected response subcommand field: %2.2x' % subcmd) if size != len(response): - raise TpmError('Size mismatch: header %d, actual %d' % ( + raise subcmd.TpmTestError('Size mismatch: header %d, actual %d' % ( size, len(response))) return response[header_size:] @@ -139,10 +137,9 @@ if __name__ == '__main__': ecc_test.ecc_test(t) hash_test.hash_test(t) rsa_test.rsa_test(t) - except (TpmError, crypto_test.CryptoError, hash_test.HashError, - rsa_test.RSAError) as e: - print() - print('Error:', e) + except subcmd.TpmTestError as e: + exc_file, exc_line = traceback.extract_tb(sys.exc_traceback)[-1][:2] + print('\nError in %s:%s: ' % (os.path.basename(exc_file), exc_line), e) if debug_needed: traceback.print_exc() sys.exit(1)