Skip to content

Commit

Permalink
Move pytest_ofrak ELF objects to LFS instead of written on every func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
alchzh committed Dec 12, 2024
1 parent eade4b4 commit 09ee52a
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 109 deletions.
22 changes: 22 additions & 0 deletions ofrak_core/pytest_ofrak/elf/assets/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

CC=gcc

default: program

program.o: program.c $(HEADERS)
$(CC) -c program.c -fno-asynchronous-unwind-tables -o program.o

program: program.o
$(CC) program.o -o program

program_no_reloc: program.o
$(CC) program.o -no-pie -o program_no_reloc

program_relocated: program_relocated.o
$(CC) program_relocated.o -o program_relocated

large_elf.o: large_elf.c $(HEADERS)
$(CC) -c large_elf.c -o large_elf.o

large_elf: large_elf.o
$(CC) large_elf.o -no-pie -o large_elf
Binary file added ofrak_core/pytest_ofrak/elf/assets/large_elf
Binary file not shown.
17 changes: 17 additions & 0 deletions ofrak_core/pytest_ofrak/elf/assets/large_elf.c

Large diffs are not rendered by default.

Binary file added ofrak_core/pytest_ofrak/elf/assets/large_elf.o
Binary file not shown.
Binary file added ofrak_core/pytest_ofrak/elf/assets/program
Binary file not shown.
18 changes: 18 additions & 0 deletions ofrak_core/pytest_ofrak/elf/assets/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include <stdio.h>

int foo();
int bar();

int main() {
printf("Hello, World!\n");
return foo();
}

int foo() {
return 12;
}

int bar() {
return 24;
}
Binary file added ofrak_core/pytest_ofrak/elf/assets/program.o
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions ofrak_core/pytest_ofrak/elf/assets/source_dir/patch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

int noop0();

int baz()
{
noop0();
return 36;
}
202 changes: 100 additions & 102 deletions ofrak_core/pytest_ofrak/elf/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,166 +1,164 @@
import os
import subprocess

# import subprocess

import pytest


MAKEFILE_CONTENTS = """
CC=gcc
# MAKEFILE_CONTENTS = """
# CC=gcc

# default: program

default: program
# program.o: program.c $(HEADERS)
# $(CC) -c program.c -fno-asynchronous-unwind-tables -o program.o

program.o: program.c $(HEADERS)
$(CC) -c program.c -fno-asynchronous-unwind-tables -o program.o
# program: program.o
# $(CC) program.o -o program

program: program.o
$(CC) program.o -o program
# program_no_reloc: program.o
# $(CC) program.o -no-pie -o program_no_reloc

program_no_reloc: program.o
$(CC) program.o -no-pie -o program_no_reloc
# program_relocated: program_relocated.o
# $(CC) program_relocated.o -o program_relocated

program_relocated: program_relocated.o
$(CC) program_relocated.o -o program_relocated
# large_elf.o: large_elf.c $(HEADERS)
# $(CC) -c large_elf.c -o large_elf.o

large_elf.o: large_elf.c $(HEADERS)
$(CC) -c large_elf.c -o large_elf.o
# large_elf: large_elf.o
# $(CC) large_elf.o -no-pie -o large_elf
# """

large_elf: large_elf.o
$(CC) large_elf.o -no-pie -o large_elf
"""
# C_SOURCE_CONTENTS = """
# #include <stdio.h>

C_SOURCE_CONTENTS = """
#include <stdio.h>
# int foo();
# int bar();

int foo();
int bar();
# int main() {
# printf("Hello, World!\\n");
# return foo();
# }

int main() {
printf("Hello, World!\\n");
return foo();
}
# int foo() {
# return 12;
# }

int foo() {
return 12;
}
# int bar() {
# return 24;
# }

int bar() {
return 24;
}

# """

"""
# LARGE_SOURCE_CONTENTS_HEADER = """
# int foo();
# int bar();

LARGE_SOURCE_CONTENTS_HEADER = """
int foo();
int bar();
# int main() {
# return bar();
# }

int main() {
return bar();
}
# """

"""
# LARGE_SOURCE_CONTENTS_FOOTER = """

LARGE_SOURCE_CONTENTS_FOOTER = """
# int foo() {
# return 12;
# }

int foo() {
return 12;
}
# int bar() {
# return 24;
# }

int bar() {
return 24;
}

# """

"""
# PATCH_CONTENTS = """
# int noop0();

PATCH_CONTENTS = """
int noop0();
# int baz()
# {
# noop0();
# return 36;
# }
# """

