diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f4933e5f..841044ee 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -155,7 +155,7 @@ jobs: with: python-version: ${{ matrix.python-version }} - name: Set up Java ${{ matrix.java-version }} - uses: actions/setup-java@v3 + uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2 # v3 with: distribution: 'temurin' java-version: ${{ matrix.java-version }} diff --git a/tests/fixtures.py b/tests/fixtures.py index 04c9c53b..612f49a7 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -183,6 +183,18 @@ def get_binja_extractor(path): return extractor +@lru_cache(maxsize=1) +def get_ghidra_extractor(path): + import capa.features.extractors.ghidra.extractor + + extractor = capa.features.extractors.ghidra.extractor.GhidraFeatureExtractor(path) + + # overload the extractor so that the fixture exposes `extractor.path` + setattr(extractor, "path", path) + + return extractor + + def extract_global_features(extractor): features = collections.defaultdict(set) for feature, va in extractor.extract_global_features(): diff --git a/tests/test_ghidra_features.py b/tests/test_ghidra_features.py index a4a9a748..ff8e6485 100644 --- a/tests/test_ghidra_features.py +++ b/tests/test_ghidra_features.py @@ -1,11 +1,21 @@ +import sys import logging +import os.path +import binascii +import traceback -import fixtures -from fixtures import * +import pytest -import capa.main +try: + sys.path.append(os.path.dirname(__file__)) + import fixtures + from fixtures import * +finally: + sys.path.pop() + + +logger = logging.getLogger("test_ghidra_features") -logger = logging.getLogger(__file__) # We need to skip the ghidra test if we cannot import ghidra modules, e.g., in GitHub CI. ghidra_present: bool = False @@ -23,3 +33,23 @@ except ImportError: pass +@pytest.mark.skipif(ghidra_present is False, reason="Skip ghidra tests if the ghidra Python API is not installed") +@fixtures.parametrize( + "sample,scope,feature,expected", + fixtures.FEATURE_PRESENCE_TESTS, + indirect=["sample", "scope"], +) +def test_ghidra_features(sample, scope, feature, expected): + fixtures.do_test_feature_presence(fixtures.get_ghidra_extractor, sample, scope, feature, expected) + + +@pytest.mark.skipif(ghidra_present is False, reason="Skip ghidra tests if the ghidra Python API is not installed") +@fixtures.parametrize( + "sample,scope,feature,expected", + fixtures.FEATURE_COUNT_TESTS, + indirect=["sample", "scope"], +) +def test_ghidra_feature_counts(sample, scope, feature, expected): + fixtures.do_test_feature_count(fixtures.get_ghidra_extractor, sample, scope, feature, expected) + +