def scan_environment(tmp_path_factory): tmp_dir = tmp_path_factory.mktemp("bang") print("DEBUG: tmp_dir=", tmp_dir) _create_clean_directory(tmp_dir / 'unpack') _create_clean_directory(tmp_dir / 'tmp') _create_clean_directory(tmp_dir / 'results') se = ScanEnvironment( maxbytes=max(200000, maxsignaturesoffset + 1), readsize=10240, createbytecounter=False, createjson=True, tlshmaximum=sys.maxsize, synthesizedminimum=10, logging=False, paddingname='PADDING', unpackdirectory=tmp_dir / 'unpack', temporarydirectory=tmp_dir / 'tmp', resultsdirectory=tmp_dir / 'results', scanfilequeue=MockQueue(), resultqueue=MockQueue(), processlock=MockLock(), checksumdict={}, ) se.set_unpackparsers(bangsignatures.get_unpackers()) return se
def setUp(self): self.testdata_dir = pathlib.Path(_scriptdir).resolve() / 'testdata' self.unpackdir = pathlib.Path(_scriptdir).resolve() / 'unpack' self.tmpdir = pathlib.Path(_scriptdir).resolve() / 'tmp' self.resultsdir = pathlib.Path(_scriptdir).resolve() / 'results' self._create_clean_directory(self.unpackdir) self._create_clean_directory(self.tmpdir) self._create_clean_directory(self.resultsdir) self.scanfile_queue = MockQueue() self.result_queue = MockQueue() self.process_lock = MockLock() self.checksum_dict = {} self.dbconn = MockDBConn() self.dbcursor = MockDBCursor() self.scan_environment = ScanEnvironment( maxbytes=max(200000, maxsignaturesoffset + 1), readsize=10240, createbytecounter=False, createjson=False, runfilescans=True, # TODO: is this the correct value? tlshmaximum=sys.maxsize, synthesizedminimum=10, logging=False, paddingname='PADDING', unpackdirectory=self.unpackdir, temporarydirectory=self.tmpdir, resultsdirectory=pathlib.Path(self.resultsdir), scanfilequeue=self.scanfile_queue, resultqueue=self.result_queue, processlock=self.process_lock, checksumdict=self.checksum_dict, ) self.scan_environment.set_unpackparsers(bangsignatures.get_unpackers())
def test_unpackdata_for_all_unpackers(self): unpackers = set(bangsignatures.get_unpackers()) tested_unpackers = { u for f, u in self.walk_available_files_with_unpackers() } untested_unpackers = unpackers.difference(tested_unpackers) print("no tests for:") print("\n".join([u.__name__ for u in untested_unpackers])) self.assertEqual(untested_unpackers, set([]))
def get_unpackers_for_file(unpackername, f): ext = pathlib.Path(f).suffix unpackers = { u for u in bangsignatures.get_unpackers() if u.is_valid_extension(ext) or u.pretty_name == unpackername } for u in unpackers: yield u
def test_all_unpack_parsers_have_attributes(self): for unpackparser in get_unpackers(): self.assertIsNotNone(unpackparser.pretty_name) self.assertIsNotNone(unpackparser.extensions) self.assertIsNotNone(unpackparser.signatures) # assert all signatures are bytestrings i = 0 for s_offset, s_text in unpackparser.signatures: self.assertEqual(type(s_text), type(b''))
def test_all_unpack_parsers_raise_exception_on_empty_file(self): rel_testfile = pathlib.Path('unpackers') / 'empty' self._copy_file_from_testdata(rel_testfile) fileresult = create_fileresult_for_path(self.unpackdir, rel_testfile, set()) data_unpack_dir = rel_testfile.parent / 'some_dir' for unpackparser in get_unpackers(): up = unpackparser(fileresult, self.scan_environment, data_unpack_dir, 0) up.open() with self.assertRaisesRegex(UnpackParserException, r".*", msg=unpackparser.__name__) as cm: r = up.parse_and_unpack() up.close()
def test_unpackparsers_are_found(): unpacker_names = [u.__name__ for u in get_unpackers()] assert 'GifUnpackParser' in unpacker_names assert 'VfatUnpackParser' in unpacker_names
def test_unpackparser_list_has_derived_classes_only(): assert UnpackParser not in get_unpackers()
import pytest from .util import * from UnpackParserException import UnpackParserException from UnpackParser import UnpackParser from bangsignatures import get_unpackers from parsers.database.sqlite.UnpackParser import SqliteUnpackParser from parsers.image.gif.UnpackParser import GifUnpackParser class InvalidUnpackParser(UnpackParser): pass @pytest.fixture(params=get_unpackers()) def unpackparser(request): return request.param def test_unpack_parser_without_parse_method(): p = InvalidUnpackParser(None, None, None, 0) with pytest.raises(UnpackParserException, match=r"undefined parse method") as cm: p.parse() def test_unpackparser_list_has_derived_classes_only(): assert UnpackParser not in get_unpackers() def test_all_unpack_parsers_have_attributes(unpackparser):
def test_unpackparsers_are_found(self): unpacker_names = [u.__name__ for u in get_unpackers()] self.assertIn('GifUnpackParser', unpacker_names) self.assertIn('VfatUnpackParser', unpacker_names)
def test_unpackparser_list_has_derived_classes_only(self): self.assertNotIn(UnpackParser, get_unpackers())