From a4dd89002fba87c51c058369c7baa74a1c114146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 5 Jun 2024 22:02:25 +0200 Subject: [PATCH] new(tests): add tests for EOF container prefix Also remove similar "manual" tests in container.py. --- .../eip3540_eof_v1/container.py | 31 ----------- .../test_container_validation.py | 53 +++++++++++++++++++ tests/prague/eip7692_eof_v1/tracker.md | 4 +- 3 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_container_validation.py diff --git a/tests/prague/eip7692_eof_v1/eip3540_eof_v1/container.py b/tests/prague/eip7692_eof_v1/eip3540_eof_v1/container.py index 76922d0413..ba03b76f42 100644 --- a/tests/prague/eip7692_eof_v1/eip3540_eof_v1/container.py +++ b/tests/prague/eip7692_eof_v1/eip3540_eof_v1/container.py @@ -4,7 +4,6 @@ from typing import List -from ethereum_test_tools.eof import LATEST_EOF_VERSION from ethereum_test_tools.eof.v1 import VERSION_MAX_SECTION_KIND, AutoSection, Container, Section from ethereum_test_tools.eof.v1 import SectionKind as Kind from ethereum_test_tools.eof.v1.constants import ( @@ -184,36 +183,6 @@ auto_type_section=AutoSection.NONE, validity_error=EOFException.MISSING_TYPE_HEADER, ), - Container( - name="invalid_magic_01", - magic=b"\xef\x01", - sections=[Section.Code(Op.STOP)], - validity_error=EOFException.INVALID_MAGIC, - ), - Container( - name="invalid_magic_ff", - magic=b"\xef\xFF", - sections=[Section.Code(Op.STOP)], - validity_error=EOFException.INVALID_MAGIC, - ), - Container( - name="invalid_version_zero", - version=b"\x00", - sections=[Section.Code(Op.STOP)], - validity_error=EOFException.INVALID_VERSION, - ), - Container( - name="invalid_version_plus_one", - version=int.to_bytes(LATEST_EOF_VERSION + 1, length=1, byteorder="big"), - sections=[Section.Code(Op.STOP)], - validity_error=EOFException.INVALID_VERSION, - ), - Container( - name="invalid_version_high", - version=b"\xFF", - sections=[Section.Code(Op.STOP)], - validity_error=EOFException.INVALID_VERSION, - ), Container( name="no_code_section", sections=[ diff --git a/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_container_validation.py b/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_container_validation.py new file mode 100644 index 0000000000..0e6efee3e4 --- /dev/null +++ b/tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_container_validation.py @@ -0,0 +1,53 @@ +""" +EOF validation tests for EIP-3540 container format +""" + +import pytest + +from ethereum_test_tools import EOFTestFiller +from ethereum_test_tools import Opcodes as Op +from ethereum_test_tools.eof.v1 import Container, EOFException, Section + +from .. import EOF_FORK_NAME + +REFERENCE_SPEC_GIT_PATH = "EIPS/eip-3540.md" +REFERENCE_SPEC_VERSION = "8dcb0a8c1c0102c87224308028632cc986a61183" + +pytestmark = pytest.mark.valid_from(EOF_FORK_NAME) + +VALID_CONTAINER = Container(sections=[Section.Code(code=Op.STOP)]) + + +@pytest.mark.parametrize("magic_0", [0, 1, 0xEE, 0xEF, 0xF0, 0xFF]) +@pytest.mark.parametrize("magic_1", [0, 1, 2, 0xFE, 0xFF]) +def test_magic_validation( + eof_test: EOFTestFiller, + magic_0: int, + magic_1: int, +): + """ + Verify EOF container 2-byte magic + """ + code = bytearray(bytes(VALID_CONTAINER)) + code[0] = magic_0 + code[1] = magic_1 + eof_test( + data=bytes(code), + expect_exception=None if magic_0 == 0xEF and magic_1 == 0 else EOFException.INVALID_MAGIC, + ) + + +@pytest.mark.parametrize("version", [0, 1, 2, 0xFE, 0xFF]) +def test_version_validation( + eof_test: EOFTestFiller, + version: int, +): + """ + Verify EOF container version + """ + code = bytearray(bytes(VALID_CONTAINER)) + code[2] = version + eof_test( + data=bytes(code), + expect_exception=None if version == 1 else EOFException.INVALID_VERSION, + ) diff --git a/tests/prague/eip7692_eof_v1/tracker.md b/tests/prague/eip7692_eof_v1/tracker.md index 6142ccde09..04b6a6e4dc 100644 --- a/tests/prague/eip7692_eof_v1/tracker.md +++ b/tests/prague/eip7692_eof_v1/tracker.md @@ -17,9 +17,9 @@ - [ ] Valid containers with max number of code sections (ethereum/tests: src/EOFTestsFiller/efValidation/many_code_sections_1024_Copier.json) - [ ] Too many code sections (ethereum/tests: ./src/EOFTestsFiller/efValidation/EOF1_too_many_code_sections_Copier.json src/EOFTestsFiller/efValidation/too_many_code_sections_Copier.json) - [ ] Truncated magic (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml) -- [ ] Valid container except magic (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml src/EOFTestsFiller/efValidation/validate_EOF_prefix_Copier.json) +- [x] Valid container except magic (./eip3540_eof_v1/test_container_validation.py::test_magic_validation ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml src/EOFTestsFiller/efValidation/validate_EOF_prefix_Copier.json) - [ ] Truncated before version (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml) -- [ ] Valid container except version (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml src/EOFTestsFiller/efValidation/validate_EOF_version_Copier.json) +- [x] Valid container except version (./eip3540_eof_v1/test_container_validation.py::test_version_validation ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml src/EOFTestsFiller/efValidation/validate_EOF_version_Copier.json) - [ ] Truncated before type section header (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml) - [ ] Truncated before type section size (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml src/EOFTestsFiller/efValidation/EOF1_incomplete_section_size_Copier.json) - [ ] Truncated type section size (ethereum/tests: ./src/EOFTestsFiller/EIP3540/validInvalidFiller.yml src/EOFTestsFiller/efValidation/EOF1_incomplete_section_size_Copier.json)