diff --git a/CHANGELOG.md b/CHANGELOG.md index 70b2505..c26aa48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,9 @@ Ninja applies to that plugin only. ### Fixed +- Get Match Result missed the jobs in which the sample is not the first argument, such as a + sample-vs-sample comparison started from the other sample. Jobs are now selected by their sample + id arguments instead of by matching the job's text. - The bundled MCRIT client's `modifyFamily`, `modifySample`, `requestMatchesForMappedBinary` and `requestMatchesForUnmappedBinary` raised `TypeError` on every call: they passed the request body positionally to the client's request wrappers, which take it only by keyword. diff --git a/mcrit_plugin/core/McritInterface.py b/mcrit_plugin/core/McritInterface.py index 8329fbf..cd6bdc3 100644 --- a/mcrit_plugin/core/McritInterface.py +++ b/mcrit_plugin/core/McritInterface.py @@ -15,6 +15,22 @@ _SMDA_IMPORT_ERROR = None +def _isMatchingJobOfSample(job, sample_id): + """Whether a matching job compares the sample, read from the job's sample id arguments like the + server's sample_ids selector; its thresholds are arguments too and must not count.""" + # a cross compare's combined result is no matching report, so the result chooser cannot load it + if "Matches" not in (job.method or "") or job.method == "combineMatchesToCross": + return False + try: + # the per-sample jobs of a cross compare; Job.sample_ids does not know them + if job.method == "getMatchesForSampleVsGroup": + return int(job.arguments[0]) == sample_id + return sample_id in job.sample_ids + except (TypeError, ValueError, IndexError, KeyError, AttributeError): + # a job whose arguments do not have the shape its method has concerns no sample + return False + + class McritInterface(object): def __init__(self, parent, backend): if Disassembler is None: @@ -201,17 +217,7 @@ def queryJobs(self, sample_id=None): return None # check if we already have a match report for the sample id if sample_id is not None: - jobs = [ - job - for job in jobs - if "Matches" in job.parameters - and ( - "(" + str(sample_id) + ")" in job.parameters - or "(" + str(sample_id) + "," in job.parameters - or "," + str(sample_id) + "," in job.parameters - or "," + str(sample_id) + ")" in job.parameters - ) - ] + jobs = [job for job in jobs if _isMatchingJobOfSample(job, sample_id)] if jobs: self.parent.local_widget.updateActivityInfo("Success! Fetched Jobs.") else: diff --git a/tests/core/test_mcrit_interface.py b/tests/core/test_mcrit_interface.py index c893154..d29a60d 100644 --- a/tests/core/test_mcrit_interface.py +++ b/tests/core/test_mcrit_interface.py @@ -1,5 +1,6 @@ """Tests for the McritInterface logic that runs without a disassembler or an MCRIT server.""" +import json import threading from types import SimpleNamespace from unittest.mock import MagicMock @@ -8,6 +9,7 @@ import requests from mcrit_plugin.core.McritInterface import McritInterface +from mcrit_plugin.core.minimcrit.queue.LocalQueue import Job class _FakeBinaryInfo: @@ -159,6 +161,13 @@ def test_leaves_a_dict_the_ui_is_iterating_unchanged(self): assert interface.parent.matched_function_entries == {1: old_entry, 7: old_entry} +def _job(method, *arguments, **named): + """A queue Job as the server stores it: the sample ids by position, the knobs by name.""" + params = {str(index): argument for index, argument in enumerate(arguments)} + params.update(named) + return Job({"payload": {"method": method, "params": json.dumps(params)}}, None) + + class TestServerErrors: def test_a_rejected_upload_reports_failure_without_a_traceback(self, capsys): interface = _make_interface() @@ -178,20 +187,52 @@ def test_a_rejected_job_query_reports_failure(self): def test_jobs_of_a_sample_are_narrowed_by_the_server_and_checked_exactly(self): interface = _make_interface() - - def job(parameters): - return SimpleNamespace(parameters=parameters) - - wanted = job("getMatchesForSample(23, 2)") + wanted = _job("getMatchesForSample", 23, 2) interface.mcrit_client.getQueueData.return_value = [ wanted, - job("getMatchesForSample(230, 2)"), - job("updateMinHashesForSample(23)"), + _job("getMatchesForSample", 230, 2), + _job("updateMinHashesForSample", 23), ] assert interface.queryJobs(sample_id=23) == [wanted] interface.mcrit_client.getQueueData.assert_called_once_with(filter="23") + def test_a_sample_compared_second_finds_the_comparison(self): + interface = _make_interface() + versus = _job("getMatchesForSampleVs", 5, 12, minhash_threshold=50) + interface.mcrit_client.getQueueData.return_value = [versus] + + assert interface.queryJobs(sample_id=5) == [versus] + assert interface.queryJobs(sample_id=12) == [versus] + + def test_a_threshold_equal_to_the_sample_id_does_not_count(self): + interface = _make_interface() + interface.mcrit_client.getQueueData.return_value = [ + _job("getMatchesForSample", 0, minhash_threshold=50, pichash_size=10, shortlist_size=50) + ] + + assert interface.queryJobs(sample_id=50) == [] + assert interface.queryJobs(sample_id=10) == [] + + def test_a_cross_compare_offers_its_per_sample_jobs_not_its_combined_result(self): + interface = _make_interface() + cross = _job("combineMatchesToCross", {"3": "job-a", "7": "job-b"}) + per_sample = _job("getMatchesForSampleVsGroup", 3, [7]) + interface.mcrit_client.getQueueData.return_value = [cross, per_sample] + + assert interface.queryJobs(sample_id=3) == [per_sample] + assert interface.queryJobs(sample_id=7) == [] + + def test_a_job_with_arguments_of_another_shape_is_skipped(self): + interface = _make_interface() + wanted = _job("getMatchesForSample", 4) + interface.mcrit_client.getQueueData.return_value = [ + _job("getMatchesForSample", "not a sample id"), + wanted, + ] + + assert interface.queryJobs(sample_id=4) == [wanted] + class TestRemoteInformation: def test_the_lists_are_downloaded_once_by_whoever_asks_first(self):