Repository: mandiant/capa
File: tests/test_ghidra_features.py (Python)

Code before:

    return wanted_str


def check_input_file(wanted):
    """check that test is running on the loaded sample

    params: wanted - PosixPath() passed from test arg
    """

    import capa.ghidra.helpers as ghidra_helpers

    found = ghidra_helpers.get_file_md5()
    sample_name = standardize_posix_str(wanted)

    if not found.startswith(fixtures.get_sample_md5_by_name(sample_name)):
        raise RuntimeError(f"please run the tests against sample with MD5: `{found}`")


def get_ghidra_extractor(path):
    import capa.features.extractors.ghidra.extractor

    extractor = capa.features.extractors.ghidra.extractor.GhidraFeatureExtractor()
    setattr(extractor, "path", path.as_posix())

    return extractor


@pytest.mark.skipif(ghidra_present is False, reason="Ghidra tests must be ran within Ghidra")
@fixtures.parametrize("sample,scope,feature,expected", fixtures.FEATURE_PRESENCE_TESTS, indirect=["sample", "scope"])
def test_ghidra_features(sample, scope, feature, expected):
    try:
        check_input_file(sample)
    except RuntimeError:
        pytest.skip(reason="Test must be ran against sample loaded in Ghidra")

    fixtures.do_test_feature_presence(get_ghidra_extractor, sample, scope, feature, expected)


@pytest.mark.skipif(ghidra_present is False, reason="Ghidra tests must be ran within Ghidra")
@fixtures.parametrize("sample,scope,feature,expected", fixtures.FEATURE_COUNT_TESTS, indirect=["sample", "scope"])
def test_ghidra_feature_counts(sample, scope, feature, expected):
    try:
        check_input_file(sample)
    except RuntimeError:
        pytest.skip(reason="Test must be ran against sample loaded in Ghidra")

    fixtures.do_test_feature_count(get_ghidra_extractor, sample, scope, feature, expected)


if __name__ == "__main__":

Review comment (refactor):
Should this be located in `capa/tests/fixtures.py` alongside the other `get_xx_extractor` functions?

Code after:

    return wanted_str


def check_input_file(wanted):
    """check that test is running on the loaded sample

    params: wanted - PosixPath() passed from test arg
    """

    import capa.ghidra.helpers as ghidra_helpers

    found = ghidra_helpers.get_file_md5()
    sample_name = standardize_posix_str(wanted)

    if not found.startswith(fixtures.get_sample_md5_by_name(sample_name)):
        raise RuntimeError(f"please run the tests against sample with MD5: `{found}`")


@pytest.mark.skipif(ghidra_present is False, reason="Ghidra tests must be ran within Ghidra")
@fixtures.parametrize("sample,scope,feature,expected", fixtures.FEATURE_PRESENCE_TESTS, indirect=["sample", "scope"])
def test_ghidra_features(sample, scope, feature, expected):
    try:
        check_input_file(sample)
    except RuntimeError:
        pytest.skip(reason="Test must be ran against sample loaded in Ghidra")

    fixtures.do_test_feature_presence(fixtures.get_ghidra_extractor, sample, scope, feature, expected)


@pytest.mark.skipif(ghidra_present is False, reason="Ghidra tests must be ran within Ghidra")
@fixtures.parametrize("sample,scope,feature,expected", fixtures.FEATURE_COUNT_TESTS, indirect=["sample", "scope"])
def test_ghidra_feature_counts(sample, scope, feature, expected):
    try:
        check_input_file(sample)
    except RuntimeError:
        pytest.skip(reason="Test must be ran against sample loaded in Ghidra")

    fixtures.do_test_feature_count(fixtures.get_ghidra_extractor, sample, scope, feature, expected)


if __name__ == "__main__":
    # No support for faulthandler module in Ghidrathon, see:
    # https://github.com/mandiant/Ghidrathon/issues/70
    sys.exit(pytest.main(["--pyargs", "-p no:faulthandler", "test_ghidra_features"]))