core: draft unit test for process_slip39

pull/602/head
matejcik 5 years ago committed by matejcik
parent f09bc4dd77
commit 861e999425

@ -0,0 +1,48 @@
if False:
from typing import Any
class Mock:
def __init__(self, return_value: Any = None, raises: BaseException = None) -> None:
self.attrs = {}
self.calls = []
self.return_value = return_value
self.raises = raises
def __getattr__(self, key: str) -> Any:
self.attrs.setdefault(key, Mock())
return self.attrs[key]
def __setattr__(self, name: str, value: Any) -> Any:
self.attrs[name] = value
return value
def __call__(self, *args, **kwargs) -> Any:
self.calls.append((args, kwargs))
if self.raises is not None:
raise self.raises
return self.return_value
class patch:
MOCK_OBJECT = object()
NO_VALUE = object()
def __init__(self, obj: Any, attr: str, value: Any = MOCK_OBJECT) -> None:
self.obj = obj
self.attr = attr
self.value = value
self.orig_value = self.NO_VALUE
def __enter__(self):
if hasattr(self.obj, self.attr):
self.orig_value = getattr(self.obj, self.attr)
patch_value = self.value if self.value is not self.MOCK_OBJECT else Mock()
setattr(self.obj, self.attr, patch_value)
def __exit__(self, exc_type, exc_value, tb):
if self.orig_value is self.NO_VALUE:
delattr(self.obj, self.attr)
else:
setattr(self.obj, self.attr, self.orig_value)

@ -0,0 +1,42 @@
from mock import patch
import apps.common.storage.common
class MockStorage:
PATCH_METHODS = ("get", "set", "delete")
def __init__(self):
self.namespace = {}
self.patches = [
patch(apps.common.storage.common, method, getattr(self, method))
for method in self.PATCH_METHODS
]
def set(self, app: int, key: int, data: bytes, public: bool = False) -> None:
self.namespace.setdefault(app, {})
self.namespace[app][key] = data
def get(self, app: int, key: int, public: bool = False) -> Optional[bytes]:
self.namespace.setdefault(app, {})
return self.namespace[app].get(key)
def delete(self, app: int, key: int, public: bool = False) -> None:
self.namespace.setdefault(app, {})
self.namespace[app].pop(key, None)
def __enter__(self):
for patch in self.patches:
patch.__enter__()
return self
def __exit__(self, exc_type, exc_value, tb):
for patch in self.patches:
patch.__exit__(exc_type, exc_value, tb)
def mock_storage(func):
def inner(*args, **kwargs):
with MockStorage():
return func(*args, **kwargs)
return inner

@ -0,0 +1,38 @@
from common import *
from storage import mock_storage
import apps.common.storage.recovery
from apps.common import storage
from apps.management.recovery_device.recover import process_slip39
MNEMONIC_SLIP39_BASIC_20_3of6 = [
"extra extend academic bishop cricket bundle tofu goat apart victim enlarge program behavior permit course armed jerky faint language modern",
"extra extend academic acne away best indicate impact square oasis prospect painting voting guest either argue username racism enemy eclipse",
"extra extend academic arcade born dive legal hush gross briefing talent drug much home firefly toxic analysis idea umbrella slice",
]
# Shamir shares (128 bits, 2 groups from 1 of 1, 1 of 1, 3 of 5, 2 of 6)
MNEMONIC_SLIP39_ADVANCED_20 = [
"eraser senior beard romp adorn nuclear spill corner cradle style ancient family general leader ambition exchange unusual garlic promise voice",
"eraser senior ceramic snake clay various huge numb argue hesitate auction category timber browser greatest hanger petition script leaf pickup",
"eraser senior ceramic shaft dynamic become junior wrist silver peasant force math alto coal amazing segment yelp velvet image paces",
"eraser senior ceramic round column hawk trust auction smug shame alive greatest sheriff living perfect corner chest sled fumes adequate",
]
# Shamir shares (256 bits, 2 groups from 1 of 1, 1 of 1, 3 of 5, 2 of 6):
MNEMONIC_SLIP39_ADVANCED_33 = [
"wildlife deal beard romp alcohol space mild usual clothes union nuclear testify course research heat listen task location thank hospital slice smell failure fawn helpful priest ambition average recover lecture process dough stadium",
"wildlife deal acrobat romp anxiety axis starting require metric flexible geology game drove editor edge screw helpful have huge holy making pitch unknown carve holiday numb glasses survive already tenant adapt goat fangs",
]
class TestSlip39(unittest.TestCase):
@mock_storage
def test_process_slip39(self):
storage.recovery.set_in_progress(True)
words = MNEMONIC_SLIP39_BASIC_20_3of6[0]
secret, share = process_slip39(words)
self.assertIsNone(secret)
self.assertEqual(share.group_count, storage.recovery.get_slip39_group_count())
if __name__ == "__main__":
unittest.main()
Loading…
Cancel
Save