import os
import sys
import tempfile
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'tests'))
from security_framework import SecurityTestFramework
from pathlib import Path
from shutil import which
TMPDIR = None
def setup_fixtures():
global TMPDIR
TMPDIR = tempfile.mkdtemp(prefix="frealpath_test_")
os.makedirs(f"{TMPDIR}/a/b", exist_ok=True)
Path(f"{TMPDIR}/realfile").touch()
Path(f"{TMPDIR}/a/b/deepfile").touch()
os.symlink(f"{TMPDIR}/realfile", f"{TMPDIR}/symlink")
os.symlink("realfile", f"{TMPDIR}/relsym")
os.symlink("nonexistent", f"{TMPDIR}/brokensym")
def cleanup_fixtures():
if TMPDIR:
import shutil
shutil.rmtree(TMPDIR, ignore_errors=True)
def tool_specific_tests(fw):
fw.log("\n=== 13. Tool-Specific: realpath ===")
gnu_path = which("realpath")
rc, out, _ = fw.run_asm([f"{TMPDIR}/realfile"])
fw.report_result(rc == 0 and out.strip() == f"{TMPDIR}/realfile".encode(),
"realpath: resolves regular file")
rc, out, _ = fw.run_asm([f"{TMPDIR}/symlink"])
fw.report_result(rc == 0 and out.strip() == f"{TMPDIR}/realfile".encode(),
"realpath: resolves symlink")
rc, out, _ = fw.run_asm([f"{TMPDIR}/a/.."])
fw.report_result(rc == 0 and out.strip() == TMPDIR.encode(),
"realpath: resolves dotdot")
rc, out, _ = fw.run_asm(["/"])
fw.report_result(out.strip() == b"/", "realpath: root resolves to /")
rc, _, _ = fw.run_asm(["-e", f"{TMPDIR}/nonexistent"])
fw.report_result(rc == 1, "realpath: -e nonexistent exits 1")
rc, out, _ = fw.run_asm(["-m", f"{TMPDIR}/nosuch/deep/path"])
fw.report_result(rc == 0 and len(out) > 0,
"realpath: -m nonexistent produces output")
rc, out, _ = fw.run_asm(["-s", f"{TMPDIR}/a/.."])
fw.report_result(rc == 0 and out.strip() == TMPDIR.encode(),
"realpath: -s resolves dotdot without symlinks")
rc, out, _ = fw.run_asm([f"{TMPDIR}/realfile", f"{TMPDIR}/a"])
lines = out.strip().split(b"\n")
fw.report_result(len(lines) == 2, "realpath: multiple files produce multiple lines")
if gnu_path:
for path in [f"{TMPDIR}/realfile", f"{TMPDIR}/symlink",
f"{TMPDIR}/a/b/deepfile", "/"]:
rc_f, out_f, _ = fw.run_asm([path])
rc_g, out_g, _ = fw.run([gnu_path, path])
fw.report_result(out_f == out_g,
f"realpath: matches GNU for {os.path.basename(path) or '/'}")
config = {
'tool_name': 'realpath',
'bin_name': 'frealpath',
'gnu_path': '/usr/bin/realpath',
'bss_size': 65536,
'max_binary_size': 30000,
'test_args': ['/etc/hosts'],
'test_stdin': None,
'timeout': 5,
}
if __name__ == '__main__':
setup_fixtures()
try:
fw = SecurityTestFramework(config)
fw.run_all(tool_specific_fn=tool_specific_tests)
finally:
cleanup_fixtures()