diff --git a/python/src/trezorlib/debuglink.py b/python/src/trezorlib/debuglink.py index 758b9f782c..5c71aa8dcf 100644 --- a/python/src/trezorlib/debuglink.py +++ b/python/src/trezorlib/debuglink.py @@ -160,6 +160,10 @@ class DebugLink: def flash_erase(self, sector): self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True) + @expect(proto.Success) + def erase_sd_card(self, format=True): + return self._call(proto.DebugLinkEraseSdCard(format=format)) + class NullDebugLink(DebugLink): def __init__(self): diff --git a/tests/conftest.py b/tests/conftest.py index 2d0d4c2636..42be2759ca 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -81,10 +81,8 @@ def client(request): if request.node.get_closest_marker("skip_t1") and client.features.model == "1": pytest.skip("Test excluded on Trezor 1") - if ( - request.node.get_closest_marker("sd_card") - and not client.features.sd_card_present - ): + sd_marker = request.node.get_closest_marker("sd_card") + if sd_marker and not client.features.sd_card_present: raise RuntimeError( "This test requires SD card.\n" "To skip all such tests, run:\n" @@ -101,6 +99,10 @@ def client(request): # we need to reseed before the wipe client.debug.reseed(0) + if sd_marker: + should_format = sd_marker.kwargs.get("formatted", True) + client.debug.erase_sd_card(format=should_format) + wipe_device(client) setup_params = dict( diff --git a/tests/device_tests/test_sdcard.py b/tests/device_tests/test_sdcard.py new file mode 100644 index 0000000000..36bd019be6 --- /dev/null +++ b/tests/device_tests/test_sdcard.py @@ -0,0 +1,45 @@ +# This file is part of the Trezor project. +# +# Copyright (C) 2012-2019 SatoshiLabs and contributors +# +# This library is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the License along with this library. +# If not, see . + +import pytest + +from trezorlib import device, messages +from trezorlib.exceptions import TrezorFailure +from trezorlib.messages import SdProtectOperationType as Op + +pytestmark = pytest.mark.skip_t1 + + +@pytest.mark.sd_card(formatted=False) +def test_sd_format(client): + device.sd_protect(client, Op.ENABLE) + assert client.features.sd_protection is True + + +@pytest.mark.sd_card(formatted=False) +def test_sd_no_format(client): + def input_flow(): + yield # enable SD protection? + client.debug.press_yes() + + yield # format SD card + client.debug.press_no() + + with pytest.raises(TrezorFailure) as e, client: + client.set_input_flow(input_flow) + device.sd_protect(client, Op.ENABLE) + + assert e.value.failure.code == messages.FailureType.ProcessError