Skip to content

Commit

Permalink
Tests: check app context after some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Jan 31, 2024
1 parent 95f60a5 commit fa1cc5c
Show file tree
Hide file tree
Showing 88 changed files with 156 additions and 47 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.
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.
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.
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.
138 changes: 92 additions & 46 deletions test/python/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
Preattestation,
Attestation,
AttestationDal,
Fitness,
BlockHeader,
Block,
DEFAULT_CHAIN_ID
)
from utils.navigator import TezosNavigator, Instructions
from common import (
DEFAULT_ACCOUNT,
TESTS_ROOT_DIR,
EMPTY_PATH
TESTS_ROOT_DIR
)


Expand Down Expand Up @@ -75,24 +75,34 @@ def test_authorize_baking(

account.check_public_key(public_key)

tezos_navigator.check_app_context(
account,
chain_id=DEFAULT_CHAIN_ID,
main_hwm=Hwm(0),
test_hwm=Hwm(0),
path=test_name
)


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
def test_deauthorize(
account: Account,
client: TezosClient,
tezos_navigator: TezosNavigator) -> None:
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the DEAUTHORIZE instruction."""

tezos_navigator.authorize_baking(account)

client.deauthorize()

# get_auth_key_with_curve raise EXC_REFERENCED_DATA_NOT_FOUND

path = client.get_auth_key()

assert path == EMPTY_PATH, \
f"Expected the empty path {EMPTY_PATH} but got {path}"
tezos_navigator.check_app_context(
None,
chain_id=DEFAULT_CHAIN_ID,
main_hwm=Hwm(0),
test_hwm=Hwm(0),
path=test_name
)

@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
def test_get_auth_key(
Expand Down Expand Up @@ -160,35 +170,25 @@ def test_get_public_key_prompt(
account.check_public_key(public_key)


def test_reset_app_context(
client: TezosClient,
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
def test_reset_app_context(tezos_navigator: TezosNavigator, test_name: Path) -> None:
"""Test the RESET instruction."""

reset_level: int = 1

tezos_navigator.reset_app_context(reset_level, path=test_name)

received_main_chain_id, received_main_hwm, received_test_hwm = client.get_all_hwm()

main_hwm = Hwm(reset_level)
test_hwm = Hwm(reset_level)

assert received_main_chain_id == DEFAULT_CHAIN_ID, \
f"Expected main chain id {DEFAULT_CHAIN_ID} but got {received_main_chain_id}"

assert received_main_hwm == main_hwm, \
f"Expected main hmw {main_hwm} but got {received_main_hwm}"

assert received_test_hwm == test_hwm, \
f"Expected test hmw {test_hwm} but got {received_test_hwm}"
tezos_navigator.check_app_context(
None,
chain_id=DEFAULT_CHAIN_ID,
main_hwm=Hwm(reset_level),
test_hwm=Hwm(reset_level),
path=test_name
)


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
def test_setup_app_context(
account: Account,
client: TezosClient,
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the SETUP instruction."""
Expand All @@ -207,16 +207,13 @@ def test_setup_app_context(

account.check_public_key(public_key)

received_main_chain_id, received_main_hwm, received_test_hwm = client.get_all_hwm()

assert received_main_chain_id == main_chain_id, \
f"Expected main chain id {main_chain_id} but got {received_main_chain_id}"

assert received_main_hwm == main_hwm, \
f"Expected main hmw {main_hwm} but got {received_main_hwm}"

assert received_test_hwm == test_hwm, \
f"Expected test hmw {test_hwm} but got {received_test_hwm}"
tezos_navigator.check_app_context(
account,
chain_id=main_chain_id,
main_hwm=main_hwm,
test_hwm=test_hwm,
path=test_name
)


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
Expand Down Expand Up @@ -279,7 +276,8 @@ def test_sign_preattestation(
account: Account,
with_hash: bool,
client: TezosClient,
tezos_navigator: TezosNavigator) -> None:
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the SIGN(_WITH_HASH) instruction on preattestation."""

main_chain_id = DEFAULT_CHAIN_ID
Expand All @@ -293,7 +291,10 @@ def test_sign_preattestation(
test_hwm
)

preattestation = Preattestation().forge(chain_id=main_chain_id)
preattestation = Preattestation(
op_level=1,
op_round=2
).forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, preattestation)
Expand All @@ -304,14 +305,23 @@ def test_sign_preattestation(
f"Expected hash {preattestation.hash.hex()} but got {preattestation_hash.hex()}"
account.check_signature(signature, bytes(preattestation))

tezos_navigator.check_app_context(
account,
chain_id=main_chain_id,
main_hwm=Hwm(1, 2),
test_hwm=Hwm(0, 0),
path=test_name
)


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
@pytest.mark.parametrize("with_hash", [False, True])
def test_sign_attestation(
account: Account,
with_hash: bool,
client: TezosClient,
tezos_navigator: TezosNavigator) -> None:
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the SIGN(_WITH_HASH) instruction on attestation."""

main_chain_id = DEFAULT_CHAIN_ID
Expand All @@ -325,7 +335,10 @@ def test_sign_attestation(
test_hwm
)

attestation = Attestation().forge(chain_id=main_chain_id)
attestation = Attestation(
op_level=1,
op_round=2
).forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, attestation)
Expand All @@ -336,14 +349,23 @@ def test_sign_attestation(
f"Expected hash {attestation.hash.hex()} but got {attestation_hash.hex()}"
account.check_signature(signature, bytes(attestation))

tezos_navigator.check_app_context(
account,
chain_id=main_chain_id,
main_hwm=Hwm(1, 2),
test_hwm=Hwm(0, 0),
path=test_name
)


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
@pytest.mark.parametrize("with_hash", [False, True])
def test_sign_attestation_dal(
account: Account,
with_hash: bool,
client: TezosClient,
tezos_navigator: TezosNavigator) -> None:
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the SIGN(_WITH_HASH) instruction on attestation."""

main_chain_id = DEFAULT_CHAIN_ID
Expand All @@ -357,7 +379,10 @@ def test_sign_attestation_dal(
test_hwm
)

attestation = AttestationDal().forge(chain_id=main_chain_id)
attestation = AttestationDal(
op_level=1,
op_round=2
).forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, attestation)
Expand All @@ -368,7 +393,13 @@ def test_sign_attestation_dal(
f"Expected hash {attestation.hash.hex()} but got {attestation_hash.hex()}"
account.check_signature(signature, bytes(attestation))


tezos_navigator.check_app_context(
account,
chain_id=main_chain_id,
main_hwm=Hwm(1, 2),
test_hwm=Hwm(0, 0),
path=test_name
)


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
Expand All @@ -377,7 +408,8 @@ def test_sign_block(
account: Account,
with_hash: bool,
client: TezosClient,
tezos_navigator: TezosNavigator) -> None:
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the SIGN(_WITH_HASH) instruction on block."""

main_chain_id = DEFAULT_CHAIN_ID
Expand All @@ -391,7 +423,12 @@ def test_sign_block(
test_hwm
)

block = Block(header=BlockHeader(level=1)).forge(chain_id=main_chain_id)
block = Block(
header=BlockHeader(
level=1,
fitness=Fitness(current_round=2)
)
).forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, block)
Expand All @@ -402,6 +439,15 @@ def test_sign_block(
f"Expected hash {block.hash.hex()} but got {block_hash.hex()}"
account.check_signature(signature, bytes(block))

tezos_navigator.check_app_context(
account,
chain_id=main_chain_id,
main_hwm=Hwm(1, 2),
test_hwm=Hwm(0, 0),
path=test_name
)


# Data generated by the old application itself
HMAC_TEST_SET = [
(DEFAULT_ACCOUNT,
Expand Down
65 changes: 64 additions & 1 deletion test/python/utils/navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from ragger.navigator import Navigator, NavInsID, NavIns

from common import TESTS_ROOT_DIR
from common import TESTS_ROOT_DIR, EMPTY_PATH
from utils.client import TezosClient, Hwm
from utils.account import Account

Expand Down Expand Up @@ -172,6 +172,69 @@ def _assert_screen(self, path: Path) -> None:
golden_run=self._golden_run
), f"Screen does not match golden {path}."

def check_app_context(self,
account: Optional[Account],
chain_id: str,
main_hwm: Hwm,
test_hwm: Hwm,
path: Path) -> None:
"""Check that the app context."""
path = Path(path)

received_chain_id, received_main_hwm, received_test_hwm = self.client.get_all_hwm()

# get_auth_key_with_curve raise EXC_REFERENCED_DATA_NOT_FOUND
received_path = self.client.get_auth_key()

assert received_chain_id == chain_id, \
f"Expected main chain id {chain_id} but got {received_chain_id}"
assert received_main_hwm == main_hwm, \
f"Expected main hmw {main_hwm} but got {received_main_hwm}"
assert received_test_hwm == test_hwm, \
f"Expected test hmw {test_hwm} but got {received_test_hwm}"

if account is None:
# get_auth_key_with_curve raise EXC_REFERENCED_DATA_NOT_FOUND
received_path = self.client.get_auth_key()
assert received_path == EMPTY_PATH, \
f"Expected the empty path {EMPTY_PATH} but got {received_path}"
else:
received_sig_scheme, received_path = self.client.get_auth_key_with_curve()
assert received_path == account.path, \
f"Expected path {account.path} but got {received_path}"
assert received_sig_scheme == account.sig_scheme, \
f"Expected signature scheme {account.sig_scheme.name} "\
f"but got {received_sig_scheme.name}"

if self.firmware.is_nano:
self.navigator.navigate(
[NavInsID.RIGHT_CLICK] * 2,
screen_change_before_first_instruction=False
)
self._assert_screen(path / "chain_id.png")
self.backend.right_click()
self.backend.wait_for_screen_change()
if account is not None and self.firmware.device == "nanos":
self._assert_screen(path / "public_key_hash_1.png")
self.backend.right_click()
self.backend.wait_for_screen_change()
self._assert_screen(path / "public_key_hash_2.png")
else:
self._assert_screen(path / "public_key_hash.png")
self.backend.right_click()
self.backend.wait_for_screen_change()
self._assert_screen(path / "high_watermark.png")
self.navigator.navigate(
[NavInsID.LEFT_CLICK] * 4,
screen_change_before_first_instruction=False
)
else:
self.home.settings()
self.backend.wait_for_screen_change()
self._assert_screen(path / "app_context.png")
self.settings.multi_page_exit()
self.backend.wait_for_screen_change()

def authorize_baking(self, account: Optional[Account], **kwargs) -> bytes:
"""Send an authorize baking request and navigate until accept"""
if 'instructions' not in kwargs:
Expand Down

0 comments on commit fa1cc5c

Please sign in to comment.