1
- from unittest .mock import Mock
1
+ from unittest .mock import Mock , patch
2
2
3
+ import pytest
3
4
from dissect .target import Target
4
- from dissect .target .filesystem import VirtualFilesystem
5
+ from dissect .target .filesystem import VirtualFile , VirtualFilesystem , VirtualSymlink
5
6
6
7
from acquire .collector import Collector
7
8
@@ -23,3 +24,92 @@ def test_collector():
23
24
collector .collect_file ("$MFT" , module_name = "test" )
24
25
25
26
assert not collector .report .was_path_seen (fs_2 .get ("$MFT" ))
27
+
28
+
29
+ @pytest .fixture
30
+ def mock_file ():
31
+ return Mock ()
32
+
33
+
34
+ @pytest .fixture
35
+ def mock_fs (mock_file ):
36
+ fs = VirtualFilesystem (case_sensitive = False )
37
+ fs .makedirs ("/foo/bar" )
38
+ fs .map_file_entry ("/foo/bar/some-file" , VirtualFile (fs , "some-file" , mock_file ))
39
+ fs .map_file_entry ("/foo/bar/some-symlink" , VirtualSymlink (fs , "some-symlink" , "/foo/bar/some_file" ))
40
+ return fs
41
+
42
+
43
+ @pytest .fixture
44
+ def mock_target (mock_fs ):
45
+ target = Target ()
46
+ target .fs .mount ("/" , mock_fs )
47
+ target .filesystems .add (mock_fs )
48
+ return target
49
+
50
+
51
+ @pytest .fixture
52
+ def mock_collector (mock_target ):
53
+ collector = Collector (mock_target , Mock ())
54
+ return collector
55
+
56
+
57
+ def test_collector_collect_path_no_module_name (mock_collector ):
58
+ with pytest .raises (ValueError ):
59
+ mock_collector .collect_path ("/some/path" )
60
+
61
+
62
+ def test_collector_collect_path (mock_fs , mock_target , mock_collector ):
63
+ mock_seen_paths = set ()
64
+ mock_module_name = "DUMMY"
65
+
66
+ with patch ("acquire.collector.log" ) as mock_log :
67
+ with patch .object (mock_collector , "report" ):
68
+ with patch .object (mock_collector , "collect_dir" ):
69
+ with patch .object (mock_collector , "collect_file" ):
70
+ path = mock_target .fs .path ("/foo/bar" )
71
+ mock_collector .collect_path (
72
+ path ,
73
+ seen_paths = mock_seen_paths ,
74
+ module_name = mock_module_name ,
75
+ )
76
+ mock_collector .collect_dir .assert_called ()
77
+
78
+ mock_collector .collect_path (
79
+ "/foo/bar" ,
80
+ seen_paths = mock_seen_paths ,
81
+ module_name = mock_module_name ,
82
+ )
83
+ mock_collector .collect_dir .assert_called ()
84
+
85
+ mock_collector .collect_path (
86
+ "/foo/bar/some-file" ,
87
+ seen_paths = mock_seen_paths ,
88
+ module_name = mock_module_name ,
89
+ )
90
+ mock_collector .collect_file .assert_called ()
91
+
92
+ mock_collector .collect_path (
93
+ "/foo/bar/some-symlink" ,
94
+ seen_paths = mock_seen_paths ,
95
+ module_name = mock_module_name ,
96
+ )
97
+ mock_log .error .assert_called ()
98
+ assert mock_log .error .call_args .args [0 ] == "- Can't collect %s (symlink to %s) in module %s"
99
+
100
+ mock_collector .collect_path (
101
+ "/foo/bar/non-existing-file" ,
102
+ seen_paths = mock_seen_paths ,
103
+ module_name = mock_module_name ,
104
+ )
105
+ mock_log .error .assert_called ()
106
+ assert mock_log .error .call_args .args [0 ] == "- Path %s is not found"
107
+
108
+ with patch .object (mock_target .fs , "lexists" , return_value = True ):
109
+ mock_collector .collect_path (
110
+ "/foo/bar/non-existing-file" ,
111
+ seen_paths = mock_seen_paths ,
112
+ module_name = mock_module_name ,
113
+ )
114
+ mock_log .error .assert_called ()
115
+ assert mock_log .error .call_args .args [0 ] == "- Don't know how to collect %s in module %s"
0 commit comments