Add more tests

This commit is contained in:
Hsiao-Wei Wang 2020-09-14 22:47:15 +08:00
parent f6dc059a8e
commit b0ee4a2ffc
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
7 changed files with 226 additions and 6 deletions

14
tests/test_credentials.py Normal file
View File

@ -0,0 +1,14 @@
import pytest
from eth2deposit.credentials import CredentialList
def test_from_mnemonic():
with pytest.raises(ValueError):
CredentialList.from_mnemonic(
mnemonic="",
num_keys=1,
amounts=[32, 32],
fork_version=bytes.fromhex('00000001'),
start_index=1,
)

View File

@ -6,12 +6,16 @@ from typing import (
)
from eth2deposit.key_handling.key_derivation.mnemonic import (
_get_word,
_get_word_list,
get_languages,
get_seed,
get_mnemonic,
)
WORD_LISTS_PATH = os.path.join(os.getcwd(), 'eth2deposit', 'key_handling', 'key_derivation', 'word_lists')
all_languages = get_languages(WORD_LISTS_PATH)
test_vector_filefolder = os.path.join('tests', 'test_key_handling',
'test_key_derivation', 'test_vectors', 'mnemonic.json')
@ -30,3 +34,24 @@ def test_bip39(language: str, test: Sequence[str]) -> None:
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
@pytest.mark.parametrize(
'language',
[language for language in all_languages]
)
@pytest.mark.parametrize(
'index, valid',
[
(0, True),
(2047, True),
(2048, False),
]
)
def test_get_word(language, index, valid):
word_list = _get_word_list(language, WORD_LISTS_PATH)
if valid:
_get_word(word_list=word_list, index=index)
else:
with pytest.raises(IndexError):
_get_word(word_list=word_list, index=index)

View File

