diff --git a/src/utils/utils.cairo b/src/utils/utils.cairo index eadeff034..9925d2a99 100644 --- a/src/utils/utils.cairo +++ b/src/utils/utils.cairo @@ -226,6 +226,9 @@ namespace Helpers { func try_parse_destination_from_bytes(bytes_len: felt, bytes: felt*) -> model.Option { if (bytes_len != 20) { + with_attr error_message("Bytes has length {bytes_len}, expected 0 or 20") { + assert bytes_len = 0; + } let res = model.Option(is_some=0, value=0); return res; } diff --git a/tests/src/utils/test_utils.py b/tests/src/utils/test_utils.py index d1bb4f3a3..476cd72d9 100644 --- a/tests/src/utils/test_utils.py +++ b/tests/src/utils/test_utils.py @@ -76,7 +76,6 @@ def test_bytes_to_uint256(cairo_run, word): @given(word=st.integers(min_value=0, max_value=2**128 - 1)) -@settings(max_examples=20) def test_should_return_bytes_used_in_128_word(cairo_run, word): bytes_length = (word.bit_length() + 7) // 8 output = cairo_run( @@ -94,7 +93,6 @@ def test_should_return_bytes_used_in_128_word(cairo_run, word): b"\x01" * 20, [1, 0x0101010101010101010101010101010101010101], ), # An address of 20 bytes - (b"\x01" * 40, [0, 0]), # and invalid address ], ) def test_should_parse_destination_from_bytes(cairo_run, bytes, expected): @@ -102,6 +100,12 @@ def test_should_parse_destination_from_bytes(cairo_run, bytes, expected): assert result == expected +@given(bytes_array=st.binary(min_size=1, max_size=32).filter(lambda x: len(x) != 20)) +def test_should_panic_incorrect_address_encoding(cairo_run, bytes_array): + with cairo_error(message=f"Bytes has length {len(bytes_array)}, expected 0 or 20"): + cairo_run("test__try_parse_destination_from_bytes", bytes=list(bytes_array)) + + @pytest.mark.parametrize( "data, expected", [