|
10 | 10 | ROOT_UID = 'root_uid' |
11 | 11 |
|
12 | 12 |
|
| 13 | +class MockRedis: |
| 14 | + def __init__(self): |
| 15 | + self.analysis_status = None |
| 16 | + |
| 17 | + def set_analysis_status(self, status): |
| 18 | + self.analysis_status = status |
| 19 | + |
| 20 | + |
13 | 21 | class TestAnalysisStatus: |
14 | 22 | def setup_method(self): |
15 | 23 | self.status = AnalysisStatus() |
| 24 | + self.status._worker.redis = MockRedis() |
16 | 25 |
|
17 | 26 | def test_add_firmware_to_current_analyses(self): |
18 | 27 | fw = Firmware(binary=b'foo') |
@@ -207,3 +216,73 @@ def test_cancel_unknown_uid(self): |
207 | 216 | self.status._worker._update_status() |
208 | 217 |
|
209 | 218 | assert ROOT_UID in self.status._worker.currently_running |
| 219 | + |
| 220 | + def test_add_update(self): |
| 221 | + fw = Firmware(binary=b'foo') |
| 222 | + fw.files_included = ['foo', 'bar'] |
| 223 | + self.status.add_update(fw, fw.files_included) |
| 224 | + # _update_status is called twice, because add_object is called first and then add_update |
| 225 | + self.status._worker._update_status() |
| 226 | + self.status._worker._update_status() |
| 227 | + |
| 228 | + assert fw.uid in self.status._worker.currently_running |
| 229 | + result = self.status._worker.currently_running[fw.uid] |
| 230 | + assert result.files_to_unpack == set() |
| 231 | + assert result.completed_files == set() |
| 232 | + assert result.unpacked_files_count == result.total_files_count |
| 233 | + assert result.analyzed_files_count == 0 |
| 234 | + assert result.files_to_analyze == {fw.uid, *fw.files_included} |
| 235 | + assert result.total_files_count == len(fw.files_included) + 1 |
| 236 | + |
| 237 | + def test_add_analysis(self): |
| 238 | + self.status._worker.currently_running = { |
| 239 | + ROOT_UID: FwAnalysisStatus( |
| 240 | + files_to_unpack=set(), |
| 241 | + files_to_analyze={'foo', 'bar'}, |
| 242 | + analysis_plugins={}, |
| 243 | + hid='', |
| 244 | + total_files_count=3, |
| 245 | + ) |
| 246 | + } |
| 247 | + self.status._currently_analyzed[ROOT_UID] = True |
| 248 | + fo = FileObject(binary=b'foo') |
| 249 | + fo.root_uid = ROOT_UID |
| 250 | + fo.uid = 'foo' |
| 251 | + |
| 252 | + result = self.status._worker.currently_running[ROOT_UID] |
| 253 | + assert result.analysis_plugins == {} |
| 254 | + |
| 255 | + self.status.add_analysis(fo, 'some_plugin') |
| 256 | + self.status._worker._update_status() |
| 257 | + |
| 258 | + assert result.analysis_plugins == {'some_plugin': 1} |
| 259 | + |
| 260 | + def test_store_status(self): |
| 261 | + self.status._worker.currently_running = { |
| 262 | + ROOT_UID: FwAnalysisStatus( |
| 263 | + files_to_unpack={'bar'}, |
| 264 | + files_to_analyze={'foo', 'bar'}, |
| 265 | + analysis_plugins={}, |
| 266 | + hid='', |
| 267 | + unpacked_files_count=2, |
| 268 | + total_files_count=3, |
| 269 | + ) |
| 270 | + } |
| 271 | + recently_finished = { |
| 272 | + 'other_UID': { |
| 273 | + 'duration': 13.37, |
| 274 | + 'total_files_count': 1337, |
| 275 | + 'time_finished': 1337, |
| 276 | + 'hid': 'analysis_status.hid', |
| 277 | + } |
| 278 | + } |
| 279 | + self.status._worker.recently_finished = recently_finished |
| 280 | + self.status._worker._store_status() |
| 281 | + |
| 282 | + status = self.status._worker.redis.analysis_status |
| 283 | + assert ROOT_UID in status['current_analyses'] |
| 284 | + assert status['current_analyses'][ROOT_UID]['unpacked_count'] == 2 |
| 285 | + assert status['current_analyses'][ROOT_UID]['analyzed_count'] == 0 |
| 286 | + assert status['current_analyses'][ROOT_UID]['total_count'] == 3 |
| 287 | + assert status['recently_canceled_analyses'] == {} |
| 288 | + assert status['recently_finished_analyses'] == recently_finished |
0 commit comments