Port scenario review in 'test_nft'

This commit is contained in:
Charles-Edouard de la Vergne
2024-04-16 23:48:16 +02:00
parent 3c169dc896
commit a05acddc88
24 changed files with 36 additions and 143 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@@ -4,17 +4,17 @@ import json
import pytest
from web3 import Web3
from client.client import EthAppClient, StatusWord
import client.response_parser as ResponseParser
from client.utils import get_selector_from_data, recover_transaction
from ragger.error import ExceptionRAPDU
from ragger.firmware import Firmware
from ragger.backend import BackendInterface
from ragger.navigator import Navigator, NavInsID
from ragger.navigator.navigation_scenario import NavigateWithScenario
from constants import ABIS_FOLDER
from client.client import EthAppClient, StatusWord
import client.response_parser as ResponseParser
from client.utils import get_selector_from_data, recover_transaction
BIP32_PATH = "m/44'/60'/0'/0/0"
NONCE = 21
@@ -42,46 +42,15 @@ class NFTCollection:
class Action:
fn_name: str
fn_args: list[Any]
nav_fn: Callable
def __init__(self, fn_name: str, fn_args: list[Any], nav_fn: Callable):
def __init__(self, fn_name: str, fn_args: list[Any]):
self.fn_name = fn_name
self.fn_args = fn_args
self.nav_fn = nav_fn
def common_nav_nft(is_nano: bool,
nano_steps: int,
stax_steps: int,
reject: bool) -> list[NavInsID]:
moves = []
if is_nano:
moves += [NavInsID.RIGHT_CLICK] * nano_steps
if reject:
moves += [NavInsID.RIGHT_CLICK]
moves += [NavInsID.BOTH_CLICK]
else:
moves += [NavInsID.USE_CASE_REVIEW_TAP] * stax_steps
if reject:
moves += [
NavInsID.USE_CASE_REVIEW_REJECT,
NavInsID.USE_CASE_CHOICE_CONFIRM
]
else:
moves += [NavInsID.USE_CASE_REVIEW_CONFIRM]
return moves
def snapshot_test_name(nft_type: str, fn: str, chain_id: int, reject: bool) -> str:
name = f"{nft_type}_{fn}_{str(chain_id)}"
if reject:
name += "-rejected"
return name
def common_test_nft(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
default_screenshot_path: Path,
collec: NFTCollection,
action: Action,
@@ -114,14 +83,17 @@ def common_test_nft(firmware: Firmware,
"data": data,
}
with app_client.sign(BIP32_PATH, tx_params):
navigator.navigate_and_compare(default_screenshot_path,
snapshot_test_name(plugin_name.lower(),
action.fn_name,
collec.chain_id,
reject),
action.nav_fn(firmware.is_nano,
collec.chain_id,
reject))
test_name = f"{plugin_name.lower()}_{action.fn_name}_{str(collec.chain_id)}"
if reject:
test_name += "-rejected"
scenario_navigator.review_reject(default_screenshot_path, test_name)
else:
if firmware.device.startswith("nano"):
end_text = "Accept"
else:
end_text = "Sign"
scenario_navigator.review_approve(default_screenshot_path, test_name, end_text)
# verify signature
vrs = ResponseParser.signature(app_client.response().data)
addr = recover_transaction(tx_params, vrs)
@@ -131,12 +103,12 @@ def common_test_nft(firmware: Firmware,
def common_test_nft_reject(test_fn: Callable,
firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
default_screenshot_path: Path,
collec: NFTCollection,
action: Action):
with pytest.raises(ExceptionRAPDU) as e:
test_fn(firmware, backend, navigator, default_screenshot_path, collec, action, True)
test_fn(firmware, backend, scenario_navigator, default_screenshot_path, collec, action, True)
assert e.value.status == StatusWord.CONDITION_NOT_SATISFIED
# ERC-721
@@ -150,39 +122,6 @@ with open(f"{ABIS_FOLDER}/erc721.json", encoding="utf-8") as file:
address=bytes(20)
)
# ui navigator functions
def nav_erc721_transfer_from(is_nano: bool,
chain_id: int,
reject: bool) -> list[NavInsID]:
nano_steps = 7
stax_steps = 3
if chain_id != 1:
nano_steps += 1
stax_steps += 1
return common_nav_nft(is_nano, nano_steps, stax_steps, reject)
def nav_erc721_approve(is_nano: bool,
chain_id: int,
reject: bool) -> list[NavInsID]:
nano_steps = 7
stax_steps = 3
if chain_id != 1:
nano_steps += 1
stax_steps += 1
return common_nav_nft(is_nano, nano_steps, stax_steps, reject)
def nav_erc721_set_approval_for_all(is_nano: bool,
chain_id: int,
reject: bool) -> list[NavInsID]:
nano_steps = 6
if chain_id != 1:
nano_steps += 1
return common_nav_nft(is_nano, nano_steps, 3, reject)
collecs_721 = [
NFTCollection(bytes.fromhex("bc4ca0eda7647a8ab7c2061c2e118a18a936f13d"),
"Bored Ape Yacht Club",
@@ -198,21 +137,11 @@ collecs_721 = [
contract_erc721),
]
actions_721 = [
Action("safeTransferFrom",
[FROM, TO, NFTS[0][0], DATA],
nav_erc721_transfer_from),
Action("safeTransferFrom",
[FROM, TO, NFTS[0][0]],
nav_erc721_transfer_from),
Action("transferFrom",
[FROM, TO, NFTS[0][0]],
nav_erc721_transfer_from),
Action("approve",
[TO, NFTS[0][0]],
nav_erc721_approve),
Action("setApprovalForAll",
[TO, False],
nav_erc721_set_approval_for_all),
Action("safeTransferFrom", [FROM, TO, NFTS[0][0], DATA]),
Action("safeTransferFrom", [FROM, TO, NFTS[0][0]]),
Action("transferFrom", [FROM, TO, NFTS[0][0]]),
Action("approve", [TO, NFTS[0][0]]),
Action("setApprovalForAll", [TO, False]),
]
@@ -228,14 +157,14 @@ def action_721_fixture(request) -> Action:
def test_erc721(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
default_screenshot_path: Path,
collec_721: NFTCollection,
action_721: Action,
reject: bool = False):
common_test_nft(firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
collec_721,
action_721,
@@ -245,12 +174,12 @@ def test_erc721(firmware: Firmware,
def test_erc721_reject(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
default_screenshot_path: Path):
common_test_nft_reject(test_erc721,
firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
collecs_721[0],
actions_721[0])
@@ -267,37 +196,6 @@ with open(f"{ABIS_FOLDER}/erc1155.json", encoding="utf-8") as file:
)
# ui navigator functions
def nav_erc1155_safe_transfer_from(is_nano: bool,
chain_id: int,
reject: bool) -> list:
nano_steps = 8
if chain_id != 1:
nano_steps += 1
return common_nav_nft(is_nano, nano_steps, 4, reject)
def nav_erc1155_safe_batch_transfer_from(is_nano: bool,
chain_id: int,
reject: bool) -> list:
nano_steps = 7
stax_steps = 3
if chain_id != 1:
nano_steps += 1
stax_steps += 1
return common_nav_nft(is_nano, nano_steps, stax_steps, reject)
def nav_erc1155_set_approval_for_all(is_nano: bool,
chain_id: int,
reject: bool) -> list:
nano_steps = 6
if chain_id != 1:
nano_steps += 1
return common_nav_nft(is_nano, nano_steps, 3, reject)
collecs_1155 = [
NFTCollection(bytes.fromhex("495f947276749ce646f68ac8c248420045cb7b5e"),
"OpenSea Shared Storefront",
@@ -313,9 +211,7 @@ collecs_1155 = [
contract_erc1155),
]
actions_1155 = [
Action("safeTransferFrom",
[FROM, TO, NFTS[0][0], NFTS[0][1], DATA],
nav_erc1155_safe_transfer_from),
Action("safeTransferFrom", [FROM, TO, NFTS[0][0], NFTS[0][1], DATA]),
Action("safeBatchTransferFrom",
[
FROM,
@@ -323,11 +219,8 @@ actions_1155 = [
list(map(lambda nft: nft[0], NFTS)),
list(map(lambda nft: nft[1], NFTS)),
DATA
],
nav_erc1155_safe_batch_transfer_from),
Action("setApprovalForAll",
[TO, False],
nav_erc1155_set_approval_for_all),
]),
Action("setApprovalForAll", [TO, False]),
]
@@ -343,14 +236,14 @@ def action_1155_fixture(request) -> Action:
def test_erc1155(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
default_screenshot_path: Path,
collec_1155: NFTCollection,
action_1155: Action,
reject: bool = False):
common_test_nft(firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
collec_1155,
action_1155,
@@ -360,12 +253,12 @@ def test_erc1155(firmware: Firmware,
def test_erc1155_reject(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
default_screenshot_path: Path):
common_test_nft_reject(test_erc1155,
firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
collecs_1155[0],
actions_1155[0])