From f52ff4006daa8585ed72c07dc13ef2ae6f62bcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Sun, 11 Aug 2024 19:51:26 +0200 Subject: [PATCH] new(tests): migrate EOF tests for truncated sections Migrate tests from EOFTests/efValidation/EOF1_truncated_section_.json. --- converted-ethereum-tests.txt | 1 + .../eip3540_eof_v1/test_section_size.py | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/converted-ethereum-tests.txt b/converted-ethereum-tests.txt index e6933cd178..2177ef29fb 100644 --- a/converted-ethereum-tests.txt +++ b/converted-ethereum-tests.txt @@ -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 diff --git a/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_size.py b/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_size.py index 38fb615437..641dcd3945 100644 --- a/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_size.py +++ b/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_section_size.py @@ -105,3 +105,56 @@ def test_section_size( data=eof_code, expect_exception=exception, ) + + +@pytest.mark.parametrize( + "trunc_size, 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, + trunc_size: 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) - trunc_size], + 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, + )