1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 13:58:08 +00:00

tests(translations): always use client version for language blobs

and ignore what is written in the jsons
This commit is contained in:
matejcik 2024-02-29 14:23:00 +01:00 committed by matejcik
parent 802958bc05
commit 00544312b5
2 changed files with 19 additions and 15 deletions

View File

@ -100,7 +100,7 @@ def test_error_invalid_data_length(client: Client):
# Invalid data length
# Sending more data than advertised in the header
with pytest.raises(exceptions.TrezorFailure, match="Invalid data length"), client:
good_data = build_and_sign_blob("cs", client.model)
good_data = build_and_sign_blob("cs", client)
bad_data = good_data + b"abcd"
device.change_language(client, language_data=bad_data)
assert client.features.language == "en-US"
@ -114,7 +114,7 @@ def test_error_invalid_header_magic(client: Client):
with pytest.raises(
exceptions.TrezorFailure, match="Invalid translations data"
), client:
good_data = build_and_sign_blob("cs", client.model)
good_data = build_and_sign_blob("cs", client)
bad_data = 4 * b"a" + good_data[4:]
device.change_language(client, language_data=bad_data)
assert client.features.language == "en-US"
@ -128,7 +128,7 @@ def test_error_invalid_data_hash(client: Client):
with pytest.raises(
exceptions.TrezorFailure, match="Translation data verification failed"
), client:
good_data = build_and_sign_blob("cs", client.model)
good_data = build_and_sign_blob("cs", client)
bad_data = good_data[:-8] + 8 * b"a"
device.change_language(
client,
@ -145,11 +145,10 @@ def test_error_version_mismatch(client: Client):
with pytest.raises(
exceptions.TrezorFailure, match="Translations version mismatch"
), client:
data = get_lang_json("cs")
data["header"]["version"] = "3.5.4"
blob = prepare_blob("cs", client.model, (3, 5, 4, 0))
device.change_language(
client,
language_data=build_and_sign_blob(data, client.model),
language_data=sign_blob(blob),
)
assert client.features.language == "en-US"
_check_ping_screen_texts(client, get_ping_title("en"), get_ping_button("en"))
@ -221,7 +220,7 @@ def test_translations_renders_on_screen(client: Client):
czech_data_copy["translations"]["words__confirm"] = new_czech_confirm
device.change_language(
client,
language_data=build_and_sign_blob(czech_data_copy, client.model),
language_data=build_and_sign_blob(czech_data_copy, client),
)
_check_ping_screen_texts(client, new_czech_confirm, get_ping_button("cs"))
@ -230,7 +229,7 @@ def test_translations_renders_on_screen(client: Client):
del czech_data_copy["translations"]["words__confirm"]
device.change_language(
client,
language_data=build_and_sign_blob(czech_data_copy, client.model),
language_data=build_and_sign_blob(czech_data_copy, client),
)
_check_ping_screen_texts(client, get_ping_title("en"), get_ping_button("cs"))
@ -238,7 +237,7 @@ def test_translations_renders_on_screen(client: Client):
def test_reject_update(client: Client):
assert client.features.language == "en-US"
lang = "cs"
language_data = build_and_sign_blob(lang, client.model)
language_data = build_and_sign_blob(lang, client)
def input_flow_reject():
yield
@ -256,7 +255,7 @@ def test_reject_update(client: Client):
def _maybe_confirm_set_language(
client: Client, lang: str, show_display: bool | None, is_displayed: bool
) -> None:
language_data = build_and_sign_blob(lang, client.model)
language_data = build_and_sign_blob(lang, client)
CHUNK_SIZE = 1024
@ -356,7 +355,7 @@ def test_header_trailing_data(client: Client):
"""
assert client.features.language == "en-US"
lang = "cs"
blob = prepare_blob(lang, client.model)
blob = prepare_blob(lang, client.model, client.version)
blob.header_bytes += b"trailing dataa"
assert len(blob.header_bytes) % 2 == 0, "Trailing data must keep the 2-alignment"
language_data = sign_blob(blob)

View File

@ -22,6 +22,7 @@ LANGUAGES = [file.stem for file in TRANSLATIONS.glob("??.json")]
def prepare_blob(
lang_or_def: translations.JsonDef | Path | str,
model: models.TrezorModel,
version: translations.VersionTuple | tuple[int, int, int] | None = None,
) -> translations.TranslationsBlob:
order = translations.order_from_json(json.loads(ORDER_FILE.read_text()))
if isinstance(lang_or_def, str):
@ -30,7 +31,11 @@ def prepare_blob(
lang_or_def = t.cast(translations.JsonDef, json.loads(lang_or_def.read_text()))
# generate raw blob
version = translations.version_from_json(lang_or_def["header"]["version"])
if version is None:
version = translations.version_from_json(lang_or_def["header"]["version"])
elif len(version) == 3:
# version coming from client object does not have build item
version = *version, 0
return translations.blob_from_defs(lang_or_def, order, model, version, FONTS_DIR)
@ -48,9 +53,9 @@ def sign_blob(blob: translations.TranslationsBlob) -> bytes:
def build_and_sign_blob(
lang_or_def: translations.JsonDef | Path | str,
model: models.TrezorModel,
client: Client,
) -> bytes:
blob = prepare_blob(lang_or_def, model)
blob = prepare_blob(lang_or_def, client.model, client.version)
return sign_blob(blob)
@ -58,7 +63,7 @@ def set_language(client: Client, lang: str):
if lang.startswith("en"):
language_data = b""
else:
language_data = build_and_sign_blob(lang, client.model)
language_data = build_and_sign_blob(lang, client)
with client:
device.change_language(client, language_data) # type: ignore