mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 12:00:59 +00:00
tests: make a fixture out of client
also implement Cancel tests
This commit is contained in:
parent
7673ebf952
commit
269eaa298f
@ -16,28 +16,22 @@
|
|||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from . import conftest
|
||||||
|
|
||||||
from trezorlib import coins
|
from trezorlib import coins
|
||||||
from trezorlib import tx_api
|
from trezorlib import tx_api
|
||||||
from trezorlib.client import TrezorClientDebugLink
|
from trezorlib.client import TrezorClientDebugLink
|
||||||
from trezorlib.transport import get_transport
|
|
||||||
|
|
||||||
tests_dir = os.path.dirname(os.path.abspath(__file__))
|
tests_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
tx_api.cache_dir = os.path.join(tests_dir, '../txcache')
|
tx_api.cache_dir = os.path.join(tests_dir, '../txcache')
|
||||||
|
|
||||||
|
|
||||||
def get_device():
|
|
||||||
path = os.environ.get('TREZOR_PATH')
|
|
||||||
return get_transport(path)
|
|
||||||
|
|
||||||
|
|
||||||
class TrezorTest:
|
class TrezorTest:
|
||||||
|
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
wirelink = get_device()
|
wirelink = conftest.get_device()
|
||||||
debuglink = wirelink.find_debug()
|
debuglink = wirelink.find_debug()
|
||||||
self.client = TrezorClientDebugLink(wirelink)
|
self.client = TrezorClientDebugLink(wirelink)
|
||||||
self.client.set_debuglink(debuglink)
|
self.client.set_debuglink(debuglink)
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
|
import functools
|
||||||
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from . import common
|
from trezorlib.transport import get_transport
|
||||||
from trezorlib.client import TrezorClient
|
from trezorlib.client import TrezorClient, TrezorClientDebugLink
|
||||||
from trezorlib import log
|
from trezorlib import log, coins
|
||||||
|
|
||||||
|
|
||||||
|
def get_device():
|
||||||
|
path = os.environ.get('TREZOR_PATH')
|
||||||
|
return get_transport(path)
|
||||||
|
|
||||||
|
|
||||||
def device_version():
|
def device_version():
|
||||||
device = common.get_device()
|
device = get_device()
|
||||||
if not device:
|
if not device:
|
||||||
raise RuntimeError()
|
raise RuntimeError()
|
||||||
client = TrezorClient(device)
|
client = TrezorClient(device)
|
||||||
@ -16,11 +23,38 @@ def device_version():
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
try:
|
TREZOR_VERSION = device_version()
|
||||||
TREZOR_VERSION = device_version()
|
|
||||||
except:
|
|
||||||
raise
|
@pytest.fixture(scope="function")
|
||||||
TREZOR_VERSION = None
|
def client():
|
||||||
|
wirelink = get_device()
|
||||||
|
debuglink = wirelink.find_debug()
|
||||||
|
client = TrezorClientDebugLink(wirelink)
|
||||||
|
client.set_debuglink(debuglink)
|
||||||
|
client.set_tx_api(coins.tx_api['Bitcoin'])
|
||||||
|
client.wipe_device()
|
||||||
|
client.transport.session_begin()
|
||||||
|
|
||||||
|
yield client
|
||||||
|
|
||||||
|
client.transport.session_end()
|
||||||
|
|
||||||
|
|
||||||
|
def setup_client(mnemonic=None, pin='', passphrase=False):
|
||||||
|
if mnemonic is None:
|
||||||
|
mnemonic = ' '.join(['all'] * 12)
|
||||||
|
if pin is True:
|
||||||
|
pin = '1234'
|
||||||
|
|
||||||
|
def client_decorator(function):
|
||||||
|
@functools.wraps(function)
|
||||||
|
def wrapper(client, *args, **kwargs):
|
||||||
|
client.load_device_by_mnemonic(mnemonic=mnemonic, pin=pin, passphrase_protection=passphrase, label='test', language='english')
|
||||||
|
return function(client, *args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return client_decorator
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config):
|
def pytest_configure(config):
|
||||||
|
27
trezorlib/tests/device_tests/test_cancel.py
Normal file
27
trezorlib/tests/device_tests/test_cancel.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from .conftest import setup_client
|
||||||
|
import trezorlib.messages as m
|
||||||
|
|
||||||
|
|
||||||
|
@setup_client()
|
||||||
|
@pytest.mark.parametrize("message", [
|
||||||
|
m.Ping(message="hello", button_protection=True),
|
||||||
|
m.GetAddress(
|
||||||
|
address_n=[0],
|
||||||
|
coin_name="Bitcoin",
|
||||||
|
script_type=m.InputScriptType.SPENDADDRESS,
|
||||||
|
show_display=True
|
||||||
|
),
|
||||||
|
])
|
||||||
|
def test_cancel_ping(client, message):
|
||||||
|
resp = client.call_raw(message)
|
||||||
|
assert isinstance(resp, m.ButtonRequest)
|
||||||
|
|
||||||
|
client.transport.write(m.ButtonAck())
|
||||||
|
client.transport.write(m.Cancel())
|
||||||
|
|
||||||
|
resp = client.transport.read()
|
||||||
|
|
||||||
|
assert isinstance(resp, m.Failure)
|
||||||
|
assert resp.code == m.FailureType.ActionCancelled
|
Loading…
Reference in New Issue
Block a user