Skip to content

Commit

Permalink
Use create_subprocess_exec with arguments instead of shell for CPIO test
Browse files Browse the repository at this point in the history
  • Loading branch information
alchzh committed Dec 13, 2024
1 parent 2b11bc4 commit d5817f7
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions ofrak_core/test_ofrak/components/test_cpio_component.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os
import subprocess
import tempfile312 as tempfile
Expand Down Expand Up @@ -26,9 +27,20 @@ async def create_root_resource(self, ofrak_context: OFRAKContext) -> Resource:
# Create a CPIO file from the current directory
with open(CPIO_ENTRY_NAME, "wb") as f:
f.write(INITIAL_DATA)
command = f"find {CPIO_ENTRY_NAME} -print | cpio -o > {TARGET_CPIO_FILE}"
subprocess.run(command, check=True, capture_output=True, shell=True)
result = await ofrak_context.create_root_resource_from_file(TARGET_CPIO_FILE)
cmd = ["cpio", "-o"]
proc = await asyncio.create_subprocess_exec(
*cmd,
cwd=tmpdir,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate(CPIO_ENTRY_NAME.encode())
if proc.returncode:
raise subprocess.CalledProcessError(
returncode=proc.returncode, cmd=cmd, stdout=stdout, stderr=stderr
)
result = await ofrak_context.create_root_resource(name=TARGET_CPIO_FILE, data=stdout)

os.chdir(wd)
return result
Expand All @@ -46,10 +58,20 @@ async def repack(self, cpio_resource: Resource) -> None:
await cpio_resource.pack_recursively()

async def verify(self, repacked_cpio_resource: Resource) -> None:
async with repacked_cpio_resource.temp_to_disk() as temp_path:
with tempfile.TemporaryDirectory() as temp_flush_dir:
command = f"(cd {temp_flush_dir} && cpio -id < {temp_path})"
subprocess.run(command, check=True, capture_output=True, shell=True)
with open(os.path.join(temp_flush_dir, CPIO_ENTRY_NAME), "rb") as f:
patched_data = f.read()
assert patched_data == EXPECTED_DATA
with tempfile.TemporaryDirectory() as temp_flush_dir:
cmd = ["cpio", "-id"]
proc = await asyncio.create_subprocess_exec(
*cmd,
cwd=temp_flush_dir,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate(await repacked_cpio_resource.get_data())
if proc.returncode:
raise subprocess.CalledProcessError(
returncode=proc.returncode, cmd=cmd, stdout=stdout, stderr=stderr
)
with open(os.path.join(temp_flush_dir, CPIO_ENTRY_NAME), "rb") as f:
patched_data = f.read()
assert patched_data == EXPECTED_DATA

0 comments on commit d5817f7

Please sign in to comment.