mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-23 06:48:16 +00:00
debuglink: add null debuglink, allow running tests interactively
run with: INTERACT=1 pytest <your options here>
This commit is contained in:
parent
3db07338ed
commit
68da6881b5
@ -26,8 +26,9 @@ EXPECTED_RESPONSES_CONTEXT_LINES = 3
|
|||||||
|
|
||||||
|
|
||||||
class DebugLink:
|
class DebugLink:
|
||||||
def __init__(self, transport):
|
def __init__(self, transport, auto_interact=True):
|
||||||
self.transport = transport
|
self.transport = transport
|
||||||
|
self.allow_interactions = auto_interact
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
self.transport.begin_session()
|
self.transport.begin_session()
|
||||||
@ -87,6 +88,8 @@ class DebugLink:
|
|||||||
return obj.passphrase_protection
|
return obj.passphrase_protection
|
||||||
|
|
||||||
def input(self, word=None, button=None, swipe=None):
|
def input(self, word=None, button=None, swipe=None):
|
||||||
|
if not self.allow_interactions:
|
||||||
|
return
|
||||||
decision = proto.DebugLinkDecision()
|
decision = proto.DebugLinkDecision()
|
||||||
if button is not None:
|
if button is not None:
|
||||||
decision.yes_no = button
|
decision.yes_no = button
|
||||||
@ -130,6 +133,24 @@ class DebugLink:
|
|||||||
self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True)
|
self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True)
|
||||||
|
|
||||||
|
|
||||||
|
class NullDebugLink(DebugLink):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(None)
|
||||||
|
|
||||||
|
def open(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _call(self, msg, nowait=False):
|
||||||
|
if not nowait:
|
||||||
|
if isinstance(msg, proto.DebugLinkGetState):
|
||||||
|
return proto.DebugLinkState()
|
||||||
|
else:
|
||||||
|
raise RuntimeError("unexpected call to a fake debuglink")
|
||||||
|
|
||||||
|
|
||||||
class DebugUI:
|
class DebugUI:
|
||||||
INPUT_FLOW_DONE = object()
|
INPUT_FLOW_DONE = object()
|
||||||
|
|
||||||
@ -171,8 +192,16 @@ class TrezorClientDebugLink(TrezorClient):
|
|||||||
# without special DebugLink interface provided
|
# without special DebugLink interface provided
|
||||||
# by the device.
|
# by the device.
|
||||||
|
|
||||||
def __init__(self, transport):
|
def __init__(self, transport, auto_interact=True):
|
||||||
self.debug = DebugLink(transport.find_debug())
|
try:
|
||||||
|
debug_transport = transport.find_debug()
|
||||||
|
self.debug = DebugLink(debug_transport, auto_interact)
|
||||||
|
except Exception:
|
||||||
|
if not auto_interact:
|
||||||
|
self.debug = NullDebugLink()
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
self.ui = DebugUI(self.debug)
|
self.ui = DebugUI(self.debug)
|
||||||
|
|
||||||
self.in_with_statement = 0
|
self.in_with_statement = 0
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
||||||
|
|
||||||
from trezorlib import debuglink, device
|
from trezorlib import debuglink, device
|
||||||
from trezorlib.debuglink import TrezorClientDebugLink
|
|
||||||
from trezorlib.messages.PassphraseSourceType import HOST as PASSPHRASE_ON_HOST
|
from trezorlib.messages.PassphraseSourceType import HOST as PASSPHRASE_ON_HOST
|
||||||
|
|
||||||
from . import conftest
|
from . import conftest
|
||||||
@ -35,8 +34,7 @@ class TrezorTest:
|
|||||||
pin8 = "45678978"
|
pin8 = "45678978"
|
||||||
|
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
wirelink = conftest.get_device()
|
self.client = conftest.get_device()
|
||||||
self.client = TrezorClientDebugLink(wirelink)
|
|
||||||
# self.client.set_buttonwait(3)
|
# self.client.set_buttonwait(3)
|
||||||
|
|
||||||
device.wipe(self.client)
|
device.wipe(self.client)
|
||||||
|
@ -30,20 +30,26 @@ TREZOR_VERSION = None
|
|||||||
def get_device():
|
def get_device():
|
||||||
path = os.environ.get("TREZOR_PATH")
|
path = os.environ.get("TREZOR_PATH")
|
||||||
if path:
|
if path:
|
||||||
return get_transport(path)
|
transport = get_transport(path)
|
||||||
else:
|
else:
|
||||||
devices = enumerate_devices()
|
devices = enumerate_devices()
|
||||||
for device in devices:
|
for device in devices:
|
||||||
if hasattr(device, "find_debug"):
|
if hasattr(device, "find_debug"):
|
||||||
return device
|
transport = device
|
||||||
raise RuntimeError("No debuggable device found")
|
break
|
||||||
|
else:
|
||||||
|
raise RuntimeError("No debuggable device found")
|
||||||
|
env_interactive = int(os.environ.get("INTERACT", 0))
|
||||||
|
try:
|
||||||
|
return TrezorClientDebugLink(transport, auto_interact=not env_interactive)
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Failed to open debuglink for {}".format(transport.get_path())
|
||||||
|
) from e
|
||||||
|
|
||||||
|
|
||||||
def device_version():
|
def device_version():
|
||||||
device = get_device()
|
client = get_device()
|
||||||
if not device:
|
|
||||||
raise RuntimeError()
|
|
||||||
client = TrezorClientDebugLink(device)
|
|
||||||
if client.features.model == "T":
|
if client.features.model == "T":
|
||||||
return 2
|
return 2
|
||||||
else:
|
else:
|
||||||
@ -52,8 +58,7 @@ def device_version():
|
|||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
def client():
|
def client():
|
||||||
wirelink = get_device()
|
client = get_device()
|
||||||
client = TrezorClientDebugLink(wirelink)
|
|
||||||
wipe_device(client)
|
wipe_device(client)
|
||||||
|
|
||||||
client.open()
|
client.open()
|
||||||
@ -100,6 +105,11 @@ def pytest_addoption(parser):
|
|||||||
"args",
|
"args",
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
parser.addoption(
|
||||||
|
"--interactive",
|
||||||
|
action="store_true",
|
||||||
|
help="Wait for user to do interaction manually",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pytest_runtest_setup(item):
|
def pytest_runtest_setup(item):
|
||||||
|
Loading…
Reference in New Issue
Block a user