trezor-agent/trezor_agent/tests/test_factory.py

98 lines
2.9 KiB
Python
Raw Normal View History

2016-01-19 20:52:52 +00:00
import mock
import pytest
2016-02-15 15:22:01 +00:00
from .. import factory
2016-01-19 20:52:52 +00:00
def test_load():
def single():
return [0]
def nothing():
return []
def double():
return [1, 2]
assert factory.load(loaders=[single]) == 0
assert factory.load(loaders=[single, nothing]) == 0
assert factory.load(loaders=[nothing, single]) == 0
with pytest.raises(IOError):
factory.load(loaders=[])
with pytest.raises(IOError):
factory.load(loaders=[single, single])
with pytest.raises(IOError):
factory.load(loaders=[double])
2016-01-22 10:04:24 +00:00
def factory_load_client(**kwargs):
# pylint: disable=protected-access
return list(factory._load_client(**kwargs))
2016-01-19 20:52:52 +00:00
def test_load_nothing():
2016-01-22 10:04:24 +00:00
hid_transport = mock.Mock(spec_set=['enumerate'])
2016-01-19 20:52:52 +00:00
hid_transport.enumerate.return_value = []
result = factory_load_client(
name=None,
client_type=None,
hid_transport=hid_transport,
passphrase_ack=None,
identity_type=None,
required_version=None,
call_exception=None)
2016-01-22 10:04:24 +00:00
assert result == []
2016-01-19 20:52:52 +00:00
def create_client_type(version):
2016-01-22 10:04:24 +00:00
conn = mock.Mock(spec=[])
conn.features = mock.Mock(spec=[])
2016-01-19 20:52:52 +00:00
major, minor, patch = version.split('.')
2016-01-22 10:04:24 +00:00
conn.features.device_id = 'DEVICE_ID'
conn.features.label = 'LABEL'
conn.features.vendor = 'VENDOR'
2016-01-19 20:52:52 +00:00
conn.features.major_version = major
conn.features.minor_version = minor
conn.features.patch_version = patch
conn.features.revision = b'\x12\x34\x56\x78'
2016-01-22 10:04:24 +00:00
return mock.Mock(spec_set=[], return_value=conn)
2016-01-19 20:52:52 +00:00
def test_load_single():
2016-01-22 10:04:24 +00:00
hid_transport = mock.Mock(spec_set=['enumerate'])
2016-01-19 20:52:52 +00:00
hid_transport.enumerate.return_value = [0]
for version in ('1.3.4', '1.3.5', '1.4.0', '2.0.0'):
2016-01-22 10:04:24 +00:00
passphrase_ack = mock.Mock(spec_set=[])
2016-01-19 20:52:52 +00:00
client_type = create_client_type(version)
2016-01-22 10:04:24 +00:00
client_wrapper, = factory_load_client(
2016-01-19 20:52:52 +00:00
name='DEVICE_NAME',
client_type=client_type,
hid_transport=hid_transport,
passphrase_ack=passphrase_ack,
identity_type=None,
required_version='>=1.3.4',
call_exception=None)
2016-01-19 20:52:52 +00:00
assert client_wrapper.connection is client_type.return_value
assert client_wrapper.device_name == 'DEVICE_NAME'
client_wrapper.connection.callback_PassphraseRequest('MESSAGE')
assert passphrase_ack.mock_calls == [mock.call(passphrase='')]
def test_load_old():
2016-01-22 10:04:24 +00:00
hid_transport = mock.Mock(spec_set=['enumerate'])
2016-01-19 20:52:52 +00:00
hid_transport.enumerate.return_value = [0]
for version in ('1.3.3', '1.2.5', '1.1.0', '0.9.9'):
with pytest.raises(ValueError):
2016-01-22 10:04:24 +00:00
factory_load_client(
2016-01-19 20:52:52 +00:00
name='DEVICE_NAME',
client_type=create_client_type(version),
hid_transport=hid_transport,
passphrase_ack=None,
identity_type=None,
required_version='>=1.3.4',
call_exception=None)