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)