From 711b083f69c3cf0bece892447bc3d10e54c16b83 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Tue, 21 Nov 2023 11:24:05 +0100 Subject: [PATCH] flake8 fixes --- tests/ragger/test_domain_name.py | 46 +++++++-------- tests/ragger/test_eip712.py | 36 ++++++------ tests/ragger/test_get_address.py | 26 +++++---- tests/ragger/test_nft.py | 98 ++++++++++++++++++++------------ 4 files changed, 118 insertions(+), 88 deletions(-) diff --git a/tests/ragger/test_domain_name.py b/tests/ragger/test_domain_name.py index 6b5b22d..36637a3 100644 --- a/tests/ragger/test_domain_name.py +++ b/tests/ragger/test_domain_name.py @@ -65,15 +65,15 @@ def test_send_fund(firmware: Firmware, }): moves = list() if firmware.device.startswith("nano"): - moves += [ NavInsID.RIGHT_CLICK ] * 4 + moves += [NavInsID.RIGHT_CLICK] * 4 if verbose: - moves += [ NavInsID.RIGHT_CLICK ] - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] * 2 + moves += [NavInsID.USE_CASE_REVIEW_TAP] * 2 if verbose: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] - moves += [ NavInsID.USE_CASE_REVIEW_CONFIRM ] + moves += [NavInsID.USE_CASE_REVIEW_TAP] + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH, "domain_name_verbose_" + str(verbose), moves) @@ -91,7 +91,7 @@ def test_send_fund_wrong_challenge(firmware: Firmware, except ExceptionRAPDU as e: assert e.status == StatusWord.INVALID_DATA else: - assert False # An exception should have been raised + assert False # An exception should have been raised def test_send_fund_wrong_addr(firmware: Firmware, @@ -118,11 +118,11 @@ def test_send_fund_wrong_addr(firmware: Firmware, }): moves = list() if firmware.device.startswith("nano"): - moves += [ NavInsID.RIGHT_CLICK ] * 4 - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] * 4 + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] * 2 - moves += [ NavInsID.USE_CASE_REVIEW_CONFIRM ] + moves += [NavInsID.USE_CASE_REVIEW_TAP] * 2 + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH, "domain_name_wrong_addr", moves) @@ -149,11 +149,11 @@ def test_send_fund_non_mainnet(firmware: Firmware, }): moves = list() if firmware.device.startswith("nano"): - moves += [ NavInsID.RIGHT_CLICK ] * 5 - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] * 5 + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] * 2 - moves += [ NavInsID.USE_CASE_REVIEW_CONFIRM ] + moves += [NavInsID.USE_CASE_REVIEW_TAP] * 2 + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH, "domain_name_non_mainnet", moves) @@ -180,11 +180,11 @@ def test_send_fund_unknown_chain(firmware: Firmware, }): moves = list() if firmware.device.startswith("nano"): - moves += [ NavInsID.RIGHT_CLICK ] * 5 - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] * 5 + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] * 3 - moves += [ NavInsID.USE_CASE_REVIEW_CONFIRM ] + moves += [NavInsID.USE_CASE_REVIEW_TAP] * 3 + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] navigator.navigate_and_compare(ROOT_SCREENSHOT_PATH, "domain_name_unknown_chain", moves) @@ -202,7 +202,7 @@ def test_send_fund_domain_too_long(firmware: Firmware, except ExceptionRAPDU as e: assert e.status == StatusWord.INVALID_DATA else: - assert False # An exception should have been raised + assert False # An exception should have been raised def test_send_fund_domain_invalid_character(firmware: Firmware, @@ -217,7 +217,7 @@ def test_send_fund_domain_invalid_character(firmware: Firmware, except ExceptionRAPDU as e: assert e.status == StatusWord.INVALID_DATA else: - assert False # An exception should have been raised + assert False # An exception should have been raised def test_send_fund_uppercase(firmware: Firmware, @@ -232,7 +232,7 @@ def test_send_fund_uppercase(firmware: Firmware, except ExceptionRAPDU as e: assert e.status == StatusWord.INVALID_DATA else: - assert False # An exception should have been raised + assert False # An exception should have been raised def test_send_fund_domain_non_ens(firmware: Firmware, @@ -247,4 +247,4 @@ def test_send_fund_domain_non_ens(firmware: Firmware, except ExceptionRAPDU as e: assert e.status == StatusWord.INVALID_DATA else: - assert False # An exception should have been raised + assert False # An exception should have been raised diff --git a/tests/ragger/test_eip712.py b/tests/ragger/test_eip712.py index 0816958..f6eb242 100644 --- a/tests/ragger/test_eip712.py +++ b/tests/ragger/test_eip712.py @@ -52,16 +52,16 @@ def test_eip712_legacy(firmware: Firmware, bytes.fromhex('eb4221181ff3f1a83ea7313993ca9218496e424604ba9492bb4052c03d5c3df8')): moves = list() if firmware.device.startswith("nano"): - moves += [ NavInsID.RIGHT_CLICK ] + moves += [NavInsID.RIGHT_CLICK] if firmware.device == "nanos": screens_per_hash = 4 else: screens_per_hash = 2 - moves += [ NavInsID.RIGHT_CLICK ] * screens_per_hash * 2 - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] * screens_per_hash * 2 + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] * 2 - moves += [ NavInsID.USE_CASE_REVIEW_CONFIRM ] + moves += [NavInsID.USE_CASE_REVIEW_TAP] * 2 + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] navigator.navigate(moves) v, r, s = ResponseParser.signature(app_client.response().data) @@ -74,9 +74,9 @@ def test_eip712_legacy(firmware: Firmware, def autonext(fw: Firmware, nav: Navigator): moves = list() if fw.device.startswith("nano"): - moves = [ NavInsID.RIGHT_CLICK ] + moves = [NavInsID.RIGHT_CLICK] else: - moves = [ NavInsID.USE_CASE_REVIEW_TAP ] + moves = [NavInsID.USE_CASE_REVIEW_TAP] nav.navigate(moves, screen_change_before_first_instruction=False, screen_change_after_last_instruction=False) @@ -113,24 +113,22 @@ def test_eip712_new(firmware: Firmware, assert InputData.process_file(app_client, input_file, filter_file, - partial(autonext, firmware, navigator)) == True + partial(autonext, firmware, navigator)) with app_client.eip712_sign_new(BIP32_PATH, verbose): - time.sleep(0.5) # tight on timing, needed by the CI otherwise might fail sometimes + # tight on timing, needed by the CI otherwise might fail sometimes + time.sleep(0.5) + moves = list() if firmware.device.startswith("nano"): - if not verbose and not filtering: # need to skip the message hash - moves = [ NavInsID.RIGHT_CLICK ] * 2 - moves += [ NavInsID.BOTH_CLICK ] + if not verbose and not filtering: # need to skip the message hash + moves = [NavInsID.RIGHT_CLICK] * 2 + moves += [NavInsID.BOTH_CLICK] else: - if not verbose and not filtering: # need to skip the message hash - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] - moves += [ NavInsID.USE_CASE_REVIEW_CONFIRM ] + if not verbose and not filtering: # need to skip the message hash + moves += [NavInsID.USE_CASE_REVIEW_TAP] + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] navigator.navigate(moves) v, r, s = ResponseParser.signature(app_client.response().data) - #print("[signature]") - #print("v = %s" % (v.hex())) - #print("r = %s" % (r.hex())) - #print("s = %s" % (s.hex())) assert v == bytes.fromhex(config["signature"]["v"]) assert r == bytes.fromhex(config["signature"]["r"]) diff --git a/tests/ragger/test_get_address.py b/tests/ragger/test_get_address.py index 33ab5f8..46c290a 100644 --- a/tests/ragger/test_get_address.py +++ b/tests/ragger/test_get_address.py @@ -6,20 +6,22 @@ from ragger.firmware import Firmware from ragger.backend import BackendInterface from ragger.navigator import Navigator, NavInsID from ledger_app_clients.ethereum.client import EthAppClient, StatusWord -from ledger_app_clients.ethereum.settings import SettingID, settings_toggle import ledger_app_clients.ethereum.response_parser as ResponseParser from ragger.bip import calculate_public_key_and_chaincode, CurveChoice ROOT_SCREENSHOT_PATH = Path(__file__).parent + @pytest.fixture(params=[True, False]) def with_chaincode(request) -> bool: return request.param + @pytest.fixture(params=[None, 1, 2, 5, 137]) def chain(request) -> Optional[int]: return request.param + def get_moves(firmware: Firmware, navigator: BackendInterface, chain: Optional[int] = None, @@ -27,25 +29,26 @@ def get_moves(firmware: Firmware, moves = list() if firmware.is_nano: - moves += [ NavInsID.RIGHT_CLICK ] + moves += [NavInsID.RIGHT_CLICK] if firmware.device == "nanos": - moves += [ NavInsID.RIGHT_CLICK ] * 3 + moves += [NavInsID.RIGHT_CLICK] * 3 else: - moves += [ NavInsID.RIGHT_CLICK ] + moves += [NavInsID.RIGHT_CLICK] if reject: - moves += [ NavInsID.RIGHT_CLICK ] - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] + moves += [NavInsID.USE_CASE_REVIEW_TAP] if chain is not None and chain > 1: - moves += [ NavInsID.USE_CASE_ADDRESS_CONFIRMATION_TAP ] + moves += [NavInsID.USE_CASE_ADDRESS_CONFIRMATION_TAP] if reject: - moves += [ NavInsID.USE_CASE_ADDRESS_CONFIRMATION_CANCEL ] + moves += [NavInsID.USE_CASE_ADDRESS_CONFIRMATION_CANCEL] else: - moves += [ NavInsID.USE_CASE_ADDRESS_CONFIRMATION_CONFIRM ] + moves += [NavInsID.USE_CASE_ADDRESS_CONFIRMATION_CONFIRM] return moves + def test_get_pk_rejected(firmware: Firmware, backend: BackendInterface, navigator: Navigator): @@ -59,7 +62,8 @@ def test_get_pk_rejected(firmware: Firmware, except ExceptionRAPDU as e: assert e.status == StatusWord.CONDITION_NOT_SATISFIED else: - assert False # An exception should have been raised + assert False # An exception should have been raised + def test_get_pk(firmware: Firmware, backend: BackendInterface, diff --git a/tests/ragger/test_nft.py b/tests/ragger/test_nft.py index be22144..17430c1 100644 --- a/tests/ragger/test_nft.py +++ b/tests/ragger/test_nft.py @@ -23,53 +23,63 @@ GAS_PRICE = 13 GAS_LIMIT = 21000 FROM = bytes.fromhex("1122334455667788990011223344556677889900") TO = bytes.fromhex("0099887766554433221100998877665544332211") -NFTS = [ (1, 3), (5, 2), (7, 4) ] # tuples of (token_id, amount) +NFTS = [(1, 3), (5, 2), (7, 4)] # tuples of (token_id, amount) DATA = "Some data".encode() DEVICE_ADDR: Optional[bytes] = None -class NFTCollection: + +class NFTCollection: addr: bytes name: str chain_id: int + def __init__(self, addr: bytes, name: str, chain_id: int, contract): self.addr = addr self.name = name self.chain_id = chain_id self.contract = contract -class Action: + +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): 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]: + +def common_nav_nft(is_nano: bool, + nano_steps: int, + stax_steps: int, + reject: bool) -> list[NavInsID]: moves = list() if is_nano: - moves += [ NavInsID.RIGHT_CLICK ] * nano_steps + moves += [NavInsID.RIGHT_CLICK] * nano_steps if reject: - moves += [ NavInsID.RIGHT_CLICK ] - moves += [ NavInsID.BOTH_CLICK ] + moves += [NavInsID.RIGHT_CLICK] + moves += [NavInsID.BOTH_CLICK] else: - moves += [ NavInsID.USE_CASE_REVIEW_TAP ] * stax_steps + 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 ] + moves += [NavInsID.USE_CASE_REVIEW_CONFIRM] return moves + def snapshot_test_name(nft_type: str, fn: str, chain_id: int, reject: bool) -> str: name = "%s_%s_%s" % (nft_type, fn, str(chain_id)) if reject: name += "-rejected" return name + def common_test_nft(fw: Firmware, back: BackendInterface, nav: Navigator, @@ -131,10 +141,11 @@ def common_test_nft_reject(test_fn: Callable, except ExceptionRAPDU as e: assert e.status == StatusWord.CONDITION_NOT_SATISFIED else: - assert False # An exception should have been raised + assert False # An exception should have been raised # ERC-721 + ERC721_PLUGIN = "ERC721" with open("%s/erc721.json" % (ABIS_FOLDER)) as file: @@ -155,6 +166,7 @@ def nav_erc721_transfer_from(is_nano: bool, 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]: @@ -165,6 +177,7 @@ def nav_erc721_approve(is_nano: bool, 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]: @@ -173,19 +186,20 @@ def nav_erc721_set_approval_for_all(is_nano: bool, nano_steps += 1 return common_nav_nft(is_nano, nano_steps, 3, reject) + collecs_721 = [ NFTCollection(bytes.fromhex("bc4ca0eda7647a8ab7c2061c2e118a18a936f13d"), - "Bored Ape Yacht Club", - 1, - contract_erc721), + "Bored Ape Yacht Club", + 1, + contract_erc721), NFTCollection(bytes.fromhex("670fd103b1a08628e9557cd66b87ded841115190"), - "y00ts", - 137, - contract_erc721), + "y00ts", + 137, + contract_erc721), NFTCollection(bytes.fromhex("2909cf13e458a576cdd9aab6bd6617051a92dacf"), - "goerlirocks", - 5, - contract_erc721), + "goerlirocks", + 5, + contract_erc721), ] actions_721 = [ Action("safeTransferFrom", @@ -209,16 +223,19 @@ actions_721 = [ @pytest.fixture(params=collecs_721) def collec_721(request) -> NFTCollection: return request.param + + @pytest.fixture(params=actions_721) def action_721(request) -> Action: return request.param + def test_erc721(firmware: Firmware, - backend: BackendInterface, - navigator: Navigator, - collec_721: NFTCollection, - action_721: Action, - reject: bool = False): + backend: BackendInterface, + navigator: Navigator, + collec_721: NFTCollection, + action_721: Action, + reject: bool = False): common_test_nft(firmware, backend, navigator, @@ -227,9 +244,10 @@ def test_erc721(firmware: Firmware, reject, ERC721_PLUGIN) + def test_erc721_reject(firmware: Firmware, - backend: BackendInterface, - navigator: Navigator): + backend: BackendInterface, + navigator: Navigator): common_test_nft_reject(test_erc721, firmware, backend, @@ -237,6 +255,7 @@ def test_erc721_reject(firmware: Firmware, collecs_721[0], actions_721[0]) + # ERC-1155 ERC1155_PLUGIN = "ERC1155" @@ -258,6 +277,7 @@ def nav_erc1155_safe_transfer_from(is_nano: bool, 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: @@ -268,6 +288,7 @@ def nav_erc1155_safe_batch_transfer_from(is_nano: bool, 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: @@ -276,19 +297,20 @@ def nav_erc1155_set_approval_for_all(is_nano: bool, nano_steps += 1 return common_nav_nft(is_nano, nano_steps, 3, reject) + collecs_1155 = [ NFTCollection(bytes.fromhex("495f947276749ce646f68ac8c248420045cb7b5e"), - "OpenSea Shared Storefront", - 1, - contract_erc1155), + "OpenSea Shared Storefront", + 1, + contract_erc1155), NFTCollection(bytes.fromhex("2953399124f0cbb46d2cbacd8a89cf0599974963"), - "OpenSea Collections", - 137, - contract_erc1155), + "OpenSea Collections", + 137, + contract_erc1155), NFTCollection(bytes.fromhex("f4910c763ed4e47a585e2d34baa9a4b611ae448c"), - "OpenSea Collections", - 5, - contract_erc1155), + "OpenSea Collections", + 5, + contract_erc1155), ] actions_1155 = [ Action("safeTransferFrom", @@ -307,13 +329,18 @@ actions_1155 = [ [TO, False], nav_erc1155_set_approval_for_all), ] + + @pytest.fixture(params=collecs_1155) def collec_1155(request) -> bool: return request.param + + @pytest.fixture(params=actions_1155) def action_1155(request) -> Action: return request.param + def test_erc1155(firmware: Firmware, backend: BackendInterface, navigator: Navigator, @@ -328,6 +355,7 @@ def test_erc1155(firmware: Firmware, reject, ERC1155_PLUGIN) + def test_erc1155_reject(firmware: Firmware, backend: BackendInterface, navigator: Navigator):