fix: restore HackRF One/Pro detection when PATH is restricted

This commit is contained in:
Smittix
2026-03-05 09:31:21 +00:00
parent 8e24ee7ff7
commit 976533a3b3
4 changed files with 296 additions and 244 deletions
+33 -12
View File
@@ -17,9 +17,9 @@ def _clear_detection_caches():
yield
@patch('utils.sdr.detection._check_tool', return_value=True)
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/rtl_test')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_rtlsdr_devices_filters_empty_serial_entries(mock_run, _mock_check_tool):
def test_detect_rtlsdr_devices_filters_empty_serial_entries(mock_run, _mock_tool_path):
"""Ignore malformed rtl_test rows that have an empty SN field."""
mock_result = MagicMock()
mock_result.stdout = ""
@@ -40,9 +40,9 @@ def test_detect_rtlsdr_devices_filters_empty_serial_entries(mock_run, _mock_chec
assert devices[0].serial == "1"
@patch('utils.sdr.detection._check_tool', return_value=True)
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/rtl_test')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_rtlsdr_devices_uses_replace_decode_mode(mock_run, _mock_check_tool):
def test_detect_rtlsdr_devices_uses_replace_decode_mode(mock_run, _mock_tool_path):
"""Run rtl_test with tolerant decoding for malformed output bytes."""
mock_result = MagicMock()
mock_result.stdout = ""
@@ -74,9 +74,9 @@ HACKRF_INFO_OUTPUT = (
)
@patch('utils.sdr.detection._check_tool', return_value=True)
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/hackrf_info')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_hackrf_from_stdout(mock_run, _mock_check_tool):
def test_detect_hackrf_from_stdout(mock_run, _mock_tool_path):
"""Parse HackRF device info from stdout."""
mock_result = MagicMock()
mock_result.stdout = HACKRF_INFO_OUTPUT
@@ -92,9 +92,9 @@ def test_detect_hackrf_from_stdout(mock_run, _mock_check_tool):
assert devices[0].index == 0
@patch('utils.sdr.detection._check_tool', return_value=True)
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/hackrf_info')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_hackrf_from_stderr(mock_run, _mock_check_tool):
def test_detect_hackrf_from_stderr(mock_run, _mock_tool_path):
"""Parse HackRF device info when output goes to stderr (newer firmware)."""
mock_result = MagicMock()
mock_result.stdout = ""
@@ -109,9 +109,9 @@ def test_detect_hackrf_from_stderr(mock_run, _mock_check_tool):
assert devices[0].serial == "0000000000000000a06063c8234e925f"
@patch('utils.sdr.detection._check_tool', return_value=True)
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/hackrf_info')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_hackrf_nonzero_exit_with_valid_output(mock_run, _mock_check_tool):
def test_detect_hackrf_nonzero_exit_with_valid_output(mock_run, _mock_tool_path):
"""Parse HackRF info even when hackrf_info exits non-zero (device busy)."""
mock_result = MagicMock()
mock_result.returncode = 1
@@ -125,9 +125,9 @@ def test_detect_hackrf_nonzero_exit_with_valid_output(mock_run, _mock_check_tool
assert devices[0].name == "HackRF One"
@patch('utils.sdr.detection._check_tool', return_value=True)
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/hackrf_info')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_hackrf_fallback_no_serial(mock_run, _mock_check_tool):
def test_detect_hackrf_fallback_no_serial(mock_run, _mock_tool_path):
"""Fallback detection when serial is missing but 'Found HackRF' present."""
mock_result = MagicMock()
mock_result.stdout = "Found HackRF\nBoard ID Number: 2 (HackRF One)\n"
@@ -139,3 +139,24 @@ def test_detect_hackrf_fallback_no_serial(mock_run, _mock_check_tool):
assert len(devices) == 1
assert devices[0].name == "HackRF One"
assert devices[0].serial == "Unknown"
@patch('utils.sdr.detection.get_tool_path', return_value='/usr/bin/hackrf_info')
@patch('utils.sdr.detection.subprocess.run')
def test_detect_hackrf_parses_legacy_serial_format(mock_run, _mock_tool_path):
"""Accept legacy 'Serial Number' casing and spaced hex format."""
mock_result = MagicMock()
mock_result.stdout = (
"Found HackRF\n"
"Index: 0\n"
"Serial Number: 0x00000000 00000000 a06063c8 234e925f\n"
"Board ID Number: 3 (HackRF Pro)\n"
)
mock_result.stderr = ""
mock_run.return_value = mock_result
devices = detect_hackrf_devices()
assert len(devices) == 1
assert devices[0].name == "HackRF Pro"
assert devices[0].serial == "0000000000000000a06063c8234e925f"