Skip to content

Commit

Permalink
Updated Ragger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apaillier-ledger committed Dec 13, 2024
1 parent 26fcd2a commit 7632baa
Show file tree
Hide file tree
Showing 172 changed files with 38 additions and 25 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ragger/snapshots/flex/test_blind_sign_rejected/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified tests/ragger/snapshots/flex/test_sign_parameter_selector/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified tests/ragger/snapshots/nanos/test_blind_sign_rejected/00002.png
Binary file modified tests/ragger/snapshots/nanos/test_blind_sign_rejected/00003.png
Binary file modified tests/ragger/snapshots/nanos/test_blind_sign_rejected/00004.png
Binary file modified tests/ragger/snapshots/nanos/test_blind_sign_rejected/00005.png
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Binary file modified tests/ragger/snapshots/nanos/test_sign_parameter_selector/00010.png
Binary file modified tests/ragger/snapshots/nanos/test_sign_parameter_selector/00011.png
Binary file modified tests/ragger/snapshots/nanos/test_sign_parameter_selector/00012.png
Binary file modified tests/ragger/snapshots/nanosp/test_blind_sign_rejected/00002.png
Binary file modified tests/ragger/snapshots/nanosp/test_blind_sign_rejected/00003.png
Diff not rendered.
Diff not rendered.
Binary file modified tests/ragger/snapshots/nanox/test_blind_sign_rejected/00002.png
Binary file modified tests/ragger/snapshots/nanox/test_blind_sign_rejected/00003.png
Diff not rendered.
Diff not rendered.
Binary file modified tests/ragger/snapshots/nanox/test_sign_parameter_selector/00008.png
Binary file modified tests/ragger/snapshots/nanox/test_sign_parameter_selector/00009.png
Binary file modified tests/ragger/snapshots/nanox/test_sign_parameter_selector/00010.png
Binary file modified tests/ragger/snapshots/stax/test_blind_sign_rejected/00002.png
Diff not rendered.
Binary file modified tests/ragger/snapshots/stax/test_sign_parameter_selector/00001.png
63 changes: 38 additions & 25 deletions tests/ragger/test_blind_sign.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pathlib import Path
import json
from typing import Optional
import time

import pytest
from web3 import Web3

Expand All @@ -23,22 +25,27 @@
# TODO: do one test with nonce display


@pytest.fixture(name="sign", params=[True, False])
def sign_fixture(request) -> bool:
@pytest.fixture(name="reject", params=[False, True])
def reject_fixture(request) -> bool:
return request.param


def common_tx_params() -> dict:
@pytest.fixture(name="amount", params=[0.0, 1.2])
def amount_fixture(request) -> float:
return request.param


