22
33from __future__ import annotations
44
5- import gc
65import json
7- import os
86import shutil
97from configparser import ConfigParser
108from pathlib import Path
119from tempfile import TemporaryDirectory
1210from unittest .mock import Mock , patch
1311
1412from helperFunctions .file_system import get_test_data_dir
13+ from plugins .base_class import UnpackingPlugin
1514from unpacker .unpack import Unpacker
1615
1716
1817class TestUnpackerBase :
19- def setup_method (self ):
20- self .config = ConfigParser ()
21- self .ds_tmp_dir = TemporaryDirectory (prefix = 'fact_tests_' )
22- self .tmp_dir = TemporaryDirectory (prefix = 'fact_tests_' )
18+ @classmethod
19+ def setup_class (cls ):
20+ cls .config = ConfigParser ()
21+ cls .ds_tmp_dir = TemporaryDirectory (prefix = 'fact_tests_' )
22+ cls .files_dir = Path (cls .ds_tmp_dir .name ) / 'files'
23+ cls .reports_dir = Path (cls .ds_tmp_dir .name ) / 'reports'
24+
25+ cls .config .add_section ('unpack' )
26+ cls .config .set ('unpack' , 'data_folder' , cls .ds_tmp_dir .name )
27+ cls .config .set ('unpack' , 'blacklist' , 'text/plain, image/png' )
28+ cls .config .add_section ('ExpertSettings' )
29+ cls .config .set ('ExpertSettings' , 'header_overhead' , '256' )
30+ cls .config .set ('ExpertSettings' , 'unpack_threshold' , '0.8' )
2331
24- self .config .add_section ('unpack' )
25- self .config .set ('unpack' , 'data_folder' , self .ds_tmp_dir .name )
26- self .config .set ('unpack' , 'blacklist' , 'text/plain, image/png' )
27- self .config .add_section ('ExpertSettings' )
28- self .config .set ('ExpertSettings' , 'header_overhead' , '256' )
29- self .config .set ('ExpertSettings' , 'unpack_threshold' , '0.8' )
32+ cls .unpacker = Unpacker (config = cls .config )
3033
31- self .unpacker = Unpacker (config = self .config )
32- os .makedirs (str (self .unpacker ._report_folder ), exist_ok = True ) # pylint: disable=protected-access
33- os .makedirs (str (self .unpacker ._file_folder ), exist_ok = True ) # pylint: disable=protected-access
34+ cls .test_file_path = Path (get_test_data_dir (), 'get_files_test/testfile1' )
3435
35- self .test_file_path = Path (get_test_data_dir (), 'get_files_test/testfile1' )
36+ def setup_method (self ):
37+ self .tmp_dir = TemporaryDirectory (prefix = 'fact_tests_' )
38+ self .unpacker ._report_folder .mkdir (parents = True , exist_ok = True )
39+ self .unpacker ._file_folder .mkdir (parents = True , exist_ok = True )
3640
3741 def teardown_method (self ):
38- self .ds_tmp_dir .cleanup ()
3942 self .tmp_dir .cleanup ()
40- gc .collect ()
43+ shutil .rmtree (self .unpacker ._report_folder )
44+ shutil .rmtree (self .unpacker ._file_folder )
45+
46+ @classmethod
47+ def teardown_class (cls ):
48+ cls .ds_tmp_dir .cleanup ()
4149
4250 def get_unpacker_meta (self ):
4351 return json .loads (
4452 Path (self .unpacker ._report_folder , 'meta.json' ).read_text () # pylint: disable=protected-access
4553 )
4654
4755 def check_unpacker_selection (self , mime_type , plugin_name ):
48- name = self .unpacker .get_unpacker (mime_type )[ 1 ]
49- assert name == plugin_name , 'wrong unpacker plugin selected'
56+ unpacker = self .unpacker .get_unpacker (mime_type )
57+ assert unpacker . NAME == plugin_name , 'wrong unpacker plugin selected' # noqa: SIM300
5058
5159 def check_unpacking_of_standard_unpack_set (
5260 self ,
@@ -59,9 +67,9 @@ def check_unpacking_of_standard_unpack_set(
5967 files = {f for f in files if not any (rule in f for rule in ignore or set ())}
6068 assert len (files ) == 3 , f'file number incorrect: { meta_data } '
6169 assert files == {
62- os . path . join ( self .tmp_dir .name , additional_prefix_folder , 'testfile1' ),
63- os . path . join ( self .tmp_dir .name , additional_prefix_folder , 'testfile2' ),
64- os . path . join ( self .tmp_dir .name , additional_prefix_folder , 'generic folder/test file 3_.txt' ),
70+ str ( Path ( self .tmp_dir .name , additional_prefix_folder , 'testfile1' ) ),
71+ str ( Path ( self .tmp_dir .name , additional_prefix_folder , 'testfile2' ) ),
72+ str ( Path ( self .tmp_dir .name , additional_prefix_folder , 'generic folder/test file 3_.txt' ) ),
6573 }, f'not all files found: { meta_data } '
6674 if output :
6775 assert 'output' in meta_data
@@ -70,9 +78,11 @@ def check_unpacking_of_standard_unpack_set(
7078
7179class TestUnpackerCore (TestUnpackerBase ):
7280 def test_generic_carver_found (self ):
73- assert 'generic/carver' in list (self .unpacker .unpacker_plugins ), 'generic carver plugin not found'
74- name = self .unpacker .unpacker_plugins ['generic/carver' ][1 ]
75- assert name == 'generic_carver' , 'generic_carver plugin not found'
81+ assert 'generic_carver' in self .unpacker .unpacking_plugins , 'generic carver plugin not found'
82+ assert 'generic/carver' in self .unpacker .plugin_by_mime , 'generic carver MIME type not found'
83+ plugin = self .unpacker .get_unpacker ('generic/carver' )
84+ assert isinstance (plugin , UnpackingPlugin )
85+ assert plugin .NAME == 'generic_carver' , 'generic_carver plugin not found'
7686
7787 def test_unpacker_selection_unknown (self ):
7888 self .check_unpacker_selection ('unknown/blah' , 'generic_carver' )
@@ -167,17 +177,17 @@ def test_main_unpack_function(self):
167177 test_file_path = Path (get_test_data_dir (), 'container/test.zip' )
168178 self .main_unpack_check (test_file_path , 3 , 0 , '7z' )
169179
170- def test_main_unpack_exclude_archive (self ):
180+ def test_main_unpack_exclude_archive (self , monkeypatch ):
171181 test_file_path = Path (get_test_data_dir (), 'container/test.zip' )
172- self .unpacker . exclude = ['*test.zip' ]
182+ monkeypatch . setattr ( self .unpacker , ' exclude' , ['*test.zip' ])
173183 self .main_unpack_check (test_file_path , 0 , 1 , None )
174184
175- def test_main_unpack_exclude_subdirectory (self ):
185+ def test_main_unpack_exclude_subdirectory (self , monkeypatch ):
176186 test_file_path = Path (get_test_data_dir (), 'container/test.zip' )
177- self .unpacker . exclude = ['*/generic folder/*' ]
187+ monkeypatch . setattr ( self .unpacker , ' exclude' , ['*/generic folder/*' ])
178188 self .main_unpack_check (test_file_path , 2 , 1 , '7z' )
179189
180- def test_main_unpack_exclude_files (self ):
190+ def test_main_unpack_exclude_files (self , monkeypatch ):
181191 test_file_path = Path (get_test_data_dir (), 'container/test.zip' )
182- self .unpacker . exclude = ['*/get_files_test/*test*' ]
192+ monkeypatch . setattr ( self .unpacker , ' exclude' , ['*/get_files_test/*test*' ])
183193 self .main_unpack_check (test_file_path , 0 , 3 , '7z' )
0 commit comments