Skip to content

Commit

Permalink
new(tests): migrate EOF tests for truncated sections
Browse files Browse the repository at this point in the history
Migrate tests from EOFTests/efValidation/EOF1_truncated_section_.json.
  • Loading branch information
chfast committed Aug 11, 2024
1 parent e15ec28 commit 4049fe4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ GeneralStateTests/stCreate2/call_outsize_then_create2_successful_then_returndata
GeneralStateTests/stCreate2/call_then_create2_successful_then_returndatasize.json

EOFTests/efValidation/EOF1_eofcreate_valid_.json
EOFTests/efValidation/EOF1_truncated_section_.json

([#647](https://github.com/ethereum/execution-spec-tests/pull/647))
EOFTests/efValidation/EOF1_returncontract_invalid_.json
Expand Down
53 changes: 53 additions & 0 deletions tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,56 @@ def test_section_size(
data=eof_code,
expect_exception=exception,
)


@pytest.mark.parametrize(
"truncation_len, exception",
[
# The original container is not valid by itself because its 2-byte code section
# starts with the terminating instruction: INVALID.
pytest.param(0, EOFException.UNREACHABLE_INSTRUCTIONS),
pytest.param(1, EOFException.INVALID_SECTION_BODIES_SIZE, id="EOF1_truncated_section_2"),
pytest.param(3, EOFException.INVALID_SECTION_BODIES_SIZE, id="EOF1_truncated_section_1"),
pytest.param(6, EOFException.INVALID_SECTION_BODIES_SIZE, id="EOF1_truncated_section_0"),
],
)
def test_truncated_container_without_data(
eof_test: EOFTestFiller,
truncation_len: int,
exception: EOFException,
):
"""
This test takes a semi-valid container and removes some bytes from its tail.
Migrated from EOFTests/efValidation/EOF1_truncated_section_.json (cases without data section).
"""
container = Container(sections=[Section.Code(Op.INVALID + Op.INVALID)])
bytecode = bytes(container)
eof_test(
data=bytecode[: len(bytecode) - truncation_len],
expect_exception=exception,
)


@pytest.mark.parametrize(
"trunc_size, exception",
[
pytest.param(0, None),
pytest.param(1, EOFException.TOPLEVEL_CONTAINER_TRUNCATED, id="EOF1_truncated_section_4"),
pytest.param(2, EOFException.TOPLEVEL_CONTAINER_TRUNCATED, id="EOF1_truncated_section_3"),
],
)
def test_truncated_container_with_data(
eof_test: EOFTestFiller,
trunc_size: int,
exception: EOFException,
):
"""
This test takes a valid container with data and removes some bytes from its tail.
Migrated from EOFTests/efValidation/EOF1_truncated_section_.json (cases with data section).
"""
container = Container(sections=[Section.Code(Op.INVALID), Section.Data("aabb")])
bytecode = bytes(container)
eof_test(
data=bytecode[: len(bytecode) - trunc_size],
expect_exception=exception,
)

0 comments on commit 4049fe4

Please sign in to comment.