repo2rlenv-swe-next / tasks /swe-next-1a113b4bab7fa544fc2b /environment /source /tests /test_elffile.py
Download tasks/swe-next-1a113b4bab7fa544fc2b/environment/source/tests/test_elffile.py from FineEnvs/repo2rlenv-swe-next: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/datasets/FineEnvs/repo2rlenv-swe-next/resolve/main/tasks/swe-next-1a113b4bab7fa544fc2b/environment/source/tests/test_elffile.py
- Command line
-
hf download hf://datasets/FineEnvs/repo2rlenv-swe-next/tasks/swe-next-1a113b4bab7fa544fc2b/environment/source/tests/test_elffile.py
-
curl -L -o test_elffile.py https://huggingface.co/datasets/FineEnvs/repo2rlenv-swe-next/resolve/main/tasks/swe-next-1a113b4bab7fa544fc2b/environment/source/tests/test_elffile.py
3.32 kB
| import io | |
| import pathlib | |
| import struct | |
| import pytest | |
| from packaging._elffile import EIClass, EIData, ELFFile, ELFInvalid, EMachine | |
| DIR_MANYLINUX = pathlib.Path(__file__, "..", "manylinux").resolve() | |
| DIR_MUSLLINUX = pathlib.Path(__file__, "..", "musllinux").resolve() | |
| BIN_MUSL_X86_64 = DIR_MUSLLINUX.joinpath("musl-x86_64").read_bytes() | |
| def test_elffile_glibc( | |
| name: str, capacity: EIClass, encoding: EIData, machine: EMachine | |
| ) -> None: | |
| path = DIR_MANYLINUX.joinpath(f"hello-world-{name}") | |
| with path.open("rb") as f: | |
| ef = ELFFile(f) | |
| assert ef.capacity == capacity | |
| assert ef.encoding == encoding | |
| assert ef.machine == machine | |
| assert ef.flags is not None | |
| def test_elffile_musl( | |
| name: str, capacity: EIClass, encoding: EIData, machine: EMachine, interpreter: str | |
| ) -> None: | |
| path = DIR_MUSLLINUX.joinpath(f"musl-{name}") | |
| with path.open("rb") as f: | |
| ef = ELFFile(f) | |
| assert ef.capacity == capacity | |
| assert ef.encoding == encoding | |
| assert ef.machine == machine | |
| assert ef.interpreter == f"/lib/ld-musl-{interpreter}.so.1" | |
| def test_elffile_bad_ident(data: bytes) -> None: | |
| with pytest.raises(ELFInvalid): | |
| ELFFile(io.BytesIO(data)) | |
| def test_elffile_no_section() -> None: | |
| """Enough for magic, but not the section definitions.""" | |
| data = BIN_MUSL_X86_64[:25] | |
| with pytest.raises(ELFInvalid): | |
| ELFFile(io.BytesIO(data)) | |
| def test_elffile_invalid_section() -> None: | |
| """Enough for section definitions, but not the actual sections.""" | |
| data = BIN_MUSL_X86_64[:58] | |
| assert ELFFile(io.BytesIO(data)).interpreter is None | |
| def test_elffle_no_interpreter_section() -> None: | |
| ef = ELFFile(io.BytesIO(BIN_MUSL_X86_64)) | |
| # Change all sections to *not* PT_INTERP. | |
| data = BIN_MUSL_X86_64 | |
| for i in range(ef._e_phnum + 1): | |
| sb = ef._e_phoff + ef._e_phentsize * i | |
| se = sb + ef._e_phentsize | |
| section = struct.unpack(ef._p_fmt, data[sb:se]) | |
| data = data[:sb] + struct.pack(ef._p_fmt, 0, *section[1:]) + data[se:] | |
| assert ELFFile(io.BytesIO(data)).interpreter is None | |