@ -11,6 +11,7 @@ from eth2deposit.key_handling.key_derivation.tree import (
from eth2deposit.key_handling.key_derivation.path import (
mnemonic_and_path_to_key,
path_to_nodes,
)
test_vector_filefolder = os.path.join(os.getcwd(), 'tests', 'test_key_handling', 'test_key_derivation',
@ -76,3 +77,19 @@ def test_mnemonic_and_path_to_key(test_vector) -> None:
path = test_vector['path']
key = test_vector['child_SK']
assert mnemonic_and_path_to_key(mnemonic=mnemonic, path=path, password=password) == key
@pytest.mark.parametrize(
'path, valid',
[
("m/12381/3600/0/0/0", True),
("x/12381/3600/0/0/0", False),
("m/qwert/3600/0/0/0", False),
]
)
def test_path_to_nodes(path, valid):
if valid:
path_to_nodes(path)
else:
with pytest.raises(ValueError):
path_to_nodes(path)

View File

@ -42,18 +42,36 @@ def test_hkdf_mod_r_key_info(seed: bytes, key_info: bytes) -> None:
'test',
test_vectors
)
def test_derive_master_SK(test) -> None:
seed = bytes.fromhex(test['seed'])
@pytest.mark.parametrize(
'is_valid_seed',
(True, False)
)
def test_derive_master_SK(test, is_valid_seed) -> None:
master_SK = test['master_SK']
assert derive_master_SK(seed=seed) == master_SK
if is_valid_seed:
seed = bytes.fromhex(test['seed'])
assert derive_master_SK(seed=seed) == master_SK
else:
seed = "\x12" * 31
with pytest.raises(ValueError):
derive_master_SK(seed=seed)
@pytest.mark.parametrize(
'test',
test_vectors
)
def test_derive_child_SK(test) -> None:
@pytest.mark.parametrize(
'is_valid_index',
(True, False)
)
def test_derive_child_SK_valid(test, is_valid_index) -> None:
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
if is_valid_index:
index = test['child_index']
assert derive_child_SK(parent_SK=parent_SK, index=index) == child_SK
else:
index = 2**32
with pytest.raises(IndexError):
derive_child_SK(parent_SK=parent_SK, index=index)

View File

@ -59,6 +59,13 @@ def test_encrypt_decrypt_scrypt_random_iv() -> None:
assert generated_keystore.decrypt(test_vector_password) == test_vector_secret
def test_encrypt_decrypt_incorrect_password() -> None:
generated_keystore = ScryptKeystore.encrypt(secret=test_vector_secret, password=test_vector_password)
incorrect_password = test_vector_password + 'incorrect'
with pytest.raises(ValueError):
generated_keystore.decrypt(incorrect_password)
@pytest.mark.parametrize(
'password,processed_password',
[

View File

@ -0,0 +1,78 @@
import pytest
from eth2deposit.utils.crypto import (
scrypt,
PBKDF2,
AES_128_CTR,
)
@pytest.mark.parametrize(
'n, r, valid',
[
(int(2**(128 * 1 / 8)) // 2, 1, True),
(int(2**(128 * 1 / 8)), 1, False),
]
)
def test_scrypt_invalid_n(n, r, valid):
if valid:
scrypt(
password="mypassword",
salt="mysalt",
n=n,
r=r,
p=1,
dklen=32,
)
else:
with pytest.raises(ValueError):
scrypt(
password="mypassword",
salt="mysalt",
n=n,
r=r,
p=1,
dklen=32,
)
@pytest.mark.parametrize(
'prf, valid',
[
("sha512", True),
("512", False),
]
)
def test_PBKDF2_invalid_prf(prf, valid):
if valid:
PBKDF2(
password="mypassword",
salt="mysalt",
dklen=64,
c=2048,
prf=prf
)
else:
with pytest.raises(ValueError):
PBKDF2(
password="mypassword",
salt="mysalt",
dklen=64,
c=2048,
prf=prf,
)
@pytest.mark.parametrize(
'key, iv, valid',
[
(b'\x12' * 16, bytes.fromhex("edc2606468f9660ad222690db8836a9d"), True),
(b'\x12' * 15, bytes.fromhex("edc2606468f9660ad222690db8836a9d"), False),
]
)
def test_AES_128_CTR(key, iv, valid):
if valid:
AES_128_CTR(key=key, iv=iv)
else:
with pytest.raises(ValueError):
AES_128_CTR(key=key, iv=iv)

View File

@ -0,0 +1,61 @@
import pytest
from eth2deposit.utils.ssz import (
DepositMessage,
compute_deposit_domain,
compute_deposit_fork_data_root,
compute_signing_root,
)
@pytest.mark.parametrize(
'fork_version, valid, result',
[
(b"\x12" * 4, True, b'\x03\x00\x00\x00\rf`\x8a\xf5W\xf4\xfa\xdb\xfc\xe2H\xac7\xf6\xe7c\x9c\xe3q\x10\x0cC\xd1Z\xad\x05\xcb'), # noqa: E501
(b"\x12" * 5, False, None),
(b"\x12" * 3, False, None),
]
)
def test_compute_deposit_domain(fork_version, valid, result):
if valid:
assert compute_deposit_domain(fork_version) == result
else:
with pytest.raises(ValueError):
compute_deposit_domain(fork_version)
@pytest.mark.parametrize(
'current_version, valid, result',
[
(b"\x12" * 4, True, b'\rf`\x8a\xf5W\xf4\xfa\xdb\xfc\xe2H\xac7\xf6\xe7c\x9c\xe3q\x10\x0cC\xd1Z\xad\x05\xcb\x08\xac\x1d\xc2'), # noqa: E501
(b"\x12" * 5, False, None),
(b"\x12" * 3, False, None),
]
)
def test_compute_deposit_fork_data_root(current_version, valid, result):
if valid:
assert compute_deposit_fork_data_root(current_version=current_version) == result
else:
with pytest.raises(ValueError):
compute_deposit_fork_data_root(current_version=current_version)
@pytest.mark.parametrize(
'domain, valid, result',
[
(b"\x12" * 32, True, b'g\xa33\x0f\xf8{\xdbF\xbb{\x80\xcazd\x1e9\x8dj\xc4\xe8zhVR|\xac\xc8)\xfba\x89o'), # noqa: E501
(b"\x12" * 31, False, None),
(b"\x12" * 33, False, None),
]
)
def test_compute_signing_root(domain, valid, result):
deposit_message = DepositMessage(
pubkey=b'\x12' * 48,
withdrawal_credentials=b'\x12' * 32,
amount=100,
)
if valid:
assert compute_signing_root(deposit_message, domain) == result
else:
with pytest.raises(ValueError):
compute_signing_root(deposit_message, domain)