Skip to content

Commit e294e69

Browse files
tests: inline more test logic
1 parent b4cf20b commit e294e69

4 files changed

Lines changed: 37 additions & 132 deletions

File tree

tests/fixtures.py

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
logger = logging.getLogger(__name__)
4545
CD = Path(__file__).resolve().parent
4646
FIXTURE_MANIFEST_DIR = CD / "fixtures" / "features"
47-
DOTNET_DIR = CD / "data" / "dotnet"
48-
DNFILE_TESTFILES = DOTNET_DIR / "dnfile-testfiles"
47+
DNFILE_TESTFILES = CD / "data" / "dotnet" / "dnfile-testfiles"
4948

5049

5150
def parse_feature_string(s: str) -> Feature | ceng.Range | ceng.Statement:
@@ -366,44 +365,6 @@ def run_feature_fixture(
366365
assert actual == fixture.expected, msg
367366

368367

369-
@contextlib.contextmanager
370-
def xfail(condition, reason: str = ""):
371-
"""
372-
context manager that wraps a block that is expected to fail in some cases.
373-
when it does fail (and is expected), then mark this as pytest.xfail.
374-
if its unexpected, raise an exception, so the test fails.
375-
376-
example::
377-
378-
# this test:
379-
# - passes on Linux if foo() works
380-
# - fails on Linux if foo() fails
381-
# - xfails on Windows if foo() fails
382-
# - fails on Windows if foo() works
383-
with xfail(sys.platform == "win32", reason="doesn't work on Windows"):
384-
foo()
385-
"""
386-
try:
387-
# do the block
388-
yield
389-
except Exception:
390-
if condition:
391-
# we expected the test to fail, so raise and register this via pytest
392-
pytest.xfail(reason or "")
393-
else:
394-
# we don't expect an exception, so the test should fail
395-
raise
396-
else:
397-
if not condition:
398-
# here we expect the block to run successfully,
399-
# and we've received no exception,
400-
# so this is good
401-
pass
402-
else:
403-
# we expected an exception, but didn't find one. that's an error.
404-
raise RuntimeError("expected to fail, but didn't")
405-
406-
407368
def extract_global_features(extractor):
408369
features = collections.defaultdict(set)
409370
for feature, va in extractor.extract_global_features():
@@ -671,11 +632,6 @@ def inner_process(extractor):
671632
raise ValueError("unexpected scope fixture")
672633

673634

674-
@pytest.fixture
675-
def scope(request):
676-
return resolve_scope(request.param)
677-
678-
679635
def make_test_id(values):
680636
return "-".join(map(str, values))
681637

@@ -692,58 +648,7 @@ def parametrize(params, values, **kwargs):
692648
return pytest.mark.parametrize(params, values, ids=ids, **kwargs)
693649

694650

695-
def get_result_doc(path: Path):
696-
return capa.render.result_document.ResultDocument.from_file(path)
697-
698-
699-
@pytest.fixture
700-
def pma0101_rd():
701-
# python -m capa.main tests/data/Practical\ Malware\ Analysis\ Lab\ 01-01.dll_ --json > tests/data/rd/Practical\ Malware\ Analysis\ Lab\ 01-01.dll_.json
702-
return get_result_doc(CD / "data" / "rd" / "Practical Malware Analysis Lab 01-01.dll_.json")
703-
704-
705-
@pytest.fixture
706-
def dotnet_1c444e_rd():
707-
# .NET sample
708-
# python -m capa.main tests/data/dotnet/1c444ebeba24dcba8628b7dfe5fec7c6.exe_ --json > tests/data/rd/1c444ebeba24dcba8628b7dfe5fec7c6.exe_.json
709-
return get_result_doc(CD / "data" / "rd" / "1c444ebeba24dcba8628b7dfe5fec7c6.exe_.json")
710-
711-
712-
@pytest.fixture
713-
def a3f3bbc_rd():
714-
# python -m capa.main tests/data/3f3bbcf8fd90bdcdcdc5494314ed4225.exe_ --json > tests/data/rd/3f3bbcf8fd90bdcdcdc5494314ed4225.exe_.json
715-
return get_result_doc(CD / "data" / "rd" / "3f3bbcf8fd90bdcdcdc5494314ed4225.exe_.json")
716-
717-
718-
@pytest.fixture
719-
def al_khaserx86_rd():
720-
# python -m capa.main tests/data/al-khaser_x86.exe_ --json > tests/data/rd/al-khaser_x86.exe_.json
721-
return get_result_doc(CD / "data" / "rd" / "al-khaser_x86.exe_.json")
722-
723-
724-
@pytest.fixture
725-
def al_khaserx64_rd():
726-
# python -m capa.main tests/data/al-khaser_x64.exe_ --json > tests/data/rd/al-khaser_x64.exe_.json
727-
return get_result_doc(CD / "data" / "rd" / "al-khaser_x64.exe_.json")
728-
729-
730-
@pytest.fixture
731-
def a076114_rd():
732-
# python -m capa.main tests/data/0761142efbda6c4b1e801223de723578.dll_ --json > tests/data/rd/0761142efbda6c4b1e801223de723578.dll_.json
733-
return get_result_doc(CD / "data" / "rd" / "0761142efbda6c4b1e801223de723578.dll_.json")
734-
735-
736-
@pytest.fixture
737-
def dynamic_a0000a6_rd():
738-
# python -m capa.main tests/data/dynamic/cape/v2.2/0000a65749f5902c4d82ffa701198038f0b4870b00a27cfca109f8f933476d82.json --json > tests/data/rd/0000a65749f5902c4d82ffa701198038f0b4870b00a27cfca109f8f933476d82.json
739-
# gzip tests/data/rd/0000a65749f5902c4d82ffa701198038f0b4870b00a27cfca109f8f933476d82.json
740-
return get_result_doc(
741-
CD / "data" / "rd" / "0000a65749f5902c4d82ffa701198038f0b4870b00a27cfca109f8f933476d82.json.gz"
742-
)
743-
744-
745651
PMA1601 = CD / "data" / "Practical Malware Analysis Lab 16-01.exe_"
746-
z9324 = CD / "data" / "9324d1a8ae37a36ae560c37448c9705a.exe_"
747652

748653

749654
# used by test_viv_features
@@ -786,7 +691,7 @@ def get_viv_extractor(path: Path):
786691

787692
@pytest.fixture
788693
def z9324d_extractor():
789-
return get_viv_extractor(z9324)
694+
return get_viv_extractor(CD / "data" / "9324d1a8ae37a36ae560c37448c9705a.exe_")
790695

791696

792697
@pytest.fixture

tests/test_capabilities.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
import textwrap
1616

17-
import capa.rules
18-
import capa.features.common
1917
import fixtures
2018

19+
import capa.rules
20+
import capa.features.common
2121
import capa.capabilities.common
2222
import capa.features.extractors.null
2323
from capa.features.address import AbsoluteVirtualAddress

tests/test_proto.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import copy
1616
from typing import Any
17+
from pathlib import Path
1718

1819
import pytest
1920

@@ -28,20 +29,21 @@
2829
import capa.features.freeze.features
2930
from capa.helpers import assert_never
3031

32+
CD = Path(__file__).resolve().parent
3133

32-
@pytest.mark.parametrize(
33-
"rd_file",
34-
[
35-
pytest.param("a3f3bbc_rd"),
36-
pytest.param("al_khaserx86_rd"),
37-
pytest.param("al_khaserx64_rd"),
38-
pytest.param("a076114_rd"),
39-
pytest.param("pma0101_rd"),
40-
pytest.param("dotnet_1c444e_rd"),
41-
],
42-
)
43-
def test_doc_to_pb2(request, rd_file):
44-
src: rd.ResultDocument = request.getfixturevalue(rd_file)
34+
STATIC_RD_FILES = [
35+
pytest.param(CD / "data" / "rd" / "3f3bbcf8fd90bdcdcdc5494314ed4225.exe_.json", id="a3f3bbc"),
36+
pytest.param(CD / "data" / "rd" / "al-khaser_x86.exe_.json", id="al_khaserx86"),
37+
pytest.param(CD / "data" / "rd" / "al-khaser_x64.exe_.json", id="al_khaserx64"),
38+
pytest.param(CD / "data" / "rd" / "0761142efbda6c4b1e801223de723578.dll_.json", id="a076114"),
39+
pytest.param(CD / "data" / "rd" / "Practical Malware Analysis Lab 01-01.dll_.json", id="pma0101"),
40+
pytest.param(CD / "data" / "rd" / "1c444ebeba24dcba8628b7dfe5fec7c6.exe_.json", id="dotnet_1c444e"),
41+
]
42+
43+
44+
@pytest.mark.parametrize("rd_path", STATIC_RD_FILES)
45+
def test_doc_to_pb2(rd_path):
46+
src = rd.ResultDocument.from_file(rd_path)
4547
dst = capa.render.proto.doc_to_pb2(src)
4648

4749
assert_meta(src.meta, dst.meta)
@@ -401,17 +403,15 @@ def assert_round_trip(doc: rd.ResultDocument):
401403

402404

403405
@pytest.mark.parametrize(
404-
"rd_file",
405-
[
406-
pytest.param("a3f3bbc_rd"),
407-
pytest.param("al_khaserx86_rd"),
408-
pytest.param("al_khaserx64_rd"),
409-
pytest.param("a076114_rd"),
410-
pytest.param("pma0101_rd"),
411-
pytest.param("dotnet_1c444e_rd"),
412-
pytest.param("dynamic_a0000a6_rd"),
406+
"rd_path",
407+
STATIC_RD_FILES
408+
+ [
409+
pytest.param(
410+
CD / "data" / "rd" / "0000a65749f5902c4d82ffa701198038f0b4870b00a27cfca109f8f933476d82.json.gz",
411+
id="dynamic_a0000a6",
412+
),
413413
],
414414
)
415-
def test_round_trip(request, rd_file):
416-
doc: rd.ResultDocument = request.getfixturevalue(rd_file)
415+
def test_round_trip(rd_path):
416+
doc = rd.ResultDocument.from_file(rd_path)
417417
assert_round_trip(doc)

tests/test_result_document.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,18 @@ def assert_round_trip(rd: rdoc.ResultDocument):
268268

269269

270270
@pytest.mark.parametrize(
271-
"rd_file",
271+
"rd_path",
272272
[
273-
pytest.param("a3f3bbc_rd"),
274-
pytest.param("al_khaserx86_rd"),
275-
pytest.param("al_khaserx64_rd"),
276-
pytest.param("a076114_rd"),
277-
pytest.param("pma0101_rd"),
278-
pytest.param("dotnet_1c444e_rd"),
273+
pytest.param(fixtures.CD / "data" / "rd" / "3f3bbcf8fd90bdcdcdc5494314ed4225.exe_.json", id="a3f3bbc"),
274+
pytest.param(fixtures.CD / "data" / "rd" / "al-khaser_x86.exe_.json", id="al_khaserx86"),
275+
pytest.param(fixtures.CD / "data" / "rd" / "al-khaser_x64.exe_.json", id="al_khaserx64"),
276+
pytest.param(fixtures.CD / "data" / "rd" / "0761142efbda6c4b1e801223de723578.dll_.json", id="a076114"),
277+
pytest.param(fixtures.CD / "data" / "rd" / "Practical Malware Analysis Lab 01-01.dll_.json", id="pma0101"),
278+
pytest.param(fixtures.CD / "data" / "rd" / "1c444ebeba24dcba8628b7dfe5fec7c6.exe_.json", id="dotnet_1c444e"),
279279
],
280280
)
281-
def test_round_trip(request, rd_file):
282-
rd: rdoc.ResultDocument = request.getfixturevalue(rd_file)
281+
def test_round_trip(rd_path):
282+
rd = rdoc.ResultDocument.from_file(rd_path)
283283
assert_round_trip(rd)
284284

285285

0 commit comments

Comments
 (0)