Skip to content

Commit

Permalink
chore(core): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ibz committed Jun 5, 2024
1 parent b1ef5ef commit 4c7963a
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 48 deletions.
1 change: 0 additions & 1 deletion core/embed/rust/librust_qstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ static void _librust_qstrs(void) {
MP_QSTR_device_name__change_template;
MP_QSTR_device_name__title;
MP_QSTR_disable_animation;
MP_QSTR_dry_run;
MP_QSTR_encode;
MP_QSTR_encoded_length;
MP_QSTR_entropy__send;
Expand Down
21 changes: 11 additions & 10 deletions core/embed/rust/src/ui/model_mercury/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ impl ConfirmBlobParams {
}
}

const RECOVERY_TYPE_DRY_RUN: u32 = 1;
const RECOVERY_TYPE_UNLOCK_REPEATED_BACKUP: u32 = 2;

extern "C" fn new_confirm_blob(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| {
let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
Expand Down Expand Up @@ -1250,15 +1253,15 @@ extern "C" fn new_confirm_recovery(n_args: usize, args: *const Obj, kwargs: *mut
let _title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let _button: TString = kwargs.get(Qstr::MP_QSTR_button)?.try_into()?;
let dry_run: bool = kwargs.get(Qstr::MP_QSTR_dry_run)?.try_into()?;
let recovery_type: u32 = kwargs.get(Qstr::MP_QSTR_recovery_type)?.try_into()?;
let _info_button: bool = kwargs.get_or(Qstr::MP_QSTR_info_button, false)?;

let paragraphs = Paragraphs::new([Paragraph::new(&theme::TEXT_NORMAL, description)]);

let notification: TString = if dry_run {
TR::recovery__title_dry_run.into()
} else {
TR::recovery__title.into()
let notification = match recovery_type {
RECOVERY_TYPE_DRY_RUN => TR::recovery__title_dry_run.into(),
RECOVERY_TYPE_UNLOCK_REPEATED_BACKUP => TR::recovery__title_dry_run.into(),
_ => TR::recovery__title.into(),
};

let content = SwipeUpScreen::new(paragraphs);
Expand All @@ -1274,10 +1277,8 @@ extern "C" fn new_confirm_recovery(n_args: usize, args: *const Obj, kwargs: *mut

extern "C" fn new_select_word_count(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| {
let _dry_run: bool = kwargs.get(Qstr::MP_QSTR_dry_run)?.try_into()?;

let obj = LayoutObj::new(Frame::left_aligned(
TR::recovery__select_num_of_words.into(),
TR::recovery__num_of_words.into(),
SelectWordCount::new(),
))?;
Ok(obj.into())
Expand Down Expand Up @@ -1903,15 +1904,15 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// title: str,
/// description: str,
/// button: str,
/// dry_run: bool,
/// recovery_type: RecoveryType,
/// info_button: bool = False,
/// ) -> LayoutObj[UiResult]:
/// """Device recovery homescreen."""
Qstr::MP_QSTR_confirm_recovery => obj_fn_kw!(0, new_confirm_recovery).as_obj(),

/// def select_word_count(
/// *,
/// dry_run: bool,
/// recovery_type: RecoveryType,
/// ) -> LayoutObj[int | str]: # TT returns int
/// """Select mnemonic word count from (12, 18, 20, 24, 33)."""
Qstr::MP_QSTR_select_word_count => obj_fn_kw!(0, new_select_word_count).as_obj(),
Expand Down
4 changes: 2 additions & 2 deletions core/mocks/generated/trezorui2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def confirm_recovery(
title: str,
description: str,
button: str,
dry_run: bool,
recovery_type: RecoveryType,
info_button: bool = False,
) -> LayoutObj[UiResult]:
"""Device recovery homescreen."""
Expand All @@ -458,7 +458,7 @@ def confirm_recovery(
# rust/src/ui/model_mercury/layout.rs
def select_word_count(
*,
dry_run: bool,
recovery_type: RecoveryType,
) -> LayoutObj[int | str]: # TT returns int
"""Select mnemonic word count from (12, 18, 20, 24, 33)."""

Expand Down
4 changes: 3 additions & 1 deletion core/src/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ async def handle_CancelAuthorization(msg: CancelAuthorization) -> protobuf.Messa
def set_homescreen() -> None:
import storage.recovery as storage_recovery

from apps.common import backup

set_default = workflow.set_default # local_cache_attribute

if storage_cache.is_set(storage_cache.APP_COMMON_BUSY_DEADLINE_MS):
Expand All @@ -369,7 +371,7 @@ def set_homescreen() -> None:

set_default(screensaver, restart=True)

elif storage_recovery.is_in_progress():
elif storage_recovery.is_in_progress() or backup.repeated_backup_enabled():
from apps.management.recovery_device.homescreen import recovery_homescreen

set_default(recovery_homescreen)
Expand Down
3 changes: 2 additions & 1 deletion core/src/apps/management/recovery_device/homescreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ async def recovery_homescreen() -> None:
from apps.homescreen import homescreen

if backup.repeated_backup_enabled():
storage_recovery.end_progress()
await _continue_repeated_backup()
elif not storage_recovery.is_in_progress():
workflow.set_default(homescreen)
Expand Down Expand Up @@ -208,6 +207,8 @@ async def _finish_recovery_unlock_repeated_backup(

result = _check_secret_against_stored_secret(secret, is_slip39, backup_type)

storage_recovery.end_progress()

if result:
backup.activate_repeated_backup()
return Success(message="Backup unlocked")
Expand Down
10 changes: 5 additions & 5 deletions core/src/trezor/ui/layouts/mercury/recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import trezorui2
from trezor import TR
from trezor.enums import ButtonRequestType
from trezor.enums import ButtonRequestType, RecoveryType

from ..common import interact
from . import RustLayout, raise_if_not_confirmed
Expand All @@ -25,8 +25,8 @@ async def _is_confirmed_info(
return result is CONFIRMED


async def request_word_count(dry_run: bool) -> int:
selector = RustLayout(trezorui2.select_word_count(dry_run=dry_run))
async def request_word_count(recovery_type: RecoveryType) -> int:
selector = RustLayout(trezorui2.select_word_count(recovery_type=recovery_type))
count = await interact(selector, "word_count", ButtonRequestType.MnemonicWordCount)
return int(count)

Expand Down Expand Up @@ -116,7 +116,7 @@ async def continue_recovery(
text: str,
subtext: str | None,
info_func: Callable | None,
dry_run: bool,
recovery_type: RecoveryType,
show_info: bool = False, # unused on TT
) -> bool:
from ..common import button_request
Expand All @@ -133,7 +133,7 @@ async def continue_recovery(
description=description,
button=button_label,
info_button=info_func is not None,
dry_run=dry_run,
recovery_type=recovery_type,
)
)

Expand Down
24 changes: 18 additions & 6 deletions tests/click_tests/recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,18 @@ def select_number_of_words(
raise ValueError("Unknown model")

if unlock_repeated_backup:
TR.assert_in(layout.text_content(), "recovery__enter_backup")
if debug.model in (models.T2B1,):
TR.assert_in(layout.text_content(), "recovery__enter_backup")
else:
TR.assert_in(layout.text_content(), "recovery__only_first_n_letters")
elif num_of_words in (20, 33):
TR.assert_in_multiple(
layout.text_content(),
["recovery__enter_any_share", "recovery__only_first_n_letters"],
[
"recovery__enter_backup",
"recovery__enter_any_share",
"recovery__only_first_n_letters",
],
)
else: # BIP-39
TR.assert_in_multiple(
Expand All @@ -125,14 +132,14 @@ def enter_share(
before_title: str = "recovery__title_recover",
) -> "LayoutContent":
if debug.model in (models.T2B1,):
TR.assert_in(debug.read_layout().title(), "recovery__title_recover")
TR.assert_in(debug.read_layout().title(), before_title)
layout = debug.wait_layout()
for _ in range(layout.page_count()):
layout = debug.press_right(wait=True)
elif debug.model in (models.T3T1,):
layout = debug.swipe_up(wait=True)
else:
TR.assert_in(debug.read_layout().title(), "recovery__title_recover")
TR.assert_in(debug.read_layout().title(), before_title)
layout = debug.click(buttons.OK, wait=True)

assert "MnemonicKeyboard" in layout.all_components()
Expand All @@ -152,15 +159,20 @@ def enter_shares(
) -> None:
TR.assert_in_multiple(
debug.read_layout().text_content(),
["recovery__enter_any_share", "recovery__only_first_n_letters", text],
[
"recovery__enter_backup",
"recovery__enter_any_share",
"recovery__only_first_n_letters",
text,
],
)
for index, share in enumerate(shares):
enter_share(
debug, share, is_first=index == 0, before_title=enter_share_before_title
)
if index < len(shares) - 1:
# FIXME: when ui-t3t1 done for shamir, we want to check the template below
TR.assert_in(debug.read_layout().title(), "recovery__title_recover")
TR.assert_in(debug.read_layout().title(), enter_share_before_title)
# TR.assert_in(
# debug.read_layout().text_content(),
# "recovery__x_of_y_entered_template",
Expand Down
9 changes: 6 additions & 3 deletions tests/click_tests/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ def confirm_read(debug: "DebugLink", middle_r: bool = False) -> None:
debug.press_right(wait=True)


def cancel_backup(debug: "DebugLink", middle_r: bool = False) -> None:
def cancel_backup(
debug: "DebugLink", middle_r: bool = False, confirm: bool = False
) -> None:
if debug.model in (models.T2T1,):
debug.click(buttons.CANCEL, wait=True)
debug.click(buttons.CANCEL, wait=True)
elif debug.model in (models.T3T1,):
debug.click(buttons.CORNER_BUTTON, wait=True)
debug.click(buttons.VERTICAL_MENU[0], wait=True)
debug.swipe_up(wait=True)
debug.click(buttons.TAP_TO_CONFIRM)
if confirm:
debug.swipe_up(wait=True)
debug.click(buttons.TAP_TO_CONFIRM)
elif debug.model in (models.T2B1,):
debug.press_left(wait=True)
debug.press_left(wait=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/click_tests/test_backup_slip39_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_backup_slip39_custom(
reset.confirm_new_wallet(debug)

# cancel back up
reset.cancel_backup(debug)
reset.cancel_backup(debug, confirm=True)

assert device_handler.result() == "Initialized"

Expand Down
8 changes: 7 additions & 1 deletion tests/click_tests/test_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pytest

from trezorlib import device, messages
from trezorlib import device, messages, models

from ..common import MNEMONIC12, MNEMONIC_SLIP39_BASIC_20_3of6
from . import recovery
Expand Down Expand Up @@ -54,6 +54,8 @@ def prepare_recovery_and_evaluate(
def test_recovery_slip39_basic(device_handler: "BackgroundDeviceHandler"):
with prepare_recovery_and_evaluate(device_handler) as debug:
recovery.confirm_recovery(debug)
if debug.model is models.T2B1:
recovery.confirm_recovery(debug)

recovery.select_number_of_words(debug)
recovery.enter_shares(debug, MNEMONIC_SLIP39_BASIC_20_3of6)
Expand All @@ -64,6 +66,8 @@ def test_recovery_slip39_basic(device_handler: "BackgroundDeviceHandler"):
def test_recovery_bip39(device_handler: "BackgroundDeviceHandler"):
with prepare_recovery_and_evaluate(device_handler) as debug:
recovery.confirm_recovery(debug)
if debug.model is models.T2B1:
recovery.confirm_recovery(debug)

recovery.select_number_of_words(debug, num_of_words=12)
recovery.enter_seed(debug, MNEMONIC12.split())
Expand All @@ -74,6 +78,8 @@ def test_recovery_bip39(device_handler: "BackgroundDeviceHandler"):
def test_recovery_bip39_previous_word(device_handler: "BackgroundDeviceHandler"):
with prepare_recovery_and_evaluate(device_handler) as debug:
recovery.confirm_recovery(debug)
if debug.model is models.T2B1:
recovery.confirm_recovery(debug)

recovery.select_number_of_words(debug, num_of_words=12)
seed_words: list[str] = MNEMONIC12.split()
Expand Down
13 changes: 9 additions & 4 deletions tests/click_tests/test_repeated_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ def test_repeated_backup(

# let's make a 1-of-1 backup to start with...

assert debug.model is not None
model_name: str = debug.model.internal_name

# shares=1
reset.set_selection(debug, buttons.RESET_MINUS, 5 - 1)
reset.set_selection(debug, buttons.reset_minus(model_name), 5 - 1)

# confirm checklist
reset.confirm_read(debug)

# threshold=1
reset.set_selection(debug, buttons.RESET_PLUS, 0)
reset.set_selection(debug, buttons.reset_plus(model_name), 0)

# confirm checklist
reset.confirm_read(debug)
Expand Down Expand Up @@ -133,17 +136,19 @@ def test_repeated_backup(

# ... so let's try to do a 2-of-3 backup

debug.wait_layout()

# confirm checklist
reset.confirm_read(debug)

# shares=3
reset.set_selection(debug, buttons.RESET_MINUS, 5 - 3)
reset.set_selection(debug, buttons.reset_minus(model_name), 5 - 3)

# confirm checklist
reset.confirm_read(debug)

# threshold=2
reset.set_selection(debug, buttons.RESET_MINUS, 1)
reset.set_selection(debug, buttons.reset_minus(model_name), 1)

# confirm checklist
reset.confirm_read(debug)
Expand Down
Loading

0 comments on commit 4c7963a

Please sign in to comment.