diff --git a/tests/test_cli.py b/tests/test_cli.py index c58effd..bee82d8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,6 +3,10 @@ import os import pytest +from typing import ( + Optional, +) + from click.testing import CliRunner from eth2deposit import deposit @@ -11,7 +15,7 @@ from eth2deposit.utils.constants import DEFAULT_VALIDATOR_KEYS_FOLDER_NAME from eth2deposit.key_handling.keystore import Keystore -def clean_key_folder(my_folder_path): +def clean_key_folder(my_folder_path: str) -> None: validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME) if not os.path.exists(validator_keys_folder_path): return @@ -23,9 +27,9 @@ def clean_key_folder(my_folder_path): os.rmdir(my_folder_path) -def test_deposit(monkeypatch): +def test_deposit(monkeypatch) -> None: # monkeypatch get_mnemonic - def get_mnemonic(language, words_path, entropy=None): + def get_mnemonic(language: str, words_path: str, entropy: Optional[bytes]=None) -> str: return "fakephrase" monkeypatch.setattr(deposit, "get_mnemonic", get_mnemonic) @@ -47,7 +51,7 @@ def test_deposit(monkeypatch): validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME) _, _, key_files = next(os.walk(validator_keys_folder_path)) - def get_uuid(key_file): + def get_uuid(key_file: str) -> str: keystore = Keystore.from_json(key_file) return keystore.uuid @@ -63,7 +67,7 @@ def test_deposit(monkeypatch): @pytest.mark.asyncio -async def test_script(): +async def test_script() -> None: my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER') if not os.path.exists(my_folder_path): os.mkdir(my_folder_path) diff --git a/tests/test_key_handling/test_key_derivation/test_mnemonic.py b/tests/test_key_handling/test_key_derivation/test_mnemonic.py index a3b254a..d374675 100644 --- a/tests/test_key_handling/test_key_derivation/test_mnemonic.py +++ b/tests/test_key_handling/test_key_derivation/test_mnemonic.py @@ -1,6 +1,9 @@ import os import pytest import json +from typing import ( + Sequence, +) from eth2deposit.key_handling.key_derivation.mnemonic import ( get_seed, @@ -17,14 +20,13 @@ with open(test_vector_filefolder, 'r', encoding='utf-8') as f: @pytest.mark.parametrize( - 'language,language_test_vectors', - [(a, b) for a, b in test_vectors.items()] + 'language,test', + [(language, test) for language, language_test_vectors in test_vectors.items() for test in language_test_vectors] ) -def test_bip39(language, language_test_vectors): - for test in language_test_vectors: - test_entropy = bytes.fromhex(test[0]) - test_mnemonic = test[1] - test_seed = bytes.fromhex(test[2]) +def test_bip39(language: str, test: Sequence[str]) -> None: + test_entropy = bytes.fromhex(test[0]) + test_mnemonic = test[1] + test_seed = bytes.fromhex(test[2]) - assert get_mnemonic(language=language, words_path=WORD_LISTS_PATH, entropy=test_entropy) == test_mnemonic - assert get_seed(mnemonic=test_mnemonic, password='TREZOR') == test_seed + assert get_mnemonic(language=language, words_path=WORD_LISTS_PATH, entropy=test_entropy) == test_mnemonic + assert get_seed(mnemonic=test_mnemonic, password='TREZOR') == test_seed diff --git a/tests/test_key_handling/test_key_derivation/test_path.py b/tests/test_key_handling/test_key_derivation/test_path.py index 8443e9f..11deedc 100644 --- a/tests/test_key_handling/test_key_derivation/test_path.py +++ b/tests/test_key_handling/test_key_derivation/test_path.py @@ -1,5 +1,6 @@ import os import json +import pytest from eth2deposit.key_handling.key_derivation.tree import ( _flip_bits_256, @@ -15,15 +16,23 @@ from eth2deposit.key_handling.key_derivation.path import ( test_vector_filefolder = os.path.join(os.getcwd(), 'tests', 'test_key_handling', 'test_key_derivation', 'test_vectors', 'tree_kdf_intermediate.json') with open(test_vector_filefolder, 'r') as f: - test_vector = json.load(f) + test_vector_dict = json.load(f) -def test_flip_bits_256() -> None: +@pytest.mark.parametrize( + 'test_vector', + [test_vector_dict] +) +def test_flip_bits_256(test_vector) -> None: test_vector_int = int(test_vector['seed'][:64], 16) # 64 comes from string chars containing .5 bytes assert test_vector_int & _flip_bits_256(test_vector_int) == 0 -def test_IKM_to_lamport_SK() -> None: +@pytest.mark.parametrize( + 'test_vector', + [test_vector_dict] +) +def test_IKM_to_lamport_SK(test_vector) -> None: test_vector_lamport_0 = [bytes.fromhex(x) for x in test_vector['lamport_0']] test_vector_lamport_1 = [bytes.fromhex(x) for x in test_vector['lamport_1']] salt = test_vector['child_index'].to_bytes(4, 'big') @@ -35,21 +44,33 @@ def test_IKM_to_lamport_SK() -> None: assert test_vector_lamport_1 == lamport_1 -def test_parent_SK_to_lamport_PK() -> None: +@pytest.mark.parametrize( + 'test_vector', + [test_vector_dict] +) +def test_parent_SK_to_lamport_PK(test_vector) -> None: parent_SK = test_vector['master_SK'] index = test_vector['child_index'] lamport_PK = bytes.fromhex(test_vector['compressed_lamport_PK']) assert lamport_PK == _parent_SK_to_lamport_PK(parent_SK=parent_SK, index=index) -def test_HKDF_mod_r() -> None: +@pytest.mark.parametrize( + 'test_vector', + [test_vector_dict] +) +def test_HKDF_mod_r(test_vector) -> None: test_0 = (bytes.fromhex(test_vector['seed']), test_vector['master_SK']) test_1 = (bytes.fromhex(test_vector['compressed_lamport_PK']), test_vector['child_SK']) for test in (test_0, test_1): assert _HKDF_mod_r(IKM=test[0]) == test[1] -def test_mnemonic_and_path_to_key() -> None: +@pytest.mark.parametrize( + 'test_vector', + [test_vector_dict] +) +def test_mnemonic_and_path_to_key(test_vector) -> None: mnemonic = test_vector['mnemonic'] password = test_vector['password'] path = test_vector['path'] diff --git a/tests/test_key_handling/test_key_derivation/test_tree.py b/tests/test_key_handling/test_key_derivation/test_tree.py index d6e735d..70c0f9e 100644 --- a/tests/test_key_handling/test_key_derivation/test_tree.py +++ b/tests/test_key_handling/test_key_derivation/test_tree.py @@ -57,8 +57,3 @@ def test_derive_child_SK(test) -> None: index = test['child_index'] child_SK = test['child_SK'] assert derive_child_SK(parent_SK=parent_SK, index=index) == child_SK - for test in test_vectors: - parent_SK = test['master_SK'] - index = test['child_index'] - child_SK = test['child_SK'] - assert derive_child_SK(parent_SK=parent_SK, index=index) == child_SK diff --git a/tests/test_key_handling/test_keystore.py b/tests/test_key_handling/test_keystore.py index 5f321c1..462e232 100644 --- a/tests/test_key_handling/test_keystore.py +++ b/tests/test_key_handling/test_keystore.py @@ -15,14 +15,14 @@ _, _, test_vector_files = next(os.walk(test_vector_folder)) # type: ignore test_vector_keystores = [Keystore.from_json(os.path.join(test_vector_folder, f)) for f in test_vector_files] -def test_json_serialization(): +def test_json_serialization() -> None: for keystore, keystore_json_file in zip(test_vector_keystores, test_vector_files): keystore_json_path = os.path.join(test_vector_folder, keystore_json_file) with open(keystore_json_path) as f: assert json.loads(keystore.as_json()) == json.load(f) -def test_encrypt_decrypt_test_vectors(): +def test_encrypt_decrypt_test_vectors() -> None: for tv in test_vector_keystores: aes_iv = tv.crypto.cipher.params['iv'] kdf_salt = tv.crypto.kdf.params['salt'] @@ -35,7 +35,7 @@ def test_encrypt_decrypt_test_vectors(): assert generated_keystore.decrypt(test_vector_password) == test_vector_secret -def test_generated_keystores(): +def test_generated_keystores() -> None: for tv in test_vector_keystores: aes_iv = tv.crypto.cipher.params['iv'] kdf_salt = tv.crypto.kdf.params['salt'] @@ -48,11 +48,11 @@ def test_generated_keystores(): assert generated_keystore.crypto == tv.crypto -def test_encrypt_decrypt_pbkdf2_random_iv(): +def test_encrypt_decrypt_pbkdf2_random_iv() -> None: generated_keystore = Pbkdf2Keystore.encrypt(secret=test_vector_secret, password=test_vector_password) assert generated_keystore.decrypt(test_vector_password) == test_vector_secret -def test_encrypt_decrypt_scrypt_random_iv(): +def test_encrypt_decrypt_scrypt_random_iv() -> None: generated_keystore = ScryptKeystore.encrypt(secret=test_vector_secret, password=test_vector_password) assert generated_keystore.decrypt(test_vector_password) == test_vector_secret