def common_tx_params(amount: float) -> dict:
with open(f"{ABIS_FOLDER}/erc20.json", encoding="utf-8") as file:
contract = Web3().eth.contract(
abi=json.load(file),
address=None
)
data = contract.encode_abi("approve", [
# Uniswap Protocol: Permit2
bytes.fromhex("000000000022d473030f116ddee9f6b43ac78ba3"),
Web3.to_wei("2", "ether")
# this function is not handled by the internal plugin, so won't check if value == 0
data = contract.encode_abi("balanceOf", [
bytes.fromhex("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"),
])

return {
"nonce": 235,
"maxFeePerGas": Web3.to_wei(100, "gwei"),
Expand All @@ -47,7 +54,8 @@ def common_tx_params() -> dict:
# Maker: Dai Stablecoin
"to": bytes.fromhex("6b175474e89094c44da98b954eedeac495271d0f"),
"data": data,
"chainId": 1
"chainId": 1,
"value": Web3.to_wei(amount, "ether"),
}


Expand All @@ -57,24 +65,28 @@ def test_blind_sign(firmware: Firmware,
navigator: Navigator,
default_screenshot_path: Path,
test_name: str,
sign: bool):
reject: bool,
amount: float):
global DEVICE_ADDR
app_client = EthAppClient(backend)

if reject and amount > 0.0:
pytest.skip()
settings_toggle(firmware, navigator, [SettingID.BLIND_SIGNING])
if DEVICE_ADDR is None:
with app_client.get_public_addr(bip32_path=BIP32_PATH, display=False):
pass
_, DEVICE_ADDR, _ = ResponseParser.pk_addr(app_client.response().data)

tx_params = common_tx_params()
tx_params = common_tx_params(amount)
try:
with app_client.sign(BIP32_PATH, tx_params):
if sign:
test_name += "_signed"
else:
if reject:
test_name += "_rejected"

if amount > 0.0:
test_name += "_nonzero"

moves = []
if firmware.is_nano:
# blind signing warning
Expand All @@ -95,6 +107,10 @@ def test_blind_sign(firmware: Firmware,
else:
moves += [NavInsID.RIGHT_CLICK]

# amount
if amount > 0.0:
moves += [NavInsID.RIGHT_CLICK]

# to
if firmware == Firmware.NANOS:
moves += [NavInsID.RIGHT_CLICK] * 3
Expand All @@ -104,7 +120,7 @@ def test_blind_sign(firmware: Firmware,
# fees
moves += [NavInsID.RIGHT_CLICK]

if not sign:
if reject:
moves += [NavInsID.RIGHT_CLICK]

moves += [NavInsID.BOTH_CLICK]
Expand All @@ -113,7 +129,7 @@ def test_blind_sign(firmware: Firmware,

moves += [NavInsID.SWIPE_CENTER_TO_LEFT] * 3

if sign:
if not reject:
moves += [NavInsID.USE_CASE_REVIEW_CONFIRM]
else:
moves += [NavInsID.USE_CASE_REVIEW_REJECT]
Expand All @@ -123,10 +139,10 @@ def test_blind_sign(firmware: Firmware,
test_name,
moves)
except ExceptionRAPDU as e:
assert not sign
assert reject
assert e.status == StatusWord.CONDITION_NOT_SATISFIED
else:
assert sign
assert not reject
# verify signature
vrs = ResponseParser.signature(app_client.response().data)
addr = recover_transaction(tx_params, vrs)
Expand All @@ -144,7 +160,7 @@ def test_blind_sign_reject_in_risk_review(firmware: Firmware,
settings_toggle(firmware, navigator, [SettingID.BLIND_SIGNING])

try:
with app_client.sign(BIP32_PATH, common_tx_params()):
with app_client.sign(BIP32_PATH, common_tx_params(0.0)):
moves = [NavInsID.USE_CASE_CHOICE_CONFIRM]
navigator.navigate(moves)
except ExceptionRAPDU as e:
Expand All @@ -170,7 +186,7 @@ def test_sign_parameter_selector(firmware: Firmware,

settings_toggle(firmware, navigator, [SettingID.BLIND_SIGNING, SettingID.DEBUG_DATA])

tx_params = common_tx_params()
tx_params = common_tx_params(0.0)
data_len = len(bytes.fromhex(tx_params["data"][2:]))
params = (data_len - 4) // 32
with app_client.sign(BIP32_PATH, tx_params):
Expand All @@ -179,11 +195,7 @@ def test_sign_parameter_selector(firmware: Firmware,
# verify | selector
moves += [NavInsID.RIGHT_CLICK] * 2 + [NavInsID.BOTH_CLICK]
if firmware == Firmware.NANOS:
# hardcoded for each because for some params take more pages than others
# parameter 1
moves += [NavInsID.RIGHT_CLICK] * 4 + [NavInsID.BOTH_CLICK]
# parameter 2
moves += [NavInsID.RIGHT_CLICK] * 3 + [NavInsID.BOTH_CLICK]
moves += ([NavInsID.RIGHT_CLICK] * 4 + [NavInsID.BOTH_CLICK]) * params
# blind signing | review | tx hash | from | to | fees
moves += [NavInsID.RIGHT_CLICK] * 13
else:
Expand Down Expand Up @@ -216,10 +228,11 @@ def test_blind_sign_not_enabled_error(firmware: Firmware,
app_client = EthAppClient(backend)

try:
with app_client.sign(BIP32_PATH, common_tx_params()):
with app_client.sign(BIP32_PATH, common_tx_params(0.0)):
moves = []
if firmware.is_nano:
if firmware == Firmware.NANOS:
time.sleep(0.5) # seems to take some time
moves += [NavInsID.RIGHT_CLICK]
moves += [NavInsID.BOTH_CLICK]
else:
Expand Down

0 comments on commit 7632baa

Please sign in to comment.