From ae6fc3b6ec1a7c740749e43ca22f61d5a44c9602 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 28 Dec 2015 04:19:20 +0300 Subject: test: refactoring --- test/cavp.py | 12 ++++++------ test/file.py | 34 +++++++++++++++++----------------- test/nist-sp-800-38a.py | 42 +++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/test/cavp.py b/test/cavp.py index 500a7ca..64caf4a 100644 --- a/test/cavp.py +++ b/test/cavp.py @@ -52,7 +52,7 @@ class _TestVectorsFile: return self._recognized def algorithm(self): - return self._algo + return self._algorithm def mode(self): return self._mode @@ -113,11 +113,11 @@ class _TestVectorsFile: return stub def _strip_algorithm(self, stub): - algo_size = stub[-3:] - maybe_algo = 'aes{0}'.format(algo_size) - self._algo = toolkit.is_algorithm_supported(maybe_algo) - if self._algo: - logging.info('\tAlgorithm: {0}'.format(self._algo)) + key_size = stub[-3:] + maybe_algorithm = 'aes{0}'.format(key_size) + self._algorithm = toolkit.is_algorithm_supported(maybe_algorithm) + if self._algorithm: + logging.info('\tAlgorithm: {0}'.format(self._algorithm)) return stub[0:-3] else: logging.warn('Unknown or unsupported algorithm: ' + self._fn) diff --git a/test/file.py b/test/file.py index d1abc3d..03a12de 100644 --- a/test/file.py +++ b/test/file.py @@ -21,13 +21,13 @@ _IV_EXT = 'iv' _PLAIN_EXT = 'plain' _CIPHER_EXT = 'cipher' -def _run_encryption_test(tools, tmp_dir, algo, mode, key, plain_path, cipher_path, iv=None, force=False): +def _run_encryption_test(tools, tmp_dir, algorithm, mode, key, plain_path, cipher_path, iv=None, force=False): logging.info('Running encryption test...') logging.info('\tPlaintext file path: ' + plain_path) logging.info('\tExpected ciphertext file path: ' + cipher_path) tmp_path = os.path.join(tmp_dir, os.path.basename(cipher_path)) logging.info('\tEncrypted file path: ' + tmp_path) - tools.run_encrypt_file(algo, mode, key, plain_path, tmp_path, iv) + tools.run_encrypt_file(algorithm, mode, key, plain_path, tmp_path, iv) if force: logging.warn('Overwriting expected ciphertext file') shutil.copy(tmp_path, cipher_path) @@ -38,13 +38,13 @@ def _run_encryption_test(tools, tmp_dir, algo, mode, key, plain_path, cipher_pat logging.error('The encrypted file doesn\'t match the ciphertext file') return _TestExitCode.FAILURE -def _run_decryption_test(tools, tmp_dir, algo, mode, key, cipher_path, plain_path, iv=None): +def _run_decryption_test(tools, tmp_dir, algorithm, mode, key, cipher_path, plain_path, iv=None): logging.info('Running decryption test...') logging.info('\tCiphertext file path: ' + cipher_path) logging.info('\tExpected plaintext file path: ' + plain_path) tmp_path = os.path.join(tmp_dir, os.path.basename(cipher_path)) logging.info('\tDecrypted file path: ' + tmp_path) - tools.run_decrypt_file(algo, mode, key, cipher_path, tmp_path, iv) + tools.run_decrypt_file(algorithm, mode, key, cipher_path, tmp_path, iv) if filecmp.cmp(tmp_path, plain_path): return _TestExitCode.SUCCESS else: @@ -92,16 +92,16 @@ def _run_tests(tools, suite_dir, force=False): suite_dir = os.path.abspath(suite_dir) logging.info('Suite directory path: ' + suite_dir) with TemporaryDirectory() as tmp_dir: - for algo_dir in _list_dirs(suite_dir): - algo = os.path.basename(algo_dir) - maybe_algo = toolkit.is_algorithm_supported(algo) - if maybe_algo is None: - logging.warn('Unknown or unsupported algorithm: ' + algo) + for algorithm_dir in _list_dirs(suite_dir): + algorithm = os.path.basename(algorithm_dir) + maybe_algorithm = toolkit.is_algorithm_supported(algorithm) + if maybe_algorithm is None: + logging.warn('Unknown or unsupported algorithm: ' + algorithm) exit_codes.append(_TestExitCode.SKIPPED) continue - algo = maybe_algo - logging.info('Algorithm: ' + algo) - for mode_dir in _list_dirs(algo_dir): + algorithm = maybe_algorithm + logging.info('Algorithm: ' + algorithm) + for mode_dir in _list_dirs(algorithm_dir): mode = os.path.basename(mode_dir) maybe_mode = toolkit.is_mode_supported(mode) if maybe_mode is None: @@ -121,11 +121,11 @@ def _run_tests(tools, suite_dir, force=False): iv = _read_iv(iv_path) plain_path = _build_plain_path(key_path) cipher_path = _build_cipher_path(key_path) - os.makedirs(os.path.join(tmp_dir, algo, mode), 0o777, True) + os.makedirs(os.path.join(tmp_dir, algorithm, mode), 0o777, True) try: exit_codes.append(_run_encryption_test( - tools, os.path.join(tmp_dir, algo, mode), - algo, mode, key, plain_path, cipher_path, iv, force)) + tools, os.path.join(tmp_dir, algorithm, mode), + algorithm, mode, key, plain_path, cipher_path, iv, force)) except Exception as e: logging.error('Encountered an exception!') logging.exception(e) @@ -133,8 +133,8 @@ def _run_tests(tools, suite_dir, force=False): if not force: try: exit_codes.append(_run_decryption_test( - tools, os.path.join(tmp_dir, algo, mode), - algo, mode, key, cipher_path, plain_path, iv)) + tools, os.path.join(tmp_dir, algorithm, mode), + algorithm, mode, key, cipher_path, plain_path, iv)) except Exception as e: logging.error('Encountered an exception!') logging.exception(e) diff --git a/test/nist-sp-800-38a.py b/test/nist-sp-800-38a.py index eb882b7..a9da9a1 100644 --- a/test/nist-sp-800-38a.py +++ b/test/nist-sp-800-38a.py @@ -146,29 +146,29 @@ def _assert_output(actual, expected): class _TestExitCode: SUCCESS, FAILURE, ERROR, SKIPPED = range(4) -def _run_encryption_tests(tools, algo, mode, use_boxes=False): +def _run_encryption_tests(tools, algorithm, mode, use_boxes=False): logging.info('Running encryption tests...') - key = _keys[algo] + key = _keys[algorithm] iv = None - if algo in _init_vectors and mode in _init_vectors[algo]: - iv = _init_vectors[algo][mode] - ciphertexts = _ciphertexts[algo][mode] + if algorithm in _init_vectors and mode in _init_vectors[algorithm]: + iv = _init_vectors[algorithm][mode] + ciphertexts = _ciphertexts[algorithm][mode] _input = toolkit.BlockInput(key, _plaintexts, iv=iv) - actual_output = tools.run_encrypt_block(algo, mode, _input, use_boxes) + actual_output = tools.run_encrypt_block(algorithm, mode, _input, use_boxes) if _assert_output(actual_output, ciphertexts): return _TestExitCode.SUCCESS else: return _TestExitCode.FAILURE -def _run_decryption_tests(tools, algo, mode, use_boxes=False): +def _run_decryption_tests(tools, algorithm, mode, use_boxes=False): logging.info('Running decryption tests...') - key = _keys[algo] + key = _keys[algorithm] iv = None - if algo in _init_vectors and mode in _init_vectors[algo]: - iv = _init_vectors[algo][mode] - ciphertexts = _ciphertexts[algo][mode] + if algorithm in _init_vectors and mode in _init_vectors[algorithm]: + iv = _init_vectors[algorithm][mode] + ciphertexts = _ciphertexts[algorithm][mode] _input = toolkit.BlockInput(key, ciphertexts, iv=iv) - actual_output = tools.run_decrypt_block(algo, mode, _input, use_boxes) + actual_output = tools.run_decrypt_block(algorithm, mode, _input, use_boxes) if _assert_output(actual_output, _plaintexts): return _TestExitCode.SUCCESS else: @@ -199,15 +199,15 @@ if __name__ == '__main__': logging.basicConfig(**logging_options) exit_codes = [] - for algo in _ciphertexts: - maybe_algo = toolkit.is_algorithm_supported(algo) - if maybe_algo is None: - logging.warn('Unknown or unsupported algorithm: ' + algo) + for algorithm in _ciphertexts: + maybe_algorithm = toolkit.is_algorithm_supported(algorithm) + if maybe_algorithm is None: + logging.warn('Unknown or unsupported algorithm: ' + algorithm) exit_codes.append(_TestExitCode.SKIPPED) continue - algo = maybe_algo - logging.info('Algorithm: ' + algo) - for mode in _ciphertexts[algo]: + algorithm = maybe_algorithm + logging.info('Algorithm: ' + algorithm) + for mode in _ciphertexts[algorithm]: maybe_mode = toolkit.is_mode_supported(mode) if maybe_mode is None: logging.warn('Unknown or unsupported mode: ' + mode) @@ -216,13 +216,13 @@ if __name__ == '__main__': mode = maybe_mode logging.info('Mode: ' + mode) try: - exit_codes.append(_run_encryption_tests(tools, algo, mode, use_boxes=args.use_boxes)) + exit_codes.append(_run_encryption_tests(tools, algorithm, mode, use_boxes=args.use_boxes)) except Exception as e: logging.error('Encountered an exception!') logging.exception(e) exit_codes.append(_TestExitCode.ERROR) try: - exit_codes.append(_run_decryption_tests(tools, algo, mode, use_boxes=args.use_boxes)) + exit_codes.append(_run_decryption_tests(tools, algorithm, mode, use_boxes=args.use_boxes)) except Exception as e: logging.error('Encountered an exception!') logging.exception(e) -- cgit v1.2.3