| 1 |
from pathlib import Path |
| 2 |
import subprocess |
| 3 |
import os |
| 4 |
from pytest import skip |
| 5 |
from types import SimpleNamespace |
| 6 |
import platform |
| 7 |
|
| 8 |
PROJ_DIR = Path(__file__).parent.parent |
| 9 |
TEST_DIR = PROJ_DIR / 'test' |
| 10 |
TXT_DIR = TEST_DIR / 'text' |
| 11 |
BPF_DIR = TEST_DIR / 'bpf' |
| 12 |
EMU_DIR = TEST_DIR / 'emu_result' |
| 13 |
BE_DIR = TEST_DIR / 'big_endian_cases' |
| 14 |
ERR_CASE_DIR = TEST_DIR / 'errors' |
| 15 |
CECCOMP = str(PROJ_DIR / 'build' / 'ceccomp') |
| 16 |
FILENAMES = sorted([p.stem for p in TXT_DIR.iterdir()]) |
| 17 |
|
| 18 |
COMMON_OPTS = ['-c', 'always', '-a', 'x86_64'] |
| 19 |
|
| 20 |
def run_process( |
| 21 |
argv: list[str], is_binary: bool=False, extra_fd: int | None=None, |
| 22 |
stdin: str | bytes | None=None, |
| 23 |
) -> tuple[int, str | bytes, str | bytes]: |
| 24 |
if extra_fd is None: |
| 25 |
result = subprocess.run(argv, timeout=5, capture_output=True, |
| 26 |
text=not is_binary, input=stdin) |
| 27 |
else: |
| 28 |
result = subprocess.run(argv, timeout=5, capture_output=True, |
| 29 |
text=not is_binary, pass_fds=(extra_fd, ), input=stdin) |
| 30 |
return result.returncode, result.stdout, result.stderr |
| 31 |
|
| 32 |
_, _verstr, _ = run_process(['pkg-config', '--modversion', 'libseccomp'], False) |
| 33 |
SKIP_CHROMIUM = tuple(_verstr.split('.')) < ('2', '5', '6') |
| 34 |
SKIP_REASON = 'libseccomp too old (<2.5.6)' |
| 35 |
def maybe_skip(filename: str): |
| 36 |
if SKIP_CHROMIUM and filename == 'chromium': |
| 37 |
skip(SKIP_REASON) |
| 38 |
|
| 39 |
def filter2text(filters: bytes) -> str: |
| 40 |
length = len(filters) # leftover (less than 8 bytes) will be discarded |
| 41 |
return '\n'.join(filters[i:i + 8].hex(' ') for i in range(0, length, 8)) |
| 42 |
|
| 43 |
os.environ['LC_ALL'] = 'C' |
| 44 |
|
| 45 |
TIER_1_ARCH = [ # tested |
| 46 |
'x86_64', 'i386', 'i686', 'riscv64', 'loongarch64', 'aarch64', |
| 47 |
'ppc', 'ppc64le', 's390x', 'arm', 'armv8l', 'armv7l', |
| 48 |
] |
| 49 |
TIER_2_ARCH = [ # untested, but listed in libseccomp |
| 50 |
'x32', 'parisc', 'parisc64', 'mips', 'm68k', 's390', 'ppc64', |
| 51 |
] |
| 52 |
XFAIL_DYNAMIC = platform.machine() not in TIER_1_ARCH \ |
| 53 |
or (platform.machine() == 'x86_64' and platform.architecture()[0] == '32bit') |
| 54 |
XFAIL_REASON = 'Dynamic test may fail on this unsupported platform' |
| 55 |
|
| 56 |
STANDARD_HELP = run_process([CECCOMP, 'help'])[1] |