import sys, os, tempfile
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'tests'))
from security_framework import SecurityTestFramework
config = {
'tool_name': 'link',
'bin_name': 'flink',
'gnu_path': '/usr/bin/link',
'bss_size': 4096,
'max_binary_size': 30000,
'test_args': ['/tmp/_flink_test_src', '/tmp/_flink_test_dst'],
'test_stdin': None,
}
if not os.path.exists('/tmp/_flink_test_src'):
with open('/tmp/_flink_test_src', 'w') as f:
f.write('test')
def tool_specific_tests(fw):
fw.log("\n=== Tool-Specific: link ===")
with tempfile.TemporaryDirectory() as tmpdir:
src = os.path.join(tmpdir, "src")
dst = os.path.join(tmpdir, "dst")
with open(src, "w") as f:
f.write("hello world")
rc, out, err = fw.run_asm([src, dst])
fw.report_result(rc == 0, "link: basic hard link creation exits 0")
fw.report_result(out == b"", "link: no stdout on success")
fw.report_result(err == b"", "link: no stderr on success")
try:
src_stat = os.stat(src)
dst_stat = os.stat(dst)
fw.report_result(src_stat.st_ino == dst_stat.st_ino, "link: same inode (hard link)")
fw.report_result(src_stat.st_nlink >= 2, "link: link count >= 2")
with open(dst) as f:
fw.report_result(f.read() == "hello world", "link: content matches")
except FileNotFoundError:
fw.report_result(False, "link: destination created")
os.unlink(dst)
with open(dst, "w") as f:
f.write("existing")
rc, out, err = fw.run_asm([src, dst])
fw.report_result(rc == 1, "link: EEXIST exits 1")
fw.report_result(b"File exists" in err, "link: EEXIST error message")
os.unlink(dst)
rc, out, err = fw.run_asm([os.path.join(tmpdir, "nosuch"), dst])
fw.report_result(rc == 1, "link: ENOENT exits 1")
fw.report_result(b"No such file" in err, "link: ENOENT error message")
rc, out, err = fw.run_asm([])
fw.report_result(b"missing operand" in err, "link: missing operand message")
rc, out, err = fw.run_asm(['--help'])
fw.report_result(rc == 0, "link: --help exits 0")
fw.report_result(b"Usage:" in out, "link: --help contains Usage:")
rc, out, err = fw.run_asm(['--version'])
fw.report_result(rc == 0, "link: --version exits 0")
try:
os.unlink('/tmp/_flink_test_src')
os.unlink('/tmp/_flink_test_dst')
except OSError:
pass
if __name__ == '__main__':
fw = SecurityTestFramework(config)
fw.run_all(tool_specific_fn=tool_specific_tests)