Skip to content

Commit

Permalink
Check terminal encoding (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickdowne authored Sep 2, 2024
1 parent 16bc75a commit 537127e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ethstaker_deposit/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def check_python_version() -> None:
'''
if sys.version_info < (3, 9):
click.pause(load_text(['err_python_version']))
sys.exit()
sys.exit(78)


def check_connectivity() -> None:
Expand Down
6 changes: 4 additions & 2 deletions ethstaker_deposit/intl/en/utils/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"msg_deposit_verification": "Verifying your deposit_data-*.json file(s):\t"
},
"validate_password_strength": {
"msg_password_length": "The password length should be at least 8. Please retype"
"msg_password_length": "The password length should be at least 8. Please retype.",
"msg_password_utf8_win32": "Your terminal is not UTF-8 encoded. To ensure the keystore file can be imported on Linux, the password should contain only English-language characters. Please retype.",
"msg_password_utf8": "Your terminal is not UTF-8 encoded. To ensure the keystore file can be imported on Linux, the password should contain only English-language characters. Please retype.\nAlternatively, you can quit this program and relaunch it from a UTF-8 encoded terminal."
},
"validate_int_range": {
"err_not_positive_integer": "That is not a positive integer. Please retype."
Expand All @@ -14,7 +16,7 @@
"validate_withdrawal_address": {
"err_invalid_ECDSA_hex_addr": "The given execution address is not in hexadecimal encoded form.",
"err_invalid_ECDSA_hex_addr_checksum": "The given execution address is not in checksum form.",
"err_missing_address": "An address must must be providing",
"err_missing_address": "An address must be provided",
"msg_ECDSA_hex_addr_withdrawal": "**[Warning] you are setting an execution address as your withdrawal address. Please ensure that you have control over this address.**"
},
"validate_partial_deposit_amount": {
Expand Down
9 changes: 9 additions & 0 deletions ethstaker_deposit/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import click
import json
import re
import sys
import concurrent.futures
from typing import Any, Dict, Sequence

Expand Down Expand Up @@ -138,6 +139,14 @@ def validate_deposit(deposit_data_dict: Dict[str, Any], credential: Credential =
def validate_password_strength(password: str) -> str:
if len(password) < 8:
raise ValidationError(load_text(['msg_password_length']))

encoding = sys.stdin.encoding.lower()
if encoding != 'utf-8' and not password.isascii():
if sys.platform == 'win32':
raise ValidationError(load_text(['msg_password_utf8_win32']))
else:
raise ValidationError(load_text(['msg_password_utf8']))

return password


Expand Down
2 changes: 1 addition & 1 deletion tests/test_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def test_should_notify_user_and_exit_if_invalid_python_version(monkeypatch) -> None:
exit_called = False

def _mock_sys_exit():
def _mock_sys_exit(arg):
nonlocal exit_called
exit_called = True

Expand Down
38 changes: 36 additions & 2 deletions tests/test_utils/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import sys
from typing import (
Any,
)
Expand All @@ -21,7 +22,38 @@
('1234567', False),
]
)
def test_validate_password_strength(password, valid):
def test_validate_password_strength(password: str, valid: bool) -> None:
if valid:
validate_password_strength(password=password)
else:
with pytest.raises(ValidationError):
validate_password_strength(password=password)


@pytest.mark.parametrize(
'password, encoding, platform, valid',
[
('Surströmming', 'utf-8', 'linux', True),
('Surströmming', 'latin-1', 'linux', False),
('Surstromming', 'latin-1', 'linux', True),
('Surströmming', 'utf-8', 'darwin', True),
('Surströmming', 'latin-1', 'darwin', False),
('Surstromming', 'latin-1', 'darwin', True),
('Surströmming', 'utf-8', 'win32', True),
('Surströmming', 'latin-1', 'win32', False),
('Surstromming', 'latin-1', 'win32', True),
]
)
def test_validate_password_strength_encoding(
monkeypatch, password: str, encoding: str, platform: str, valid: bool
) -> None:
class MockStdin:
def __init__(self, encoding):
self.encoding = encoding
mock_stdin = MockStdin(encoding=encoding)
monkeypatch.setattr(sys, 'stdin', mock_stdin)
monkeypatch.setattr(sys, 'platform', platform)

if valid:
validate_password_strength(password=password)
else:
Expand Down Expand Up @@ -119,7 +151,9 @@ def test_normalize_input_list(input, result):
('mainnet', 0, 0, valid_pubkey, invalid_signature, False),
]
)
def test_validate_signed_exit(chain, epoch, validator_index, pubkey, signature, result):
def test_validate_signed_exit(
chain: str, epoch: int, validator_index: int, pubkey: str, signature: str, result: bool
) -> None:
chain_settings = get_chain_setting(chain)

assert validate_signed_exit(validator_index, epoch, signature, pubkey, chain_settings) == result

0 comments on commit 537127e

Please sign in to comment.