Skip to content

Commit 5abed8f

Browse files
authored
Merge pull request #1129 from onekey-sec/sandbox-handlers-tests
fix(tests): run handlers integration tests in landlock sandbox
2 parents 9cc53d4 + 46a2f85 commit 5abed8f

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ paths = ["python/", "vulture_whitelist.py"]
178178

179179
[tool.pyright]
180180
exclude = [
181+
".devenv",
181182
".venv",
182183
"build",
183184
]

python/unblob/testing.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import binascii
22
import glob
33
import io
4-
import platform
54
import shlex
65
import subprocess
76
from pathlib import Path
@@ -17,7 +16,6 @@
1716
from unblob.models import ProcessResult
1817
from unblob.processing import ExtractionConfig
1918
from unblob.report import ExtractCommandFailedReport
20-
from unblob.sandbox import AccessFS, SandboxError, restrict_access
2119

2220

2321
@pytest.fixture(scope="session", autouse=True)
@@ -219,17 +217,3 @@ def start(self, s):
219217
rv.write(line.data)
220218

221219
return rv.getvalue()
222-
223-
224-
def is_sandbox_available():
225-
is_sandbox_available = True
226-
227-
try:
228-
restrict_access(AccessFS.read_write("/"))
229-
except SandboxError:
230-
is_sandbox_available = False
231-
232-
if platform.architecture == "x86_64" and platform.system == "linux":
233-
assert is_sandbox_available, "Sandboxing should work at least on Linux-x86_64"
234-
235-
return is_sandbox_available

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from click.testing import CliRunner
88

99
import unblob.cli
10+
from rust.test_sandbox import landlock_supported
1011
from unblob.extractors import Command
1112
from unblob.extractors.command import MultiFileCommand
1213
from unblob.handlers import BUILTIN_HANDLERS
@@ -18,7 +19,6 @@
1819
DEFAULT_SKIP_MAGIC,
1920
ExtractionConfig,
2021
)
21-
from unblob.testing import is_sandbox_available
2222
from unblob.ui import (
2323
NullProgressReporter,
2424
ProgressReporter,
@@ -431,7 +431,7 @@ def test_clear_skip_magics(
431431

432432

433433
@pytest.mark.skipif(
434-
not is_sandbox_available(), reason="Sandboxing is only available on Linux"
434+
not landlock_supported(), reason="Sandboxing is only available on Linux"
435435
)
436436
def test_sandbox_escape(tmp_path: Path):
437437
runner = CliRunner()

tests/test_handlers.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unblob import handlers
1616
from unblob.models import Handler
1717
from unblob.processing import ExtractionConfig, process_file
18+
from unblob.sandbox import AccessFS, Sandbox
1819
from unblob.testing import (
1920
check_output_is_the_same,
2021
check_result,
@@ -29,10 +30,28 @@
2930
"input_dir, output_dir", gather_integration_tests(TEST_DATA_PATH)
3031
)
3132
def test_all_handlers(
32-
input_dir: Path, output_dir: Path, extraction_config: ExtractionConfig
33+
input_dir: Path,
34+
output_dir: Path,
35+
extraction_config: ExtractionConfig,
36+
request: pytest.FixtureRequest,
3337
):
38+
log_path = Path("/dev/null") # no logging
39+
report_file = None # no reporting
40+
41+
passthrough = [
42+
# .pytest_cache
43+
AccessFS.read_write(request.config.rootpath),
44+
]
45+
junit_xmlpath = request.config.getvalue("xmlpath")
46+
if junit_xmlpath:
47+
passthrough += [
48+
# junit reports are written to the argument of --junit-xml
49+
AccessFS.read_write(junit_xmlpath) # type: ignore
50+
]
51+
52+
sandbox = Sandbox(extraction_config, log_path, report_file, passthrough)
3453
for input_file in input_dir.iterdir():
35-
reports = process_file(extraction_config, input_file)
54+
reports = sandbox.run(process_file, extraction_config, input_file)
3655
check_result(reports)
3756

3857
check_output_is_the_same(output_dir, extraction_config.extract_root)

tests/test_sandbox.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import pytest
44

5+
from rust.test_sandbox import landlock_supported
56
from unblob.processing import ExtractionConfig
67
from unblob.sandbox import Sandbox
7-
from unblob.testing import is_sandbox_available
88

99
pytestmark = pytest.mark.skipif(
10-
not is_sandbox_available(), reason="Sandboxing only works on Linux"
10+
not landlock_supported(), reason="Sandboxing only works on Linux"
1111
)
1212

1313

0 commit comments

Comments
 (0)