Exemple #1
0
def mimetype_filter_scan_request(scan_request_dict):
    try:
        with session_transaction() as session:
            scan_request = IrmaScanRequest(scan_request_dict)
            for file in scan_request.files():
                filtered_probelist = []
                mimetype = scan_request.get_mimetype(file)
                mimetype_probelist = probe_ctrl.mimetype_probelist(mimetype,
                                                                   session)
                probe_list = scan_request.get_probelist(file)
                log.debug("file: %s probe_list: %s",
                          file, "-".join(probe_list))
                # first check probe_list for unknown probe
                for probe in probe_list:
                    # check if probe exists
                    if probe not in probe_ctrl.available_probes:
                        log.warning("probe %s not available", probe)
                    if probe in mimetype_probelist:
                        # probe is asked and supported by mimetype
                        log.debug("file %s probe %s asked" +
                                  " and supported for %s append to request",
                                  file, probe, mimetype)
                        filtered_probelist.append(probe)
                # update probe list in scan request
                scan_request.set_probelist(file, filtered_probelist)
            return IrmaTaskReturn.success(scan_request.to_dict())
    except Exception as e:
        log.exception(e)
        return IrmaTaskReturn.error("Brain error")
Exemple #2
0
def mimetype_filter_scan_request(scan_request_dict):
    try:
        with session_transaction() as session:
            scan_request = IrmaScanRequest(scan_request_dict)
            for file in scan_request.files():
                filtered_probelist = []
                mimetype = scan_request.get_mimetype(file)
                mimetype_probelist = probe_ctrl.mimetype_probelist(
                    mimetype, session)
                probe_list = scan_request.get_probelist(file)
                log.debug("file: %s probe_list: %s", file,
                          "-".join(probe_list))
                # first check probe_list for unknown probe
                for probe in probe_list:
                    # check if probe exists
                    if probe not in probe_ctrl.available_probes:
                        log.warning("probe %s not available", probe)
                    if probe in mimetype_probelist:
                        # probe is asked and supported by mimetype
                        log.debug(
                            "file %s probe %s asked" +
                            " and supported for %s append to request", file,
                            probe, mimetype)
                        filtered_probelist.append(probe)
                # update probe list in scan request
                scan_request.set_probelist(file, filtered_probelist)
            return IrmaTaskReturn.success(scan_request.to_dict())
    except Exception as e:
        log.exception(type(e).__name__ + " : " + str(e))
        return IrmaTaskReturn.error("Brain error")
Exemple #3
0
class TestIrmaScanRequest(unittest.TestCase):
    def setUp(self):
        self.isr = IrmaScanRequest()

    def test_init(self):
        isr = IrmaScanRequest({"foo": Mock(), "bar": Mock()})
        self.assertEqual(isr.nb_files, 2)

    def test_add_file(self):
        self.isr.add_file("foo", "probelist", "mimetype")
        self.assertDictEqual(self.isr.request["foo"], {
            "probe_list": "probelist",
            "mimetype": "mimetype"
        })
        self.assertEqual(self.isr.nb_files, 1)

    def test_del_file0(self):
        self.isr.add_file("foo", "probelist", "mimetype")
        self.isr.del_file("foo")
        self.assertNotIn("foo", self.isr.request)
        self.assertEqual(self.isr.nb_files, 0)

    def test_del_file1(self):
        self.isr.del_file("foo")
        self.assertNotIn("foo", self.isr.request)
        self.assertEqual(self.isr.nb_files, 0)

    def test_get_probelist(self):
        self.isr.add_file("foo", "bar", "mimetype")
        result = self.isr.get_probelist("foo")
        self.assertEqual(result, "bar")
        self.assertEqual(self.isr.nb_files, 1)

    def test_set_probelist(self):
        self.isr.add_file("foo", "bar", "mimetype")
        self.isr.set_probelist("foo", "baz")
        self.assertEqual(self.isr.get_probelist("foo"), "baz")
        self.assertEqual(self.isr.nb_files, 1)

    def test_get_mimetype(self):
        self.isr.add_file("foo", "probelist", "bar")
        result = self.isr.get_mimetype("foo")
        self.assertEqual(result, "bar")
        self.assertEqual(self.isr.nb_files, 1)

    def test_to_dict(self):
        self.assertIs(self.isr.request, self.isr.to_dict())

    def test_files(self):
        self.assertEqual(self.isr.request.keys(), self.isr.files())