int baz()
{
noop0();
return 36;
}
"""

# def create_noops():
# noops = []

def create_noops():
noops = []
# i = 0

i = 0
# for i in range(8000):
# instruction = f"int noop{i}(){{}}"
# noops.append(instruction)

for i in range(8000):
instruction = f"int noop{i}(){{}}"
noops.append(instruction)
# return noops

return noops

@pytest.fixture(scope="session")
def elf_test_directory():
return os.path.join(os.path.dirname(__file__), "assets")

@pytest.fixture
def elf_test_directory(tmpdir):
makefile_path = os.path.join(tmpdir, "Makefile")
c_source_path = os.path.join(tmpdir, "program.c")
large_source_path = os.path.join(tmpdir, "large_elf.c")
# makefile_path = os.path.join(tmpdir, "Makefile")
# c_source_path = os.path.join(tmpdir, "program.c")
# large_source_path = os.path.join(tmpdir, "large_elf.c")

patch_dir = os.path.join(tmpdir, "source_dir")
if not os.path.exists(patch_dir):
os.mkdir(patch_dir)
patch_path = os.path.join(patch_dir, "patch.c")
# patch_dir = os.path.join(tmpdir, "source_dir")
# if not os.path.exists(patch_dir):
# os.mkdir(patch_dir)
# patch_path = os.path.join(patch_dir, "patch.c")

noops = create_noops()
# noops = create_noops()

LARGE_SOURCE_CONTENTS = LARGE_SOURCE_CONTENTS_HEADER
for noop in noops:
LARGE_SOURCE_CONTENTS += noop
LARGE_SOURCE_CONTENTS += LARGE_SOURCE_CONTENTS_FOOTER
# LARGE_SOURCE_CONTENTS = LARGE_SOURCE_CONTENTS_HEADER
# for noop in noops:
# LARGE_SOURCE_CONTENTS += noop
# LARGE_SOURCE_CONTENTS += LARGE_SOURCE_CONTENTS_FOOTER

with open(makefile_path, "w") as f:
f.write(MAKEFILE_CONTENTS)
with open(c_source_path, "w") as f:
f.write(C_SOURCE_CONTENTS)
with open(large_source_path, "w") as f:
f.write(LARGE_SOURCE_CONTENTS)
with open(patch_path, "w") as f:
f.write(PATCH_CONTENTS)
# with open(makefile_path, "w") as f:
# f.write(MAKEFILE_CONTENTS)
# with open(c_source_path, "w") as f:
# f.write(C_SOURCE_CONTENTS)
# with open(large_source_path, "w") as f:
# f.write(LARGE_SOURCE_CONTENTS)
# with open(patch_path, "w") as f:
# f.write(PATCH_CONTENTS)

return tmpdir
# return tmpdir


@pytest.fixture
def elf_object_file(elf_test_directory, skipif_windows):
subprocess.run(["make", "-C", elf_test_directory, "program.o"])
@pytest.fixture(scope="session")
def elf_object_file(elf_test_directory):
return os.path.join(elf_test_directory, "program.o")


@pytest.fixture
def elf_executable_file(elf_test_directory, skipif_windows):
subprocess.run(["make", "-C", elf_test_directory, "program"])
@pytest.fixture(scope="session")
def elf_executable_file(elf_test_directory):
return os.path.join(elf_test_directory, "program")


@pytest.fixture
def elf_no_pie_executable_file(elf_test_directory, skipif_windows):
subprocess.run(["make", "-C", elf_test_directory, "program_no_reloc"])
@pytest.fixture(scope="session")
def elf_no_pie_executable_file(elf_test_directory):
return os.path.join(elf_test_directory, "program_no_reloc")


@pytest.fixture
@pytest.fixture(scope="session")
def large_elf_source_file(elf_test_directory):
return os.path.join(elf_test_directory, "large_elf.c")


@pytest.fixture
def large_elf_object_file(elf_test_directory, skipif_windows):
subprocess.run(["make", "-C", elf_test_directory, "large_elf.o"])
@pytest.fixture(scope="session")
def large_elf_object_file(elf_test_directory):
return os.path.join(elf_test_directory, "large_elf.o")


@pytest.fixture
def large_elf_file(elf_test_directory, skipif_windows):
subprocess.run(["make", "-C", elf_test_directory, "large_elf"])
@pytest.fixture(scope="session")
def large_elf_file(elf_test_directory):
return os.path.join(elf_test_directory, "large_elf")


@pytest.fixture
@pytest.fixture(scope="session")
def patch_file(elf_test_directory):
return os.path.join(elf_test_directory, "source_dir", "patch.c")
7 changes: 0 additions & 7 deletions ofrak_core/pytest_ofrak/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import sys

import pytest

Expand Down Expand Up @@ -33,9 +32,3 @@ async def ofrak_context(ofrak):
context = await ofrak.create_ofrak_context()
yield context
await context.shutdown_context()


@pytest.fixture
def skipif_windows():
if sys.platform == "win32":
pytest.skip("Test cannot run on Windows.")

0 comments on commit 09ee52a

Please sign in to comment.