Skip to content

Commit 3f66e3e

Browse files
authored
Support analysis termination check in multiple temp locations (kevoreilly#2790)
* Support analysis termination check in multiple temp locations Updated analyzer to check for the completion folder in both TMP and SystemRoot\Temp directories to handle different temp path scenarios. Added a test script to verify the logic for both locations. * move to test * gemini fixes * Update test_analyzer_logic.py
1 parent 5eead8c commit 3f66e3e

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

analyzer/windows/analyzer.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,25 @@ def analysis_loop(self, aux_modules):
657657

658658
emptytime = None
659659
complete_folder = hashlib.md5(f"cape-{self.config.id}".encode()).hexdigest()
660-
complete_analysis_pattern = os.path.join(os.environ["TMP"], complete_folder)
660+
complete_analysis_patterns = [os.path.join(os.environ["TMP"], complete_folder)]
661+
if "SystemRoot" in os.environ:
662+
complete_analysis_patterns.append(os.path.join(os.environ["SystemRoot"], "Temp", complete_folder))
663+
661664
while self.do_run:
662665
self.time_counter = timeit.default_timer() - time_start
663666
if self.time_counter >= int(self.config.timeout):
664667
log.info("Analysis timeout hit, terminating analysis")
665668
ANALYSIS_TIMED_OUT = True
666669
break
667670

668-
if os.path.isdir(complete_analysis_pattern):
671+
if any(os.path.isdir(p) for p in complete_analysis_patterns):
669672
log.info("Analysis termination requested by user")
670673
ANALYSIS_TIMED_OUT = True
671674
break
672675

676+
if ANALYSIS_TIMED_OUT:
677+
break
678+
673679
# If the process lock is locked, it means that something is
674680
# operating on the list of monitored processes. Therefore we
675681
# cannot proceed with the checks until the lock is released.

tests/test_analyzer_logic.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
import os
3+
import hashlib
4+
import tempfile
5+
6+
# Ideally, this function would be imported from your application code
7+
def check_completion_logic(config):
8+
complete_folder = hashlib.md5(f"cape-{config.id}".encode()).hexdigest()
9+
complete_analysis_patterns = [os.path.join(os.environ["TMP"], complete_folder)]
10+
if "SystemRoot" in os.environ:
11+
complete_analysis_patterns.append(os.path.join(os.environ["SystemRoot"], "Temp", complete_folder))
12+
13+
return any(os.path.isdir(path) for path in complete_analysis_patterns)
14+
15+
class MockConfig:
16+
id = 123
17+
18+
@pytest.fixture
19+
def mock_env(monkeypatch):
20+
"""Pytest fixture to mock environment and create temp dirs."""
21+
with tempfile.TemporaryDirectory() as tmp_dir, tempfile.TemporaryDirectory() as sysroot_dir:
22+
monkeypatch.setenv("TMP", tmp_dir)
23+
monkeypatch.setenv("SystemRoot", sysroot_dir)
24+
os.makedirs(os.path.join(sysroot_dir, "Temp"), exist_ok=True)
25+
yield
26+
27+
def test_completion_folder_in_tmp(mock_env):
28+
config = MockConfig()
29+
complete_folder = hashlib.md5(f"cape-{config.id}".encode()).hexdigest()
30+
path = os.path.join(os.environ["TMP"], complete_folder)
31+
os.makedirs(path)
32+
33+
assert check_completion_logic(config) is True
34+
35+
os.rmdir(path)
36+
assert check_completion_logic(config) is False
37+
38+
def test_completion_folder_in_systemroot(mock_env):
39+
config = MockConfig()
40+
complete_folder = hashlib.md5(f"cape-{config.id}".encode()).hexdigest()
41+
path = os.path.join(os.environ["SystemRoot"], "Temp", complete_folder)
42+
os.makedirs(path)
43+
44+
assert check_completion_logic(config) is True
45+
46+
os.rmdir(path)
47+
assert check_completion_logic(config) is False

0 commit comments

Comments
 (0)