def test_clear_session(client: Client): is_trezor1 = client.features.model == "1" init_responses = [ messages.PinMatrixRequest if is_trezor1 else messages.ButtonRequest, messages.PassphraseRequest, ] cached_responses = [messages.PublicKey] with client: client.use_pin_sequence([PIN4]) client.set_expected_responses(init_responses + cached_responses) assert get_public_node(client, ADDRESS_N).xpub == XPUB with client: # pin and passphrase are cached client.set_expected_responses(cached_responses) assert get_public_node(client, ADDRESS_N).xpub == XPUB client.clear_session() # session cache is cleared with client: client.use_pin_sequence([PIN4]) client.set_expected_responses(init_responses + cached_responses) assert get_public_node(client, ADDRESS_N).xpub == XPUB with client: # pin and passphrase are cached client.set_expected_responses(cached_responses) assert get_public_node(client, ADDRESS_N).xpub == XPUB
class TrezorTest: # fmt: off # 1 2 3 4 5 6 7 8 9 10 11 12 mnemonic12 = "alcohol woman abuse must during monitor noble actual mixed trade anger aisle" mnemonic18 = "owner little vague addict embark decide pink prosper true fork panda embody mixture exchange choose canoe electric jewel" mnemonic24 = "dignity pass list indicate nasty swamp pool script soccer toe leaf photo multiply desk host tomato cradle drill spread actor shine dismiss champion exotic" mnemonic_all = " ".join(["all"] * 12) # fmt: on pin4 = "1234" pin6 = "789456" pin8 = "45678978" def setup_method(self, method): wirelink = conftest.get_device() self.client = TrezorClientDebugLink(wirelink) # self.client.set_buttonwait(3) device.wipe(self.client) self.client.open() def teardown_method(self, method): self.client.close() def _setup_mnemonic(self, mnemonic=None, pin="", passphrase=False): if mnemonic is None: mnemonic = TrezorTest.mnemonic12 debuglink.load_device_by_mnemonic( self.client, mnemonic=mnemonic, pin=pin, passphrase_protection=passphrase, label="test", language="english", ) if conftest.TREZOR_VERSION == 1: # remove cached PIN (introduced via load_device) self.client.clear_session() if conftest.TREZOR_VERSION > 1 and passphrase: device.apply_settings(self.client, passphrase_source=PASSPHRASE_ON_HOST) def setup_mnemonic_allallall(self): self._setup_mnemonic(mnemonic=TrezorTest.mnemonic_all) def setup_mnemonic_nopin_nopassphrase(self): self._setup_mnemonic() def setup_mnemonic_nopin_passphrase(self): self._setup_mnemonic(passphrase=True) def setup_mnemonic_pin_nopassphrase(self): self._setup_mnemonic(pin=TrezorTest.pin4) def setup_mnemonic_pin_passphrase(self): self._setup_mnemonic(pin=TrezorTest.pin4, passphrase=True)
def _assert_protection(client: Client, pin: bool = True, passphrase: bool = True) -> None: """Make sure PIN and passphrase protection have expected values""" with client: client.use_pin_sequence([PIN4]) client.ensure_unlocked() assert client.features.pin_protection is pin assert client.features.passphrase_protection is passphrase client.clear_session()
def test_256bit_passphrase(client: Client): """ BIP32 Root Key for passphrase TREZOR: provided by Andrew, address calculated via https://iancoleman.io/bip39/ xprv9s21ZrQH143K2UspC9FRPfQC9NcDB4HPkx1XG9UEtuceYtpcCZ6ypNZWdgfxQ9dAFVeD1F4Zg4roY7nZm2LB7THPD6kaCege3M7EuS8v85c """ assert client.features.passphrase_protection is True client.use_passphrase("TREZOR") address = get_test_address(client) assert address == "mxVtGxUJ898WLzPMmy6PT1FDHD1GUCWGm7" client.clear_session() client.use_passphrase("ROZERT") address_compare = get_test_address(client) assert address != address_compare
def test_128bit_passphrase(client: Client): """ BIP32 Root Key for passphrase TREZOR: provided by Andrew, address calculated via https://iancoleman.io/bip39/ xprv9s21ZrQH143K3dzDLfeY3cMp23u5vDeFYftu5RPYZPucKc99mNEddU4w99GxdgUGcSfMpVDxhnR1XpJzZNXRN1m6xNgnzFS5MwMP6QyBRKV """ assert client.features.passphrase_protection is True client.use_passphrase("TREZOR") address = get_test_address(client) assert address == "mkKDUMRR1CcK8eLAzCZAjKnNbCquPoWPxN" client.clear_session() client.use_passphrase("ROZERT") address_compare = get_test_address(client) assert address != address_compare
def load_client(): devices = enumerate_devices() for device in devices: try: client = TrezorClientDebugLink(device) break except Exception: pass else: raise RuntimeError("No debuggable device found") wipe_device(client) debuglink.load_device_by_mnemonic( client, mnemonic=" ".join(["all"] * 12), pin=None, passphrase_protection=False, label="test", language="english", ) client.clear_session() client.open() return client
def client(request: pytest.FixtureRequest, _raw_client: Client) -> Generator[Client, None, None]: """Client fixture. Every test function that requires a client instance will get it from here. If we can't connect to a debuggable device, the test will fail. If 'skip_t2' is used and TT is connected, the test is skipped. Vice versa with T1 and 'skip_t1'. The client instance is wiped and preconfigured with "all all all..." mnemonic, no password and no pin. It is possible to customize this with the `setup_client` marker. To specify a custom mnemonic and/or custom pin and/or enable passphrase: @pytest.mark.setup_client(mnemonic=MY_MNEMONIC, pin="9999", passphrase=True) To receive a client instance that was not initialized: @pytest.mark.setup_client(uninitialized=True) """ if request.node.get_closest_marker( "skip_t2") and _raw_client.features.model == "T": pytest.skip("Test excluded on Trezor T") if request.node.get_closest_marker( "skip_t1") and _raw_client.features.model == "1": pytest.skip("Test excluded on Trezor 1") sd_marker = request.node.get_closest_marker("sd_card") if sd_marker and not _raw_client.features.sd_card_present: raise RuntimeError("This test requires SD card.\n" "To skip all such tests, run:\n" " pytest -m 'not sd_card' <test path>") test_ui = request.config.getoption("ui") _raw_client.reset_debug_features() _raw_client.open() try: _raw_client.init_device() except Exception: request.session.shouldstop = "Failed to communicate with Trezor" pytest.fail("Failed to communicate with Trezor") if test_ui: # we need to reseed before the wipe _raw_client.debug.reseed(0) if sd_marker: should_format = sd_marker.kwargs.get("formatted", True) _raw_client.debug.erase_sd_card(format=should_format) wipe_device(_raw_client) setup_params = dict( uninitialized=False, mnemonic=" ".join(["all"] * 12), pin=None, passphrase=False, needs_backup=False, no_backup=False, ) marker = request.node.get_closest_marker("setup_client") if marker: setup_params.update(marker.kwargs) use_passphrase = setup_params["passphrase"] is True or isinstance( setup_params["passphrase"], str) if not setup_params["uninitialized"]: debuglink.load_device( _raw_client, mnemonic=setup_params["mnemonic"], pin=setup_params["pin"], passphrase_protection=use_passphrase, label="test", language="en-US", needs_backup=setup_params["needs_backup"], no_backup=setup_params["no_backup"], ) if _raw_client.features.model == "T": apply_settings(_raw_client, experimental_features=True) if use_passphrase and isinstance(setup_params["passphrase"], str): _raw_client.use_passphrase(setup_params["passphrase"]) _raw_client.clear_session() if test_ui: with ui_tests.screen_recording(_raw_client, request): yield _raw_client else: yield _raw_client _raw_client.close()